Вы находитесь на странице: 1из 29

15/9/2015

Tentative NumPy Tutorial -

TentativeNumPyTutorial

Pleasedonothesitatetoclicktheeditbutton.YouwillneedtocreateaUserAccount
first.
Sommaire
1. Prerequisites
2. TheBasics
1. Anexample
2. ArrayCreation
3. PrintingArrays
4. BasicOperations
5. UniversalFunctions
6. Indexing,SlicingandIterating
3. ShapeManipulation
1. Changingtheshapeofanarray
2. Stackingtogetherdifferentarrays
3. Splittingonearrayintoseveralsmallerones
4. CopiesandViews
1. NoCopyatAll
2. VieworShallowCopy
3. DeepCopy
4. FunctionsandMethodsOverview
5. LessBasic
1. Broadcastingrules
6. Fancyindexingandindextricks
1. IndexingwithArraysofIndices
2. IndexingwithBooleanArrays
3. Theix_()function
4. Indexingwithstrings
7. LinearAlgebra
1. SimpleArrayOperations
2. TheMatrixClass
3. Indexing:ComparingMatricesand2DArrays
http://wiki.scipy.org/Tentative_NumPy_Tutorial

1/29

15/9/2015

Tentative NumPy Tutorial -

8. TricksandTips
1. "Automatic"Reshaping
2. VectorStacking
3. Histograms
9. References

Prerequisites
BeforereadingthistutorialyoushouldknowabitofPython.Ifyouwouldliketo
refreshyourmemory,takealookatthePythontutorial.
Ifyouwishtoworktheexamplesinthistutorial,youmustalsohavesomesoftware
installedonyourcomputer.Minimally:
Python
NumPy
Theseyoumayfinduseful:
ipythonisanenhancedinteractivePythonshellwhichisveryconvenientfor
exploringNumPy'sfeatures
matplotlibwillenableyoutoplotgraphics
SciPyprovidesalotofscientificroutinesthatworkontopofNumPy

TheBasics
NumPy'smainobjectisthehomogeneousmultidimensionalarray.Itisatableof
elements(usuallynumbers),allofthesametype,indexedbyatupleofpositive
integers.InNumpydimensionsarecalledaxes.Thenumberofaxesisrank.
Forexample,thecoordinatesofapointin3Dspace[1,2,1]isanarrayofrank1,
becauseithasoneaxis.Thataxishasalengthof3.Inexamplepicturedbelow,the
arrayhasrank2(itis2dimensional).Thefirstdimension(axis)hasalengthof2,the
seconddimensionhasalengthof3.
[[1.,0.,0.],
[0.,1.,2.]]

Numpy'sarrayclassiscalledndarray.Itisalsoknownbythealiasarray.Notethat
numpy.arrayisnotthesameastheStandardPythonLibraryclassarray.array,which
onlyhandlesonedimensionalarraysandofferslessfunctionality.Themoreimportant
attributesofanndarrayobjectare:
ndarray.ndim
thenumberofaxes(dimensions)ofthearray.InthePythonworld,thenumber
ofdimensionsisreferredtoasrank.
ndarray.shape
thedimensionsofthearray.Thisisatupleofintegersindicatingthesizeofthe
arrayineachdimension.Foramatrixwithnrowsandmcolumns,shapewillbe
(n,m).Thelengthoftheshapetupleisthereforetherank,ornumberof
dimensions,ndim.
http://wiki.scipy.org/Tentative_NumPy_Tutorial

2/29

15/9/2015

Tentative NumPy Tutorial -

ndarray.size
thetotalnumberofelementsofthearray.Thisisequaltotheproductofthe
elementsofshape.
ndarray.dtype
anobjectdescribingthetypeoftheelementsinthearray.Onecancreateor
specifydtype'susingstandardPythontypes.AdditionallyNumPyprovidestypes
ofitsown.numpy.int32,numpy.int16,andnumpy.float64aresomeexamples.
ndarray.itemsize
thesizeinbytesofeachelementofthearray.Forexample,anarrayofelements
oftypefloat64hasitemsize8(=64/8),whileoneoftypecomplex32has
itemsize4(=32/8).Itisequivalenttondarray.dtype.itemsize.
ndarray.data
thebuffercontainingtheactualelementsofthearray.Normally,wewon'tneed
tousethisattributebecausewewillaccesstheelementsinanarrayusing
indexingfacilities.

Anexample
>>>fromnumpyimport*
>>>a=arange(15).reshape(3,5)
>>>a
array([[0,1,2,3,4],
[5,6,7,8,9],
[10,11,12,13,14]])
>>>a.shape
(3,5)
>>>a.ndim
2
>>>a.dtype.name
'int32'
>>>a.itemsize
4
>>>a.size
15
>>>type(a)
numpy.ndarray
>>>b=array([6,7,8])
>>>b
array([6,7,8])
>>>type(b)
numpy.ndarray

ArrayCreation
Thereareseveralwaystocreatearrays.
Forexample,youcancreateanarrayfromaregularPythonlistortupleusingthe
arrayfunction.Thetypeoftheresultingarrayisdeducedfromthetypeofthe
elementsinthesequences.
http://wiki.scipy.org/Tentative_NumPy_Tutorial

3/29

15/9/2015

Tentative NumPy Tutorial -

>>>fromnumpyimport*
>>>a=array([2,3,4])
>>>a
array([2,3,4])
>>>a.dtype
dtype('int32')
>>>b=array([1.2,3.5,5.1])
>>>b.dtype
dtype('float64')

Afrequenterrorconsistsincallingarraywithmultiplenumericarguments,ratherthan
providingasinglelistofnumbersasanargument.
>>>a=array(1,2,3,4)#WRONG
>>>a=array([1,2,3,4])#RIGHT
arraytransformssequencesofsequencesintotwodimensionalarrays,sequencesof

sequencesofsequencesintothreedimensionalarrays,andsoon.
>>>b=array([(1.5,2,3),(4,5,6)])
>>>b
array([[1.5,2.,3.],
[4.,5.,6.]])

Thetypeofthearraycanalsobeexplicitlyspecifiedatcreationtime:
>>>c=array([[1,2],[3,4]],dtype=complex)
>>>c
array([[1.+0.j,2.+0.j],
[3.+0.j,4.+0.j]])

