© 2012 M.

Scott Shell 1/24 last modified 3/22/2012
An introduction to Numpy and Scipy
Table of contents
Table of contents ............................................................................................................................ 1
Overview ......................................................................................................................................... 2
Installation ...................................................................................................................................... 2
Other resorces .............................................................................................................................. 2
Im!ortin" the #m$% modle ........................................................................................................ 2
&rra%s .............................................................................................................................................. 3
Other wa%s to create arra%s............................................................................................................ '
&rra% mathematics .......................................................................................................................... (
&rra% iteration ............................................................................................................................... 10
)asic arra% o!erations .................................................................................................................. 11
*om!arison o!erators and vale testin" ..................................................................................... 12
&rra% item selection and mani!lation ........................................................................................ 14
+ector and matri, mathematics ................................................................................................... 1-
$ol%nomial mathematics .............................................................................................................. 1(
Statistics ........................................................................................................................................ 1.
/andom nmbers .......................................................................................................................... 1.
Other fnctions to 0now abot .................................................................................................... 21
Modles available in Sci$% ............................................................................................................ 21


© 2012 M. Scott Shell 2/24 last modified 3/22/2012
Overview
#m$% and Sci$% are o!en1sorce add1on modles to $%thon that !rovide common
mathematical and nmerical rotines in !re1com!iled2 fast fnctions. These are "rowin" into
hi"hl% matre !ac0a"es that !rovide fnctionalit% that meets2 or !erha!s e,ceeds2 that
associated with common commercial software li0e Mat3ab. The #m$% 4#meric $%thon5
!ac0a"e !rovides basic rotines for mani!latin" lar"e arra%s and matrices of nmeric data.
The Sci$% 4Scientific $%thon5 !ac0a"e e,tends the fnctionalit% of #m$% with a sbstantial
collection of sefl al"orithms2 li0e minimi6ation2 7orier transformation2 re"ression2 and other
a!!lied mathematical techni8es.
Installation
If %o installed $%thon4,2%5 on a 9indows !latform2 then %o shold be read% to "o. If not2 then
%o will have to install these add1ons manall% after installin" $%thon2 in the order of #m$%
and then Sci$%. Installation files are available for both at:
htt!://www.sci!%.or"/;ownload
7ollow lin0s on this !a"e to download the official releases2 which will be in the form of .e,e
install files for 9indows and .dm" install files for MacOS.
Other resources
The #m$% and Sci$% develo!ment commnit% maintains an e,tensive online docmentation
s%stem2 incldin" ser "ides and ttorials2 at:
htt!://docs.sci!%.or"/doc/
Importing the NumPy module
There are several wa%s to im!ort #m$%. The standard a!!roach is to se a sim!le im!ort
statement:
>>> import numpy
<owever2 for lar"e amonts of calls to #m$% fnctions2 it can become tedios to write
numpy.X over and over a"ain. Instead2 it is common to im!ort nder the briefer name np:
>>> import numpy as np
© 2012 M. Scott Shell 3/24 last modified 3/22/2012
This statement will allow s to access #m$% ob=ects sin" np.X instead of numpy.X. It is
also !ossible to im!ort #m$% directl% into the crrent names!ace so that we don>t have to se
dot notation at all2 bt rather sim!l% call the fnctions as if the% were bilt1in:
>>> from numpy import *
<owever2 this strate"% is sall% frowned !on in $%thon !ro"rammin" becase it starts to
remove some of the nice or"ani6ation that modles !rovide. 7or the remainder of this ttorial2
we will assme that the import numpy as np has been sed.
Arrays
The central featre of #m$% is the array ob=ect class. &rra%s are similar to lists in $%thon2
e,ce!t that ever% element of an arra% mst be of the same t%!e2 t%!icall% a nmeric t%!e li0e
float or int. &rra%s ma0e o!erations with lar"e amonts of nmeric data ver% fast and are
"enerall% mch more efficient than lists.
&n arra% can be created from a list:
>>> a = np.array([1, 4, 5, 8], float)
>>> a
array([ 1., 4., 5., 8.])
>>> type(a)
<type 'numpy.ndarray'>
<ere2 the fnction array ta0es two ar"ments: the list to be converted into the arra% and the
t%!e of each member of the list. &rra% elements are accessed2 sliced2 and mani!lated =st li0e
lists:
>>> a[:2]
array([ 1., 4.])
>>> a[3]
8.0
>>> a[0] = 5.
>>> a
array([ 5., 4., 5., 8.])
&rra%s can be mltidimensional. ?nli0e lists2 different a,es are accessed sin" commas inside
brac0et notation. <ere is an e,am!le with a two1dimensional arra% 4e.".2 a matri,5:
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> a
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
>>> a[0,0]
1.0
>>> a[0,1]
2.0
© 2012 M. Scott Shell 4/24 last modified 3/22/2012
&rra% slicin" wor0s with mlti!le dimensions in the same wa% as sal2 a!!l%in" each slice
s!ecification as a filter to a s!ecified dimension. ?se of a sin"le @:@ in a dimension indicates the
se of ever%thin" alon" that dimension:
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> a[1,:]
array([ 4., 5., 6.])
>>> a[:,2]
array([ 3., 6.])
>>> a[-1:,-2:]
array([[ 5., 6.]])
The shape !ro!ert% of an arra% retrns a t!le with the si6e of each arra% dimension:
>>> a.shape
(2, 3)
The dtype !ro!ert% tells %o what t%!e of vales are stored b% the arra%:
>>> a.dtype
dtype('float64')
<ere2 float64 is a nmeric t%!e that #m$% ses to store doble1!recision 4(1b%te5 real
nmbers2 similar to the float t%!e in $%thon.
9hen sed with an arra%2 the len fnction retrns the len"th of the first a,is:
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> len(a)
2
The in statement can be sed to test if vales are !resent in an arra%:
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> 2 in a
True
>>> 0 in a
False
&rra%s can be resha!ed sin" t!les that s!ecif% new dimensions. In the followin" e,am!le2 we
trn a ten1element one1dimensional arra% into a two1dimensional one whose first a,is has five
elements and whose second a,is has two elements:
>>> a = np.array(range(10), float)
>>> a
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> a = a.reshape((5, 2))
>>> a
array([[ 0., 1.],
[ 2., 3.],
© 2012 M. Scott Shell A/24 last modified 3/22/2012
[ 4., 5.],
[ 6., 7.],
[ 8., 9.]])
>>> a.shape
(5, 2)
#otice that the reshape fnction creates a new arra% and does not itself modif% the ori"inal
arra%.
Bee! in mind that $%thon>s name1bindin" a!!roach still a!!lies to arra%s. The copy fnction
can be sed to create a new2 se!arate co!% of an arra% in memor% if needed:
>>> a = np.array([1, 2, 3], float)
>>> b = a
>>> c = a.copy()
>>> a[0] = 0
>>> a
array([0., 2., 3.])
>>> b
array([0., 2., 3.])
>>> c
array([1., 2., 3.])
3ists can also be created from arra%s:
>>> a = np.array([1, 2, 3], float)
>>> a.tolist()
[1.0, 2.0, 3.0]
>>> list(a)
[1.0, 2.0, 3.0]
One can convert the raw data in an arra% to a binar% strin" 4i.e.2 not in hman1readable form5
sin" the tostring fnction. The fromstring fnction then allows an arra% to be created
from this data later on. These rotines are sometimes convenient for savin" lar"e amont of
arra% data in files that can be read later on:
>>> a = array([1, 2, 3], float)
>>> s = a.tostring()
>>> s
'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00
\x00\x00\x08@'
>>> np.fromstring(s)
array([ 1., 2., 3.])
One can fill an arra% with a sin"le vale:
>>> a = array([1, 2, 3], float)
>>> a
array([ 1., 2., 3.])
>>> a.fill(0)
>>> a
© 2012 M. Scott Shell -/24 last modified 3/22/2012
array([ 0., 0., 0.])
Trans!osed versions of arra%s can also be "enerated2 which will create a new arra% with the
final two a,es switched:
>>> a = np.array(range(6), float).reshape((2, 3))
>>> a
array([[ 0., 1., 2.],
[ 3., 4., 5.]])
>>> a.transpose()
array([[ 0., 3.],
[ 1., 4.],
[ 2., 5.]])
One1dimensional versions of mlti1dimensional arra%s can be "enerated with flatten:
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> a
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
>>> a.flatten()
array([ 1., 2., 3., 4., 5., 6.])
Two or more arra%s can be concatenated to"ether sin" the concatenate fnction with a
t!le of the arra%s to be =oined:
>>> a = np.array([1,2], float)
>>> b = np.array([3,4,5,6], float)
>>> c = np.array([7,8,9], float)
>>> np.concatenate((a, b, c))
array([1., 2., 3., 4., 5., 6., 7., 8., 9.])
If an arra% has more than one dimension2 it is !ossible to s!ecif% the a,is alon" which mlti!le
arra%s are concatenated. )% defalt 4withot s!ecif%in" the a,is52 #m$% concatenates alon"
the first dimension:
>>> a = np.array([[1, 2], [3, 4]], float)
>>> b = np.array([[5, 6], [7,8]], float)
>>> np.concatenate((a,b))
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.],
[ 7., 8.]])
>>> np.concatenate((a,b), axis=0)
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.],
[ 7., 8.]])
>>> np.concatenate((a,b), axis=1)
array([[ 1., 2., 5., 6.],
[ 3., 4., 7., 8.]])
© 2012 M. Scott Shell '/24 last modified 3/22/2012
7inall%2 the dimensionalit% of an arra% can be increased sin" the newaxis constant in brac0et
notation:
>>> a = np.array([1, 2, 3], float)
>>> a
array([1., 2., 3.])
>>> a[:,np.newaxis]
array([[ 1.],
[ 2.],
[ 3.]])
>>> a[:,np.newaxis].shape
(3,1)
>>> b[np.newaxis,:]
array([[ 1., 2., 3.]])
>>> b[np.newaxis,:].shape
(1,3)
#otice here that in each case the new arra% has two dimensionsC the one created b% newaxis
has a len"th of one. The newaxis a!!roach is convenient for "eneratin" the !ro!er1
dimensioned arra%s for vector and matri, mathematics.
Other ways to create arrays
The arange fnction is similar to the range fnction bt retrns an arra%:
>>> np.arange(5, dtype=float)
array([ 0., 1., 2., 3., 4.])
>>> np.arange(1, 6, 2, dtype=int)
array([1, 3, 5])
The fnctions zeros and ones create new arra%s of s!ecified dimensions filled with these
vales. These are !erha!s the most commonl% sed fnctions to create new arra%s:
>>> np.ones((2,3), dtype=float)
array([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> np.zeros(7, dtype=int)
array([0, 0, 0, 0, 0, 0, 0])
The zeros_like and ones_like fnctions create a new arra% with the same dimensions
and t%!e of an e,istin" one:
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> np.zeros_like(a)
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
>>> np.ones_like(a)
array([[ 1., 1., 1.],
[ 1., 1., 1.]])
© 2012 M. Scott Shell (/24 last modified 3/22/2012
There are also a nmber of fnctions for creatin" s!ecial matrices 42; arra%s5. To create an
identit% matri, of a "iven si6e2
>>> np.identity(4, dtype=float)
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
The eye fnction retrns matrices with ones alon" the 0th dia"onal:
>>> np.eye(4, k=1, dtype=float)
array([[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.],
[ 0., 0., 0., 0.]])
Array mathematics
9hen standard mathematical o!erations are sed with arra%s2 the% are a!!lied on an element1
b%1element basis. This means that the arra%s shold be the same si6e drin" addition2
sbtraction2 etc.:
>>> a = np.array([1,2,3], float)
>>> b = np.array([5,2,6], float)
>>> a + b
array([6., 4., 9.])
>>> a – b
array([-4., 0., -3.])
>>> a * b
array([5., 4., 18.])
>>> b / a
array([5., 1., 2.])
>>> a % b
array([1., 0., 3.])
>>> b**a
array([5., 4., 216.])
7or two1dimensional arra%s2 mlti!lication remains elementwise and does not corres!ond to
matri, mlti!lication. There are s!ecial fnctions for matri, math that we will cover later.
>>> a = np.array([[1,2], [3,4]], float)
>>> b = np.array([[2,0], [1,3]], float)
>>> a * b
array([[2., 0.], [3., 12.]])
Drrors are thrown if arra%s do not match in si6e:
>>> a = np.array([1,2,3], float)
>>> b = np.array([4,5], float)
>>> a + b
© 2012 M. Scott Shell ./24 last modified 3/22/2012
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
<owever2 arra%s that do not match in the nmber of dimensions will be broadcasted b% $%thon
to !erform mathematical o!erations. This often means that the smaller arra% will be re!eated
as necessar% to !erform the o!eration indicated. *onsider the followin":
>>> a = np.array([[1, 2], [3, 4], [5, 6]], float)
>>> b = np.array([-1, 3], float)
>>> a
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
>>> b
array([-1., 3.])
>>> a + b
array([[ 0., 5.],
[ 2., 7.],
[ 4., 9.]])
<ere2 the one1dimensional arra% b was broadcasted to a two1dimensional arra% that matched
the si6e of a. In essence2 b was re!eated for each item in a2 as if it were "iven b%
array([[-1., 3.],
[-1., 3.],
[-1., 3.]])
$%thon atomaticall% broadcasts arra%s in this manner. Sometimes2 however2 how we shold
broadcast is ambi"os. In these cases2 we can se the newaxis constant to s!ecif% how we
want to broadcast:
>>> a = np.zeros((2,2), float)
>>> b = np.array([-1., 3.], float)
>>> a
array([[ 0., 0.],
[ 0., 0.]])
>>> b
array([-1., 3.])
>>> a + b
array([[-1., 3.],
[-1., 3.]])
>>> a + b[np.newaxis,:]
array([[-1., 3.],
[-1., 3.]])
>>> a + b[:,np.newaxis]
array([[-1., -1.],
[ 3., 3.]])
In addition to the standard o!erators2 #m$% offers a lar"e librar% of common mathematical
fnctions that can be a!!lied elementwise to arra%s. &mon" these are the fnctions: abs,
© 2012 M. Scott Shell 10/24 last modified 3/22/2012
sign, sqrt, log, log10, exp, sin, cos, tan, arcsin, arccos,
arctan, sinh, cosh, tanh, arcsinh, arccosh, and arctanh.
>>> a = np.array([1, 4, 9], float)
>>> np.sqrt(a)
array([ 1., 2., 3.])
The fnctions floor, ceil2 and rint "ive the lower2 !!er2 or nearest 4ronded5 inte"er:
>>> a = np.array([1.1, 1.5, 1.9], float)
>>> np.floor(a)
array([ 1., 1., 1.])
>>> np.ceil(a)
array([ 2., 2., 2.])
>>> np.rint(a)
array([ 1., 2., 2.])
&lso inclded in the #m$% modle are two im!ortant mathematical constants:
>>> np.pi
3.1415926535897931
>>> np.e
2.7182818284590451
Array iteration
It is !ossible to iterate over arra%s in a manner similar to that of lists:
>>> a = np.array([1, 4, 5], int)
>>> for x in a:
... print x
... <hit return>
1
4
5
7or mltidimensional arra%s2 iteration !roceeds over the first a,is sch that each loo! retrns a
sbsection of the arra%:
>>> a = np.array([[1, 2], [3, 4], [5, 6]], float)
>>> for x in a:
... print x
... <hit return>
[ 1. 2.]
[ 3. 4.]
[ 5. 6.]
Mlti!le assi"nment can also be sed with arra% iteration:
>>> a = np.array([[1, 2], [3, 4], [5, 6]], float)
>>> for (x, y) in a:
... print x * y
© 2012 M. Scott Shell 11/24 last modified 3/22/2012
... <hit return>
2.0
12.0
30.0
Basic array operations
Man% fnctions e,ist for e,tractin" whole1arra% !ro!erties. The items in an arra% can be
smmed or mlti!lied:
>>> a = np.array([2, 4, 3], float)
>>> a.sum()
9.0
>>> a.prod()
24.0
In this e,am!le2 member fnctions of the arra%s were sed. &lternativel%2 standalone fnctions
in the #m$% modle can be accessed:
>>> np.sum(a)
9.0
>>> np.prod(a)
24.0
7or most of the rotines described below2 both standalone and member fnctions are available.
& nmber of rotines enable com!tation of statistical 8antities in arra% datasets2 sch as the
mean 4avera"e52 variance2 and standard deviation:
>>> a = np.array([2, 1, 9], float)
>>> a.mean()
4.0
>>> a.var()
12.666666666666666
>>> a.std()
3.5590260840104371
It>s also !ossible to find the minimm and ma,imm element vales:
>>> a = np.array([2, 1, 9], float)
>>> a.min()
1.0
>>> a.max()
9.0
The argmin and argmax fnctions retrn the arra% indices of the minimm and ma,imm
vales:
>>> a = np.array([2, 1, 9], float)
>>> a.argmin()
1
© 2012 M. Scott Shell 12/24 last modified 3/22/2012
>>> a.argmax()
2
7or mltidimensional arra%s2 each of the fnctions ths far described can ta0e an o!tional
ar"ment axis that will !erform an o!eration alon" onl% the s!ecified a,is2 !lacin" the reslts
in a retrn arra%:
>>> a = np.array([[0, 2], [3, -1], [3, 5]], float)
>>> a.mean(axis=0)
array([ 2., 2.])
>>> a.mean(axis=1)
array([ 1., 1., 4.])
>>> a.min(axis=1)
array([ 0., -1., 3.])
>>> a.max(axis=0)
array([ 3., 5.])
3i0e lists2 arra%s can be sorted:
>>> a = np.array([6, 2, 5, -1, 0], float)
>>> sorted(a)
[-1.0, 0.0, 2.0, 5.0, 6.0]
>>> a.sort()
>>> a
array([-1., 0., 2., 5., 6.])
+ales in an arra% can be @cli!!ed@ to be within a !res!ecified ran"e. This is the same as
a!!l%in" min(max(x, minval), maxval) to each element x in an arra%.
>>> a = np.array([6, 2, 5, -1, 0], float)
>>> a.clip(0, 5)
array([ 5., 2., 5., 0., 0.])
?ni8e elements can be e,tracted from an arra%:
>>> a = np.array([1, 1, 4, 5, 5, 5, 7], float)
>>> np.unique(a)
array([ 1., 4., 5., 7.])
7or two dimensional arra%s2 the dia"onal can be e,tracted:
>>> a = np.array([[1, 2], [3, 4]], float)
>>> a.diagonal()
array([ 1., 4.])
Comparison operators and value testing
)oolean com!arisons can be sed to com!are members elementwise on arra%s of e8al si6e.
The retrn vale is an arra% of )oolean True / False vales:
>>> a = np.array([1, 3, 0], float)
© 2012 M. Scott Shell 13/24 last modified 3/22/2012
>>> b = np.array([0, 3, 2], float)
>>> a > b
array([ True, False, False], dtype=bool)
>>> a == b
array([False, True, False], dtype=bool)
>>> a <= b
array([False, True, True], dtype=bool)
The reslts of a )oolean com!arison can be stored in an arra%:
>>> c = a > b
>>> c
array([ True, False, False], dtype=bool)
&rra%s can be com!ared to sin"le vales sin" broadcastin":
>>> a = np.array([1, 3, 0], float)
>>> a > 2
array([False, True, False], dtype=bool)
The any and all o!erators can be sed to determine whether or not an% or all elements of a
)oolean arra% are tre:
>>> c = np.array([ True, False, False], bool)
>>> any(c)
True
>>> all(c)
False
*om!ond )oolean e,!ressions can be a!!lied to arra%s on an element1b%1element basis sin"
s!ecial fnctions logical_and2 logical_or2 and logical_not.
>>> a = np.array([1, 3, 0], float)
>>> np.logical_and(a > 0, a < 3)
array([ True, False, False], dtype=bool)
>>> b = np.array([True, False, True], bool)
>>> np.logical_not(b)
array([False, True, False], dtype=bool)
>>> c = np.array([False, True, False], bool)
>>> np.logical_or(b, c)
array([ True, True, False], dtype=bool)
The where fnction forms a new arra% from two arra%s of e8ivalent si6e sin" a )oolean filter
to choose between elements of the two. Its basic s%nta, is where(boolarray,
truearray, falsearray):
>>> a = np.array([1, 3, 0], float)
>>> np.where(a != 0, 1 / a, a)
array([ 1. , 0.33333333, 0. ])
)roadcastin" can also be sed with the where fnction:
© 2012 M. Scott Shell 14/24 last modified 3/22/2012
>>> np.where(a > 0, 3, 2)
array([3, 3, 2])
& nmber of fnctions allow testin" of the vales in an arra%. The nonzero fnction "ives a
t!le of indices of the non6ero vales in an arra%. The nmber of items in the t!le e8als the
nmber of a,es of the arra%:
>>> a = np.array([[0, 1], [3, 0]], float)
>>> a.nonzero()
(array([0, 1]), array([1, 0]))
It is also !ossible to test whether or not vales are #a# 4@not a nmber@5 or finite:
>>> a = np.array([1, np.NaN, np.Inf], float)
>>> a
array([ 1., NaN, Inf])
>>> np.isnan(a)
array([False, True, False], dtype=bool)
>>> np.isfinite(a)
array([ True, False, False], dtype=bool)
&ltho"h here we sed #m$% constants to add the #a# and infinite vales2 these can reslt
from standard mathematical o!erations.
Array item selection and manipulation
9e have alread% seen that2 li0e lists2 individal elements and slices of arra%s can be selected
sin" brac0et notation. ?nli0e lists2 however2 arra%s also !ermit selection sin" other arra%s.
That is2 we can se array selectors to filter for s!ecific sbsets of elements of other arra%s.
)oolean arra%s can be sed as arra% selectors:
>>> a = np.array([[6, 4], [5, 9]], float)
>>> a >= 6
array([[ True, False],
[False, True]], dtype=bool)
>>> a[a >= 6]
array([ 6., 9.])
#otice that sendin" the )oolean arra% "iven b% a>=6 to the brac0et selection for a2 an arra%
with onl% the True elements is retrned. 9e cold have also stored the selector arra% in a
variable:
>>> a = np.array([[6, 4], [5, 9]], float)
>>> sel = (a >= 6)
>>> a[sel]
array([ 6., 9.])
More com!licated selections can be achieved sin" )oolean e,!ressions:
© 2012 M. Scott Shell 1A/24 last modified 3/22/2012
>>> a[np.logical_and(a > 5, a < 9)]
>>> array([ 6.])
In addition to )oolean selection2 it is !ossible to select sin" inte"er arra%s. <ere2 the inte"er
arra%s contain the indices of the elements to be ta0en from an arra%. *onsider the followin"
one1dimensional e,am!le:
>>> a = np.array([2, 4, 6, 8], float)
>>> b = np.array([0, 0, 1, 3, 2, 1], int)
>>> a[b]
array([ 2., 2., 4., 8., 6., 4.])
In other words2 we ta0e the 0
th
2 0
th
2 1
st
2 3
rd
2 2
nd
2 and 1
st
elements of a2 in that order2 when we
se b to select elements from a. 3ists can also be sed as selection arra%s:
>>> a = np.array([2, 4, 6, 8], float)
>>> a[[0, 0, 1, 3, 2, 1]]
array([ 2., 2., 4., 8., 6., 4.])
7or mltidimensional arra%s2 we have to send mlti!le one1dimensional inte"er arra%s to the
selection brac0et2 one for each a,is. Then2 each of these selection arra%s is traversed in
se8ence: the first element ta0en has a first a,is inde, ta0en from the first member of the first
selection arra%2 a second inde, from the first member of the second selection arra%2 and so on.
&n e,am!le:
>>> a = np.array([[1, 4], [9, 16]], float)
>>> b = np.array([0, 0, 1, 1, 0], int)
>>> c = np.array([0, 1, 1, 1, 1], int)
>>> a[b,c]
array([ 1., 4., 16., 16., 4.])
& s!ecial fnction take is also available to !erform selection with inte"er arra%s. This wor0s in
an identical manner as brac0et selection:
>>> a = np.array([2, 4, 6, 8], float)
>>> b = np.array([0, 0, 1, 3, 2, 1], int)
>>> a.take(b)
array([ 2., 2., 4., 8., 6., 4.])
take also !rovides an a,is ar"ment2 sch that sbsections of an mlti1dimensional arra% can
be ta0en across a "iven dimension.
>>> a = np.array([[0, 1], [2, 3]], float)
>>> b = np.array([0, 0, 1], int)
>>> a.take(b, axis=0)
array([[ 0., 1.],
[ 0., 1.],
[ 2., 3.]])
>>> a.take(b, axis=1)
© 2012 M. Scott Shell 1-/24 last modified 3/22/2012
array([[ 0., 0., 1.],
[ 2., 2., 3.]])
The o!!osite of the take fnction is the put fnction2 which will ta0e vales from a sorce
arra% and !lace them at s!ecified indices in the arra% callin" put.
>>> a = np.array([0, 1, 2, 3, 4, 5], float)
>>> b = np.array([9, 8, 7], float)
>>> a.put([0, 3], b)
>>> a
array([ 9., 1., 2., 8., 4., 5.])
#ote that the vale ' from the sorce arra% b is not sed2 since onl% two indices E02 3F are
s!ecified. The sorce arra% will be re!eated as necessar% if not the same si6e:
>>> a = np.array([0, 1, 2, 3, 4, 5], float)
>>> a.put([0, 3], 5)
>>> a
array([ 5., 1., 2., 5., 4., 5.])
Vector and matri mathematics
#m$% !rovides man% fnctions for !erformin" standard vector and matri, mlti!lication
rotines. To !erform a dot !rodct2
>>> a = np.array([1, 2, 3], float)
>>> b = np.array([0, 1, 1], float)
>>> np.dot(a, b)
5.0
The dot fnction also "enerali6es to matri, mlti!lication:
>>> a = np.array([[0, 1], [2, 3]], float)
>>> b = np.array([2, 3], float)
>>> c = np.array([[1, 1], [4, 0]], float)
>>> a
array([[ 0., 1.],
[ 2., 3.]])
>>> np.dot(b, a)
array([ 6., 11.])
>>> np.dot(a, b)
array([ 3., 13.])
>>> np.dot(a, c)
array([[ 4., 0.],
[ 14., 2.]])
>>> np.dot(c, a)
array([[ 2., 4.],
[ 0., 4.]])
It is also !ossible to "enerate inner2 oter2 and cross !rodcts of matrices and vectors. 7or
vectors2 note that the inner !rodct is e8ivalent to the dot !rodct:
© 2012 M. Scott Shell 1'/24 last modified 3/22/2012
>>> a = np.array([1, 4, 0], float)
>>> b = np.array([2, 2, 1], float)
>>> np.outer(a, b)
array([[ 2., 2., 1.],
[ 8., 8., 4.],
[ 0., 0., 0.]])
>>> np.inner(a, b)
10.0
>>> np.cross(a, b)
array([ 4., -1., -6.])
#m$% also comes with a nmber of bilt1in rotines for linear al"ebra calclations. These can
be fond in the sb1modle linalg. &mon" these are rotines for dealin" with matrices and
their inverses. The determinant of a matri, can be fond:
>>> a = np.array([[4, 2, 0], [9, 3, 7], [1, 2, 1]], float)
>>> a
array([[ 4., 2., 0.],
[ 9., 3., 7.],
[ 1., 2., 1.]])
>>> np.linalg.det(a)
-53.999999999999993
One can find the ei"envales and ei"envectors of a matri,:
>>> vals, vecs = np.linalg.eig(a)
>>> vals
array([ 9. , 2.44948974, -2.44948974])
>>> vecs
array([[-0.3538921 , -0.56786837, 0.27843404],
[-0.88473024, 0.44024287, -0.89787873],
[-0.30333608, 0.69549388, 0.34101066]])
The inverse of a matri, can be fond:
>>> b = np.linalg.inv(a)
>>> b
array([[ 0.14814815, 0.07407407, -0.25925926],
[ 0.2037037 , -0.14814815, 0.51851852],
[-0.27777778, 0.11111111, 0.11111111]])
>>> np.dot(a, b)
array([[ 1.00000000e+00, 5.55111512e-17, 2.22044605e-16],
[ 0.00000000e+00, 1.00000000e+00, 5.55111512e-16],
[ 1.11022302e-16, 0.00000000e+00, 1.00000000e+00]])
Sin"lar vale decom!osition 4analo"os to dia"onali6ation of a nons8are matri,5 can also be
!erformed:
>>> a = np.array([[1, 3, 4], [5, 2, 3]], float)
>>> U, s, Vh = np.linalg.svd(a)
>>> U
array([[-0.6113829 , -0.79133492],
[-0.79133492, 0.6113829 ]])
© 2012 M. Scott Shell 1(/24 last modified 3/22/2012
>>> s
array([ 7.46791327, 2.86884495])
>>> Vh
array([[-0.61169129, -0.45753324, -0.64536587],
[ 0.78971838, -0.40129005, -0.46401635],
[-0.046676 , -0.79349205, 0.60678804]])
Polynomial mathematics
#m$% s!!lies methods for wor0in" with !ol%nomials. Given a set of roots2 it is !ossible to
show the !ol%nomial coefficients:
>>> np.poly([-1, 1, 1, 10])
array([ 1, -11, 9, 11, -10])
<ere2 the retrn arra% "ives the coefficients corres!ondin" to x
4
− 11x
3
+ 9x
2
+ 11x − 1u.
The o!!osite o!eration can be !erformed: "iven a set of coefficients2 the root fnction retrns
all of the !ol%nomial roots:
>>> np.roots([1, 4, -2, 3])
array([-4.57974010+0.j , 0.28987005+0.75566815j,
0.28987005-0.75566815j])
#otice here that two of the roots of x
3
+ 4x
2
− 2x + S are ima"inar%.
*oefficient arra%s of !ol%nomials can be inte"rated. *onsider inte"ratin" x
3
+ x
2
+ x + 1 to
x
4
4 ⁄ + x
3
S ⁄ +x
2
2 ⁄ + x + C. )% defalt2 the constant C is set to 6ero:
>>> np.polyint([1, 1, 1, 1])
array([ 0.25 , 0.33333333, 0.5 , 1. , 0. ])
Similarl%2 derivatives can be ta0en:
>>> np.polyder([1./4., 1./3., 1./2., 1., 0.])
array([ 1., 1., 1., 1.])
The fnctions polyadd2 polysub2 polymul2 and polydiv also handle !ro!er addition2
sbtraction2 mlti!lication2 and division of !ol%nomial coefficients2 res!ectivel%.
The fnction polyval evalates a !ol%nomial at a !articlar !oint. *onsider x
3
− 2x
2
+ 2
evalated at x = 4:
>>> np.polyval([1, -2, 0, 2], 4)
34
7inall%2 the polyfit fnction can be sed to fit a !ol%nomial of s!ecified order to a set of data
sin" a least1s8ares a!!roach:
© 2012 M. Scott Shell 1./24 last modified 3/22/2012
>>> x = [1, 2, 3, 4, 5, 6, 7, 8]
>>> y = [0, 2, 1, 3, 7, 10, 11, 19]
>>> np.polyfit(x, y, 2)
array([ 0.375 , -0.88690476, 1.05357143])
The retrn vale is a set of !ol%nomial coefficients. More so!histicated inter!olation rotines
can be fond in the Sci$% !ac0a"e.
!tatistics
In addition to the mean2 var2 and std fnctions2 #m$% s!!lies several other methods for
retrnin" statistical featres of arra%s.
The median can be fond:
>>> a = np.array([1, 4, 3, 8, 9, 2, 3], float)
>>> np.median(a)
3.0
The correlation coefficient for mlti!le variables observed at mlti!le instances can be fond
for arra%s of the form EE,12 ,22 HF2 E%12 %22 HF2 E612 622 HF2 HF where ,2 %2 6 are different
observables and the nmbers indicate the observation times:
>>> a = np.array([[1, 2, 1, 3], [5, 3, 1, 8]], float)
>>> c = np.corrcoef(a)
>>> c
array([[ 1. , 0.72870505],
[ 0.72870505, 1. ]])
<ere the retrn arra% c[i,j] "ives the correlation coefficient for the ith and =th observables.
Similarl%2 the covariance for data can be fond:
>>> np.cov(a)
array([[ 0.91666667, 2.08333333],
[ 2.08333333, 8.91666667]])
"andom numbers
&n im!ortant !art of an% simlation is the abilit% to draw random nmbers. 7or this !r!ose2
we se #m$%>s bilt1in !sedorandom nmber "enerator rotines in the sb1modle
random. The nmbers are pseudo random in the sense that the% are "enerated
deterministicall% from a seed nmber2 bt are distribted in what has statistical similarities to
random fashion. #m$% ses a !articlar al"orithm called the Mersenne Twister to "enerate
!sedorandom nmbers.
The random nmber seed can be set:
© 2012 M. Scott Shell 20/24 last modified 3/22/2012
>>> np.random.seed(293423)
The seed is an inte"er vale. &n% !ro"ram that starts with the same seed will "enerate e,actl%
the same se8ence of random nmbers each time it is rn. This can be sefl for deb""in"
!r!oses2 bt one does not need to s!ecif% the seed and in fact2 when we !erform mlti!le
rns of the same simlation to be avera"ed to"ether2 we want each sch trial to have a
different se8ence of random nmbers. If this command is not rn2 #m$% atomaticall%
selects a random seed 4based on the time5 that is different ever% time a !ro"ram is rn.
&n arra% of random nmbers in the half1o!en interval E0.02 1.05 can be "enerated:
>>> np.random.rand(5)
array([ 0.40783762, 0.7550402 , 0.00919317, 0.01713451, 0.95299583])
The rand fnction can be sed to "enerate two1dimensional random arra%s2 or the resize
fnction cold be em!lo%ed here:
>>> np.random.rand(2,3)
array([[ 0.50431753, 0.48272463, 0.45811345],
[ 0.18209476, 0.48631022, 0.49590404]])
>>> np.random.rand(6).reshape((2,3))
array([[ 0.72915152, 0.59423848, 0.25644881],
[ 0.75965311, 0.52151819, 0.60084796]])
To "enerate a sin"le random nmber in E0.02 1.052
>>> np.random.random()
0.70110427435769551
To "enerate random inte"ers in the ran"e Emin2 ma,5 se randint(min, max):
>>> np.random.randint(5, 10)
9
In each of these e,am!les2 we drew random nmbers form a niform distribtion. #m$% also
incldes "enerators for man% other distribtions2 incldin" the )eta2 binomial2 chi1s8are2
;irichlet2 e,!onential2 72 Gamma2 "eometric2 Gmbel2 h%!er"eometric2 3a!lace2 lo"istic2 lo"1
normal2 lo"arithmic2 mltinomial2 mltivariate2 ne"ative binomial2 noncentral chi1s8are2
noncentral 72 normal2 $areto2 $oisson2 !ower2 /a%lei"h2 *ach%2 stdent>s t2 trian"lar2 von
Mises2 9ald2 9eibll2 and Ii!f distribtions. <ere we onl% "ive e,am!les for two of these.
To draw from the discrete $oisson distribtion with z = 6.u2
>>> np.random.poisson(6.0)
5
© 2012 M. Scott Shell 21/24 last modified 3/22/2012
To draw from a continos normal 4Gassian5 distribtion with mean p = 1.S and standard
deviation o = 4.u:
>>> np.random.normal(1.5, 4.0)
0.83636555041094318
To draw from a standard normal distribtion 4p = u2 o = 152 omit the ar"ments:
>>> np.random.normal()
0.27548716940682932
To draw mlti!le vales2 se the o!tional size ar"ment:
>>> np.random.normal(size=5)
array([-1.67215088, 0.65813053, -0.70150614, 0.91452499, 0.71440557])
The random modle can also be sed to randoml% shffle the order of items in a list. This is
sometimes sefl if we want to sort a list in random order:
>>> l = range(10)
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> np.random.shuffle(l)
>>> l
[4, 9, 5, 0, 2, 7, 6, 8, 1, 3]
#otice that the shffle fnction modifies the list in place2 meanin" it does not retrn a new list
bt rather modifies the ori"inal list itself.
Other functions to #now about
#m$% contains man% other bilt1in fnctions that we have not covered here. In !articlar2
there are rotines for discrete 7orier transforms2 more com!le, linear al"ebra o!erations2 si6e
/ sha!e / t%!e testin" of arra%s2 s!littin" and =oinin" arra%s2 histo"rams2 creatin" arra%s of
nmbers s!aced in varios wa%s2 creatin" and evalatin" fnctions on "rid arra%s2 treatin"
arra%s with s!ecial 4#a#2 Inf5 vales2 set o!erations2 creatin" varios 0inds of s!ecial matrices2
and evalatin" s!ecial mathematical fnctions 4e.".2 )essel fnctions5. Jo are encora"ed to
conslt the #m$% docmentation at htt!://docs.sci!%.or"/doc/ for more details.
$odules available in !ciPy
Sci$% "reatl% e,tends the fnctionalit% of the #m$% rotines. 9e will not cover this modle in
detail bt rather mention some of its ca!abilities. Man% Sci$% rotines can be accessed b%
sim!l% im!ortin" the modle:
>>> import scipy
© 2012 M. Scott Shell 22/24 last modified 3/22/2012
The hel! fnction !rovides sefl information on the !ac0a"es that Sci$% offers:
>>> help(scipy)
Help on package scipy:

