Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
numpy

numpy

Ratings: (0)|Views: 0 |Likes:

Availability:

See more
See less

06/13/2014

pdf

text

original

An introduction to Numpy and Scipy

Overview ......................................................................................................................................... 2

Installation ...................................................................................................................................... 2

Other resorces .............................................................................................................................. 2

Im!ortin" the #m\$% modle ........................................................................................................ 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 vale testin" ..................................................................................... 12

&rra% item selection and mani!lation ........................................................................................ 14

+ector and matri, mathematics ................................................................................................... 1-

\$ol%nomial mathematics .............................................................................................................. 1(

Statistics ........................................................................................................................................ 1

andom nmbers .......................................................................................................................... 1

Other fnctions to now abot .................................................................................................... 21

Modles available in Sci\$% ............................................................................................................ 21

Overview
#m\$% and Sci\$% are o!ensorce addon modles to \$%thon that !rovide common mathematical and nmerical rotines in !recom!iled fast fnctions. These are "rowin" into hi"hl% matre !aca"es that !rovide fnctionalit% that meets or !erha!s e,ceeds that associated with common commercial software lie Matab. The #m\$% #meric \$%thon5 !aca"e !rovides basic rotines for mani!latin" lar"e arra%s and matrices of nmeric data. The Sci\$% Scientific \$%thon5 !aca"e e,tends the fnctionalit% of #m\$% with a sbstantial collection of sefl al"orithms lie minimi6ation 7orier transformation re"ression and other a!!lied mathematical techni8es.
Installation
If %o installed \$%thon,%5 on a 9indows !latform then %o shold be read% to "o. If not then %o will have to install these addons manall% after installin" \$%thon in the order of #m\$% and then Sci\$%. Installation files are available for both at: htt!://www.sci!%.or"/;ownload 7ollow lins on this !a"e to download the official releases 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 commnit% maintains an e,tensive online docmentation s%stem incldin" ser "ides and ttorials 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
<owever for lar"e amonts of calls to #m\$% fnctions it can become tedios to write
numpy.X
over and over a"ain. Instead 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
numpy.X
. It is also !ossible to im!ort #m\$% directl% into the crrent names!ace so that we don>t have to se dot notation at all bt rather sim!l% call the fnctions as if the% were biltin:
>>> from numpy import *
<owever this strate"% is sall% frowned !on in \$%thon !ro"rammin" becase it starts to remove some of the nice or"ani6ation that modles !rovide. 7or the remainder of this ttorial we will assme that the
import numpy as np
has been sed.
Arrays
The central featre of #m\$% is the
array
ob=ect class. &rra%s are similar to lists in \$%thon e,ce!t that ever% element of an arra% mst be of the same t%!e t%!icall% a nmeric t%!e lie
float
or
int
. &rra%s mae o!erations with lar"e amonts of nmeric data ver% fast and are "enerall% mch 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'>
<ere the fnction
array
taes two ar"ments: the list to be converted into the arra% and the t%!e of each member of the list. &rra% elements are accessed sliced and mani!lated =st lie lists:
>>> a[:2] array([ 1., 4.]) >>> a[3] 8.0 >>> a[0] = 5. >>> a array([ 5., 4., 5., 8.])
&rra%s can be mltidimensional. ?nlie lists different a,es are accessed sin" commas inside bracet notation. <ere is an e,am!le with a twodimensional arra% e.". 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