import numpy as np from numpy import * v = [1,2,3,4] av = np.array(v) av m = [[1,2],[3,4]] am = array(m) am am.size #total number of elements am.shape #array shape (equivalent to Matlab's size) am.size am.shape am.ndim len(dir(am)) am.dtype array([1,2,3]).dtype array([1.1,2.2,3.4]).dtype array([1+2j,2+4j,3]).dtype array([True,False]).dtype sctypes arange(0,10,2) arange(-1,1,0.1) linspace(0,10,6) _.dtype linspace(0,10,6,dtype='uint32') _.dtype linspace(-1,10,6,dtype='uint32') finfo('float64') iinfo(np.int8), iinfo('uint8') logspace(0,1,10) x,y = mgrid[0:5,0:5] x, y x = linspace(0,1,10) y = linspace(-1,1,5) meshgrid(x,y) from numpy import random random.normal(0,1) random.normal(-1,0.001) random.normal(0,1,[2,3]) random.poisson() random.rand(5,5) a = random.rand(5,5) a a[0,0] a[0] a[:,0] aview1 = a[[0],:] aview1 aview2 = a[0] aview2 aview3 = a[[0]] aview3 print aview1.shape, aview2.shape, aview3.shape from IPython.display import YouTubeVideo YouTubeVideo('q_2TBbfMLzs',start=15) a a[:,:] a[0:2,1:3] a = arange(10) a[1:6:3] a[::2] a[::-1] %%timeit a[::-1] %%timeit reversed(a) print a[5:], a[:4], a[-2:], a[:-4] a = random.rand(5,5) a a[0] rows = [1,3] cols = [0,2] a[rows], a[:,cols], a[rows,cols] mask = np.around(a) mask = mask.astype('bool') a[mask] mask = a > 1.0 a[mask] mask = a < 1.0 a[mask] mask = a > 0.7 a[mask] where(mask) from numpy.linalg import * x = arange(10) b = random.rand(10,1) A = random.randn(10,10) print x x*2 x+5 x**2 x*x from IPython.display import Image Image('https://scipy-lectures.github.io/_images/numpy_broadcasting.png',width=800) x.shape, b.shape x+b x2 = solve(A,b) A*x2-b A.dot(x2)-b np.allclose(A.dot(x2),b) invA = inv(A) np.allclose(invA.dot(A),identity(shape(A)[0])) A.transpose() A.sum() np.sum(A) sum(A) sum is np.sum from IPython.core.display import HTML def css_styling(): styles = open("./styles/custom.css", "r").read() return HTML(styles) css_styling()