NAME
scipy

FILE
c:\python25\lib\site-packages\scipy\__init__.py

DESCRIPTION
SciPy --- A scientific computing package for Python
===================================================

Documentation is available in the docstrings and
online at http://docs.scipy.org.

Contents
--------
SciPy imports all the functions from the NumPy namespace, and in
addition provides:

Available subpackages
---------------------
odr --- Orthogonal Distance Regression [*]
misc --- Various utilities that don't have
another home.
sparse.linalg.eigen.arpack --- Eigenvalue solver using iterative
methods. [*]
fftpack --- Discrete Fourier Transform algorithms
[*]
io --- Data input and output [*]
sparse.linalg.eigen.lobpcg --- Locally Optimal Block Preconditioned
Conjugate Gradient Method (LOBPCG) [*]
special --- Airy Functions [*]
lib.blas --- Wrappers to BLAS library [*]
sparse.linalg.eigen --- Sparse Eigenvalue Solvers [*]
stats --- Statistical Functions [*]
lib --- Python wrappers to external libraries
[*]
lib.lapack --- Wrappers to LAPACK library [*]
maxentropy --- Routines for fitting maximum entropy
models [*]
integrate --- Integration routines [*]
ndimage --- n-dimensional image package [*]
linalg --- Linear algebra routines [*]
spatial --- Spatial data structures and algorithms
[*]
interpolate --- Interpolation Tools [*]
sparse.linalg --- Sparse Linear Algebra [*]
sparse.linalg.dsolve.umfpack --- :Interface to the UMFPACK library: [*]
sparse.linalg.dsolve --- Linear Solvers [*]
optimize --- Optimization Tools [*]
cluster --- Vector Quantization / Kmeans [*]
signal --- Signal Processing Tools [*]
sparse --- Sparse Matrices [*]
[*] - using a package requires explicit import (see pkgload)
...
© 2012 M. Scott Shell 23/24 last modified 3/22/2012
#otice that a nmber of sb1modles in Sci$% re8ire e,!licit im!ort2 as indicated b% the star
notation above:
>>> import scipy
>>> import scipy.interpolate
The fnctions in each modle are well1docmented in both the internal docstrin"s and at the
Sci$% docmentation website. Man% of these fnctions !rovide instant access to common
nmerical al"orithms2 and are ver% eas% to im!lement. Ths2 Sci$% can save tremendos
amonts of time in scientific com!tin" a!!lications since it offers a librar% of !re1written2 !re1
tested rotines.
9e will not cover what Sci$% has to offer in detail2 bt in the table below we mention a sbset
of its ca!abilities:
module code for…
scipy.constants
Man% mathematical and !h%sical constants.
scipy.special
S!ecial fnctions for mathematical !h%sics2 sch as ir%2
elli!tic2 bessel2 "amma2 beta2 h%!er"eometric2 !arabolic
c%linder2 mathie2 s!heroidal wave2 strve2 and 0elvin
fnctions.
scipy.integrate
7nctions for !erformin" nmerical inte"ration sin"
tra!e6oidal2 Sim!son>s2 /omber"2 and other methods. &lso
!rovides methods for inte"ration of ordinar% differential
e8ations.
scipy.optimize
Standard minimi6ation / ma,imi6ation rotines that o!erate
on "eneric ser1defined ob=ective fnctions. &l"orithms
inclde: #elder1Mead Sim!le,2 $owell>s2 con="ate "radient2
)7GS2 least1s8ares2 constrained o!timi6ers2 simlated
annealin"2 brte force2 )rent>s method2 #ewton>s method2
bisection method2 )ro%den2 &nderson2 and line search.
scipy.linalg
Mch broader base of linear al"ebra rotines than #m$%.
Offers more control for sin" s!ecial2 faster rotines for
s!ecific cases 4e.".2 tridia"onal matrices5. Methods inclde:
inverse2 determinant2 solvin" a linear s%stem of e8ations2
com!tin" norms and !sedo/"enerali6ed inverses2
ei"envale/ei"envector decom!osition2 sin"lar vale
decom!osition2 3? decom!osition2 *holes0% decom!osition2
K/ decom!osition2 Schr decom!osition2 and varios other
mathematical o!erations on matrices.
scipy.sparse
/otines for wor0in" with lar"e2 s!arse matrices.
scipy.interpolate
/otines and classes for inter!olation ob=ects that can be
sed with discrete nmeric data. 3inear and s!line
inter!olation available for one1 and two1dimensional data
sets.
scipy.fftpack
7ast 7orier transform rotines and !rocessin".
© 2012 M. Scott Shell 24/24 last modified 3/22/2012
scipy.signal
Si"nal !rocessin" rotines2 sch as convoltion2 correlation2
finite forier transforms2 )1s!line smoothin"2 filterin"2 etc.
scipy.stats
<"e librar% of varios statistical distribtions and statistical
fnctions for o!eratin" on sets of data.

& lar"e commnit% of develo!ers continall% bilds new fnctionalit% into Sci$%. & "ood rle of
thmb is: if %o are thin0in" abot im!lementin" a nmerical rotine into %or code2 chec0 the
Sci$% docmentation website first. *hances are2 if it>s a common tas02 someone will have
added it to Sci$%.

Sign up to vote on this title
UsefulNot useful