Often,theelementsofanarrayareoriginallyunknown,butitssizeisknown.Hence,
NumPyoffersseveralfunctionstocreatearrayswithinitialplaceholdercontent.These
minimizethenecessityofgrowingarrays,anexpensiveoperation.
Thefunctionzeroscreatesanarrayfullofzeros,thefunctiononescreatesanarray
fullofones,andthefunctionemptycreatesanarraywhoseinitialcontentisrandom
anddependsonthestateofthememory.Bydefault,thedtypeofthecreatedarrayis
float64.
>>>zeros((3,4))
array([[0.,0.,0.,0.],
[0.,0.,0.,0.],
[0.,0.,0.,0.]])
>>>ones((2,3,4),dtype=int16)#dtypecanalso
bespecified
array([[[1,1,1,1],
[1,1,1,1],
[1,1,1,1]],
http://wiki.scipy.org/Tentative_NumPy_Tutorial

4/29

15/9/2015

Tentative NumPy Tutorial -

[[1,1,1,1],
[1,1,1,1],
[1,1,1,1]]],dtype=int16)
>>>empty((2,3))
array([[3.73603959e262,6.02658058e154,6.55490914e260],
[5.30498948e313,3.14673309e307,1.00000000e+000]])

Tocreatesequencesofnumbers,NumPyprovidesafunctionanalogoustorangethat
returnsarraysinsteadoflists
>>>arange(10,30,5)
array([10,15,20,25])
>>>arange(0,2,0.3)#itacceptsfloat
arguments
array([0.,0.3,0.6,0.9,1.2,1.5,1.8])

Whenarangeisusedwithfloatingpointarguments,itisgenerallynotpossibleto
predictthenumberofelementsobtained,duetothefinitefloatingpointprecision.For
thisreason,itisusuallybettertousethefunctionlinspacethatreceivesasan
argumentthenumberofelementsthatwewant,insteadofthestep:
>>>linspace(0,2,9)#9numbersfrom0to2
array([0.,0.25,0.5,0.75,1.,1.25,1.5,1.75,2.
])
>>>x=linspace(0,2*pi,100)#usefultoevaluate
functionatlotsofpoints
>>>f=sin(x)

Seealso
array,zeros,zeros_like,ones,ones_like,empty,empty_like,arange,linspace,
rand,randn,fromfunction,fromfile

PrintingArrays
Whenyouprintanarray,NumPydisplaysitinasimilarwaytonestedlists,butwith
thefollowinglayout:
thelastaxisisprintedfromlefttoright,
thesecondtolastisprintedfromtoptobottom,
therestarealsoprintedfromtoptobottom,witheachsliceseparatedfromthe
nextbyanemptyline.
Onedimensionalarraysarethenprintedasrows,bidimensionalsasmatricesand
tridimensionalsaslistsofmatrices.
>>>a=arange(6)#1darray
>>>printa
[012345]
>>>
http://wiki.scipy.org/Tentative_NumPy_Tutorial

5/29

15/9/2015

Tentative NumPy Tutorial -

>>>b=arange(12).reshape(4,3)#2darray
>>>printb
[[012]
[345]
[678]
[91011]]
>>>
>>>c=arange(24).reshape(2,3,4)#3darray
>>>printc
[[[0123]
[4567]
[891011]]
[[12131415]
[16171819]
[20212223]]]

Seebelowtogetmoredetailsonreshape.
Ifanarrayistoolargetobeprinted,NumPyautomaticallyskipsthecentralpartofthe
arrayandonlyprintsthecorners:
>>>printarange(10000)
[012...,999799989999]
>>>
>>>printarange(10000).reshape(100,100)
[[012...,979899]
[100101102...,197198199]
[200201202...,297298299]
...,
[970097019702...,979797989799]
[980098019802...,989798989899]
[990099019902...,999799989999]]

TodisablethisbehaviourandforceNumPytoprinttheentirearray,youcanchange
theprintingoptionsusingset_printoptions.
>>>set_printoptions(threshold='nan')

BasicOperations
Arithmeticoperatorsonarraysapplyelementwise.Anewarrayiscreatedandfilled
withtheresult.
>>>a=array([20,30,40,50])
>>>b=arange(4)
>>>b
array([0,1,2,3])
>>>c=ab
>>>c
http://wiki.scipy.org/Tentative_NumPy_Tutorial

6/29

15/9/2015

Tentative NumPy Tutorial -

array([20,29,38,47])
>>>b**2
array([0,1,4,9])
>>>10*sin(a)
array([9.12945251,9.88031624,7.4511316,2.62374854])
>>>a<35
array([True,True,False,False],dtype=bool)

Unlikeinmanymatrixlanguages,theproductoperator*operateselementwisein
NumPyarrays.Thematrixproductcanbeperformedusingthedotfunctionorcreating
matrixobjects(seematrixsectionofthistutorial).
>>>A=array([[1,1],
...[0,1]])
>>>B=array([[2,0],
...[3,4]])
>>>A*B#elementwiseproduct
array([[2,0],
[0,4]])
>>>dot(A,B)#matrixproduct
array([[5,4],
[3,4]])

Someoperations,suchas+=and*=,actinplacetomodifyanexistingarrayratherthan
createanewone.
>>>a=ones((2,3),dtype=int)
>>>b=random.random((2,3))
>>>a*=3
>>>a
array([[3,3,3],
[3,3,3]])
>>>b+=a
>>>b
array([[3.69092703,3.8324276,3.0114541],
[3.18679111,3.3039349,3.37600289]])
>>>a+=b#bisconvertedto
integertype
>>>a
array([[6,6,6],
[6,6,6]])

Whenoperatingwitharraysofdifferenttypes,thetypeoftheresultingarray
correspondstothemoregeneralorpreciseone(abehaviorknownasupcasting).
>>>a=ones(3,dtype=int32)
>>>b=linspace(0,pi,3)
>>>b.dtype.name
'float64'
>>>c=a+b
http://wiki.scipy.org/Tentative_NumPy_Tutorial

7/29

15/9/2015

Tentative NumPy Tutorial -

