from __future__ import print_function, division %matplotlib inline import matplotlib.pyplot as plt # Setup ggplot style, if using matplotlib 1.4 or newer try: plt.style.use('ggplot') print("Setting Matplotlib style to ggplot") except AttributeError: print("Update matplotlib to enable styles") import math xmin = 0 xmax = 2 * math.pi N = 1000 # build the arrays x = [] y = [] for i in range(N): xval = xmin + i * (xmax - xmin) / N x.append(xval) y.append(math.sin(xval)) # Plot the result with matplotlib plt.plot(x, y); x = [xmin + i * (xmax - xmin) / N for i in range(N)] y = [math.sin(xi) for xi in x] plt.plot(x, y); import numpy as np x = np.linspace(xmin, xmax, N) y = np.sin(x) plt.plot(x, y); #--------------------------------------------------- # 1. C/Fortran style x = [] y = [] for i in range(N): xval = xmin + i * (xmax - xmin) / N x.append(xval) y.append(math.sin(xval)) #--------------------------------------------------- # 2. Pythonic approach x = [xmin + i * (xmax - xmin) / N for i in range(N)] y = [math.sin(xi) for xi in x] #--------------------------------------------------- # 3. NumPy approach x = np.linspace(xmin, xmax, N) y = np.sin(x) N = 100000 %%timeit # 1. C/Fortran style x = [] y = [] for i in range(N): xval = xmin + i * (xmax - xmin) / N x.append(xval) y.append(math.sin(xval)) %%timeit # 2. Pythonic style x = [xmin + i * (xmax - xmin) / N for i in range(N)] y = [math.sin(xi) for xi in x] %%timeit # 3. NumPy approach x = np.linspace(xmin, xmax, N) y = np.sin(x) L = list(range(5)) L L[3] = "three" L [2 * Li for Li in L] L = list(range(1, 9)) L A = np.arange(1, 9) A x = np.arange(10) x # data type x.dtype # array shape x.shape # number of dimensions x.ndim # size of the array x.size # number of bytes to step for each dimension x.strides # flags specifying array properties x.flags y = x[::2] y y.flags y[2] = 100 x import ctypes print(x.data, x.itemsize, x.strides) print(y.data, y.itemsize, y.strides) print(x) print(y) # Side note: if you actually want a copy, you can do, e.g. y = x[::2].copy() x2 = x.reshape(2, 5) x2 x2[1, 4] = 42 x2 x # number of dimensions x2.ndim # shape of the array x2.shape # size of the array x2.size # number of bytes to step for each dimension x2.strides x2.flags # Array of zeros np.zeros((2, 3)) # Array of ones np.ones((3, 4)) # Like Python's range() function np.arange(10) # 5 steps from 0 to 1 np.linspace(0, 1, 5) # uninitialized array np.empty(10) # random numbers between 0 and 1 np.random.rand(5) # standard-norm-distributed random numbers np.random.randn(5) x = np.linspace(0, 2 * np.pi) y = np.sin(x) print(x.shape) print(y.shape) plt.plot(x, y); plt.plot(x, y) plt.plot(x, y + 1) plt.plot(x, -2 * y) plt.plot(x, 1 / (1 + y ** 2)); for f in [np.sin, np.cos, np.exp, np.log, np.sinc]: plt.plot(x, f(x), label='np.' + f.__name__) plt.legend() plt.ylim(-1, 5) from scipy import special x = np.linspace(0, 30, 500) plt.plot(x, special.j0(x), label='j0') plt.plot(x, special.j1(x), label='j1') plt.plot(x, special.jn(3, x), label='j3') plt.legend(); from scipy import special special? x = np.random.random(100) x.min() # Maximum value x.max() # Sum x.sum() # Mean x.mean() # Product x.prod() # Standard Deviation x.std() # Variance x.var() # Median np.median(x) # Quantiles np.percentile(x, [25, 50, 75]) y = np.arange(12).reshape(3, 4) y # sum of each column y.sum(0) # sum of each row y.sum(1) # Keep the dimensions (NumPy 1.7 or later) y.sum(1, keepdims=True) np.std(y, 0)