This page contains a large database of examples demonstrating most of the Numpy functionality. Numpy Example List With Doc has these examples interleaved with the built-in documentation, but is not as regularly updated as this page. The examples here can be easily accessed from Python using the Numpy Example Fetcher.
Contents
- ...
- []
- abs()
- absolute()
- accumulate()
- add()
- all()
- allclose()
- alltrue()
- angle()
- any()
- append()
- apply_along_axis()
- apply_over_axes()
- arange()
- arccos()
- arccosh()
- arcsin()
- arcsinh()
- arctan()
- arctan2()
- arctanh()
- argmax()
- argmin()
- argsort()
- array()
- arrayrange()
- array_split()
- asarray()
- asanyarray()
- asmatrix()
- astype()
- atleast_1d()
- atleast_2d()
- atleast_3d()
- average()
- beta()
- binary_repr()
- bincount()
- binomial()
- bitwise_and()
- bitwise_or()
- bitwise_xor()
- bmat()
- broadcast()
- bytes()
- c_[]
- cast[]()
- ceil()
- choose()
- clip()
- column_stack()
- compress()
- concatenate()
- conj()
- conjugate()
- copy()
- corrcoef()
- cos()
- cov()
- cross()
- cumprod()
- cumsum()
- delete()
- diag()
- diagflat()
- diagonal()
- diff()
- digitize()
- dot()
- dsplit()
- dstack()
- dtype()
- empty()
- empty_like()
- expand_dims()
- eye()
- fft()
- fftfreq()
- fftshift()
- fill()
- finfo()
- fix()
- flat
- flatten()
- fliplr()
- flipud()
- floor()
- fromarrays()
- frombuffer()
- fromfile()
- fromfunction()
- fromiter()
- generic
- gumbel()
- histogram()
- hsplit()
- hstack()
- hypot()
- identity()
- ifft()
- imag
- index_exp[]
- indices()
- inf
- inner()
- insert()
- inv()
- iscomplex()
- iscomplexobj()
- item()
- ix_()
- lexsort()
- linspace()
- loadtxt()
- logical_and()
- logical_not()
- logical_or()
- logical_xor()
- logspace()
- lstsq()
- mat()
- matrix()
- max()
- maximum()
- mean()
- median()
- mgrid[]
- min()
- minimum()
- multiply()
- nan
- ndenumerate()
- ndim
- ndindex()
- newaxis
- nonzero()
- ogrid()
- ones()
- ones_like()
- outer()
- permutation()
- piecewise()
- pinv()
- poisson()
- poly1d()
- polyfit()
- prod()
- ptp()
- put()
- putmask()
- r_[]
- rand()
- randint()
- randn()
- random_integers()
- random_sample()
- ranf()
- ravel()
- real
- recarray()
- reduce()
- repeat()
- reshape()
- resize()
- rollaxis()
- round()
- rot90()
- s_[]
- sample()
- savetxt()
- searchsorted()
- seed()
- select()
- set_printoptions()
- shape
- shuffle()
- slice()
- solve()
- sometrue()
- sort()
- split()
- squeeze()
- std()
- standard_normal()
- sum()
- svd()
- swapaxes()
- T
- take()
- tensordot()
- tile()
- tofile()
- tolist()
- trace()
- transpose()
- tri()
- tril()
- trim_zeros()
- triu()
- typeDict()
- uniform()
- unique()
- vander()
- var()
- vdot()
- vectorize()
- view()
- vonmises()
- vsplit()
- vstack()
- weibull()
- where()
- zeros()
- zeros_like()
...
>>> from numpy import *
>>> a = arange(12)
>>> a = a.reshape(3,2,2)
>>> print a
[[[ 0 1]
[ 2 3]]
[[ 4 5]
[ 6 7]]
[[ 8 9]
[10 11]]]
>>> a[...,0] # same as a[:,:,0]
array([[ 0, 2],
[ 4, 6],
[ 8, 10]])
>>> a[1:,...] # same as a[1:,:,:] or just a[1:]
array([[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]])
[]
>>> from numpy import *
>>> a = array([ [ 0, 1, 2, 3, 4],
... [10,11,12,13,14],
... [20,21,22,23,24],
... [30,31,32,33,34] ])
>>>
>>> a[0,0] # indices start by zero
0
>>> a[-1] # last row
array([30, 31, 32, 33, 34])
>>> a[1:3,1:4] # subarray
array([[11, 12, 13],
[21, 22, 23]])
>>>
>>> i = array([0,1,2,1]) # array of indices for the first axis
>>> j = array([1,2,3,4]) # array of indices for the second axis
>>> a[i,j]
array([ 1, 12, 23, 14])
>>>
>>> a[a<13] # boolean indexing
array([ 0, 1, 2, 3, 4, 10, 11, 12])
>>>
>>> b1 = array( [True,False,True,False] ) # boolean row selector
>>> a[b1,:]
array([[ 0, 1, 2, 3, 4],
[20, 21, 22, 23, 24]])
>>>
>>> b2 = array( [False,True,True,False,True] ) # boolean column selector
>>> a[:,b2]
array([[ 1, 2, 4],
[11, 12, 14],
[21, 22, 24],
[31, 32, 34]])
See also: ..., newaxis, ix_, indices, nonzero, where, slice
abs()
>>> from numpy import *
>>> abs(-1)
1
>>> abs(array([-1.2, 1.2]))
array([ 1.2, 1.2])
>>> abs(1.2+1j)
1.5620499351813308
absolute()
Synonym for abs()
See abs
accumulate()
>>> from numpy import *
>>> add.accumulate(array([1.,2.,3.,4.])) # like reduce() but also gives intermediate results
array([ 1., 3., 6., 10.])
>>> array([1., 1.+2., (1.+2.)+3., ((1.+2.)+3.)+4.]) # this is what it computed
array([ 1., 3., 6., 10.])
>>> multiply.accumulate(array([1.,2.,3.,4.])) # works also with other operands
array([ 1., 2., 6., 24.])
>>> array([1., 1.*2., (1.*2.)*3., ((1.*2.)*3.)*4.]) # this is what it computed
array([ 1., 2., 6., 24.])
>>> add.accumulate(array([[1,2,3],[4,5,6]]), axis = 0) # accumulate every column separately
array([[1, 2, 3],
[5, 7, 9]])
>>> add.accumulate(array([[1,2,3],[4,5,6]]), axis = 1) # accumulate every row separately
array([[ 1, 3, 6],
[ 4, 9, 15]])
See also: reduce, cumprod, cumsum
add()
>>> from numpy import *
>>> add(array([-1.2, 1.2]), array([1,3]))
array([-0.2, 4.2])
>>> array([-1.2, 1.2]) + array([1,3])
array([-0.2, 4.2])
all()
>>> from numpy import *
>>> a = array([True, False, True])
>>> a.all() # if all elements of a are True: return True; otherwise False
False
>>> all(a) # this form also exists
False
>>> a = array([1,2,3])
>>> all(a > 0) # equivalent to (a > 0).all()
True
See also: any, alltrue, sometrue
allclose()
>>> allclose(array([1e10,1e-7]), array([1.00001e10,1e-8]))
False
>>> allclose(array([1e10,1e-8]), array([1.00001e10,1e-9]))
True
>>> allclose(array([1e10,1e-8]), array([1.0001e10,1e-9]))
False
alltrue()
>>> from numpy import *
>>> b = array([True, False, True, True])
>>> alltrue(b)
False
>>> a = array([1, 5, 2, 7])
>>> alltrue(a >= 5)
False
angle()
>>> from numpy import *
>>> angle(1+1j) # in radians
0.78539816339744828
>>> angle(1+1j,deg=True) # in degrees
45.0
any()
>>> from numpy import *
>>> a = array([True, False, True])
>>> a.any() # gives True if at least 1 element of a is True, otherwise False
True
>>> any(a) # this form also exists
True
>>> a = array([1,2,3])
>>> (a >= 1).any() # equivalent to any(a >= 1)
True
See also: all, alltrue, sometrue
append()
>>> from numpy import *
>>> a = array([10,20,30,40])
>>> append(a,50)
array([10, 20, 30, 40, 50])
>>> append(a,[50,60])
array([10, 20, 30, 40, 50, 60])
>>> a = array([[10,20,30],[40,50,60],[70,80,90]])
>>> append(a,[[15,15,15]],axis=0)
array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90],
[15, 15, 15]])
>>> append(a,[[15],[15],[15]],axis=1)
array([[10, 20, 30, 15],
[40, 50, 60, 15],
[70, 80, 90, 15]])
See also: insert, delete, concatenate
apply_along_axis()
>>> from numpy import *
>>> def myfunc(a): # function works on a 1d arrays, takes the average of the 1st an last element
... return (a[0]+a[-1])/2
...
>>> b = array([[1,2,3],[4,5,6],[7,8,9]])
>>> apply_along_axis(myfunc,0,b) # apply myfunc to each column (axis=0) of b
array([4, 5, 6])
>>> apply_along_axis(myfunc,1,b) # apply myfunc to each row (axis=1) of b
array([2, 5, 8])
See also: apply_over_axes, vectorize
apply_over_axes()
>>> from numpy import *
>>> a = arange(24).reshape(2,3,4) # a has 3 axes: 0,1 and 2
>>> a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> apply_over_axes(sum, a, [0,2]) # sum over all axes except axis=1, result has same shape as original
array([[[ 60],
[ 92],
[124]]])
See also: apply_along_axis, vectorize
arange()
>>> from numpy import *
>>> arange(3)
array([0, 1, 2])
>>> arange(3.0)
array([ 0., 1., 2.])
>>> arange(3, dtype=float)
array([ 0., 1., 2.])
>>> arange(3,10) # start,stop
array([3, 4, 5, 6, 7, 8, 9])
>>> arange(3,10,2) # start,stop,step
array([3, 5, 7, 9])
See also: r_, linspace, logspace, mgrid, ogrid
arccos()
>>> from numpy import *
>>> arccos(array([0, 1]))
array([ 1.57079633, 0. ])
See also: arcsin, arccosh, arctan, arctan2
arccosh()
>>> from numpy import *
>>> arccosh(array([e, 10.0]))
array([ 1.65745445, 2.99322285])
See also: arccos, arcsinh, arctanh
arcsin()
>>> from numpy import *
>>> arcsin(array([0, 1]))
array([ 0. , 1.57079633])
See also: arccos, arctan, arcsinh
arcsinh()
>>> from numpy import *
>>> arcsinh(array([e, 10.0]))
array([ 1.72538256, 2.99822295])
See also: arccosh, arcsin, arctanh
arctan()
>>> from numpy import *
>>> arctan(array([0, 1]))
array([ 0. , 0.78539816])
See also: arccos, arcsin, arctanh
arctan2()
>>> from numpy import *
>>> arctan2(array([0, 1]), array([1, 0]))
array([ 0. , 1.57079633])
See also: arcsin, arccos, arctan, arctanh
arctanh()
>>> from numpy import *
>>> arctanh(array([0, -0.5]))
array([ 0. , -0.54930614])
See also: arcsinh, arccosh, arctan, arctan2
argmax()
>>> from numpy import *
>>> a = array([10,20,30])
>>> maxindex = a.argmax()
>>> a[maxindex]
30
>>> a = array([[10,50,30],[60,20,40]])
>>> maxindex = a.argmax()
>>> maxindex
3
>>> a.ravel()[maxindex]
60
>>> a.argmax(axis=0) # for each column: the row index of the maximum value
array([1, 0, 1])
>>> a.argmax(axis=1) # for each row: the column index of the maximum value
array([1, 0])
>>> argmax(a) # also exists, slower, default is axis=-1
array([1, 0])
See also: argmin, nan, min, max, maximum, minimum
argmin()
>>> from numpy import *
>>> a = array([10,20,30])
>>> minindex = a.argmin()
>>> a[minindex]
10
>>> a = array([[10,50,30],[60,20,40]])
>>> minindex = a.argmin()
>>> minindex
0
>>> a.ravel()[minindex]
10
>>> a.argmin(axis=0) # for each column: the row index of the minimum value
array([0, 1, 0])
>>> a.argmin(axis=1) # for each row: the column index of the minimum value
array([0, 1])
>>> argmin(a) # also exists, slower, default is axis=-1
array([0, 1])
See also: argmax, nan, min, max, maximum, minimum