>>>c
array([1.,2.57079633,4.14159265])
>>>c.dtype.name
'float64'
>>>d=exp(c*1j)
>>>d
array([0.54030231+0.84147098j,0.84147098+0.54030231j,
0.540302310.84147098j])
>>>d.dtype.name
'complex128'

Manyunaryoperations,suchascomputingthesumofalltheelementsinthearray,are
implementedasmethodsofthendarrayclass.
>>>a=random.random((2,3))
>>>a
array([[0.6903007,0.39168346,0.16524769],
[0.48819875,0.77188505,0.94792155]])
>>>a.sum()
3.4552372100521485
>>>a.min()
0.16524768654743593
>>>a.max()
0.9479215542670073

Bydefault,theseoperationsapplytothearrayasthoughitwerealistofnumbers,
regardlessofitsshape.However,byspecifyingtheaxisparameteryoucanapplyan
operationalongthespecifiedaxisofanarray:
>>>b=arange(12).reshape(3,4)
>>>b
array([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]])
>>>
>>>b.sum(axis=0)#sumofeachcolumn
array([12,15,18,21])
>>>
>>>b.min(axis=1)#minofeachrow
array([0,4,8])
>>>
>>>b.cumsum(axis=1)#cumulativesum
alongeachrow
array([[0,1,3,6],
[4,9,15,22],
[8,17,27,38]])

UniversalFunctions
http://wiki.scipy.org/Tentative_NumPy_Tutorial

8/29

15/9/2015

Tentative NumPy Tutorial -

NumPyprovidesfamiliarmathematicalfunctionssuchassin,cos,andexp.InNumPy,
thesearecalled"universalfunctions"(ufunc).WithinNumPy,thesefunctionsoperate
elementwiseonanarray,producinganarrayasoutput.
>>>B=arange(3)
>>>B
array([0,1,2])
>>>exp(B)
array([1.,2.71828183,7.3890561])
>>>sqrt(B)
array([0.,1.,1.41421356])
>>>C=array([2.,1.,4.])
>>>add(B,C)
array([2.,0.,6.])

Seealso
all,alltrue,any,applyalongaxis,argmax,argmin,argsort,average,bincount,
ceil,clip,conj,conjugate,corrcoef,cov,cross,cumprod,cumsum,diff,dot,
floor,inner,inv,lexsort,max,maximum,mean,median,min,minimum,
nonzero,outer,prod,re,round,sometrue,sort,std,sum,trace,transpose,var,
vdot,vectorize,where

Indexing,SlicingandIterating
Onedimensionalarrayscanbeindexed,slicedanditeratedover,muchlikelistsand
otherPythonsequences.
>>>a=arange(10)**3
>>>a
array([0,1,8,27,64,125,216,343,512,729])
>>>a[2]
8
>>>a[2:5]
array([8,27,64])
>>>a[:6:2]=1000#equivalenttoa[0:6:2]=1000from
starttoposition6,exclusive,setevery2ndelementto1000
>>>a
array([1000,1,1000,27,1000,125,216,343,
512,729])
>>>a[::1]#reverseda
array([729,512,343,216,125,1000,27,1000,
1,1000])
>>>foriina:
...printi**(1/3.),
...
nan1.0nan3.0nan5.06.07.08.09.0

Multidimensionalarrayscanhaveoneindexperaxis.Theseindicesaregivenina
tupleseparatedbycommas:
http://wiki.scipy.org/Tentative_NumPy_Tutorial

9/29

15/9/2015

Tentative NumPy Tutorial -

>>>deff(x,y):
...return10*x+y
...
>>>b=fromfunction(f,(5,4),dtype=int)
>>>b
array([[0,1,2,3],
[10,11,12,13],
[20,21,22,23],
[30,31,32,33],
[40,41,42,43]])
>>>b[2,3]
23
>>>b[0:5,1]#eachrowinthesecond
columnofb
array([1,11,21,31,41])
>>>b[:,1]#equivalenttotheprevious
example
array([1,11,21,31,41])
>>>b[1:3,:]#eachcolumninthesecond
andthirdrowofb
array([[10,11,12,13],
[20,21,22,23]])

Whenfewerindicesareprovidedthanthenumberofaxes,themissingindicesare
consideredcompleteslices:
>>>b[1]#thelastrow.
Equivalenttob[1,:]
array([40,41,42,43])

Theexpressionwithinbracketsinb[i]istreatedasanifollowedbyasmany
instancesof:asneededtorepresenttheremainingaxes.NumPyalsoallowsyouto
writethisusingdotsasb[i,...].
Thedots(...)representasmanycolonsasneededtoproduceacompleteindexing
tuple.Forexample,ifxisarank5array(i.e.,ithas5axes),then
x[1,2,...]isequivalenttox[1,2,:,:,:],
x[...,3]tox[:,:,:,:,3]and
x[4,...,5,:]tox[4,:,:,5,:].

>>>c=array([[[0,1,2],#a3Darray(two
stacked2Darrays)
...[10,12,13]],
...
...[[100,101,102],
...[110,112,113]]])
>>>c.shape
(2,2,3)
http://wiki.scipy.org/Tentative_NumPy_Tutorial

10/29

15/9/2015

Tentative NumPy Tutorial -

>>>c[1,...]#sameasc[1,:,:]
orc[1]
array([[100,101,102],
[110,112,113]])
>>>c[...,2]#sameasc[:,:,2]
array([[2,13],
[102,113]])

Iteratingovermultidimensionalarraysisdonewithrespecttothefirstaxis:
>>>forrowinb:
...printrow
...
[0123]
[10111213]
[20212223]
[30313233]
[40414243]

However,ifonewantstoperformanoperationoneachelementinthearray,onecan
usetheflatattributewhichisaniteratoroveralltheelementsofthearray:
>>>forelementinb.flat:
...printelement,
...
012310111213202122233031323340414243

Seealso
[],...,newaxis,ndenumerate,indices,indexexp

ShapeManipulation
Changingtheshapeofanarray
Anarrayhasashapegivenbythenumberofelementsalongeachaxis:
>>>a=floor(10*random.random((3,4)))
>>>a
array([[7.,5.,9.,3.],
[7.,2.,7.,8.],
[6.,8.,3.,2.]])
>>>a.shape
(3,4)

