A selection of useful NumPy core functions
Greg von Winckel
Albuquerque Python Meetup
April 2, 2014
Greg von Winckel
Core NumPy Functions
array
array is the basic data type handled by all NumPy functions.
array(object, dtype=None, copy=True, order=None,
subok=False, ndmin=0)
Usually constructed from list or tuple.
All elements have the same type (e.g. bool, int, complex,
float, or str.)
ipython interactive shell
In [1]: import numpy as np
In [2]: np.array(((1,2),(3,4)))
Out[2]: array([[1, 2],
..............[3, 4]])
Greg von Winckel
Core NumPy Functions
reshape and transpose
reshape(a, newshape, order=C)
transpose(a,axes=None)
reshape changes the shape of an array and transpose can either
transpose a 2D array or permute the elements.
ipython interactive shell
In [1]: a=range(6)
In [2]: A=np.reshape(a,(2,3))
In [3]: print(A)
[[0 1 2]
[3 4 5]]
In [4]: A=np.reshape(a,(2,3),F)
In [5]: print(A)
[[0 2 4]
[1 3 5]]
Greg von Winckel
In [6]: np.transpose(A)
Out[6]:
array([[0, 1],
.......[2, 3],
.......[4, 5]])
In [7]: print(A.T)
[[0, 1],
[2, 3],
[4, 5]]
Core NumPy Functions
any and all
The NumPy functions any and all determine whether any or all
of the elements in an array are True or False.
ipython interactive shell
In [1]: A=np.array(((0,1),(0,0)),dtype=bool)
In [2]: np.any(A,axis=0)
Out[2]: array([False, True], dtype=bool)
In [3]: np.any(A,axis=1)
Out[3]: array([True, False], dtype=bool)
In [4]: A.any()
Out[4]: True
In [5]: A.all()
Out[5]: False
Greg von Winckel
Core NumPy Functions
ceil, floor, trunc, and rint
ceil and floor will round up or down an array of floating point
numbers to the nearest integer value, but will not convert the type.
trunc truncates to the integer closest to zero and rint rounds to
the nearest integer
ipython interactive shell
In [1]: np.floor(np.arange(-2,2,0.5))
Out[1]: array([ -2., -2., -1., -1., 0., 0., 1., 1.])
In [2]: np.ceil(np.arange(-2,2,0.5))
Out[2]: array([ -2., -1., -1., 0., 0., 1., 1., 2.])
In [3]: np.trunc(np.arange(-2,2,0.5))
Out[3]: array([-2., -1., -1., -0., 0., 0., 1., 1.])
In [4]: np.rint(np.linspace(0,1,6))
Out[4]: array([ 0., 0., 0., 1., 1., 1.])
Greg von Winckel
Core NumPy Functions
diag and diagonal
These two functions are useful for working with 2D arrays. diag
extracts a particular diagonal band from a 2D array. diagonal
creates a 2D array from a 1D array.
ipython interactive shell
In [1]: a=np.arange(9)
In [2]: A=a.reshape(3,3)
In [3]: print(A)
[[0 1 2]
[3 4 5]
[6 7 8]]
In [4]: np.diag(A)
Out[4]: array([0, 4, 8])
In [5]: np.diag(A,1)
Out[5]: array([1, 5])
In [6]: np.diag(A,-1)
Out[6]: array([3, 7])
Greg von Winckel
In [7]: np.diagonal([1,2])
Out[7]:
array([[1, 0],
.......[0, 2]])
In [8]: np.diagonal([1,2],1)
Out[8]:
array([[0, 1, 0],
.......[0, 0, 2],
.......[0, 0, 0]])
Core NumPy Functions
cumsum and diff
cumsum computes the cumulative sum of an array and diff
performs finite differencing.
cumsum(a, axis=None, dtype=keywNone, out=None)
diff(a, n=1, axis=-1)
ipython interactive shell
In [1]: A=np.diag([1,2,3])
In [2]: print(A)
[[1 0 0]
.[0 2 0]
.[0 0 3]]
In [3]: np.cumsum(A,0)
Out[3]:
array([[1, 0, 0],
.......[1, 2, 0],
.......[1, 2, 3]])
Greg von Winckel
In [4]: np.diff(A,1,0)
Out[4]:
array([[-1, 2, 0],
.......[0, -2, 3]])
In [5]: np.diff(A,2,1)
Out[5]:
array([[ 1],
.......[-4],
.......[ 3]]
Core NumPy Functions
dot, inner, and outer
dot and inner compute the dot product when given two 1D
arrays. outer computes the dyadic product 1D arrays.
ipython interactive shell
In [1]: a=np.array((1,2))
In [2]: np.dot(a,a)
Out[2]: 5
In [3]: np.inner(a,a)
Out[3]: 5
In [4]: np.outer(a,a)
Out[4]:
array([[1, 2],
.......[2, 4]])
In [5]: A=np.array(((1,2),(1,2)))
In [6]: np.dot(A,A)
Out[6]:
array([[3, 6],
.......[3, 6]])
In [7]: np.inner(A,A)
Out[7]:
array([[5, 5],
.......[5, 5]])
np.dot(A,A.T) produces the same
output as as np.inner(A,A).
Greg von Winckel
Core NumPy Functions
eye and identity
eye and identity both create 2D arrays with ones along a
diagonal and zeros elsewhere, however, eye is more general.
eye(N, M=None, k=0, dtype=<type float>)
identity(n, dtype=None)
ipython interactive shell
In [1]: np.identity(3))
Out[1]:
array([[ 1., 0., 0.],
.......[ 0., 1., 0.]
.......[ 0., 0., 1.]
In [2]: np.eye(3,2)
Out[2]:
array([[ 1., 0., 0.],
.......[ 0., 1., 0.]
In [3]: np.eye(2,2,-1,dtype=bool)
Out[3]:
array([[False, False],
.......[ True, False]], dtype=bool)
In [4]: np.eye(3,2,dtype=int)
Out[4]:
array([[1, 0],
.......[0, 1],
.......[0, 0]])
Greg von Winckel
Core NumPy Functions
histogram
This function compartmentalizes an array of data into bins
histogram(a, bins=10, range=None, normed=False,
weights=None, density=None)
Two arrays are returned: The value of each bin and the limits of
each bin
ipython interactive shell
In [1]: a = np.arange(10)
In [2]: np.histogram(a,3)
Out[2]: (array([3, 3, 4]), array([ 0., 3., 6., 9.]))
Greg von Winckel
Core NumPy Functions
polyfit, polyval, and roots
polyfit(x,y,deg,rcond=None,full=False,w=None,cov=False)
polyval(p,x)
roots(p)
The three core polynomial functions fit data with polynomials,
evaluate polynomials on a grid, and compute roots of polynomials
In [1]: x=np.linspace(-1,1,5)
In [2]: f=1-np.exp(-x)
In [3]: p=np.polyfit(x,f,3)
In [4]: np.set printoptions(precision=4,linewidth=80)
In [5]: print(f)
[-1.7183 -0.6487 0. 0.3935 0.6321]
In [6]: print(np.polyval(p,x))
[-1.7174 -0.6524 0.0056 0.3897 0.6331]
In [7]: print(np.roots(p))
[ 1.5470+1.8029j 1.5470-1.8029j -0.0056+0.j ]
Greg von Winckel
Core NumPy Functions
repeat and tile
repeat(a, repeats, axis=None)
tile(A,reps)
repeat and tile provide ways to repeat parts or all of an array
In [1]: np.repeat([0,1,2,3],[3,2,1,0])
Out[1]: array([0, 0, 0, 1, 1, 2])
In [2]: np.tile([1,0],(2,2,3))
Out[2]:
array([[[1, 0, 1, 0, 1, 0],
.........[1, 0, 1, 0, 1, 0]],
blank
.........[[1, 0, 1, 0, 1, 0],
.........[1, 0, 1, 0, 1, 0]]])
Greg von Winckel
Core NumPy Functions
mean and std
The mean and standard deviation of NumPy arrays can be
computed with these two functions.
mean(a, axis=None, dtype=None, out=None)
std(a, axis=None, dtype=None, out=None, ddof=0)
ipython interactive shell
In [1]: a=np.arange(8)
In [2]: print(a)
[0 1 2 3 4 5 6 7]
In [2]: np.mean(a)
Out[2]: 3.5
In [3]: np.std(a)
Out[3]: 2.29128784747792
In [4]: A=np.tile([1,2,3],(3,1))
In [5]: np.mean(A,0)
Out[5]: array([ 1., 2., 3.])
In [6]: np.mean(A,1)
Out[6]: array([ 2., 2., 2.])
In [7]: np.std(A,0)
Out[7]: array([ 0., 0., 0.])
numpy.average generalizes on mean to compute weighted
averages.
Greg von Winckel
Core NumPy Functions
nonzero and where
Given an array of numerical values, nonzero returns the indices
where the data contained is not zero. where extends this idea to
any condition you impose.
ipython interactive shell
In [1]: np.nonzero([1,2,0,9,5])
Out[1]: (array([0, 1, 3, 4]),)
In [2]: np.nonzero(np.eye(3))
Out[2]: (array([0, 1, 2]), array([0, 1, 2]))
In [3]: x=np.linspace(0,1,10)
In [4]: np.where(x>0.7)
Out[4]: (array([7, 8, 9]),)
Greg von Winckel
Core NumPy Functions