Numpy_Example_List - SciPy wiki dump (original) (raw)
This is an archival dump of old wiki content --- see scipy.org for current material
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.
This example list is incredibly useful, and we would like to get all the good examples and comments integrated in the official numpy documentation so that they are also shipped with numpy. You can help. The official numpy documentation can be edited on http://docs.scipy.org.
- 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()
- det()
- 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()
- unique1d()
- 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]
array([[ 0, 2], [ 4, 6], [ 8, 10]]) 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] 0 a[-1] array([30, 31, 32, 33, 34]) a[1:3,1:4] array([[11, 12, 13], [21, 22, 23]])
i = array([0,1,2,1]) j = array([1,2,3,4]) a[i,j] array([ 1, 12, 23, 14])
a[a<13] array([ 0, 1, 2, 3, 4, 10, 11, 12])
b1 = array( [True,False,True,False] ) a[b1,:] array([[ 0, 1, 2, 3, 4], [20, 21, 22, 23, 24]])
b2 = array( [False,True,True,False,True] ) 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.])) array([ 1., 3., 6., 10.]) array([1., 1.+2., (1.+2.)+3., ((1.+2.)+3.)+4.]) array([ 1., 3., 6., 10.]) multiply.accumulate(array([1.,2.,3.,4.])) array([ 1., 2., 6., 24.]) array([1., 1.*2., (1.*2.)*3., ((1.*2.)*3.)*4.]) array([ 1., 2., 6., 24.]) add.accumulate(array([[1,2,3],[4,5,6]]), axis = 0) array([[1, 2, 3], [5, 7, 9]]) add.accumulate(array([[1,2,3],[4,5,6]]), axis = 1) 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() False all(a) False a = array([1,2,3]) all(a > 0) 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) 0.78539816339744828 angle(1+1j,deg=True) 45.0
any()
from numpy import * a = array([True, False, True]) a.any() True any(a) True a = array([1,2,3]) (a >= 1).any() 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): ... return (a[0]+a[-1])/2 ... b = array([[1,2,3],[4,5,6],[7,8,9]]) apply_along_axis(myfunc,0,b) array([4, 5, 6]) apply_along_axis(myfunc,1,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 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]) 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) array([3, 4, 5, 6, 7, 8, 9]) arange(3,10,2) 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) array([1, 0, 1]) a.argmax(axis=1) array([1, 0]) argmax(a) 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) array([0, 1, 0]) a.argmin(axis=1) array([0, 1]) argmin(a) array([0, 1])
See also: argmax, nan, min, max, maximum, minimum
argsort()
argsort(axis=-1, kind="quicksort")
from numpy import * a = array([2,0,8,4,1]) ind = a.argsort() ind array([1, 4, 0, 3, 2]) a[ind] array([0, 1, 2, 4, 8]) ind = a.argsort(kind='merge') a = array([[8,4,1],[2,0,9]]) ind = a.argsort(axis=0) ind array([[1, 1, 0], [0, 0, 1]]) a[ind,[[0,1,2],[0,1,2]]] array([[2, 0, 1], [8, 4, 9]]) ind = a.argsort(axis=1) ind array([[2, 1, 0], [1, 0, 2]]) a = ones(17) a.argsort() array([ 0, 14, 13, 12, 11, 10, 9, 15, 8, 6, 5, 4, 3, 2, 1, 7, 16]) a.argsort(kind="mergesort") array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) ind = argsort(a)
array()
from numpy import * array([1,2,3]) array([1, 2, 3]) array([1,2,3], dtype=complex) array([ 1.+0.j, 2.+0.j, 3.+0.j]) array(1, copy=0, subok=1, ndmin=1) array([1]) array(1, copy=0, subok=1, ndmin=2) array([[1]]) array(1, subok=1, ndmin=2) array([[1]]) mydescriptor = {'names': ('gender','age','weight'), 'formats': ('S1', 'f4', 'f4')} a = array([('M',64.0,75.0),('F',25.0,60.0)], dtype=mydescriptor) print a [('M', 64.0, 75.0) ('F', 25.0, 60.0)] a['weight'] array([ 75., 60.], dtype=float32) a.dtype.names ('gender','age','weight') mydescriptor = [('age',int16),('Nchildren',int8),('weight',float32)] a = array([(64,2,75.0),(25,0,60.0)], dtype=mydescriptor) a['Nchildren'] array([2, 0], dtype=int8) mydescriptor = dtype([('x', 'f4'),('y', 'f4'), ... ('nested', [('i', 'i2'),('j','i2')])]) array([(1.0, 2.0, (1,2))], dtype=mydescriptor) array([(1.0, 2.0, (1, 2))], dtype=[('x', '<f4'), ('y', '<f4'), ('nested', [('i', '<i2'), ('j', '<i2')])]) array([(1.0, 2.0, (1,2)), (2.1, 3.2, (3,2))], dtype=mydescriptor) array([(1.0, 2.0, (1, 2)), (2.0999999046325684, 3.2000000476837158, (3, 2))], dtype=[('x', '<f4'), ('y', '<f4'), ('nested', [('i', '<i2'), ('j', '<i2')])]) a=array([(1.0, 2.0, (1,2)), (2.1, 3.2, (3,2))], dtype=mydescriptor) a['x'] array([ 1. , 2.0999999], dtype=float32) a['nested'] array([(1, 2), (3, 2)], dtype=[('i', '<i2'), ('j', '<i2')]) a['nested']['i'] mydescriptor = dtype([('x', 'f4'),('y', 'f4'), ... ('nested', [('i', 'i2'),('j','i2')])]) array([(1.0, 2.0, (1,2))], dtype=mydescriptor) array([(1.0, 2.0, (1, 2))], dtype=[('x', '<f4'), ('y', '<f4'), ('nested', [('i', '<i2'), ('j', '<i2')])]) array([(1.0, 2.0, (1,2)), (2.1, 3.2, (3,2))], dtype=mydescriptor) array([(1.0, 2.0, (1, 2)), (2.0999999046325684, 3.2000000476837158, (3, 2))], dtype=[('x', '<f4'), ('y', '<f4'), ('nested', [('i', '<i2'), ('j', '<i2')])]) a=array([(1.0, 2.0, (1,2)), (2.1, 3.2, (3,2))], dtype=mydescriptor) a['x'] array([ 1. , 2.0999999], dtype=float32) a['nested'] array([(1, 2), (3, 2)], dtype=[('i', '<i2'), ('j', '<i2')]) a['nested']['i'] array([1, 3], dtype=int16)
arrayrange()
Synonym for arange()
See arange
array_split()
from numpy import * a = array([[1,2,3,4],[5,6,7,8]]) array_split(a,2,axis=0) [array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]])] array_split(a,4,axis=1) [array([[1], [5]]), array([[2], [6]]), array([[3], [7]]), array([[4], [8]])] array_split(a,3,axis=1) [array([[1, 2], [5, 6]]), array([[3], [7]]), array([[4], [8]])] array_split(a,[2,3],axis=1) [array([[1, 2], [5, 6]]), array([[3], [7]]), array([[4], [8]])]
See also: dsplit, hsplit, vsplit, split, concatenate
asarray()
from numpy import * m = matrix('1 2; 5 8') m matrix([[1, 2], [5, 8]]) a = asarray(m) a array([[1, 2], [5, 8]]) m[0,0] = -99 m matrix([[-99, 2], [ 5, 8]]) a array([[-99, 2], [ 5, 8]])
See also: asmatrix, array, matrix, mat
asanyarray()
from numpy import * a = array([[1,2],[5,8]]) a array([[1, 2], [5, 8]]) m = matrix('1 2; 5 8') m matrix([[1, 2], [5, 8]]) asanyarray(a) array([[1, 2], [5, 8]]) asanyarray(m) matrix([[1, 2], [5, 8]]) asanyarray([1,2,3]) array([1, 2, 3])
See also: asmatrix, asarray, array, mat
asmatrix()
from numpy import * a = array([[1,2],[5,8]]) a array([[1, 2], [5, 8]]) m = asmatrix(a) m matrix([[1, 2], [5, 8]]) a[0,0] = -99 a array([[-99, 2], [ 5, 8]]) m matrix([[-99, 2], [ 5, 8]])
See also: asarray, array, matrix, mat
astype()
from numpy import * x = array([1,2,3]) y = x.astype(float64) type(y[0]) <type 'numpy.float64'> x.astype(None) array([1., 2., 3.])
See also: cast, dtype, ceil, floor, round_, fix
atleast_1d()
from numpy import * a = 1 b = array([2,3]) c = array([[4,5],[6,7]]) d = arange(8).reshape(2,2,2) d array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) atleast_1d(a,b,c,d) [array([1]), array([2, 3]), array([[4, 5], [6, 7]]), array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])]
See also: atleast_2d, atleast_3d, newaxis, expand_dims
atleast_2d()
from numpy import * a = 1 b = array([2,3]) c = array([[4,5],[6,7]]) d = arange(8).reshape(2,2,2) d array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) atleast_2d(a,b,c,d) [array([[1]]), array([[2, 3]]), array([[4, 5], [6, 7]]), array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])]
See also: atleast_1d, atleast_3d, newaxis, expand_dims
atleast_3d()
from numpy import * a = 1 b = array([2,3]) c = array([[4,5],[6,7]]) d = arange(8).reshape(2,2,2) d array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) atleast_3d(a,b,c,d) [array([[[1]]]), array([[[2], [3]]]), array([[[4], [5]], [[6], [7]]]), array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])]
See also: atleast_1d, atleast_2d, newaxis, expand_dims
average()
from numpy import * a = array([1,2,3,4,5]) w = array([0.1, 0.2, 0.5, 0.2, 0.2]) average(a) 3.0 average(a,weights=w) 3.1666666666666665 average(a,weights=w,returned=True) (3.1666666666666665, 1.2)
beta()
from numpy import * from numpy.random import * beta(a=1,b=10,size=(2,2)) array([[ 0.02571091, 0.04973536], [ 0.04887027, 0.02382052]])
See also: seed
binary_repr()
from numpy import * a = 25 binary_repr(a) '11001' b = float_(pi) b.nbytes 8 binary_repr(b.view('u8')) '1010100010001000010110100011000'
bincount()
from numpy import * a = array([1,1,1,1,2,2,4,4,5,6,6,6]) bincount(a) array([0, 4, 2, 0, 2, 1, 3]) a = array([5,4,4,2,2]) w = array([0.1, 0.2, 0.1, 0.3, 0.5]) bincount(a) array([0, 0, 2, 0, 2, 1]) bincount(a, weights=w) array([ 0. , 0. , 0.8, 0. , 0.3, 0.1])
binomial()
from numpy import * from numpy.random import * binomial(n=100,p=0.5,size=(2,3)) array([[38, 50, 53], [56, 48, 54]]) from pylab import * hist(binomial(100,0.5,(1000)), 20)
See also: random_sample, uniform, standard_normal, seed
bitwise_and()
from numpy import * bitwise_and(array([2,5,255]), array([4,4,4])) array([0, 4, 4]) bitwise_and(array([2,5,255,2147483647L],dtype=int32), array([4,4,4,2147483647L],dtype=int32)) array([ 0, 4, 4, 2147483647])
See also: bitwise_or, bitwise_xor, logical_and
bitwise_or()
from numpy import * bitwise_or(array([2,5,255]), array([4,4,4])) array([ 6, 5, 255]) bitwise_or(array([2,5,255,2147483647L],dtype=int32), array([4,4,4,2147483647L],dtype=int32)) array([ 6, 5, 255, 2147483647])
See also: bitwise_and, bitwise_xor, logical_or
bitwise_xor()
from numpy import * bitwise_xor(array([2,5,255]), array([4,4,4])) array([ 6, 1, 251]) bitwise_xor(array([2,5,255,2147483647L],dtype=int32), array([4,4,4,2147483647L],dtype=int32)) array([ 6, 1, 251, 0])
See also: bitwise_and, bitwise_or, logical_xor
bmat()
from numpy import * a = mat('1 2; 3 4') b = mat('5 6; 7 8') bmat('a b; b a') matrix([[1, 2, 5, 6], [3, 4, 7, 8], [5, 6, 1, 2], [7, 8, 3, 4]])
See also: mat
broadcast()
from numpy import * a = array([[1,2],[3,4]]) b = array([5,6]) c = broadcast(a,b) c.nd 2 c.shape (2, 2) c.size 4 for value in c: print value ... (1, 5) (2, 6) (3, 5) (4, 6) c.reset() c.next() (1, 5)
See also: ndenumerate, ndindex, flat
bytes()
from numpy import * from numpy.random import bytes print repr(bytes(5)) 'o\x07\x9f\xdf\xdf' print repr(bytes(5)) '\x98\xc9KD\xe0'
See also: shuffle, permutation, seed
c_[]
from numpy import * c_[1:5] array([1, 2, 3, 4]) c_[1:5,2:6] array([[1, 2], [2, 3], [3, 4], [4, 5]]) a = array([[1,2,3],[4,5,6]]) c_[a,a] array([[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]]) c_['0',a,a] array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]])
See also: r_, hstack, vstack, column_stack, concatenate, bmat, s_
cast[]()
from numpy import * x = arange(3) x.dtype dtype('int32') cast'int64' array([0, 1, 2], dtype=int64) cast'uint' array([0, 1, 2], dtype=uint32) castfloat128 array([0.0, 1.0, 2.0], dtype=float128) cast.keys()
ceil()
from numpy import * a = array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7]) ceil(a) array([-1., -1., -0., 1., 2., 2.])
See also: floor, round_, fix, astype
choose()
from numpy import * choice0 =array([10,12,14,16]) choice1 =array([20,22,24,26]) choice2 =array([30,32,34,36]) selector = array([0,0,2,1]) selector.choose(choice0,choice1,choice2) array([10, 12, 34, 26]) a = arange(4) choose(a >= 2, (choice0, choice1)) array([10, 12, 24, 26])
See also: compress, take, where, select
clip()
from numpy import * a = array([5,15,25,3,13]) a.clip(min=10,max=20) array([10, 15, 20, 10, 13]) clip(a,10,20)
column_stack()
from numpy import * a = array([1,2]) b = array([3,4]) c = array([5,6]) column_stack((a,b,c)) array([[1, 3, 5], [2, 4, 6]])
See also: concatenate, dstack, hstack, vstack, c_
compress()
from numpy import * a = array([10, 20, 30, 40]) condition = (a > 15) & (a < 35) condition array([False, True, True, False], dtype=bool) a.compress(condition) array([20, 30]) a[condition] array([20, 30]) compress(a >= 30, a) array([30, 40]) b = array([[10,20,30],[40,50,60]]) b.compress(b.ravel() >= 22) array([30, 40, 50, 60]) x = array([3,1,2]) y = array([50, 101]) b.compress(x >= 2, axis=1) array([[10, 30], [40, 60]]) b.compress(y >= 100, axis=0) array([[40, 50, 60]])
See also: choose, take, where, trim_zeros, unique, unique1d
concatenate()
from numpy import * x = array([[1,2],[3,4]]) y = array([[5,6],[7,8]]) concatenate((x,y)) array([[1, 2], [3, 4], [5, 6], [7, 8]]) concatenate((x,y),axis=1) array([[1, 2, 5, 6], [3, 4, 7, 8]])
See also: append, column_stack, dstack, hstack, vstack, array_split
conj()
Synonym for conjugate()
See conjugate()
conjugate()
a = array([1+2j,3-4j]) a.conj() array([ 1.-2.j, 3.+4.j]) a.conjugate() array([ 1.-2.j, 3.+4.j]) conj(a) conjugate(a)
See also: vdot
copy()
from numpy import * a = array([1,2,3]) a array([1, 2, 3]) b = a b[1] = 4 a array([1, 4, 3]) a = array([1,2,3]) b = a.copy() b[1] = 4 a array([1, 2, 3]) b array([1, 4, 3])
See also: view
corrcoef()
from numpy import * T = array([1.3, 4.5, 2.8, 3.9]) P = array([2.7, 8.7, 4.7, 8.2]) print corrcoef([T,P]) [[ 1. 0.98062258] [ 0.98062258 1. ]] rho = array([8.5, 5.2, 6.9, 6.5]) data = column_stack([T,P,rho]) print corrcoef([T,P,rho]) [[ 1. 0.98062258 -0.97090288] [ 0.98062258 1. -0.91538464] [-0.97090288 -0.91538464 1. ]]
cos()
cos(array([0, pi/2, pi])) array([ 1.00000000e+00, 6.12303177e-17, -1.00000000e+00])
cov()
from numpy import * x = array([1., 3., 8., 9.]) variance = cov(x) variance = cov(x, bias=1) T = array([1.3, 4.5, 2.8, 3.9]) P = array([2.7, 8.7, 4.7, 8.2]) cov(T,P) 3.9541666666666657 rho = array([8.5, 5.2, 6.9, 6.5]) data = column_stack([T,P,rho]) print cov(data) [[ 1.97583333 3.95416667 -1.85583333] [ 3.95416667 8.22916667 -3.57083333] [-1.85583333 -3.57083333 1.84916667]]
cross()
from numpy import * x = array([1,2,3]) y = array([4,5,6]) cross(x,y) array([-3, 6, -3])
cumprod()
from numpy import * a = array([1,2,3]) a.cumprod() array([1, 2, 6]) cumprod(a) array([1, 2, 6]) a = array([[1,2,3],[4,5,6]]) a.cumprod(dtype=float) array([1., 2., 6., 24., 120., 720.]) a.cumprod(axis=0) array([[ 1, 2, 3], [ 4, 10, 18]]) a.cumprod(axis=1) array([[ 1, 2, 6], [ 4, 20, 120]])
See also: accumulate, prod, cumsum
cumsum()
from numpy import * a = array([1,2,3]) a.cumsum() array([1, 3, 6]) cumsum(a) array([1, 3, 6]) a = array([[1,2,3],[4,5,6]]) a.cumsum(dtype=float) array([ 1., 3., 6., 10., 15., 21.]) a.cumsum(axis=0) array([[1, 2, 3], [5, 7, 9]]) a.cumsum(axis=1) array([[ 1, 3, 6], [ 4, 9, 15]])
See also: accumulate, sum, cumprod
delete()
from numpy import * a = array([0, 10, 20, 30, 40]) delete(a, [2,4]) array([ 0, 10, 30]) a = arange(16).reshape(4,4) a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) delete(a, s_[1:3], axis=0) array([[ 0, 1, 2, 3], [12, 13, 14, 15]]) delete(a, s_[1:3], axis=1) array([[ 0, 3], [ 4, 7], [ 8, 11], [12, 15]])
det()
from numpy import * from numpy.linalg import det A = array([[1., 2.],[3., 4.]]) det(A) -2.0
See also: inv
diag()
from numpy import * a = arange(12).reshape(4,3) print a [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] print diag(a,k=0) [0 4 8] print diag(a,k=1) [1 5] print diag(array([1,4,5]),k=0) [[1 0 0] [0 4 0] [0 0 5]] print diag(array([1,4,5]),k=1) [[0 1 0 0] [0 0 4 0] [0 0 0 5] [0 0 0 0]]
See also: diagonal, diagflat, trace
diagflat()
from numpy import * x = array([[5,6],[7,8]]) diagflat(x) array([[5, 0, 0, 0], [0, 6, 0, 0], [0, 0, 7, 0], [0, 0, 0, 8]])
See also: diag, diagonal, flatten
diagonal()
from numpy import * a = arange(12).reshape(3,4) print a [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] a.diagonal() array([ 0, 5, 10]) a.diagonal(offset=1) array([ 1, 6, 11]) diagonal(a) array([ 0, 5, 10])
See also: diag, diagflat, trace
diff()
from numpy import * x = array([0,1,3,9,5,10]) diff(x) array([ 1, 2, 6, -4, 5]) diff(x,n=2) array([ 1, 4, -10, 9]) x = array([[1,3,6,10],[0,5,6,8]]) diff(x) array([[2, 3, 4], [5, 1, 2]]) diff(x,axis=0) array([[-1, 2, 0, -2]])
digitize()
from numpy import * x = array([0.2, 6.4, 3.0, 1.6]) bins = array([0.0, 1.0, 2.5, 4.0, 10.0]) d = digitize(x,bins) d array([1, 4, 3, 2]) for n in range(len(x)): ... print bins[d[n]-1], "<=", x[n], "<", bins[d[n]] ... 0.0 <= 0.2 < 1.0 4.0 <= 6.4 < 10.0 2.5 <= 3.0 < 4.0 1.0 <= 1.6 < 2.5
dot()
from numpy import * x = array([[1,2,3],[4,5,6]]) x.shape (2, 3) y = array([[1,2],[3,4],[5,6]]) y.shape (3, 2) dot(x,y) array([[22, 28], [49, 64]])
import numpy if id(dot) == id(numpy.core.multiarray.dot): ... print "Not using blas/lapack!"
See also: vdot, inner, multiply
dsplit()
from numpy import * a = array([[1,2],[3,4]]) b = dstack((a,a,a,a)) b.shape (2, 2, 4) c = dsplit(b,2) print c[0].shape, c[1].shape (2, 2, 2) (2, 2, 2) d = dsplit(b,[1,2]) print d[0].shape, d[1].shape, d[2].shape (2, 2, 1) (2, 2, 1) (2, 2, 2)
See also: split, array_split, hsplit, vsplit, dstack
dstack()
from numpy import * a = array([[1,2],[3,4]]) b = array([[5,6],[7,8]]) dstack((a,b)) array([[[1, 5], [2, 6]], [[3, 7], [4, 8]]])
See also: column_stack, concatenate, hstack, vstack, dsplit
dtype()
from numpy import * dtype('int16') dtype('int16') dtype([('f1', 'int16')]) dtype([('f1', '<i2')]) dtype([('f1', [('f1', 'int16')])]) dtype([('f1', [('f1', '<i2')])]) dtype([('f1', 'uint'), ('f2', 'int32')]) dtype([('f1', '<u4'), ('f2', '<i4')]) dtype([('a','f8'),('b','S10')]) dtype([('a', '<f8'), ('b', '|S10')]) dtype("i4, (2,3)f8") dtype([('f0', '<i4'), ('f1', '<f8', (2, 3))]) dtype([('hello',('int',3)),('world','void',10)]) dtype([('hello', '<i4', 3), ('world', '|V10')]) dtype(('int16', {'x':('int8',0), 'y':('int8',1)})) dtype(('<i2', [('x', '|i1'), ('y', '|i1')])) dtype({'names':['gender','age'], 'formats':['S1',uint8]}) dtype([('gender', '|S1'), ('age', '|u1')]) dtype({'surname':('S25',0),'age':(uint8,25)}) dtype([('surname', '|S25'), ('age', '|u1')])
a = dtype('int32') a dtype('int32') a.type <type 'numpy.int32'> a.kind 'i' a.char 'l' a.num 7 a.str '<i4' a.name 'int32' a.byteorder '=' a.itemsize 4 a = dtype({'surname':('S25',0),'age':(uint8,25)}) a.fields.keys() ['age', 'surname'] a.fields.values() [(dtype('uint8'), 25), (dtype('|S25'), 0)] a = dtype([('x', 'f4'),('y', 'f4'), ... ('nested', [('i', 'i2'),('j','i2')])]) a.fields['nested'] (dtype([('i', '<i2'), ('j', '<i2')]), 8) a.fields['nested'][0].fields['i'] (dtype('int16'), 0) a.fields['nested'][0].fields['i'][0].type <type 'numpy.int16'>
See also: array, typeDict, astype
empty()
from numpy import * empty(3) array([ 6.08581638e+000, 3.45845952e-323, 4.94065646e-324]) empty((2,3),int) array([[1075337192, 1075337192, 135609024], [1084062604, 1197436517, 1129066306]])
See also: ones, zeros, eye, identity
empty_like()
from numpy import * a = array([[1,2,3],[4,5,6]]) empty_like(a) array([[ 0, 25362433, 6571520], [ 21248, 136447968, 4]])
See also: ones_like, zeros_like
expand_dims()
from numpy import * x = array([1,2]) expand_dims(x,axis=0) array([[1, 2]]) expand_dims(x,axis=1) array([[1], [2]])
See also: newaxis, atleast_1d, atleast_2d, atleast_3d
eye()
from numpy import * eye(3,4,0,dtype=float) array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.]]) eye(3,4,1,dtype=float) array([[ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])
See also: ones, zeros, empty, identity
fft()
from numpy import * from numpy.fft import * signal = array([-2., 8., -6., 4., 1., 0., 3., 5.]) fourier = fft(signal) fourier array([ 13. +0.j , 3.36396103 +4.05025253j, 2. +1.j , -9.36396103-13.94974747j, -21. +0.j , -9.36396103+13.94974747j, 2. -1.j , 3.36396103 -4.05025253j])
N = len(signal) fourier = empty(N,complex) for k in range(N): ... fourier[k] = sum(signal * exp(-1j2pikarange(N)/N)) ... timestep = 0.1 fftfreq(N, d=timestep) array([ 0. , 1.25, 2.5 , 3.75, -5. , -3.75, -2.5 , -1.25])
See also: ifft, fftfreq, fftshift
fftfreq()
from numpy import * from numpy.fft import * signal = array([-2., 8., -6., 4., 1., 0., 3., 5.]) fourier = fft(signal) N = len(signal) timestep = 0.1 freq = fftfreq(N, d=timestep) freq array([ 0. , 1.25, 2.5 , 3.75, -5. , -3.75, -2.5 , -1.25])
fftshift(freq) array([-5. , -3.75, -2.5 , -1.25, 0. , 1.25, 2.5 , 3.75])
fftshift()
from numpy import * from numpy.fft import * signal = array([-2., 8., -6., 4., 1., 0., 3., 5.]) fourier = fft(signal) N = len(signal) timestep = 0.1 freq = fftfreq(N, d=timestep) freq array([ 0. , 1.25, 2.5 , 3.75, -5. , -3.75, -2.5 , -1.25])
freq = fftshift(freq) freq array([-5. , -3.75, -2.5 , -1.25, 0. , 1.25, 2.5 , 3.75]) fourier = fftshift(fourier)
freq = ifftshift(freq) fourier = ifftshift(fourier)
fill()
from numpy import * a = arange(4, dtype=int) a array([0, 1, 2, 3]) a.fill(7) a array([7, 7, 7, 7]) a.fill(6.5) a array([6, 6, 6, 6])
See also: empty, zeros, ones, repeat
finfo()
from numpy import * f = finfo(float) f.nmant, f.nexp (52, 11) f.machep -52 f.eps array(2.2204460492503131e-16) f.precision 15 f.resolution array(1.0000000000000001e-15) f.negep -53 f.epsneg array(1.1102230246251565e-16) f.minexp -1022 f.tiny array(2.2250738585072014e-308) f.maxexp 1024 f.min, f.max (-1.7976931348623157e+308, array(1.7976931348623157e+308))
fix()
from numpy import * a = array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7]) fix(a) array([-1., -1., 0., 0., 1., 1.])
See also: round_, ceil, floor, astype
flat
from numpy import * a = array([[10,30],[40,60]]) iter = a.flat iter.next() 10 iter.next() 30 iter.next() 40
flatten()
from numpy import * a = array([[[1,2]],[[3,4]]]) print a [[[1 2]] [[3 4]]] b = a.flatten() print b [1 2 3 4]
fliplr()
from numpy import * a = arange(12).reshape(4,3) a array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) fliplr(a) array([[ 2, 1, 0], [ 5, 4, 3], [ 8, 7, 6], [11, 10, 9]])
flipud()
from numpy import * a = arange(12).reshape(4,3) a array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) flipud(a) array([[ 9, 10, 11], [ 6, 7, 8], [ 3, 4, 5], [ 0, 1, 2]])
floor()
from numpy import * a = array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7]) floor(a) array([-2., -2., -1., 0., 1., 1.])
See also: ceil, round_, fix, astype
fromarrays()
from numpy import * x = array(['Smith','Johnson','McDonald']) y = array(['F','F','M'], dtype='S1') z = array([20,25,23]) data = rec.fromarrays([x,y,z], names='surname, gender, age') data[0] ('Smith', 'F', 20) data.age array([20, 25, 23])
See also: view
frombuffer()
from numpy import * buffer = "\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08
... @\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@" a = frombuffer(buffer, complex128) a array([ 1.+2.j, 3.+4.j, 5.+6.j])
See also: fromfunction, fromfile
fromfile()
from numpy import * y = array([2.,4.,6.,8.]) y.tofile("myfile.dat") y.tofile("myfile.txt", sep='\n', format = "%e") fromfile('myfile.dat', dtype=float) array([ 2., 4., 6., 8.]) fromfile('myfile.txt', dtype=float, sep='\n') array([ 2., 4., 6., 8.])
See also: loadtxt, fromfunction, tofile, frombuffer, savetxt
fromfunction()
from numpy import * def f(i,j): ... return i2 + j2 ... fromfunction(f, (3,3)) array([[0, 1, 4], [1, 2, 5], [4, 5, 8]])
See also: fromfile, frombuffer
fromiter()
from numpy import * import itertools mydata = [[55.5, 40],[60.5, 70]] mydescriptor = {'names': ('weight','age'), 'formats': (float32, int32)} myiterator = itertools.imap(tuple,mydata)
a = fromiter(myiterator, dtype = mydescriptor) a array([(55.5, 40), (60.5, 70)], dtype=[('weight', '<f4'), ('age', '<i4')])
See also: fromarrays, frombuffer, fromfile, fromfunction
generic
from numpy import * numpyscalar = string_('7') numpyscalar '7' type(numpyscalar) <type 'numpy.string_'> buildinscalar = '7' type(buildinscalar) <type 'str'> isinstance(numpyscalar, generic) True isinstance(buildinscalar, generic) False
gumbel()
from numpy import * from numpy.random import * gumbel(loc=0.0,scale=1.0,size=(2,3)) array([[-1.25923601, 1.68758144, 1.76620507], [ 1.96820048, -0.21219499, 1.83579566]]) from pylab import * hist(gumbel(0,1,(1000)), 50)
See also: random_sample, uniform, poisson, seed
histogram()
from numpy import * x = array([0.2, 6.4, 3.0, 1.6, 0.9, 2.3, 1.6, 5.7, 8.5, 4.0, 12.8]) bins = array([0.0, 1.0, 2.5, 4.0, 10.0]) N,bins = histogram(x,bins) N,bins (array([2, 3, 1, 4]), array([ 0. , 1. , 2.5, 4. , 10. ])) for n in range(len(bins)-1): ... print "# ", N[n], "number fall into bin [", bins[n], ",", bins[n+1], "[" ...
N,bins = histogram(x,5,range=(0.0, 10.0)) N,bins (array([4, 2, 2, 1, 2]), array([ 0., 2., 4., 6., 8.])) N,bins = histogram(x,5,range=(0.0, 10.0), normed=True) N,bins (array([ 0.18181818, 0.09090909, 0.09090909, 0.04545455, 0.09090909]), array([ 0., 2., 4., 6., 8.]))
hsplit()
from numpy import * a = array([[1,2,3,4],[5,6,7,8]]) hsplit(a,2) [array([[1, 2], [5, 6]]), array([[3, 4], [7, 8]])] hsplit(a,[1,2]) [array([[1], [5]]), array([[2], [6]]), array([[3, 4], [7, 8]])]
See also: split, array_split, dsplit, vsplit, hstack
hstack()
from numpy import * a =array([[1],[2]]) b = array([[3,4],[5,6]]) hstack((a,b,a)) array([[1, 3, 4, 1], [2, 5, 6, 2]])
See also: column_stack, concatenate, dstack, vstack, hsplit
hypot()
from numpy import * hypot(3.,4.) 5.0 z = array([2+3j, 3+4j]) hypot(z.real, z.imag) array([ 3.60555128, 5. ])
identity()
from numpy import * identity(3,float) array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])
See also: empty, eye, ones, zeros
ifft()
from numpy import * from numpy.fft import * signal = array([-2., 8., -6., 4., 1., 0., 3., 5.]) fourier = fft(signal) ifft(fourier) array([-2. +0.00000000e+00j, 8. +1.51410866e-15j, -6. +3.77475828e-15j, 4. +2.06737026e-16j, 1. +0.00000000e+00j, 0. -1.92758271e-15j, 3. -3.77475828e-15j, 5. +2.06737026e-16j])
allclose(signal.astype(complex), ifft(fft(signal))) True
N = len(fourier) signal = empty(N,complex) for k in range(N): ... signal[k] = sum(fourier * exp(+1j2pikarange(N)/N)) / N
See also: fft, fftfreq, fftshift
imag
from numpy import * a = array([1+2j,3+4j,5+6j]) a.imag array([ 2., 4., 6.]) a.imag = 9 a array([ 1.+9.j, 3.+9.j, 5.+9.j]) a.imag = array([9,8,7]) a array([ 1.+9.j, 3.+8.j, 5.+7.j])
index_exp[]
from numpy import * myslice = index_exp[2:4,...,4,::-1] print myslice (slice(2, 4, None), Ellipsis, 4, slice(None, None, -1))
indices()
from numpy import * indices((2,3)) array([[[0, 0, 0], [1, 1, 1]], [[0, 1, 2], [0, 1, 2]]]) a = array([ [ 0, 1, 2, 3, 4], ... [10,11,12,13,14], ... [20,21,22,23,24], ... [30,31,32,33,34] ]) i,j = indices((2,3)) a[i,j] array([[ 0, 1, 2], [10, 11, 12]])
See also: mgrid, [], ix_, slice
inf
from numpy import * exp(array([1000.])) array([ inf]) x = array([2,-inf,1,inf]) isfinite(x) array([True, False, True, False], dtype=bool) isinf(x) array([False, True, False, True], dtype=bool) isposinf(x) array([False, False, False, True], dtype=bool) isneginf(x) array([False, True, False, False], dtype=bool) nan_to_num(x) array([ 2.00000000e+000, -1.79769313e+308, 1.00000000e+000, 1.79769313e+308])
inner()
from numpy import * x = array([1,2,3]) y = array([10,20,30]) inner(x,y) 140
insert()
from numpy import * a = array([10,20,30,40]) insert(a,[1,3],50) array([10, 50, 20, 30, 50, 40]) insert(a,[1,3],[50,60]) array([10, 50, 20, 30, 60, 40]) a = array([[10,20,30],[40,50,60],[70,80,90]]) insert(a, [1,2], 100, axis=0) array([[ 10, 20, 30], [100, 100, 100], [ 40, 50, 60], [100, 100, 100], [ 70, 80, 90]]) insert(a, [0,1], [[100],[200]], axis=0) array([[100, 100, 100], [ 10, 20, 30], [200, 200, 200], [ 40, 50, 60], [ 70, 80, 90]]) insert(a, [0,1], [100,200], axis=1) array([[100, 10, 200, 20, 30], [100, 40, 200, 50, 60], [100, 70, 200, 80, 90]])
inv()
from numpy import * from numpy.linalg import inv a = array([[3,1,5],[1,0,8],[2,1,4]]) print a [[3 1 5] [1 0 8] [2 1 4]] inva = inv(a) print inva [[ 1.14285714 -0.14285714 -1.14285714] [-1.71428571 -0.28571429 2.71428571] [-0.14285714 0.14285714 0.14285714]] dot(a,inva) array([[ 1.00000000e-00, 2.77555756e-17, 3.60822483e-16], [ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00], [ -1.11022302e-16, 0.00000000e+00, 1.00000000e+00]])
iscomplex()
import numpy as np a = np.array([1,2,3.j]) np.iscomplex(a) array([False, False, True], dtype=bool)
iscomplexobj()
import numpy as np a = np.array([1,2,3.j]) np.iscomplexobj(a) True a = np.array([1,2,3]) np.iscomplexobj(a) False a = np.array([1,2,3], dtype=np.complex) np.iscomplexobj(a) True
item()
from numpy import * a = array([5]) type(a[0]) <type 'numpy.int32'> a.item() 5 type(a.item()) <type 'int'> b = array([2,3,4]) b[1].item() 3 type(b[1].item()) <type 'int'> b.item(2) 4 type(b.item(2)) <type 'int'> type(b[2]) <type 'numpy.int32'>
See also: []
ix_()
from numpy import * a = arange(9).reshape(3,3) print a [[0 1 2] [3 4 5] [6 7 8]] indices = ix_([0,1,2],[1,2,0]) print indices (array([[0], [1], [2]]), array([[1, 2, 0]])) print a[indices] [[1 2 0] [4 5 3] [7 8 6]]
... ... ...
See also: [], indices, cross, outer
lexsort()
from numpy import * serialnr = array([1023, 5202, 6230, 1671, 1682, 5241]) height = array([40., 42., 60., 60., 98., 40.]) width = array([50., 20., 70., 60., 15., 30.])
indices = lexsort(keys = (width, height)) indices array([5, 0, 1, 3, 2, 4]) for n in indices: ... print serialnr[n], height[n], width[n] ... 5241 40.0 30.0 1023 40.0 50.0 5202 42.0 20.0 1671 60.0 60.0 6230 60.0 70.0 1682 98.0 15.0
a = vstack([serialnr,width,height]) print a [[ 1023. 5202. 6230. 1671. 1682. 5241.] [ 50. 20. 70. 60. 15. 30.] [ 40. 42. 60. 60. 98. 40.]] indices = lexsort(a) a.take(indices, axis=-1) array([[ 5241., 1023., 5202., 1671., 6230., 1682.], [ 30., 50., 20., 60., 70., 15.], [ 40., 40., 42., 60., 60., 98.]])
linspace()
from numpy import * linspace(0,5,num=6) array([ 0., 1., 2., 3., 4., 5.]) linspace(0,5,num=10) array([ 0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222, 2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ]) linspace(0,5,num=10,endpoint=False) array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5]) stepsize = linspace(0,5,num=10,endpoint=False,retstep=True) stepsize (array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5]), 0.5) myarray, stepsize = linspace(0,5,num=10,endpoint=False,retstep=True) stepsize 0.5
See also: arange, logspace, r_
loadtxt()
from numpy import *
data = loadtxt("myfile.txt") t,z = data[:,0], data[:,3]
t,x,y,z = loadtxt("myfile.txt", unpack=True) t,z = loadtxt("myfile.txt", usecols = (0,3), unpack=True) data = loadtxt("myfile.txt", skiprows = 7) data = loadtxt("myfile.txt", comments = '!') data = loadtxt("myfile.txt", delimiter=';') data = loadtxt("myfile.txt", dtype = int)
logical_and()
from numpy import * logical_and(array([0,0,1,1]), array([0,1,0,1])) array([False, False, False, True], dtype=bool) logical_and(array([False,False,True,True]), array([False,True,False,True])) array([False, False, False, True], dtype=bool)
See also: logical_or, logical_not, logical_xor, bitwise_and
logical_not()
from numpy import * logical_not(array([0,1])) logical_not(array([False,True]))
See also: logical_or, logical_not, logical_xor, bitwise_and
logical_or()
from numpy import * logical_or(array([0,0,1,1]), array([0,1,0,1])) logical_or(array([False,False,True,True]), array([False,True,False,True]))
See also: logical_and, logical_not, logical_xor, bitwise_or
logical_xor()
from numpy import * logical_xor(array([0,0,1,1]), array([0,1,0,1])) logical_xor(array([False,False,True,True]), array([False,True,False,True]))
See also: logical_or, logical_not, logical_or, bitwise_xor
logspace()
from numpy import * logspace(-2, 3, num = 6) array([ 1.00000000e-02, 1.00000000e-01, 1.00000000e+00, 1.00000000e+01, 1.00000000e+02, 1.00000000e+03]) logspace(-2, 3, num = 10) array([ 1.00000000e-02, 3.59381366e-02, 1.29154967e-01, 4.64158883e-01, 1.66810054e+00, 5.99484250e+00, 2.15443469e+01, 7.74263683e+01, 2.78255940e+02, 1.00000000e+03]) logspace(-2, 3, num = 6, endpoint=False) array([ 1.00000000e-02, 6.81292069e-02, 4.64158883e-01, 3.16227766e+00, 2.15443469e+01, 1.46779927e+02]) exp(linspace(log(0.01), log(1000), num=6, endpoint=False)) array([ 1.00000000e-02, 6.81292069e-02, 4.64158883e-01, 3.16227766e+00, 2.15443469e+01, 1.46779927e+02])
See also: arange, linspace, r_
lstsq()
lstsq() is most often used in the context of least-squares fitting of data. Suppose you obtain some noisy data y as a function of a variable t, e.g. velocity as a function of time. You can use lstsq() to fit a model to the data, if the model is linear in its parameters, that is if
y = p0 * f0(t) + p1 * f1(t) + ... + pN-1 * fN-1(t) + noise
where the pi are the parameters you want to obtain through fitting and the fi(t) are known functions of t. What follows is an example how you can do this.
First, for the example's sake, some data is simulated:
from numpy import * from numpy.random import normal t = arange(0.0, 10.0, 0.05) y = 2.0 * sin(2.pit0.6) + 2.7 * cos(2.pit0.6) + normal(0.0, 1.0, len(t))
We would like to fit this data with: model(t) = p0 * sin(2.*pi*t*0.6) + p1 * cos(2.*pi*t*0.6), where p0 and p1 are the unknown fit parameters. Here we go:
from numpy.linalg import lstsq Nparam = 2 A = zeros((len(t),Nparam), float) A[:,0] = sin(2.pit0.6) A[:,1] = cos(2.pit0.6) (p, residuals, rank, s) = lstsq(A,y) p array([ 1.9315685 , 2.71165171]) residuals array([ 217.23783374]) rank 2 s array([ 10., 10.])
See also: pinv, polyfit, solve
mat()
from numpy import * mat('1 3 4; 5 6 9') matrix([[1, 3, 4], [5, 6, 9]]) a = array([[1,2],[3,4]]) m = mat(a) m matrix([[1, 2], [3, 4]]) a[0] array([1, 2]) m[0] matrix([[1, 2]]) a.ravel() array([1, 2, 3, 4]) m.ravel() matrix([[1, 2, 3, 4]]) aa array([[ 1, 4], [ 9, 16]]) mm matrix([[ 7, 10], [15, 22]]) a3 array([[ 1, 8], [27, 64]]) m3 matrix([[ 37, 54], [ 81, 118]]) m.T matrix([[1, 3], [2, 4]]) m.H matrix([[1, 3], [2, 4]]) m.I matrix([[-2. , 1. ], [ 1.5, -0.5]])
See also: bmat, array, dot, asmatrix
matrix()
from numpy import * matrix('1 3 4; 5 6 9') matrix([[1, 3, 4], [5, 6, 9]])
max()
from numpy import * a = array([10,20,30]) a.max() 30 a = array([[10,50,30],[60,20,40]]) a.max() 60 a.max(axis=0) array([60, 50, 40]) a.max(axis=1) array([50, 60]) max(a)
See also: nan, argmax, maximum, ptp
maximum()
from numpy import * a = array([1,0,5]) b = array([3,2,4]) maximum(a,b) array([3, 2, 5]) max(a.tolist(),b.tolist()) [3, 2, 4]
See also: minimum, max, argmax
mean()
from numpy import * a = array([1,2,7]) a.mean() 3.3333333333333335 a = array([[1,2,7],[4,9,6]]) a.mean() 4.833333333333333 a.mean(axis=0) array([ 2.5, 5.5, 6.5]) a.mean(axis=1) array([ 3.33333333, 6.33333333])
See also: average, median, var, std, sum
median()
from numpy import * a = array([1,2,3,4,9]) median(a) 3 a = array([1,2,3,4,9,0]) median(a) 2.5
See also: average, mean, var, std
mgrid[]
from numpy import * m = mgrid[1:3,2:5] print m [[[1 1 1] [2 2 2]] [[2 3 4] [2 3 4]]] m[0,1,2] 2 m[1,1,2] 4
min()
from numpy import * a = array([10,20,30]) a.min() 10 a = array([[10,50,30],[60,20,40]]) a.min() 10 a.min(axis=0) array([10, 20, 30]) a.min(axis=1) array([10, 20]) min(a)
See also: nan, max, minimum, argmin, ptp
minimum()
from numpy import * a = array([1,0,5]) b = array([3,2,4]) minimum(a,b) array([1, 0, 4]) min(a.tolist(),b.tolist()) [1, 0, 5]
See also: min, maximum, argmin
multiply()
from numpy import * multiply(array([3,6]), array([4,7])) array([12, 42])
See also: dot
nan
from numpy import * sqrt(array([-1.0])) array([ nan]) x = array([2, nan, 1]) isnan(x) array([False, True, False], dtype=bool) isfinite(x) array([True, False, True], dtype=bool) nansum(x) 3.0 nanmax(x) 2.0 nanmin(x) 1.0 nanargmin(x) 2 nanargmax(x) 0 nan_to_num(x) array([ 2., 0., 1.])
See also: inf
ndenumerate()
from numpy import * a = arange(9).reshape(3,3) + 10 a array([[10, 11, 12], [13, 14, 15], [16, 17, 18]]) b = ndenumerate(a) for position,value in b: print position,value ... (0, 0) 10 (0, 1) 11 (0, 2) 12 (1, 0) 13 (1, 1) 14 (1, 2) 15 (2, 0) 16 (2, 1) 17 (2, 2) 18
ndim
from numpy import * a = arange(12).reshape(3,4) a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) a.ndim 2 a.shape = (2,2,3) array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]]]) a.ndim 3 len(a.shape) 3
See also: shape
ndindex()
for index in ndindex(4,3,2): print index (0,0,0) (0,0,1) (0,1,0) ... (3,1,1) (3,2,0) (3,2,1)
See also: broadcast, ndenumerate
newaxis
from numpy import * x = arange(3) x array([0, 1, 2]) x[:,newaxis] array([[0], [1], [2]]) x[:,newaxis,newaxis] array([[[0]], [[1]], [[2]]]) x[:,newaxis] * x array([[0, 0, 0], [0, 1, 2], [0, 2, 4]]) y = arange(3,6) x[:,newaxis] * y array([[ 0, 0, 0], [ 3, 4, 5], [ 6, 8, 10]]) x.shape (3,) x[newaxis,:].shape (1,3) x[:,newaxis].shape (3,1)
See also: [], atleast_1d, atleast_2d, atleast_3d, expand_dims
nonzero()
from numpy import * x = array([1,0,2,-1,0,0,8]) indices = x.nonzero() indices (array([0, 2, 3, 6]),) x[indices] array([1, 2, -1, 8]) y = array([[0,1,0],[2,0,3]]) indices = y.nonzero() indices (array([0, 1, 1]), array([1, 0, 2])) y[indices[0],indices[1]] array([1, 2, 3]) y[indices] array([1, 2, 3]) y = array([1,3,5,7]) indices = (y >= 5).nonzero() y[indices] array([5, 7]) nonzero(y) (array([0, 1, 2, 3]),)
See also: [], where, compress, choose, take
ogrid()
from numpy import * x,y = ogrid[0:3,0:3] x array([[0], [1], [2]]) y array([[0, 1, 2]]) print x*y [[0 0 0] [0 1 2] [0 2 4]]
See also: mgrid
ones()
from numpy import * ones(5) array([ 1., 1., 1., 1., 1.]) ones((2,3), int) array([[1, 1, 1], [1, 1, 1]])
See also: ones_like, zeros, empty, eye, identity
ones_like()
from numpy import * a = array([[1,2,3],[4,5,6]]) ones_like(a) array([[1, 1, 1], [1, 1, 1]])
See also: ones, zeros_like
outer()
from numpy import * x = array([1,2,3]) y = array([10,20,30]) outer(x,y) array([[10, 20, 30], [20, 40, 60], [30, 60, 90]])
permutation()
from numpy import * from numpy.random import permutation permutation(4) array([0, 3, 1, 2]) permutation(4) array([2, 1, 0, 3]) permutation(4) array([3, 0, 2, 1])
See also: shuffle, bytes, seed
piecewise()
from numpy import * f1 = lambda x: xx f2 = lambda x: 2x x = arange(-2.,3.,0.1) condition = (x>1)&(x<2) y = piecewise(x,condition, [f1,1.]) y = piecewise(x, fabs(x)<=1, [f1,0]) + piecewise(x, x>1, [f2,0]) print y
See also: select
pinv()
from numpy import * from numpy.linalg import pinv,svd,lstsq A = array([[1., 3., 5.],[2., 4., 6.]]) b = array([1., 3.])
pinvA = pinv(A) print pinvA [[-1.33333333 1.08333333] [-0.33333333 0.33333333] [ 0.66666667 -0.41666667]] x = dot(pinvA, b) print x [ 1.91666667 0.66666667 -0.58333333]
x,resids,rank,s = lstsq(A,b) print x [ 1.91666667 0.66666667 -0.58333333]
U,sigma,V = svd(A) S = zeros_like(A.transpose()) for n in range(len(sigma)): S[n,n] = 1. / sigma[n] dot(V.transpose(), dot(S, U.transpose())) array([[-1.33333333, 1.08333333], [-0.33333333, 0.33333333], [ 0.66666667, -0.41666667]])
See also: inv, lstsq, solve, svd
poisson()
from numpy import * from numpy.random import * poisson(lam=0.5, size=(2,3)) array([[2, 0, 0], [1, 1, 0]])
See also: random_sample, uniform, standard_normal, seed
poly1d()
from numpy import * p1 = poly1d([2,3],r=1) print p1 2 1 x - 5 x + 6 p2 = poly1d([2,3],r=0) print p2 2 x + 3 print p1+p2 2 1 x - 3 x + 9 quotient,remainder = p1/p2 print quotient,remainder 0.5 x - 3 15 p3 = p1*p2 print p3 3 2 2 x - 7 x - 3 x + 18 p3([1,2,3,4]) array([10, 0, 0, 22]) p3[2] -7 p3.r array([-1.5, 3. , 2. ]) p3.c array([ 2, -7, -3, 18]) p3.o 3 print p3.deriv(m=2) 12 x - 14 print p3.integ(m=2,k=[1,2]) 5 4 3 2 0.1 x - 0.5833 x - 0.5 x + 9 x + 1 x + 2
polyfit()
from numpy import * x = array([1,2,3,4,5]) y = array([6, 11, 18, 27, 38]) polyfit(x,y,2) array([ 1., 2., 3.]) polyfit(x,y,1) array([ 8., -4.])
See also: lstsq
prod()
from numpy import * a = array([1,2,3]) a.prod() 6 prod(a) 6 a = array([[1,2,3],[4,5,6]]) a.prod(dtype=float) 720.0 a.prod(axis=0) array([ 4, 10, 18]) a.prod(axis=1) array([ 6, 120])
ptp()
from numpy import * a = array([5,15,25]) a.ptp() 20 a = array([[5,15,25],[3,13,33]]) a.ptp() 30 a.ptp(axis=0) array([2, 2, 8]) a.ptp(axis=1) array([20, 30])
put()
from nump import * a = array([10,20,30,40]) a.put([60,70,80], [0,3,2]) a array([60, 20, 80, 70]) a[[0,3,2]] = [60,70,80] a.put([40,50], [0,3,2,1]) a array([40, 50, 40, 50]) put(a, [0,3], [90]) a array([90, 50, 40, 90])
putmask()
from nump import * a = array([10,20,30,40]) mask = array([True,False,True,True]) a.putmask([60,70,80,90], mask) a array([60, 20, 80, 90]) a = array([10,20,30,40]) a[mask] array([60, 80, 90]) a[mask] = array([60,70,80,90]) a array([60, 20, 70, 80]) a.putmask([10,90], mask) a array([10, 20, 10, 90]) putmask(a, mask, [60,70,80,90])
r_[]
from numpy import * r_[1:5] array([1, 2, 3, 4]) r_[1:10:4] array([1, 5, 9]) r_[1:10:4j] array([ 1., 4., 7., 10.]) r_[1:5,7,1:10:4] array([1, 2, 3, 4, 7, 1, 5, 9]) r_['r', 1:3] matrix([[1, 2]]) r_['c',1:3] matrix([[1], [2]]) a = array([[1,2,3],[4,5,6]]) r_[a,a] array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]) r_['-1',a,a] array([[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]])
See also: c_, s_, arange, linspace, hstack, vstack, column_stack, concatenate, bmat
rand()
from numpy import * from numpy.random import * rand(3,2) array([[ 0.65159297, 0.78872335], [ 0.09385959, 0.02834748], [ 0.8357651 , 0.43276707]])
See also: random_sample, seed
randint()
Synonym for random_integers()
See random_integers
randn()
randn(2,3) array([[ 1.22497074, -0.29508896, -0.75040033], [-0.54822685, -0.98032155, -1.40467696]])
See also: standard_normal, poisson, seed
random_integers()
from numpy import * from numpy.random import * random_integers(-1,5,(2,2)) array([[ 3, -1], [-1, 0]])
See also: random_sample, uniform, poisson, seed
random_sample()
from numpy import * from numpy.random import * random_sample((3,2)) array([[ 0.76228008, 0.00210605], [ 0.44538719, 0.72154003], [ 0.22876222, 0.9452707 ]])
See also: ranf, sample, rand, seed
ranf()
Synonym for random_sample
See random_sample, sample
ravel()
from numpy import * a = array([[1,2],[3,4]]) a.ravel() array([1, 2, 3, 4]) b = a[:,0].ravel() b array([1, 3]) c = a[0,:].ravel() c array([1, 2]) b[0] = -1 c[1] = -2 a array([[ 1, -2], [ 3, 4]]) ravel(a)
See also: flatten
real
from numpy import * a = array([1+2j,3+4j,5+6j]) a.real array([ 1., 3., 5.]) a.real = 9 a array([ 9.+2.j, 9.+4.j, 9.+6.j]) a.real = array([9,8,7]) a array([ 9.+2.j, 8.+4.j, 7.+6.j])
recarray()
from numpy import * num = 2 a = recarray(num, formats='i4,f8,f8',names='id,x,y') a['id'] = [3,4] a['id'] array([3, 4]) a = rec.fromrecords([(35,1.2,7.3),(85,9.3,3.2)], names='id,x,y') a['id'] array([35, 85])
reduce()
from numpy import * add.reduce(array([1.,2.,3.,4.])) 10.0 multiply.reduce(array([1.,2.,3.,4.])) 24.0 add.reduce(array([[1,2,3],[4,5,6]]), axis = 0) array([5, 7, 9]) add.reduce(array([[1,2,3],[4,5,6]]), axis = 1) array([ 6, 15])
See also: accumulate, sum, prod
repeat()
from numpy import * repeat(7., 4) array([ 7., 7., 7., 7.]) a = array([10,20]) a.repeat([3,2]) array([10, 10, 10, 20, 20]) repeat(a, [3,2]) a = array([[10,20],[30,40]]) a.repeat([3,2,1,1]) array([10, 10, 10, 20, 20, 30, 40]) a.repeat([3,2],axis=0) array([[10, 20], [10, 20], [10, 20], [30, 40], [30, 40]]) a.repeat([3,2],axis=1) array([[10, 10, 10, 20, 20], [30, 30, 30, 40, 40]])
See also: tile
reshape()
from numpy import * x = arange(12) x.reshape(3,4) array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) x.reshape(3,2,2) array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]]]) x.reshape(2,-1) array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]) x.reshape(12) array([0,1,2,3,4,5,6,7,8,9,10,11]) reshape(x,(2,6))
resize()
from numpy import * a = array([1,2,3,4]) a.resize(2,2) print a [[1 2] [3 4]] a.resize(3,2) print a [[1 2] [3 4] [0 0]] a.resize(2,4) print a [[1 2 3 4] [0 0 0 0]] a.resize(2,1) print a [[1] [2]]
But, there is a caveat:
b = array([1,2,3,4]) c = b # c is reference to b, it doesn't 'own' its data c.resize(2,2) # no problem, nr of elements doesn't change c.resize(2,3) # doesn't work, c is only a reference Traceback (most recent call last): File "", line 1, in ? ValueError: cannot resize an array that has been referenced or is referencing another array in this way. Use the resize function b.resize(2,3) # doesn't work, b is referenced by another array Traceback (most recent call last): File "", line 1, in ? ValueError: cannot resize an array that has been referenced or is referencing another array in this way. Use the resize function
and it's not always obvious what the reference is:
d = arange(4) d array([0, 1, 2, 3]) d.resize(5) # doesn't work, but where's the reference? Traceback (most recent call last): File "", line 1, in ? ValueError: cannot resize an array that has been referenced or is referencing another array in this way. Use the resize function _ # '_' was a reference to d! array([0, 1, 2, 3]) d = resize(d, 5) # this does work, however d array([0, 1, 2, 3, 0])
See also: reshape
rollaxis()
from numpy import * a = arange(345).reshape(3,4,5) a.shape (3, 4, 5) b = rollaxis(a,1,0) b.shape (4, 3, 5) b = rollaxis(a,0,2) b.shape (4, 3, 5)
round()
round(decimals=0, out=None) -> reference to rounded values.
from numpy import * array([1.2345, -1.647]).round() array([ 1., -2.]) array([1, -1]).round() array([ 1, -1]) array([1.2345, -1.647]).round(decimals=1) array([ 1.2, -1.6]) array([1.2345+2.34j, -1.647-0.238j]).round() array([ 1.+2.j, -2.-0.j]) array([0.0, 0.5, 1.0, 1.5, 2.0, 2.5]).round() array([ 0., 0., 1., 2., 2., 2.]) a = zeros(3, dtype=int) array([1.2345, -1.647, 3.141]).round(out=a) array([ 1, -2, 3]) a array([ 1, -2, 3]) round_(array([1.2345, -1.647])) array([ 1., -2.]) around(array([1.2345, -1.647])) array([ 1., -2.])
See also: ceil, floor, fix, astype
rot90()
from numpy import * a = arange(12).reshape(4,3) a array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) rot90(a) array([[ 2, 5, 8, 11], [ 1, 4, 7, 10], [ 0, 3, 6, 9]])
s_[]
from numpy import * s_[1:5] slice(1, 5, None) s_[1:10:4] slice(1, 10, 4) s_[1:10:4j] slice(1, 10, 4j) s_['r',1:3] ('r', slice(1, 3, None)) s_['c',1:3] ('c', slice(1, 3, None))
See also: r_, c_, slice, index_exp
sample()
Synonym for random_sample
See also: random_sample, ranf
savetxt()
from numpy import * savetxt("myfile.txt", data) savetxt("myfile.txt", x) savetxt("myfile.txt", (x,y)) savetxt("myfile.txt", transpose((x,y))) savetxt("myfile.txt", transpose((x,y)), fmt='%6.3f') savetxt("myfile.txt", data, delimiter = ';')
searchsorted()
searchsorted(keys, side="left")
from numpy import * a = array([1,2,2,3]) a.searchsorted(2) 1 a.searchsorted(2, side='right') 3 a.searchsorted(4) 4 a.searchsorted([[1,2],[2,3]]) array([[0, 1], [1, 3]]) searchsorted(a,2) 1
seed()
seed([1]) rand(3) array([ 0.13436424, 0.84743374, 0.76377462]) seed([1]) rand(3) array([ 0.13436424, 0.84743374, 0.76377462]) rand(3) array([ 0.25506903, 0.49543509, 0.44949106])
select()
from numpy import * x = array([5., -2., 1., 0., 4., -1., 3., 10.]) select([x < 0, x == 0, x <= 5], [x-0.1, 0.0, x+0.2], default = 100.) array([ 5.2, -2.1, 1.2, 0. , 4.2, -1.1, 3.2, 100. ])
result = zeros_like(x) for n in range(len(x)): ... if x[n] < 0: result[n] = x[n]-0.1 ... elif x[n] == 0: result[n] = 0.0 ... elif x[n] <= 5: result[n] = x[n]+0.2 ... else: result[n] = 100. ... result array([ 5.2, -2.1, 1.2, 0. , 4.2, -1.1, 3.2, 100. ])
set_printoptions()
from numpy import * x = array([pi, 1.e-200]) x array([ 3.14159265e+000, 1.00000000e-200]) set_printoptions(precision=3, suppress=True) x array([ 3.142, 0. ])
help(set_printoptions)
shape
from numpy import * x = arange(12) x.shape (12,) x.shape = (3,4) x array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) x.shape = (3,2,2) x array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]]]) x.shape = (2,-1) x array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]) x.shape = 12 x array([0,1,2,3,4,5,6,7,8,9,10,11])
See also: reshape
shuffle()
from numpy import * from numpy.random import shuffle x = array([1,50,-1,3]) shuffle(x) print x [-1 3 50 1] x = ['a','b','c','z'] shuffle(x) print x ['a', 'c', 'z', 'b']
See also: permutation, bytes
slice()
s = slice(3,9,2) from numpy import * a = arange(20) a[s] array([3, 5, 7]) a[3:9:2] array([3, 5, 7])
See also: [], ..., newaxis, s_, ix_, indices, index_exp
solve()
from numpy import * from numpy.linalg import solve
a = array([[3,1,5],[1,0,8],[2,1,4]]) b = array([6,7,8]) x = solve(a,b) print x [-3.28571429 9.42857143 1.28571429]
dot(a,x) array([ 6., 7., 8.])
See also: inv
sometrue()
from numpy import * b = array([True, False, True, True]) sometrue(b) True a = array([1, 5, 2, 7]) sometrue(a >= 5) True
sort()
sort(axis=-1, kind="quicksort")
from numpy import * a = array([2,0,8,4,1]) a.sort() a array([0, 1, 2, 4, 8]) a.sort(kind='mergesort') a = array([[8,4,1],[2,0,9]]) a.sort(axis=0) a array([[2, 0, 1], [8, 4, 9]]) a = array([[8,4,1],[2,0,9]]) a.sort(axis=1) a array([[1, 4, 8], [0, 2, 9]]) sort(a)
split()
from numpy import * a = array([[1,2,3,4],[5,6,7,8]]) split(a,2,axis=0) array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]])] split(a,4,axis=1) [array([[1], [5]]), array([[2], [6]]), array([[3], [7]]), array([[4], [8]])] split(a,3,axis=1) Traceback (most recent call last): ValueError: array split does not result in an equal division split(a,[2,3],axis=1) [array([[1, 2], [5, 6]]), array([[3], [7]]), array([[4], [8]])]
See also: dsplit, hsplit, vsplit, array_split, concatenate
squeeze()
from numpy import * a = arange(6) a = a.reshape(1,2,1,1,3,1) a array([[[[[[0], [1], [2]]]], [[[[3], [4], [5]]]]]]) a.squeeze() array([[0, 1, 2], [3, 4, 5]]) squeeze(a)
std()
from numpy import * a = array([1.,2,7]) a.std() 2.6246692913372702 a = array([[1.,2,7],[4,9,6]]) a.std() 2.793842435706702 a.std(axis=0) array([ 1.5, 3.5, 0.5]) a.std(axis=1) array([ 2.62466929, 2.05480467])
standard_normal()
standard_normal((2,3)) array([[ 1.12557608, -0.13464922, -0.35682992], [-1.54090277, 1.21551589, -1.82854551]])
See also: randn, uniform, poisson, seed
sum()
from numpy import * a = array([1,2,3]) a.sum() 6 sum(a) a = array([[1,2,3],[4,5,6]]) a.sum() 21 a.sum(dtype=float) 21.0 a.sum(axis=0) array([5, 7, 9]) a.sum(axis=1) array([ 6, 15])
See also: accumulate, nan, cumsum, prod
svd()
from numpy import * from numpy.linalg import svd A = array([[1., 3., 5.],[2., 4., 6.]]) U,sigma,V = svd(A) print U [[-0.61962948 -0.78489445] [-0.78489445 0.61962948]] print sigma [ 9.52551809 0.51430058] print V [[-0.2298477 -0.52474482 -0.81964194] [ 0.88346102 0.24078249 -0.40189603] [ 0.40824829 -0.81649658 0.40824829]] Sigma = zeros_like(A) n = min(A.shape) Sigma[:n,:n] = diag(sigma) print dot(U,dot(Sigma,V)) [[ 1. 3. 5.] [ 2. 4. 6.]]
See also: pinv
swapaxes()
from numpy import * a = arange(30) a = a.reshape(2,3,5) 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, 24], [25, 26, 27, 28, 29]]]) b = a.swapaxes(1,2) b array([[[ 0, 5, 10], [ 1, 6, 11], [ 2, 7, 12], [ 3, 8, 13], [ 4, 9, 14]], [[15, 20, 25], [16, 21, 26], [17, 22, 27], [18, 23, 28], [19, 24, 29]]]) b.shape (2, 5, 3) b[0,0,0] = -1 print a[0,0,0]
T
from numpy import * x = array([[1.,2.],[3.,4.]]) x array([[ 1., 2.], [ 3., 4.]]) x.T array([[ 1., 3.], [ 2., 4.]])
See also: transpose
take()
from numpy import * a= array([10,20,30,40]) a.take([0,0,3]) array([10, 10, 40]) a[[0,0,3]] array([10, 10, 40]) a.take([[0,1],[0,1]]) array([[10, 20], [10, 20]]) a = array([[10,20,30],[40,50,60]]) a.take([0,2],axis=1) array([[10, 30], [40, 60]]) take(a,[0,2],axis=1)
See also: [], put, putmask, compress, choose
tensordot()
from numpy import * a = arange(60.).reshape(3,4,5) b = arange(24.).reshape(4,3,2) c = tensordot(a,b, axes=([1,0],[0,1])) c.shape (5,2)
c = zeros((5,2)) for i in range(5): ... for j in range(2): ... for k in range(3): ... for n in range(4): ... c[i,j] += a[k,n,i] * b[n,k,j] ...
See also: dot
tile()
from numpy import * a = array([10,20]) tile(a, (3,2)) array([[10, 20, 10, 20], [10, 20, 10, 20], [10, 20, 10, 20]]) tile(42.0, (3,2)) array([[ 42., 42.], [ 42., 42.], [ 42., 42.]]) tile([[1,2],[4,8]], (2,3)) array([[1, 2, 1, 2, 1, 2], [4, 8, 4, 8, 4, 8], [1, 2, 1, 2, 1, 2], [4, 8, 4, 8, 4, 8]])
See also: hstack, vstack, r_, c_, concatenate, repeat
tofile()
from numpy import * x = arange(10.) y = x**2 y.tofile("myfile.dat") y.tofile("myfile.txt", sep=' ', format = "%e") y.tofile("myfile.txt", sep='\n', format = "%e")
See also: fromfile, loadtxt, savetxt
tolist()
from numpy import * a = array([[1,2],[3,4]]) a.tolist() [[1, 2], [3, 4]]
trace()
from numpy import * a = arange(12).reshape(3,4) a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) a.diagonal() array([ 0, 5, 10]) a.trace() 15 a.diagonal(offset=1) array([ 1, 6, 11]) a.trace(offset=1) 18
transpose()
A very simple example:
a = array([[1,2,3],[4,5,6]]) print a.shape (2, 3) b = a.transpose() print b [[1 4] [2 5] [3 6]] print b.shape (3, 2)
From this, a more elaborate example can be understood:
a = arange(30) a = a.reshape(2,3,5) 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, 24], [25, 26, 27, 28, 29]]]) b = a.transpose() b array([[[ 0, 15], [ 5, 20], [10, 25]], [[ 1, 16], [ 6, 21], [11, 26]], [[ 2, 17], [ 7, 22], [12, 27]], [[ 3, 18], [ 8, 23], [13, 28]], [[ 4, 19], [ 9, 24], [14, 29]]]) b.shape (5, 3, 2) b = a.transpose(1,0,2) b array([[[ 0, 1, 2, 3, 4], [15, 16, 17, 18, 19]], [[ 5, 6, 7, 8, 9], [20, 21, 22, 23, 24]], [[10, 11, 12, 13, 14], [25, 26, 27, 28, 29]]]) b.shape (3, 2, 5) b = transpose(a, (1,0,2))
See also: T, swapaxes, rollaxis
tri()
from numpy import * tri(3,4,k=0,dtype=float) array([[ 1., 0., 0., 0.], [ 1., 1., 0., 0.], [ 1., 1., 1., 0.]]) tri(3,4,k=1,dtype=int) array([[1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1]])
tril()
from numpy import * a = arange(10,100,10).reshape(3,3) a array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) tril(a,k=0) array([[10, 0, 0], [40, 50, 0], [70, 80, 90]]) tril(a,k=1) array([[10, 20, 0], [40, 50, 60], [70, 80, 90]])
trim_zeros()
from numpy import * x = array([0, 0, 0, 1, 2, 3, 0, 0]) trim_zeros(x,'f') array([1, 2, 3, 0, 0]) trim_zeros(x,'b') array([0, 0, 0, 1, 2, 3]) trim_zeros(x,'bf') array([1, 2, 3])
See also: compress
triu()
from numpy import * a = arange(10,100,10).reshape(3,3) a array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) triu(a,k=0) array([[10, 20, 30], [ 0, 50, 60], [ 0, 0, 90]]) triu(a,k=1) array([[ 0, 20, 30], [ 0, 0, 60], [ 0, 0, 0]])
typeDict()
from numpy import * typeDict['short'] <type 'numpy.int16'> typeDict['uint16'] <type 'numpy.uint16'> typeDict['void'] <type 'numpy.void'> typeDict['S'] <type 'numpy.string_'>
uniform()
from numpy import * from numpy.random import * uniform(low=0,high=10,size=(2,3)) array([[ 6.66689951, 4.50623001, 4.69973967], [ 6.52977732, 3.24688284, 5.01917021]])
See also: standard_normal, poisson, seed
unique()
from numpy import * x = array([2,3,2,1,0,3,4,0]) unique(x) array([0, 1, 2, 3, 4])
unique1d()
np.unique1d([1, 1, 2, 2, 3, 3]) array([1, 2, 3]) a = np.array([[1, 1], [2, 3]]) np.unique1d(a) array([1, 2, 3]) np.unique1d([1,2,6,4,2,3,2], return_index=True) (array([1, 2, 3, 4, 6]), array([0, 1, 5, 3, 2])) x = [1,2,6,4,2,3,2] u, i = np.unique1d(x, return_inverse=True) u array([1, 2, 3, 4, 6]) i array([0, 1, 4, 3, 1, 2, 1]) [u[p] for p in i] [1, 2, 6, 4, 2, 3, 2]
vander()
from numpy import * x = array([1,2,3,5]) N=3 vander(x,N) array([[ 1, 1, 1], [ 4, 2, 1], [ 9, 3, 1], [25, 5, 1]]) column_stack([x**(N-1-i) for i in range(N)]) array([[ 1, 1, 1], [ 4, 2, 1], [ 9, 3, 1], [25, 5, 1]])
var()
from numpy import * a = array([1,2,7]) a.var() 6.8888888888888875 a = array([[1,2,7],[4,9,6]]) a.var() 7.8055555555555571 a.var(axis=0) array([ 2.25, 12.25, 0.25]) a.var(axis=1) array([ 6.88888889, 4.22222222])
vdot()
from numpy import * x = array([1+2j,3+4j]) y = array([5+6j,7+8j]) vdot(x,y) (70-8j)
See also: dot, inner, cross, outer
vectorize()
from numpy import * def myfunc(x): ... if x >= 0: return x**2 ... else: return -x ... myfunc(2.) 4.0 myfunc(array([-2,2])) vecfunc = vectorize(myfunc, otypes=[float]) vecfunc(array([-2,2])) array([ 2., 4.])
See also: apply_along_axis, apply_over_axes
view()
from numpy import * a = array([1., 2.]) a.view() array([ 1., 2.]) a.view(complex) array([ 1.+2.j]) a.view(int) array([ 0, 1072693248, 0, 1073741824])
mydescr = dtype({'names': ['gender','age'], 'formats': ['S1', 'i2']}) a = array([('M',25),('F',30)], dtype = mydescr) b = a.view(recarray)
a['age'] array([25, 30], dtype=int16) b.age array([25, 30], dtype=int16)
See also: copy
vonmises()
from numpy import * from numpy.random import * vonmises(mu=1,kappa=1,size=(2,3)) array([[ 0.81960554, 1.37470839, -0.15700173], [ 1.2974554 , 2.94229797, 0.32462307]]) from pylab import * hist(vonmises(1,1,(10000)), 50)
See also: random_sample, uniform, standard_normal, seed
vsplit()
from numpy import * a = array([[1,2],[3,4],[5,6],[7,8]]) vsplit(a,2) [array([[1, 2], [3, 4]]), array([[5, 6], [7, 8]])] vsplit(a,[1,2]) [array([[1, 2]]), array([[3, 4]]), array([[5, 6], [7, 8]])]
See also: split, array_split, dsplit, hsplit, vstack
vstack()
from numpy import * a =array([1,2]) b = array([[3,4],[5,6]]) vstack((a,b,a)) array([[1, 2], [3, 4], [5, 6], [1, 2]])
See also: hstack, column_stack, concatenate, dstack, vsplit
weibull()
from numpy import * from numpy.random import * weibull(a=1,size=(2,3)) array([[ 0.08303065, 3.41486412, 0.67430149], [ 0.41383893, 0.93577601, 0.45431195]]) from pylab import * hist(weibull(5, (1000)), 50)
See also: random_sample, uniform, standard_normal, seed
where()
from numpy import * a = array([3,5,7,9]) b = array([10,20,30,40]) c = array([2,4,6,8]) where(a <= 6, b, c) array([10, 20, 6, 8]) where(a <= 6, b, -1) array([10, 20, -1, -1]) indices = where(a <= 6) indices (array([0, 1]),) b[indices] array([10, 20]) b[a <= 6] array([10, 20]) d = array([[3,5,7,9],[2,4,6,8]]) where(d <= 6) (array([0, 0, 1, 1, 1]), array([0, 1, 0, 1, 2]))
Be aware of the difference between x[list of bools] and x[list of integers]!
from numpy import * x = arange(5,0,-1) print x [5 4 3 2 1] criterion = (x <= 2) | (x >= 5) criterion array([True, False, False, True, True], dtype=bool) indices = where(criterion, 1, 0) print indices [1 0 0 1 1] x[indices] array([4, 5, 5, 4, 4]) x[criterion] array([5, 2, 1]) indices = where(criterion) print indices (array([0, 3, 4]),) x[indices] array([5, 2, 1])
zeros()
from numpy import * zeros(5) array([ 0., 0., 0., 0., 0.]) zeros((2,3), int) array([[0, 0, 0], [0, 0, 0]])
See also: zeros_like, ones, empty, eye, identity
zeros_like()
from numpy import * a = array([[1,2,3],[4,5,6]]) zeros_like(a) array([[0, 0, 0], [0, 0, 0]])