Theshapeofanarraycanbechangedwithvariouscommands:
>>>a.ravel()#flattenthearray
array([7.,5.,9.,3.,7.,2.,7.,8.,6.,8.,3.,
http://wiki.scipy.org/Tentative_NumPy_Tutorial

11/29

15/9/2015

Tentative NumPy Tutorial -

2.])
>>>a.shape=(6,2)
>>>a.transpose()
array([[7.,9.,7.,7.,6.,3.],
[5.,3.,2.,8.,8.,2.]])

Theorderoftheelementsinthearrayresultingfromravel()isnormally"Cstyle",that
is,therightmostindex"changesthefastest",sotheelementaftera[0,0]isa[0,1].Ifthe
arrayisreshapedtosomeothershape,againthearrayistreatedas"Cstyle".Numpy
normallycreatesarraysstoredinthisorder,soravel()willusuallynotneedtocopyits
argument,butifthearraywasmadebytakingslicesofanotherarrayorcreatedwith
unusualoptions,itmayneedtobecopied.Thefunctionsravel()andreshape()canalso
beinstructed,usinganoptionalargument,touseFORTRANstylearrays,inwhichthe
leftmostindexchangesthefastest.
Thereshapefunctionreturnsitsargumentwithamodifiedshape,whereastheresize
methodmodifiesthearrayitself:
>>>a
array([[7.,5.],
[9.,3.],
[7.,2.],
[7.,8.],
[6.,8.],
[3.,2.]])
>>>a.resize((2,6))
>>>a
array([[7.,5.,9.,3.,7.,2.],
[7.,8.,6.,8.,3.,2.]])

Ifadimensionisgivenas1inareshapingoperation,theotherdimensionsare
automaticallycalculated:
>>>a.reshape(3,1)
array([[7.,5.,9.,3.],
[7.,2.,7.,8.],
[6.,8.,3.,2.]])

Seealso::shapeexample,reshapeexample,resizeexample,ravelexample

Stackingtogetherdifferentarrays
Severalarrayscanbestackedtogetheralongdifferentaxes:
>>>a=floor(10*random.random((2,2)))
>>>a
array([[1.,1.],
[5.,8.]])
>>>b=floor(10*random.random((2,2)))
>>>b
array([[3.,3.],
http://wiki.scipy.org/Tentative_NumPy_Tutorial

12/29

15/9/2015

Tentative NumPy Tutorial -

[6.,0.]])
>>>vstack((a,b))
array([[1.,1.],
[5.,8.],
[3.,3.],
[6.,0.]])
>>>hstack((a,b))
array([[1.,1.,3.,3.],
[5.,8.,6.,0.]])

Thefunctioncolumn_stackstacks1Darraysascolumnsintoa2Darray.Itis
equivalenttovstackonlyfor1Darrays:
>>>column_stack((a,b))#With2Darrays
array([[1.,1.,3.,3.],
[5.,8.,6.,0.]])
>>>a=array([4.,2.])
>>>b=array([2.,8.])
>>>a[:,newaxis]#Thisallowstohavea2Dcolumnsvector
array([[4.],
[2.]])
>>>column_stack((a[:,newaxis],b[:,newaxis]))
array([[4.,2.],
[2.,8.]])
>>>vstack((a[:,newaxis],b[:,newaxis]))#Thebehaviorofvstack
isdifferent
array([[4.],
[2.],
[2.],
[8.]])

Thefunctionrow_stack,ontheotherhand,stacks1Darraysasrowsintoa2Darray.
Forarraysofwithmorethantwodimensions,hstackstacksalongtheirsecondaxes,
vstackstacksalongtheirfirstaxes,andconcatenateallowsforanoptionalarguments
givingthenumberoftheaxisalongwhichtheconcatenationshouldhappen.
Note
Incomplexcases,r_[]andc_[]areusefulforcreatingarraysbystackingnumbers
alongoneaxis.Theyallowtheuseofrangeliterals(":"):
>>>r_[1:4,0,4]
array([1,2,3,0,4])

Whenusedwitharraysasarguments,r_[]andc_[]aresimilartovstackandhstackin
theirdefaultbehavior,butallowforanoptionalargumentgivingthenumberofthe
axisalongwhichtoconcatenate.
Seealso:hstackexample,vstackexammple,column_stackexample,row_stack
example,concatenateexample,c_example,r_example

http://wiki.scipy.org/Tentative_NumPy_Tutorial

13/29

15/9/2015

Tentative NumPy Tutorial -

Splittingonearrayintoseveralsmallerones
Usinghsplit,youcansplitanarrayalongitshorizontalaxis,eitherbyspecifyingthe
numberofequallyshapedarraystoreturn,orbyspecifyingthecolumnsafterwhich
thedivisionshouldoccur:
>>>a=floor(10*random.random((2,12)))
>>>a
array([[8.,8.,3.,9.,0.,4.,3.,0.,0.,6.,4.,
4.],
[0.,3.,2.,9.,6.,0.,4.,5.,7.,5.,1.,
4.]])
>>>hsplit(a,3)#Splitainto3
[array([[8.,8.,3.,9.],
[0.,3.,2.,9.]]),array([[0.,4.,3.,0.],
[6.,0.,4.,5.]]),array([[0.,6.,4.,4.],
[7.,5.,1.,4.]])]
>>>hsplit(a,(3,4))#Splitaafterthethirdandthefourth
column
[array([[8.,8.,3.],
[0.,3.,2.]]),array([[9.],
[9.]]),array([[0.,4.,3.,0.,0.,6.,4.,4.],
[6.,0.,4.,5.,7.,5.,1.,4.]])]

vsplitsplitsalongtheverticalaxis,andarraysplitallowsonetospecifyalongwhich
axistosplit.

CopiesandViews
Whenoperatingandmanipulatingarrays,theirdataissometimescopiedintoanew
arrayandsometimesnot.Thisisoftenasourceofconfusionforbeginners.Thereare
threecases:

NoCopyatAll
Simpleassignmentsmakenocopyofarrayobjectsoroftheirdata.
>>>a=arange(12)
>>>b=a#nonewobjectiscreated
>>>bisa#aandbaretwonamesforthesamendarray
object
True
>>>b.shape=3,4#changestheshapeofa
>>>a.shape
(3,4)

