%%timeit # Ejemplo de medida de tiempo de ejecución import time def foo(x, y): time.sleep(0.01) return x ** y for ii in range(10): for jj in range(10): kk = foo(ii, jj) %%prun -s cumtime # Ejemplo de profiling import time def foo(x, y): time.sleep(0.01) return x ** y for ii in range(10): for jj in range(10): kk = foo(ii, jj) # Ejemplo de depuración interactiva %pdb on def bar(t): ret = 1 / t return ret bar(0) from IPython.display import Image Image('pyladies_square.png') %pdb off import numpy as np mi_primer_array = np.array([1, 2, 3, 4]) mi_primer_array mi_segundo_array = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) np.zeros(10) np.zeros([10, 10]) np.ones([3, 2]) np.identity(4) np.arange(0, 5) np.arange(0,11,3) np.linspace(0, 1, 21) arr = np.arange(11) arr arr + 10 arr * 2 np.sin(arr) datos = np.loadtxt("temperaturas.dat") datos = datos / 10 datos[:10] from IPython.display import HTML HTML('') %matplotlib inline import matplotlib.pyplot as plt plt.plot([0.0, 0.1, 0.2, 0.7, 0.9], [1, -2, 3, 4, 1]) def f(x): return np.exp(-x ** 2) x = np.linspace(-1, 3, 100) plt.plot(x, f(x), label="Función f(x)") plt.xlabel("Eje $x$") plt.ylabel("$f(x)$") plt.legend() plt.title("Función $f(x)$") with plt.xkcd(): plt.plot(x, f(x)) plt.plot(x, 1 - f(x)) plt.xlabel("Eje x") N = 100 a = np.random.randn(N) b = np.random.randn(N) plt.scatter(a, b) s = np.abs(50 + 50 * np.random.randn(N)) c = np.random.randn(N) plt.scatter(a, b, s=s, c=c, cmap=plt.cm.Blues) plt.colorbar() def g(x, y): pass # Necesitamos muchos puntos en la malla, para que cuando se # crucen las líneas no se vean irregularidades #x = ? #y = ? xx, yy = np.meshgrid(x, y) zz = g(xx, yy) # Podemos ajustar el tamaño de la figura con figsize fig = plt.figure(figsize=(6, 6)) # Ajustamos para que tenga 13 niveles y que use el colormap Spectral # Tenemos que asignar la salida a la variable cs para luego crear el colorbar #cs = ? # Creamos la barra de colores #plt.? # Con `colors='k'` dibujamos todas las líneas negras # Asignamos la salida a la variable cs2 para crear las etiquetas #cs = ? # Creamos las etiquetas sobre las líneas plt.clabel(cs) # Ponemos las etiquetas de los ejes def frecuencias(f1=10.0, f2=100.0): max_time = 0.5 times = np.linspace(0, max_time, 1000) signal = np.sin(2 * np.pi * f1 * times) + np.sin(2 * np.pi * f2 * times) with plt.style.context("ggplot"): plt.plot(signal, label="Señal") plt.xlabel("Tiempo ($t$)") plt.title("Dos frecuencias") plt.legend() frecuencias() from IPython.html.widgets import interactive interactive(frecuencias, f1=(10.0,200.0), f2=(10.0,200.0))