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 arrayx
np.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 withn
rows andn
columnsnp.arange(start, stop, step)
| create evenly spaced values that arestep
apart, between astart
andend
valuenp.linspace(start, stop, num)
| createnum
evenly spaced values between a start and end value.np.reshape(x, newshape)
| change the shape ofx
tonewshape
np.random.random(size)
| returnsize
random 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)
| drawsize
random samples from aN ( loc, scale^2 )
distribution.np.random.randint(low, high, size)
| drawsize
random 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 valuesx
down the diagonalk
.np.tile(x, reps)
| repeatx
a total ofreps
times, wherereps
can be of multiple dimensions.np.unravel_index(indices, dims)
| in an array of shapedims
, what is the index of theindices
th 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 matricesA
andB
.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 arrayx1
to that of elements in array x2, comparing element|wise.np.intersect1d(x1, x2)
| find the intersection of arrayx1
and arrayx2
, returning an ordered set.np.union1d(x1, x2)
| find the union of arrayx1
and arrayx2
, returning an ordered set.np.datetime64('s1')
| convert a strings1
to a numpy datetime.np.timedelta64('s1')
| convert a strings1
to a numpy timedelta, with which you can perform date arithmetic.np.arange('s1', 's2', dtype='datetime64[D]')
| get a list of days between two datess1
ands2
.np.add(x1, x2, out)
| add two arraysx1
andx2
. Ifout
equalsx1
, thenx1
will 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 arrayx
in ascending ordernp.sum(x, axis)
| return the sum of an arrayx
over a particular axis.np.add.reduce(x, axis)
| a quicker way of finding sum of an arrayx
over a particular axis, for smallx
. This is an example of a ufunc.np.array_equal(x1, x2)
| check to see if two arraysx1
andx2
are equal.np.meshgrid(x1, x2)
| create a 2d rectangular grid of values from arrayx1
and arrayx2
. See here for further explanation.np.outer(x, y)
| calculate the outer product of two vectorsx
andy
.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 valuesv
into an arrayx
at 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 ofx
along 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!