Pythonpassesmutableobjectsasreferences,sofunctioncallsmakenocopy.
>>>deff(x):
...printid(x)
http://wiki.scipy.org/Tentative_NumPy_Tutorial

14/29

15/9/2015

Tentative NumPy Tutorial -

...
>>>id(a)#idisauniqueidentifierof
anobject
148293216
>>>f(a)
148293216

VieworShallowCopy
Differentarrayobjectscansharethesamedata.Theviewmethodcreatesanewarray
objectthatlooksatthesamedata.
>>>c=a.view()
>>>cisa
False
>>>c.baseisa#cisaviewofthedata
ownedbya
True
>>>c.flags.owndata
False
>>>
>>>c.shape=2,6#a'sshapedoesn'tchange
>>>a.shape
(3,4)
>>>c[0,4]=1234#a'sdatachanges
>>>a
array([[0,1,2,3],
[1234,5,6,7],
[8,9,10,11]])

Slicinganarrayreturnsaviewofit:
>>>s=a[:,1:3]#spacesaddedforclaritycouldalsobe
written"s=a[:,1:3]"
>>>s[:]=10#s[:]isaviewofs.Notethedifference
betweens=10ands[:]=10
>>>a
array([[0,10,10,3],
[1234,10,10,7],
[8,10,10,11]])

DeepCopy
Thecopymethodmakesacompletecopyofthearrayanditsdata.
>>>d=a.copy()#anewarrayobject
withnewdataiscreated
>>>disa
False
http://wiki.scipy.org/Tentative_NumPy_Tutorial

15/29

15/9/2015

Tentative NumPy Tutorial -

>>>d.baseisa#ddoesn'tshare
anythingwitha
False
>>>d[0,0]=9999
>>>a
array([[0,10,10,3],
[1234,10,10,7],
[8,10,10,11]])

FunctionsandMethodsOverview
HereisalistofNumPyfunctionsandmethodsnamesorderedinsomecategories.The
nameslinktotheNumpyExampleListsothatyoucanseethefunctionsinaction.
ArrayCreation
arange,array,copy,empty,empty_like,eye,fromfile,fromfunction,identity,
linspace,logspace,mgrid,ogrid,ones,ones_like,r,zeros,zeros_like
Conversions
astype,atleast1d,atleast2d,atleast3d,mat
Manipulations
arraysplit,columnstack,concatenate,diagonal,dsplit,dstack,hsplit,hstack,
item,newaxis,ravel,repeat,reshape,resize,squeeze,swapaxes,take,transpose,
vsplit,vstack
Questions
all,any,nonzero,where
Ordering
argmax,argmin,argsort,max,min,ptp,searchsorted,sort
Operations
choose,compress,cumprod,cumsum,inner,fill,imag,prod,put,putmask,real,
sum
BasicStatistics
cov,mean,std,var
BasicLinearAlgebra
cross,dot,outer,svd,vdot

LessBasic
Broadcastingrules
Broadcastingallowsuniversalfunctionstodealinameaningfulwaywithinputsthat
donothaveexactlythesameshape.
Thefirstruleofbroadcastingisthatifallinputarraysdonothavethesamenumberof
dimensions,a"1"willberepeatedlyprependedtotheshapesofthesmallerarraysuntil
allthearrayshavethesamenumberofdimensions.
Thesecondruleofbroadcastingensuresthatarrayswithasizeof1alongaparticular
dimensionactasiftheyhadthesizeofthearraywiththelargestshapealongthat
dimension.Thevalueofthearrayelementisassumedtobethesamealongthat
dimensionforthe"broadcast"array.
http://wiki.scipy.org/Tentative_NumPy_Tutorial

16/29

15/9/2015

Tentative NumPy Tutorial -

Afterapplicationofthebroadcastingrules,thesizesofallarraysmustmatch.More
detailscanbefoundinthisdocumentation.

Fancyindexingandindextricks
NumPyoffersmoreindexingfacilitiesthanregularPythonsequences.Inadditionto
indexingbyintegersandslices,aswesawbefore,arrayscanbeindexedbyarraysof
integersandarraysofbooleans.

IndexingwithArraysofIndices
>>>a=arange(12)**2#thefirst12
squarenumbers
>>>i=array([1,1,3,8,5])#anarrayof
indices
>>>a[i]#theelementsofa
atthepositionsi
array([1,1,9,64,25])
>>>
>>>j=array([[3,4],[9,7]])#abidimensional
arrayofindices
>>>a[j]#thesameshapeas
j
array([[9,16],
[81,49]])

Whentheindexedarrayaismultidimensional,asinglearrayofindicesreferstothe
firstdimensionofa.Thefollowingexampleshowsthisbehaviorbyconvertingan
imageoflabelsintoacolorimageusingapalette.
>>>palette=array([[0,0,0],#black
...[255,0,0],#red
...[0,255,0],#green
...[0,0,255],#blue
...[255,255,255]])#white
>>>image=array([[0,1,2,0],#eachvalue
correspondstoacolorinthepalette
...[0,3,4,0]])
>>>palette[image]#the(2,4,3)color
image
array([[[0,0,0],
[255,0,0],
[0,255,0],
[0,0,0]],
[[0,0,0],
[0,0,255],
[255,255,255],
[0,0,0]]])

http://wiki.scipy.org/Tentative_NumPy_Tutorial

17/29

15/9/2015

Tentative NumPy Tutorial -

Wecanalsogiveindexesformorethanonedimension.Thearraysofindicesforeach
dimensionmusthavethesameshape.
>>>a=arange(12).reshape(3,4)
>>>a
array([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]])
>>>i=array([[0,1],#indicesforthe
firstdimofa
...[1,2]])
>>>j=array([[2,1],#indicesforthe
seconddim
...[3,3]])
>>>
>>>a[i,j]#iandjmusthave
equalshape
array([[2,5],
[7,11]])
>>>
>>>a[i,2]
array([[2,6],
[6,10]])
>>>
>>>a[:,j]#i.e.,a[:,j]
array([[[2,1],
[3,3]],
[[6,5],
[7,7]],
[[10,9],
[11,11]]])

Naturally,wecanputiandjinasequence(sayalist)andthendotheindexingwith
thelist.
>>>l=[i,j]
>>>a[l]#equivalentto
a[i,j]
array([[2,5],
[7,11]])

