Numpy tutorial
blockquote not implementedReference
Scipy official reference: http://docs.scipy.org/
Scipy Lecture Notes: http://www.scipy-lectures.org/
Python Data Science Handbook: https://jakevdp.github.io/PythonDataScienceHandbook/
Python course .eu: https://www.python-course.eu/numpy.php
Call the function
np.func(a, x, y) is the same as a.func(x, y).
Convention
The common short name for numpy is np.
import numpy as npnp.__version__Creating an array from a sequence
array(seq), asarray(seq)
seq can be a tuple, list, or a numpy array.
asarray() does not make a new copy if seq is already a numpy array.
Creating a 1D numpy array from a list.
Array basics
a.ndim: number of dimensionsa.shape: Tuple of lengths for each dimensiona.size: total size (product of shape) =len(a)a.dtype: data typea[i]: accessing i th element in the 1D array (start from 0)a[i, j]: accessing element i th row, j th column (2D array, start from 0)a[:, j]: accessing j th column(2D array)a[i, :]: accessing j th row (2D array)a.T: Transpose ofaa.copy(): make a copy of a that does not share memorya.reshape(shape): reshape the array if the new size is compatible (i.e. the same total size)
a = np.array([1, 9, 8, 7])aa.ndima.shapea.sizea.dtypea[3]For complex numbers, j being the imaginary part.
np.array([1+2j, 3+4j, 5+6*1j]).dtypeCreating a multidimensional array from a nested list , with complex numbers
b = np.asarray([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0+1j]])bb.ndimb.shapeb.dtypeb[1, :]b[:, 0]b.Tb.reshape((3, 2))b.reshape((1, -1)) # -1 mean caculate dim automaticallyCreating an array from a function
arange(start, stop, step)linspace(start, stop, num, endpoint=True)logspace(start, stop, num, endpoint=True)ones((d1, d2, ...))ones_like(arr)zeros((d1, d2, ...))zeros_like(arr)full((d1, d2, ...), val)eye(k)diag(seq)fromfunction(f, (d1, d2, ...))fromiter(iter)meshgrid(x1, x2, ...)
np.arange(10, 0, -1)np.linspace(0, 1, 5)np.linspace(0, 1, 5, endpoint=False)np.logspace(-10.0, 10.0, 11)np.ones((3, 3))a = np.arange(5.0)print(np.ones_like(a))np.full((3, 4), 42)np.full_like(a, 69)np.eye(3)np.diag([4, 5, 6, 7])np.fromfunction(lambda i, j: i >= j, (4, 4))np.fromiter((x*x for x in range(5)) , dtype=np.float)import numpy as npimport matplotlib.pyplot as pltx = np.linspace(-5, 5, 100)y = np.linspace(-5, 5, 100)# sparse=True to save some memoryxx, yy = np.meshgrid(x, y, sparse=True)print('xx =', xx, sep='\n')print('yy =', yy, sep='\n')plt.contourf(x,y, np.sin(xx**2 + yy**2) / (xx**2 + yy**2))plt.colorbar()Random
The new API: https://numpy.org/doc/stable/reference/random/index.html?highlight=random#module-numpy.random
from numpy.random import default_rngrng = default_rng()rng.random()# Uniform [0, 1)rng.random((4, 3))# Integersrng.integers(1, 7, (10, 20))# Standard uniform distributionrng.standard_normal(10)# Random choicechoices = np.array(["one", "two"])# Select by indexchoices[rng.integers(0, 2, (3, 4))]# Or choice function, prob weights supportedrng.choice(choices, size=(5, 3), p=[0.3, 0.7])Selecting elements
a = np.arange(10)a# a[idx]a[0], a[3], a[7]# a[[indices]]a[[0, 3, 7]]# a[condition]# Selection from an array of true/false valuea[a<5]# Slice: a[start:end:step]a[1::2]# Reversea[::-1]# Mutating the elementsa[0] = 1000aIndexing for 2D / 3D arrays
In 2D, the first dimension corresponds to rows, the second to columns. Numpy is row-major by default, as in C-styled arrays.
a[i, j] for the element from ith row and jth column.
b = np.arange(25).reshape((5,5))b# Each index is separated by commab[2, 3]# Slices share the same underlying object of the original.c = b[1::2, 1::2]cc[0, 0] = 666 # Mutates b !!!print("After mutating:")bnp.may_share_memory(c, b)# Use copy to prevent unwanted overwritinga = np.arange(10)c = a[::2].copy()c[0] = 12a# combining assignment and slicinga = np.arange(10)a[5:] = 10ab = np.arange(5)a[5:] = b[::-1]aNumerical operations on arrays
Element-wise (broadcasting) operations by default.
Some math functions could be found in numpy (e.g. sin, cos): use
np.lookfor(desc.)Others could be found in scipy documentations.
a = np.arange(10)aa+1a-3a*2a/42**aWith an array: Only if dimension sizes are compatible: either the same or 1.
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])b = rng.random((2, 4))a+ba-ba*ba/bnp.sin(b)a = np.array([1, 2, 3, 4])b = np.array([4, 2, 2, 4])a == ba = np.arange(1, 10)b = np.arange(1, 8).reshape((-1, 1))ab# Broadcasting: A(1*M) * B(N*1) = C(N * M)a*bMatrix multiplication
dot(a, b), a@b
a = rng.random((5, 5))b = rng.random((5, 5))# Element-wise multiplicationa*b# Matrix multiplicationa@b# Matrix multiplicationnp.dot(a,b)# No need to transpose 1D array for `dot(a, b)`a = rng.random((5, 5))b = rng.random(5)c = rng.random(5)# Matrix x vectornp.dot(a,b)# Vector * vectornp.dot(c,b)Combing Arrays
This one will give your headaches.
concatenate((a, b), axis=n)stack((a,b), axis=n)
The former joins arrays in the existing axis; the latter creates a new axis.
a = np.arange(0, 10)b = np.arange(0, 10) + 10# along the row (1st axis), existing axisnp.concatenate((a, b), axis=0)# along the column (2nd axis)np.stack((a, b), axis=1)Reduction
sum(v, axis=n), cumsum(v, axis=n)
a = np.arange(0, 6).reshape((2, 3))anp.sum(a)np.sum(a, axis=1)np.sum(a, axis=0)np.cumsum(a)np.cumsum(a, axis=1)np.cumsum(a, axis=0)amin(v, axis=n)amax(v, axis=n)minimum(a, b)maximum(a, b)argmin(v, axis=n)argmax(v, axis=n)
np.amin(a)np.argmin(a)np.amax(a)np.argmax(a)b = (rng.standard_normal((2, 3)) + 1) * 5bnp.minimum(a, b)np.maximum(a, b)np.all([True, True, False])np.any([True, True, False])