Important types
ndarrayfloat64int32Important ndarray attributes:
ndimshapesize: total number of elementsdtypearray = np.array(Iterable, [dtype=...], copy=True)The type of the array’s elements is optional and will usually be
deduced as one of int64, float64.
Sequences of sequences will get transformed to 2D arrays. Can keep nesting sequences to increase the number of dimensions.
If shape = (dim0,dim1,...) is a tuple:
np.zeros(shape)np.ones(shape)np.full(shape, constant)np.empty(shape): initial content is random (based on
memory state)np.arange(start_incl, stop_excl, step)np.linspace(start_incl, stop_incl, n_elements)np.eye(constant)np.diag(array)np.fromfunction(lambda indices: ..., shape)np.full_like(a, fill_value): same shape, dtype as
anp.zeros_like(a)np.asarray(input): input will be returned uncopied iff
it’s a compatible ndarray (copy=False)np.triUseful methods
a.transpose(), a.Ta.reshape(shape)a.flip()# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])Slicing, and integer array indexing. Returns a view.
a[1, :] # [5 6 7 8] of shape (4,)
a[1:2, :] # [[5 6 7 8]] of shape (1, 4)
a[[1,2], [0,1]] # [[5 6], [9 10]]Can also use ndarrays to index.
Boolean array indexing
a[a < 3] # [1 2]
a[(a > 2) & (a < 5)]
# & (and), | (or), ^ (xor), ~ (not)By default, 1D arrays are treated as row vectors
((n,), (1,n)) in 2D operations. To convert a 1D array to a
column vector, use e.g.
a.reshape(-1,1): -1 calculates the dim
size automatically, ora[:, None] == a[:, np.newaxis].Transpose (.T) can be used to switch between row and
column 2D vectors.
+-*/<>, **: all work elementwise,
create a new arrayA @ B, A.dot(B): matrix product, creates a
new array+=, -=, *=: in-place
modificationWhen operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (upcasting).
Universal functions are instances of the numpy.ufunc class.
Operates on ndarrays element-wise (ufunc). Supports broadcasting.
Examples
sqrt() exp() log()add()sin() sinh() arcsin() arcsinh()bitwise_or()greater_equal(),
maximum()isnan()https://numpy.org/devdocs/reference/ufuncs.html
Can create your own ufunc by using np.frompyfunc.
Methods on the ufunc itself. These methods only make sense on scalar ufuncs that take two input arguments and return one output.
np.multiply.reduce([1,2,3]) -> 6np.add.accumulate([1, 2, 3]) -> 1, 3, 6np.multiply.outer([1, 2, 3], [4, 5, 6])np.negative.at([1, 2, 3], [0, 1]) -> -1, -2, 3Prefer functions defined on the array object where possible. Less Python overhead, though might work in-place.
sum([axis=...])min([axis=...]), max()argmin([axis=...]), argmax(): returns indexargsort()cumsum([axis=...])a.dot(b), a.cross(b)By default axis=None. For a_ij:
i is axis=0, and j is axis=1. So axis 0 refers
to the rows e.g. array.sum(axis=0) gives the row-wise sum
of array.
mean(), std(), var()sort(): in-placesearchsorted(v, side='left'|'right'): index where
v should be inserted to maintain order, else returns
0 or a.lenflat: an iterator attribute, provides for 1D
iterationflatten(): returns a copyravel(a, order="C"): like flatten, but returns a
viewclip(a, a_min, a_max): values outside the interval are
clipped to the edgesSuppose a = [1, 2, 3, 4]
np.einsum(subscripts, operands)
np.einsum('ij,ij', a, b): c=\sum_{i,j} a_{ij}b_{ij}np.einsum('ik,jk -> ij', a, b): c_{ij}=\sum_k a_{ik}b_{jk}np.hypot(a, b)np.interp(x, xp, fp)np.where(a > 2) == np.nonzero(a > 2) == [3, 4]np.where(a > 2, 1, 0) == [0, 0, 1, 1]np.floor(), np.ceil(), np.round()np.sort(): returns a copynp.allclose(a, b, rtol=1e-05, atol=1e-08)np.diff(a, axis, prepend=None, append=None)np.average(a, weights=None)np.quantile(a, 0.5), to ignore nans:
np.nanquantile(a, 0.5)np.percentile(a, 50)np.diagonal(a) returns diagonalSet routines
unique, counts = np.unique(x, return_counts=True)
Single array
np.repeat(a, nreps, axis)
e.g. np.repeat(a[:, :, np.newaxis], 3, axis=2)np.tile(a, reps_shape)np.broadcast_to(a, shape): Broadcast an array to a new
shape using views.np.broadcast_shapes(shapes): Broadcast the input shapes
into a single shape (so can see shape of output)Joining arrays
np.concatenate(arrays, axis=0)
np.stack(arrays, axis=0): creates a new axis at
axisnp.block(arrays): Assemble an nd-array from nested
lists of blocks. Can make a block matrix.linalg.eig()linalg.qr()linalg.cholesky()u, s, vh = linalg.svd(a)from numpy.random import default_rng
rng = default_rng(seed)
vals = rng.standard_normal(10)
more_vals = rng.standard_normal(10)
rng.binomial(n, p[, size])
rng.exponential([scale, size])
rng.normal(loc=0, scale=1, size=None)
rng.uniform(low=0, high=1, size=None)
rng.shuffle(arr)
rng.integers(low_incl, high_excl, size=None|shape) # prefer to randint
rng.choice(a, size=None, replace=True, shuffle=True)https://numpy.org/devdocs/reference/random/generator.html
from numpy.polynomial import Polynomial as poly
p1 = poly([coefs])
p2 = poly.fit(x,y,deg)
p3 = poly.fromroots([roots])
p1(x) # evaluate
p1 '+-/*' p2
p.roots()
# calculus: n is nr of times to apply
p.integ(n, lbnd=-1, k=1) # k is the integ constant
p.deriv(n)import numpy as np
date = np.datetime64('2020-07-04')
date = np.datetime64('2020', 'D') # 2020-01-01
date_array = np.array('2020-07-04', dtype=np.datetime64)
one_day = np.timedelta64(1, 'D')
date +-/ one_daynp.save('filename', a)
b = np.load('filename.npy')
np.savetxt('filename.csv', a)
np.loadtxt('filename.csv')
np.savez_compressed('filename', a)
np.array2string(a, max_line_width=10).npy and .npz files are smaller and faster
to read.
help(function)function?, object? in IPythonlookfor(keyword)info(func/class/module)source(npy_obj)obj?? gives the source code in IPython