However,wecannotdothisbyputtingiandjintoanarray,becausethisarraywillbe
interpretedasindexingthefirstdimensionofa.
>>>s=array([i,j])
>>>a[s]#notwhatwewant
Traceback(mostrecentcalllast):
File"<stdin>",line1,in?
IndexError:index(3)outofrange(0<=index<=2)indimension0
>>>
http://wiki.scipy.org/Tentative_NumPy_Tutorial

18/29

15/9/2015

Tentative NumPy Tutorial -

>>>a[tuple(s)]#sameasa[i,j]
array([[2,5],
[7,11]])

Anothercommonuseofindexingwitharraysisthesearchofthemaximumvalueof
timedependentseries:
>>>time=linspace(20,145,5)#timescale
>>>data=sin(arange(20)).reshape(5,4)#4timedependent
series
>>>time
array([20.,51.25,82.5,113.75,145.])
>>>data
array([[0.,0.84147098,0.90929743,0.14112001],
[0.7568025,0.95892427,0.2794155,0.6569866],
[0.98935825,0.41211849,0.54402111,0.99999021],
[0.53657292,0.42016704,0.99060736,0.65028784],
[0.28790332,0.96139749,0.75098725,0.14987721]])
>>>
>>>ind=data.argmax(axis=0)#indexofthe
maximaforeachseries
>>>ind
array([2,0,3,1])
>>>
>>>time_max=time[ind]#times
correspondingtothemaxima
>>>
>>>data_max=data[ind,xrange(data.shape[1])]#=>
data[ind[0],0],data[ind[1],1]...
>>>
>>>time_max
array([82.5,20.,113.75,51.25])
>>>data_max
array([0.98935825,0.84147098,0.99060736,0.6569866])
>>>
>>>all(data_max==data.max(axis=0))
True

Youcanalsouseindexingwitharraysasatargettoassignto:
>>>a=arange(5)
>>>a
array([0,1,2,3,4])
>>>a[[1,3,4]]=0
>>>a
array([0,0,2,0,0])

However,whenthelistofindicescontainsrepetitions,theassignmentisdoneseveral
times,leavingbehindthelastvalue:
http://wiki.scipy.org/Tentative_NumPy_Tutorial

19/29

15/9/2015

Tentative NumPy Tutorial -

>>>a=arange(5)
>>>a[[0,0,2]]=[1,2,3]
>>>a
array([2,1,3,3,4])

Thisisreasonableenough,butwatchoutifyouwanttousePython's+=construct,asit
maynotdowhatyouexpect:
>>>a=arange(5)
>>>a[[0,0,2]]+=1
>>>a
array([1,1,3,3,4])

Eventhough0occurstwiceinthelistofindices,the0thelementisonlyincremented
once.ThisisbecausePythonrequires"a+=1"tobeequivalentto"a=a+1".

IndexingwithBooleanArrays
Whenweindexarrayswitharraysof(integer)indicesweareprovidingthelistof
indicestopick.Withbooleanindicestheapproachisdifferentweexplicitlychoose
whichitemsinthearraywewantandwhichoneswedon't.
Themostnaturalwayonecanthinkofforbooleanindexingistousebooleanarrays
thathavethesameshapeastheoriginalarray:
>>>a=arange(12).reshape(3,4)
>>>b=a>4
>>>b#bisaboolean
witha'sshape
array([[False,False,False,False],
[False,True,True,True],
[True,True,True,True]],dtype=bool)
>>>a[b]#1darraywiththe
selectedelements
array([5,6,7,8,9,10,11])

Thispropertycanbeveryusefulinassignments:
>>>a[b]=0#Allelementsof
'a'higherthan4become0
>>>a
array([[0,1,2,3],
[4,0,0,0],
[0,0,0,0]])

YoucanlookattheMandelbrotsetexampletoseehowtousebooleanindexingto
generateanimageoftheMandelbrotset.
Thesecondwayofindexingwithbooleansismoresimilartointegerindexingfor
eachdimensionofthearraywegivea1Dbooleanarrayselectingthesliceswewant.
http://wiki.scipy.org/Tentative_NumPy_Tutorial

20/29

15/9/2015

Tentative NumPy Tutorial -

>>>a=arange(12).reshape(3,4)
>>>b1=array([False,True,True])#firstdim
selection
>>>b2=array([True,False,True,False])#seconddim
selection
>>>
>>>a[b1,:]#selectingrows
array([[4,5,6,7],
[8,9,10,11]])
>>>
>>>a[b1]#samething
array([[4,5,6,7],
[8,9,10,11]])
>>>
>>>a[:,b2]#selectingcolumns
array([[0,2],
[4,6],
[8,10]])
>>>
>>>a[b1,b2]#aweirdthingto
do
array([4,10])

Notethatthelengthofthe1Dbooleanarraymustcoincidewiththelengthofthe
dimension(oraxis)youwanttoslice.Inthepreviousexample,b1isa1rankarray
withlength3(thenumberofrowsina),andb2(oflength4)issuitabletoindexthe
2ndrank(columns)ofa.

Theix_()function
Theix_functioncanbeusedtocombinedifferentvectorssoastoobtaintheresultfor
eachnuplet.Forexample,ifyouwanttocomputeallthea+b*cforallthetriplets
takenfromeachofthevectorsa,bandc:
>>>a=array([2,3,4,5])
>>>b=array([8,5,4])
>>>c=array([5,4,6,8,3])
>>>ax,bx,cx=ix_(a,b,c)
>>>ax
array([[[2]],
[[3]],
[[4]],
[[5]]])
>>>bx
array([[[8],
[5],
[4]]])
http://wiki.scipy.org/Tentative_NumPy_Tutorial

21/29

15/9/2015

Tentative NumPy Tutorial -

>>>cx
array([[[5,4,6,8,3]]])
>>>ax.shape,bx.shape,cx.shape
((4,1,1),(1,3,1),(1,1,5))
>>>result=ax+bx*cx
>>>result
array([[[42,34,50,66,26],
[27,22,32,42,17],
[22,18,26,34,14]],
[[43,35,51,67,27],
[28,23,33,43,18],
[23,19,27,35,15]],
[[44,36,52,68,28],
[29,24,34,44,19],
[24,20,28,36,16]],
[[45,37,53,69,29],
[30,25,35,45,20],
[25,21,29,37,17]]])
>>>result[3,2,4]
17
>>>a[3]+b[2]*c[4]
17

