This is an auto-generated version of Numpy Example List with added documentation from doc strings and arguments specification for methods and functions of Numpy 1.0.4.

Please do not edit this page directly. To update this page just follow the instructions.


...

>>> 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]]])

See also: [], newaxis

[]

>>> 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

T

ndarray.T

Same as self.transpose() except self is returned for self.ndim < 2.

>>> from numpy import *
>>> x = array([[1.,2.],[3.,4.]])
>>> x
array([[ 1.,  2.],
[ 3.,  4.]])
>>> x.T                                           # shortcut for transpose()
array([[ 1.,  3.],
[ 2.,  4.]])

See also: transpose

abs()

numpy.abs(...)

y = absolute(x) takes |x| elementwise.

>>> from numpy import *
>>> abs(-1)
1
>>> abs(array([-1.2, 1.2]))
array([ 1.2,  1.2])
>>> abs(1.2+1j)
1.5620499351813308

See also: absolute, angle

absolute()

numpy.absolute(...)

y = absolute(x) takes |x| elementwise.

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()

numpy.add(...)

y = add(x1,x2) adds the arguments elementwise.

>>> 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])

alen()

numpy.alen(a)

Return the length of a Python object interpreted as an array
of at least 1 dimension.

Blah, Blah.

all()

numpy.all(a, axis=None, out=None)

Return true if all elements of a are true:

*See Also*:

    `ndarray.all` : equivalent method

    `alltrue` : equivalent function

ndarray.all(...)

a.all(axis=None)

>>> 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()

numpy.allclose(a, b, rtol=1.0000000000000001e-005, atol=1e-008)

Returns True if all components of a and b are equal subject to given
tolerances.

The relative error rtol must be positive and << 1.0
The absolute error atol usually comes into play for those elements of b that
are very small or zero; it says how small a must be also.

>>> 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()

numpy.alltrue(a, axis=None, out=None)

Perform a logical_and over the given axis.

*See Also*:

    `ndarray.all` : equivalent method

    `all` : equivalent function

>>> from numpy import *
>>> b = array([True, False, True, True])
>>> alltrue(b)
False
>>> a = array([1, 5, 2, 7])
>>> alltrue(a >= 5)
False

See also: sometrue, all, any

alterdot()

numpy.alterdot(...)

alterdot() changes all dot functions to use blas.

amax()

numpy.amax(a, axis=None, out=None)

Return the maximum of 'a' along dimension axis.

Blah, Blah.

amin()

numpy.amin(a, axis=None, out=None)

Return the minimum of a along dimension axis.

Blah, Blah.

angle()

numpy.angle(z, deg=0)

Return the angle of the complex argument z.

>>> from numpy import *
>>> angle(1+1j)                                   # in radians
0.78539816339744828
>>> angle(1+1j,deg=True)                          # in degrees
45.0

See also: real, imag, hypot

any()

numpy.any(a, axis=None, out=None)

Return true if any elements of a are true.

*See Also*:

    `ndarray.any` : equivalent method

ndarray.any(...)

a.any(axis=None, out=None)

>>> 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()

numpy.append(arr, values, axis=None)

Append to the end of an array along axis (ravel first if None)

>>> 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()

numpy.apply_along_axis(func1d, axis, arr, *args)

Execute func1d(arr[i],*args) where func1d takes 1-D arrays
and arr is an N-d array.  i varies so as to apply the function
along the given axis for each 1-d subarray in arr.

>>> 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()

numpy.apply_over_axes(func, a, axes)

Apply a function repeatedly over multiple axes, keeping the same shape
for the resulting array.

func is called as res = func(a, axis).  The result is assumed
to be either the same shape as a or have one less dimension.
This call is repeated for each axis in the axes sequence.

>>> 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]