Numpy Cheat Sheet¶
Linear algebra¶
-
Kronecker product of two arrays.
Examples:
>>> np.kron(np.eye(2), np.ones((2,2))) array([[ 1., 1., 0., 0.], [ 1., 1., 0., 0.], [ 0., 0., 1., 1.], [ 0., 0., 1., 1.]])
In linear system \(Ax = b\), if A is a full matrix, system of equations can always be solved by using numpy.linalg.solve(), or numpy.linalg.inv(). However, if A is arbitrary matrix, i.e. under-, well-, or over- determined, we’d better solve the system of equations by least-squares method, i.e. the following two functions
numpy.linalg.lstsq(a, b, rcond=’warn’)
Return the least-squares solution to a linear matrix equation.
Examples:
>>> x = np.array([0, 1, 2, 3]) >>> y = np.array([-1, 0.2, 0.9, 2.1]) >>> A = np.vstack([x, np.ones(len(x))]).T >>> A array([[ 0., 1.], [ 1., 1.], [ 2., 1.], [ 3., 1.]]) >>> m, c = np.linalg.lstsq(A, y)[0] >>> print(m, c) 1.0 -0.95
numpy.linalg.pinv(a, rcond=1e-15)
Calculate the generalized inverse of a matrix using its singular-value decomposition (SVD) and including all large singular values.
Examples:
>>> a = np.random.randn(9, 6) >>> B = np.linalg.pinv(a) >>> np.allclose(a, np.dot(a, np.dot(B, a))) True >>> np.allclose(B, np.dot(B, np.dot(a, B))) True
To see whether a linear system can be easly solve numerically, we always look into the condition number of matrix A,
numpy.linalg.cond(x, p=None)[source]
This function is capable of returning the condition number using one of seven different norms, depending on the value of p.
Example:
>>> from numpy import linalg as LA >>> a = np.array([[1, 0, -1], [0, 1, 0], [1, 0, 1]]) >>> a array([[ 1, 0, -1], [ 0, 1, 0], [ 1, 0, 1]]) >>> LA.cond(a) 1.4142135623730951
Searching and Sorting¶
The most used searching function is numpy.where(). Besides, There are many other useful functions.
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
Find the unique elements of an array.
Example:
>>> a = np.array([[1, 1], [2, 3]]) >>> np.unique(a) array([1, 2, 3]) >>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]]) >>> np.unique(a, axis=0) array([[1, 0, 0], [2, 3, 4]])
while numpy.sort() returns a sorted array, if you need the order of how computer sort your array, you should use
numpy.argsort(a, axis=-1, kind=’quicksort’, order=None)
Returns the indices that would sort an array.
Example:
>>> x = np.array([3, 1, 2]) >>> np.argsort(x) array([1, 2, 0])
Iterating¶
Rarely used but seems interesting. For details, see the introductory guide to array iteration
-
class, returning an iterator yielding pairs of array coordinates and values.
Example:
>>> a = np.array([[1, 2], [3, 4]]) >>> for index, x in np.ndenumerate(a): ... print(index, x) (0, 0) 1 (0, 1) 2 (1, 0) 3 (1, 1) 4
-
iterates over the N-dimensional index of the array. At each iteration a tuple of indices is returned, the last dimension is iterated over first.
Example:
>>> for index in np.ndindex(3, 2, 1): ... print(index) (0, 0, 0) (0, 1, 0) (1, 0, 0) (1, 1, 0) (2, 0, 0) (2, 1, 0)
Standard Universal Function¶
There are currently more than 60 universal functions defined in numpy
on one or more types,
covering a wide variety of operations. Some of these ufuncs are called automatically on arrays
when the relevant infix notation is used (e.g., add(a, b)
is called internally
when a + b
is written and a or b is an ndarray
).
Nevertheless, you may still want to use the ufunc call in order to use the optional
output argument(s) to place the output(s) in an object (or objects) of your choice.
Tip:
The optional output arguments can be used to help you save memory for large calculations. If your arrays are large, complicated expressions can take longer than absolutely necessary due to the creation and (later) destruction of temporary calculation spaces.
For example, the expression G = a * b + c
is equivalent to t1 = A * B; G = T1 + C; del t1
.
It will be more quickly executed as G = A * B; add(G, C, G)
which is the same as G = A * B; G += C
.