Youcouldalsoimplementthereduceasfollows:
defufunc_reduce(ufct,*vectors):
vs=ix_(*vectors)
r=ufct.identity
forvinvs:
r=ufct(r,v)
returnr

andthenuseitas:
>>>ufunc_reduce(add,a,b,c)
array([[[15,14,16,18,13],
[12,11,13,15,10],
[11,10,12,14,9]],
[[16,15,17,19,14],
[13,12,14,16,11],
[12,11,13,15,10]],
[[17,16,18,20,15],
[14,13,15,17,12],
[13,12,14,16,11]],
[[18,17,19,21,16],
[15,14,16,18,13],
[14,13,15,17,12]]])

Theadvantageofthisversionofreducecomparedtothenormalufunc.reduceisthatit
makesuseoftheBroadcastingRulesinordertoavoidcreatinganargumentarraythe
http://wiki.scipy.org/Tentative_NumPy_Tutorial

22/29

15/9/2015

Tentative NumPy Tutorial -

sizeoftheoutputtimesthenumberofvectors.

Indexingwithstrings
SeeRecordArrays.

LinearAlgebra
Workinprogress.Basiclinearalgebratobeincludedhere.

SimpleArrayOperations
Seelinalg.pyinnumpyfolderformore.
>>>fromnumpyimport*
>>>fromnumpy.linalgimport*
>>>a=array([[1.0,2.0],[3.0,4.0]])
>>>printa
[[1.2.]
[3.4.]]
>>>a.transpose()
array([[1.,3.],
[2.,4.]])
>>>inv(a)
array([[2.,1.],
[1.5,0.5]])
>>>u=eye(2)#unit2x2matrix"eye"represents"I"
>>>u
array([[1.,0.],
[0.,1.]])
>>>j=array([[0.0,1.0],[1.0,0.0]])
>>>dot(j,j)#matrixproduct
array([[1.,0.],
[0.,1.]])
>>>trace(u)#trace
2.0
>>>y=array([[5.],[7.]])
>>>solve(a,y)
array([[3.],
[4.]])
>>>eig(j)
(array([0.+1.j,0.1.j]),
http://wiki.scipy.org/Tentative_NumPy_Tutorial

23/29

15/9/2015

Tentative NumPy Tutorial -

array([[0.70710678+0.j,0.70710678+0.j],
[0.000000000.70710678j,0.00000000+0.70710678j]]))
Parameters:
squarematrix
Returns
Theeigenvalues,eachrepeatedaccordingtoitsmultiplicity.
Thenormalized(unit"length")eigenvectors,suchthatthe
column``v[:,i]``istheeigenvectorcorrespondingtothe
eigenvalue``w[i]``.

TheMatrixClass
HereisashortintrototheMatrixclass.
>>>A=matrix('1.02.03.04.0')
>>>A
[[1.2.]
[3.4.]]
>>>type(A)#filewhereclassisdefined
<class'numpy.matrixlib.defmatrix.matrix'>
>>>A.T#transpose
[[1.3.]
[2.4.]]
>>>X=matrix('5.07.0')
>>>Y=X.T
>>>Y
[[5.]
[7.]]
>>>printA*Y#matrixmultiplication
[[19.]
[43.]]
>>>printA.I#inverse
[[2.1.]
[1.50.5]]
>>>solve(A,Y)#solvinglinearequation
matrix([[3.],
[4.]])

Indexing:ComparingMatricesand2DArrays
NotethattherearesomeimportantdifferencesbetweenNumPyarraysandmatrices.
NumPyprovidestwofundamentalobjects:anNdimensionalarrayobjectanda
http://wiki.scipy.org/Tentative_NumPy_Tutorial

24/29

15/9/2015

Tentative NumPy Tutorial -

universalfunctionobject.Otherobjectsarebuiltontopofthese.Inparticular,matrices
are2dimensionalarrayobjectsthatinheritfromtheNumPyarrayobject.Forboth
arraysandmatrices,indicesmustconsistofapropercombinationofoneormoreofthe
following:integerscalars,ellipses,alistofintegersorbooleanvalues,atupleof
integersorbooleanvalues,anda1dimensionalarrayofintegerorbooleanvalues.A
matrixcanbeusedasanindexformatrices,butcommonlyanarray,list,orotherform
isneededtoaccomplishagiventask.
AsusualinPython,indexingiszerobased.Traditionallywerepresenta2Darrayor
matrixasarectangulararrayofrowsandcolumns,wheremovementalongaxis0is
movementacrossrows,whilemovementalongaxis1ismovementacrosscolumns.
Let'smakeanarrayandmatrixtoslice:
>>>A=arange(12)
>>>A
array([0,1,2,3,4,5,6,7,8,9,10,11])
>>>A.shape=(3,4)
>>>M=mat(A.copy())
>>>printtype(A),"",type(M)
<type'numpy.ndarray'><class'numpy.core.defmatrix.matrix'>
>>>printA
[[0123]
[4567]
[891011]]
>>>printM
[[0123]
[4567]
[891011]]

Now,let'stakesomesimpleslices.Basicslicingusessliceobjectsorintegers.For
example,theevaluationofA[:]andM[:]willappearfamiliarfromPythonindexing,
howeveritisimportanttonotethatslicingNumPyarraysdoes*not*makeacopyof
thedataslicingprovidesanewviewofthesamedata.
>>>printA[:]printA[:].shape
[[0123]
[4567]
[891011]]
(3,4)
>>>printM[:]printM[:].shape
[[0123]
[4567]
[891011]]
(3,4)

NowforsomethingthatdiffersfromPythonindexing:youmayusecommaseparated
indicestoindexalongmultipleaxesatthesametime.
>>>printA[:,1]printA[:,1].shape
[159]
(3,)
http://wiki.scipy.org/Tentative_NumPy_Tutorial

25/29

15/9/2015

Tentative NumPy Tutorial -

