44 Numpy Functions
Have you heard about the Python package numpy? Probably.
But maybe you’re looking for an easy reference of useful numpy functions. Maybe that’s why you’re here. Well, you’re in luck!
Get started by opening up a editor. First, we’ll import the numpy package: import numpy as np.
Now cast your eye over these functions. In no particular order:
np.__version__| Return the version of numpy you have loaded.np.shape(x)| return the shape of an arrayx. It’s essentially the number of rows and columns inx.np.ndim(x)| return the number of dimensions of an arrayxnp.zeros(shape)| create an array of zeros in the shape you specifynp.ones(shape)| create an array of ones in the shape you specifynp.eye(n)| create a identity matrix withnrows andncolumnsnp.arange(start, stop, step)| create evenly spaced values that arestepapart, between astartandendvaluenp.linspace(start, stop, num)| createnumevenly spaced values between a start and end value.np.reshape(x, newshape)| change the shape ofxtonewshapenp.random.random(size)| returnsizerandom numbers between [0,1).np.random.rand(d0, d1, ..., dn)| random uniformly distributed values in [0,1), in shape (d0,d1, …,dn).np.random.randn(d0, d1, ..., dn)| random normally distributed values from the standard normal distribution, in a shape(d0 , d1 , ..., dn ).np.random.normal(loc, scale, size)| drawsizerandom samples from aN ( loc, scale^2 )distribution.np.random.randint(low, high, size)| drawsizerandom numbers from a U(low, high) distribution.np.pad(x)| pads an array. Parameters determine what you pad the array with, how large the pad is and the mode of padding (there are lots!).np.diag(x, k)| construct a diagonal array, with valuesxdown the diagonalk.np.tile(x, reps)| repeatxa total ofrepstimes, whererepscan be of multiple dimensions.np.unravel_index(indices, dims)| in an array of shapedims, what is the index of theindicesth element? For example,np.unravel_index( 32, (3,3,5) ) # = (2, 0, 2).np.dtype()| create your own custom data types.np.dot(A, B)| find the dot product of two matricesAandB.np.ndarray.astype(dtype)| change the data type of an array while making a copy of it.np.ceil(x)| rounds decimal numbers up to the nearest integer.np.floor(x)| rounds decimal numbers down to the nearest integer.np.copysign(x1, x2)| changes the sign of elements in arrayx1to that of elements in array x2, comparing element|wise.np.intersect1d(x1, x2)| find the intersection of arrayx1and arrayx2, returning an ordered set.np.union1d(x1, x2)| find the union of arrayx1and arrayx2, returning an ordered set.np.datetime64('s1')| convert a strings1to a numpy datetime.np.timedelta64('s1')| convert a strings1to a numpy timedelta, with which you can perform date arithmetic.np.arange('s1', 's2', dtype='datetime64[D]')| get a list of days between two datess1ands2.np.add(x1, x2, out)| add two arraysx1andx2. Ifoutequalsx1, thenx1will be overwritten with the result of the addition. Same thing fornp.multiply,np.divide,np.negative.np.trunc(x)| get rid of decimal points in an floating point arrayx, leaving just the integer components.np.sort(x)| sort an arrayxin ascending ordernp.sum(x, axis)| return the sum of an arrayxover a particular axis.np.add.reduce(x, axis)| a quicker way of finding sum of an arrayxover a particular axis, for smallx. This is an example of a ufunc.np.array_equal(x1, x2)| check to see if two arraysx1andx2are equal.np.meshgrid(x1, x2)| create a 2d rectangular grid of values from arrayx1and arrayx2. See here for further explanation.np.outer(x, y)| calculate the outer product of two vectorsxandy.np.setprintoptions(threshold)| change the number of elements displayed when printing an array to the console.np.argmax(x)| return the indices of the maximum values along an axis for an arrayx.np.argmin(x)| return the indices of the minimum values along an axis for an arrayx.np.put(x, ind, v)| put valuesvinto an arrayxat indicesind,replacing what was there before.np.argsort(x)| return indices that would sort an arrayx. See here for further explanation.np.any(x, axis)| test if any array element ofxalong a given axis evaluates to True.np.ndarray.flat()| a flat iterator object to iterate over arrays. Can be indexed with square brackets.
Knowing how to use these functions will give you a great starting base for your numpy adventures. Good luck!