>>>printM[:,1]printM[:,1].shape
[[1]
[5]
[9]]
(3,1)

Noticethedifferenceinthelasttworesults.Useofasinglecolonforthe2Darray
producesa1dimensionalarray,whileforamatrixitproducesa2dimensionalmatrix.
Asliceofamatrixwillalwaysproduceamatrix.Forexample,asliceM[2,:]produces
amatrixofshape(1,4).Incontrast,asliceofanarraywillalwaysproduceanarrayof
thelowestpossibledimension.Forexample,ifCwerea3dimensionalarray,C[...,1]
producesa2DarraywhileC[1,:,1]producesa1dimensionalarray.Fromthispointon,
wewillshowresultsonlyforthearraysliceiftheresultsforthecorrespondingmatrix
sliceareidentical.
Letssaythatwewantedthe1stand3rdcolumnofanarray.Onewayistosliceusinga
list:
>>>A[:,[1,3]]
array([[1,3],
[5,7],
[9,11]])

Aslightlymorecomplicatedwayistousethetake()method:
>>>A[:,].take([1,3],axis=1)
array([[1,3],
[5,7],
[9,11]])

Ifwewantedtoskipthefirstrow,wecoulduse:
>>>A[1:,].take([1,3],axis=1)
array([[5,7],
[9,11]])

OrwecouldsimplyuseA[1:,[1,3]].Yetanotherwaytoslicetheaboveistouseacross
product:
>>>A[ix_((1,2),(1,3))]
array([[5,7],
[9,11]])

Forthereader'sconvenience,hereisourarrayagain:
>>>printA
[[0123]
[4567]
[891011]]

http://wiki.scipy.org/Tentative_NumPy_Tutorial

26/29

15/9/2015

Tentative NumPy Tutorial -

Nowlet'sdosomethingabitmorecomplicated.Letssaythatwewanttoretainall
columnswherethefirstrowisgreaterthan1.Onewayistocreateabooleanindex:
>>>A[0,:]>1
array([False,False,True,True],dtype=bool)
>>>A[:,A[0,:]>1]
array([[2,3],
[6,7],
[10,11]])

Justwhatwewanted!Butindexingthematrixisnotsoconvenient.
>>>M[0,:]>1
matrix([[False,False,True,True]],dtype=bool)
>>>M[:,M[0,:]>1]
matrix([[2,3]])

Theproblemofcourseisthatslicingthematrixsliceproducedamatrix.Butmatrices
haveaconvenient'A'attributewhosevalueisthearrayrepresentation,sowecanjust
dothisinstead:
>>>M[:,M.A[0,:]>1]
matrix([[2,3],
[6,7],
[10,11]])

Ifwewantedtoconditionallyslicethematrixintwodirections,wemustadjustour
strategyslightly.Insteadof
>>>A[A[:,0]>2,A[0,:]>1]
array([6,11])
>>>M[M.A[:,0]>2,M.A[0,:]>1]
matrix([[6,11]])

weneedtousethecrossproduct'ix_':
>>>A[numpy.ix_(A[:,0]>2,A[0,:]>1)]
array([[6,7],
[10,11]])
>>>M[numpy.ix_(M.A[:,0]>2,M.A[0,:]>1)]
matrix([[6,7],
[10,11]])

TricksandTips
Herewegivealistofshortandusefultips.

"Automatic"Reshaping
http://wiki.scipy.org/Tentative_NumPy_Tutorial

27/29

15/9/2015

Tentative NumPy Tutorial -

Tochangethedimensionsofanarray,youcanomitoneofthesizeswhichwillthenbe
deducedautomatically:
>>>a=arange(30)
>>>a.shape=2,1,3#1means"whateverisneeded"
>>>a.shape
(2,5,3)
>>>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]]])

VectorStacking
Howdoweconstructa2Darrayfromalistofequallysizedrowvectors?InMATLAB
thisisquiteeasy:ifxandyaretwovectorsofthesamelengthyouonlyneeddom=
[x;y].InNumPythisworksviathefunctionscolumn_stack,dstack,hstackand
vstack,dependingonthedimensioninwhichthestackingistobedone.Forexample:
x=arange(0,10,2)#x=([0,2,4,6,8])
y=arange(5)#y=([0,1,2,3,4])
m=vstack([x,y])#m=([[0,2,4,6,8],
#[0,1,2,3,4]])
xy=hstack([x,y])#xy=
([0,2,4,6,8,0,1,2,3,4])

Thelogicbehindthosefunctionsinmorethantwodimensionscanbestrange.
SeealsoNumPyforMatlabUsersandaddyournewfindingsthere.

Histograms
TheNumPyhistogramfunctionappliedtoanarrayreturnsapairofvectors:the
histogramofthearrayandthevectorofbins.Beware:matplotlibalsohasafunction
tobuildhistograms(calledhist,asinMatlab)thatdiffersfromtheoneinNumPy.
Themaindifferenceisthatpylab.histplotsthehistogramautomatically,while
numpy.histogramonlygeneratesthedata.
importnumpy
importpylab
#Buildavectorof10000normaldeviateswithvariance0.5^2and
mean2
mu,sigma=2,0.5
http://wiki.scipy.org/Tentative_NumPy_Tutorial

28/29

15/9/2015

Tentative NumPy Tutorial -

v=numpy.random.normal(mu,sigma,10000)
#Plotanormalizedhistogramwith50bins
pylab.hist(v,bins=50,normed=1)#matplotlibversion(plot)
pylab.show()
#Computethehistogramwithnumpyandthenplotit
(n,bins)=numpy.histogram(v,bins=50,normed=True)#NumPy
version(noplot)
pylab.plot(.5*(bins[1:]+bins[:1]),n)
pylab.show()

References
ThePythontutorial.
TheNumpyExampleList.
ThenonexistentNumPyTutorialatscipy.org,wherewecanfindtheold
Numericdocumentation.
TheGuidetoNumPybook.
TheSciPyTutorialandaSciPycourseonline
NumPyforMatlabUsers.
Amatlab,R,IDL,NumPy/SciPydictionary.
TentativeNumPyTutorial(dernireditionle2015022302:47:12parRalfGommers)

http://wiki.scipy.org/Tentative_NumPy_Tutorial

29/29

Вам также может понравиться