Sunteți pe pagina 1din 30

8/28/13

Tentative NumPy Tutorial -

Tentative NumPy Tutorial


Please do not hesitate to click the edit button. You will need to create a User Account first. Contents 1. Prerequisites 2. The Basics 1. An example 2. Array Creation 3. Printing Arrays 4. Basic Operations 5. Universal Functions 6. Indexing, Slicing and Iterating 3. Shape Manipulation 1. Changing the shape of an array 2. Stacking together different arrays 3. Splitting one array into several smaller ones 4. Copies and Views 1. No Copy at All 2. View or Shallow Copy 3. Deep Copy 4. Functions and Methods Overview 5. Less Basic 1. Broadcasting rules 6. Fancy indexing and index tricks 1. Indexing with Arrays of Indices 2. Indexing with Boolean Arrays 3. The ix_() function 4. Indexing with strings 7. Linear Algebra 1. Simple Array Operations 2. The Matrix Class 3. Indexing: Comparing Matrices and 2D Arrays 8. Tricks and Tips 1. "Automatic" Reshaping 2. Vector Stacking 3. Histograms 9. References
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 1/30

8/28/13

Tentative NumPy Tutorial -

Prerequisites
Before reading this tutorial you should know a bit of Python. If you would like to refresh your memory, take a look at the Python tutorial. If you wish to work the examples in this tutorial, you must also have some software installed on your computer. Minimally: Python NumPy These you may find useful: ipython is an enhanced interactive Python shell which is very convenient for exploring NumPy's features matplotlib will enable you to plot graphics SciPy provides a lot of scientific routines that work on top of NumPy

The Basics
NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy dimensions are called axes. The number of axes is rank. For example, the coordinates of a point in 3D space [ 1 ,2 ,1 ]is an array of rank 1, because it has one axis. That axis has a length of 3. In example pictured below, the array has rank 2 (it is 2-dimensional). The first dimension (axis) has a length of 2, the second dimension has a length of 3.
[ [1 . ,0 . ,0 . ] , [0 . ,1 . ,2 . ] ]

Numpy's array class is called n d a r r a y . It is also known by the alias a r r a y . Note that n u m p y . a r r a yis not the same as the Standard Python Library class a r r a y . a r r a y , which only handles one-dimensional arrays and offers less functionality. The more important attributes of an n d a r r a yobject are: ndarray.ndim the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank. ndarray.shape the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, s h a p ewill be ( n , m ) . The length of the s h a p etuple is therefore the rank, or number of dimensions, n d i m . ndarray.size the total number of elements of the array. This is equal to the product of the elements of s h a p e . ndarray.dtype
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 2/30

8/28/13

Tentative NumPy Tutorial -

an object describing the type of the elements in the array. One can create or specify dtype's using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples. ndarray.itemsize the size in bytes of each element of the array. For example, an array of elements of type f l o a t 6 4has i t e m s i z e8 (=64/8), while one of type c o m p l e x 3 2has i t e m s i z e4 (=32/8). It is equivalent to n d a r r a y . d t y p e . i t e m s i z e . ndarray.data the buffer containing the actual elements of the array. Normally, we won't need to use this attribute because we will access the elements in an array using indexing facilities.

An example
> > >f r o mn u m p y i m p o r t* > > >a=a r a n g e ( 1 5 ) . r e s h a p e ( 3 ,5 ) > > >a a r r a y ( [ [0 , 1 , 2 , 3 , 4 ] , [5 , 6 , 7 , 8 , 9 ] , [ 1 0 ,1 1 ,1 2 ,1 3 ,1 4 ] ] ) > > >a . s h a p e ( 3 ,5 ) > > >a . n d i m 2 > > >a . d t y p e . n a m e ' i n t 3 2 ' > > >a . i t e m s i z e 4 > > >a . s i z e 1 5 > > >t y p e ( a ) n u m p y . n d a r r a y > > >b=a r r a y ( [ 6 ,7 ,8 ] ) > > >b a r r a y ( [ 6 ,7 ,8 ] ) > > >t y p e ( b ) n u m p y . n d a r r a y

Array Creation
There are several ways to create arrays. For example, you can create an array from a regular Python list or tuple using the a r r a y function. The type of the resulting array is deduced from the type of the elements in the sequences.
> > >f r o mn u m p y i m p o r t* > > >a=a r r a y ([ 2 , 3 , 4 ])
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 3/30

8/28/13

Tentative NumPy Tutorial -

> > >a a r r a y ( [ 2 ,3 ,4 ] ) > > >a . d t y p e d t y p e ( ' i n t 3 2 ' ) > > >b=a r r a y ( [ 1 . 2 ,3 . 5 ,5 . 1 ] ) > > >b . d t y p e d t y p e ( ' f l o a t 6 4 ' )

A frequent error consists in calling a r r a ywith multiple numeric arguments, rather than providing a single list of numbers as an argument.
> > >a=a r r a y ( 1 , 2 , 3 , 4 ) #W R O N G

> > >a=a r r a y ( [ 1 , 2 , 3 , 4 ] ) #R I G H T a r r a ytransforms sequences of sequences into

two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.
> > >b=a r r a y ([( 1 . 5 , 2 , 3 ) ,( 4 , 5 , 6 )]) > > >b a r r a y ( [ [1 . 5 , 2 ., 3 .] , [4 ., 5 ., 6 .] ] )

The type of the array can also be explicitly specified at creation time:
> > >c=a r r a y ([[ 1 , 2 ] ,[ 3 , 4 ]] ,d t y p e = c o m p l e x) > > >c a r r a y ( [ [1 . + 0 . j , 2 . + 0 . j ] , [3 . + 0 . j , 4 . + 0 . j ] ] )

Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. The function z e r o screates an array full of zeros, the function o n e screates an array full of ones, and the function e m p t ycreates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is f l o a t 6 4 .
> > >z e r o s (( 3 , 4 )) a r r a y ( [ [ 0 . , 0 . , 0 . , 0 . ] , [ 0 . , 0 . , 0 . , 0 . ] , [ 0 . , 0 . , 0 . , 0 . ] ] ) > > >o n e s (( 2 , 3 , 4 ) ,d t y p e = i n t 1 6) s p e c i f i e d a r r a y ( [ [ [1 ,1 ,1 ,1 ] , [1 ,1 ,1 ,1 ] , [1 ,1 ,1 ,1 ] ] ,
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

#d t y p ec a na l s ob e

4/30

8/28/13

Tentative NumPy Tutorial -

[ [1 ,1 ,1 ,1 ] , [1 ,1 ,1 ,1 ] , [1 ,1 ,1 ,1 ] ] ] ,d t y p e = i n t 1 6 ) > > >e m p t y (( 2 , 3 )) a r r a y ( [ [ 3 . 7 3 6 0 3 9 5 9 e 2 6 2 , 6 . 0 2 6 5 8 0 5 8 e 1 5 4 , [ 5 . 3 0 4 9 8 9 4 8 e 3 1 3 , 3 . 1 4 6 7 3 3 0 9 e 3 0 7 ,

6 . 5 5 4 9 0 9 1 4 e 2 6 0 ] , 1 . 0 0 0 0 0 0 0 0 e + 0 0 0 ] ] )

To create sequences of numbers, NumPy provides a function analogous to r a n g ethat returns arrays instead of lists
> > >a r a n g e (1 0 ,3 0 ,5) a r r a y ( [ 1 0 ,1 5 ,2 0 ,2 5 ] ) > > >a r a n g e (0 ,2 ,0 . 3) #i ta c c e p t sf l o a ta r g u m e n t s a r r a y ( [0 ., 0 . 3 , 0 . 6 , 0 . 9 , 1 . 2 , 1 . 5 , 1 . 8 ] )

When a r a n g eis used with floating point arguments, it is generally not possible to predict the number of elements obtained, due to the finite floating point precision. For this reason, it is usually better to use the function l i n s p a c ethat receives as an argument the number of elements that we want, instead of the step:
> > >l i n s p a c e (0 ,2 ,9) #9n u m b e r sf r o m0t o2 a r r a y ( [0 . , 0 . 2 5 , 0 . 5, 0 . 7 5 , 1 . , 1 . 2 5 , 1 . 5, 1 . 7 5 , 2 . ] ) > > >x=l i n s p a c e (0 ,2 * p i ,1 0 0) #u s e f u lt oe v a l u a t ef u n c t i o n a tl o t so fp o i n t s > > >f=s i n ( x )

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

Printing Arrays
When you print an array, NumPy displays it in a similar way to nested lists, but with the following layout: the last axis is printed from left to right, the second-to-last is printed from top to bottom, the rest are also printed from top to bottom, with each slice separated from the next by an empty line. One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices.
> > >a=a r a n g e ( 6 ) > > >p r i n ta
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

#1 da r r a y

5/30

8/28/13

Tentative NumPy Tutorial -

[ 012345 ] > > > > > >b=a r a n g e ( 1 2 ) . r e s h a p e ( 4 , 3 ) > > >p r i n tb [ [0 1 2 ] [3 4 5 ] [6 7 8 ] [91 01 1 ] ] > > > > > >c=a r a n g e ( 2 4 ) . r e s h a p e ( 2 , 3 , 4 ) > > >p r i n tc [ [ [0 1 2 3 ] [4 5 6 7 ] [8 91 01 1 ] ] [ [ 1 21 31 41 5 ] [ 1 61 71 81 9 ] [ 2 02 12 22 3 ] ] ]

#2 da r r a y

#3 da r r a y

See below to get more details on r e s h a p e . If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners:
> > >p r i n ta r a n g e ( 1 0 0 0 0 ) [ 0 1 2. . . ,9 9 9 79 9 9 89 9 9 9 ] > > > > > >p r i n ta r a n g e ( 1 0 0 0 0 ) . r e s h a p e ( 1 0 0 , 1 0 0 ) [ [ 0 1 2. . . , 9 7 9 8 9 9 ] [1 0 0 1 0 1 1 0 2. . . , 1 9 7 1 9 8 1 9 9 ] [2 0 0 2 0 1 2 0 2. . . , 2 9 7 2 9 8 2 9 9 ] . . . , [ 9 7 0 09 7 0 19 7 0 2. . . ,9 7 9 79 7 9 89 7 9 9 ] [ 9 8 0 09 8 0 19 8 0 2. . . ,9 8 9 79 8 9 89 8 9 9 ] [ 9 9 0 09 9 0 19 9 0 2. . . ,9 9 9 79 9 9 89 9 9 9 ] ]

To disable this behaviour and force NumPy to print the entire array, you can change the printing options using s e t _ p r i n t o p t i o n s .
> > >s e t _ p r i n t o p t i o n s ( t h r e s h o l d = ' n a n ' )

Basic Operations
Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.
> > >a=a r r a y ([ 2 0 , 3 0 , 4 0 , 5 0 ])
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 6/30

8/28/13

Tentative NumPy Tutorial -

> > >b=a r a n g e (4) > > >b a r r a y ( [ 0 ,1 ,2 ,3 ] ) > > >c=a b > > >c a r r a y ( [ 2 0 ,2 9 ,3 8 ,4 7 ] ) > > >b * * 2 a r r a y ( [ 0 ,1 ,4 ,9 ] ) > > >1 0 * s i n ( a ) a r r a y ( [9 . 1 2 9 4 5 2 5 1 ,9 . 8 8 0 3 1 6 2 4 , 7 . 4 5 1 1 3 1 6,2 . 6 2 3 7 4 8 5 4 ] ) > > >a < 3 5 a r r a y ( [ T r u e ,T r u e ,F a l s e ,F a l s e ] ,d t y p e = b o o l )

Unlike in many matrix languages, the product operator *operates elementwise in NumPy arrays. The matrix product can be performed using the d o tfunction or creating m a t r i x objects ( see matrix section of this tutorial ).
> > >A=a r r a y ([ [ 1 , 1 ] , . . . [ 0 , 1 ] ]) > > >B=a r r a y ([ [ 2 , 0 ] , . . . [ 3 , 4 ] ]) > > >A * B a r r a y ( [ [ 2 ,0 ] , [ 0 ,4 ] ] ) > > >d o t ( A , B ) a r r a y ( [ [ 5 ,4 ] , [ 3 ,4 ] ] )

#e l e m e n t w i s ep r o d u c t

#m a t r i xp r o d u c t

Some operations, such as + =and * = , act in place to modify an existing array rather than create a new one.
> > >a=o n e s ( ( 2 , 3 ) ,d t y p e = i n t ) > > >b=r a n d o m . r a n d o m ( ( 2 , 3 ) ) > > >a* =3 > > >a a r r a y ( [ [ 3 ,3 ,3 ] , [ 3 ,3 ,3 ] ] ) > > >b+ =a > > >b a r r a y ( [ [3 . 6 9 0 9 2 7 0 3 , 3 . 8 3 2 4 2 7 6, 3 . 0 1 1 4 5 4 1] , [3 . 1 8 6 7 9 1 1 1 , 3 . 3 0 3 9 3 4 9, 3 . 3 7 6 0 0 2 8 9 ] ] ) > > >a+ =b #bi sc o n v e r t e dt o i n t e g e rt y p e > > >a a r r a y ( [ [ 6 ,6 ,6 ] , [ 6 ,6 ,6 ] ] )

wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

7/30

8/28/13

Tentative NumPy Tutorial -

When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting).
> > >a=o n e s ( 3 ,d t y p e = i n t 3 2 ) > > >b=l i n s p a c e ( 0 , p i , 3 ) > > >b . d t y p e . n a m e ' f l o a t 6 4 ' > > >c=a + b > > >c a r r a y ( [1 . , 2 . 5 7 0 7 9 6 3 3 , 4 . 1 4 1 5 9 2 6 5 ] ) > > >c . d t y p e . n a m e ' f l o a t 6 4 ' > > >d=e x p ( c * 1 j ) > > >d a r r a y ( [0 . 5 4 0 3 0 2 3 1 + 0 . 8 4 1 4 7 0 9 8 j ,0 . 8 4 1 4 7 0 9 8 + 0 . 5 4 0 3 0 2 3 1 j , 0 . 5 4 0 3 0 2 3 1 0 . 8 4 1 4 7 0 9 8 j ] ) > > >d . d t y p e . n a m e ' c o m p l e x 1 2 8 '

Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the n d a r r a yclass.
> > >a=r a n d o m . r a n d o m ( ( 2 , 3 ) ) > > >a a r r a y ( [ [0 . 6 9 0 3 0 0 7, 0 . 3 9 1 6 8 3 4 6 , 0 . 1 6 5 2 4 7 6 9 ] , [0 . 4 8 8 1 9 8 7 5 , 0 . 7 7 1 8 8 5 0 5 , 0 . 9 4 7 9 2 1 5 5 ] ] ) > > >a . s u m ( ) 3 . 4 5 5 2 3 7 2 1 0 0 5 2 1 4 8 5 > > >a . m i n ( ) 0 . 1 6 5 2 4 7 6 8 6 5 4 7 4 3 5 9 3 > > >a . m a x ( ) 0 . 9 4 7 9 2 1 5 5 4 2 6 7 0 0 7 3

By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the a x i sparameter you can apply an operation along the specified axis of an array:
> > >b=a r a n g e ( 1 2 ) . r e s h a p e ( 3 , 4 ) > > >b a r r a y ( [ [0 , 1 , 2 , 3 ] , [4 , 5 , 6 , 7 ] , [8 , 9 ,1 0 ,1 1 ] ] ) > > > > > >b . s u m ( a x i s = 0 ) a r r a y ( [ 1 2 ,1 5 ,1 8 ,2 1 ] ) > > > > > >b . m i n ( a x i s = 1 )
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

#s u mo fe a c hc o l u m n

#m i no fe a c hr o w
8/30

8/28/13

Tentative NumPy Tutorial -

a r r a y ( [ 0 ,4 ,8 ] ) > > > > > >b . c u m s u m ( a x i s = 1 ) e a c hr o w a r r a y ( [ [0 , 1 , 3 , 6 ] , [4 , 9 ,1 5 ,2 2 ] , [8 ,1 7 ,2 7 ,3 8 ] ] ) #c u m u l a t i v es u ma l o n g

Universal Functions
NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called "universal functions"(u f u n c ). Within NumPy, these functions operate elementwise on an array, producing an array as output.
> > >B=a r a n g e ( 3 ) > > >B a r r a y ( [ 0 ,1 ,2 ] ) > > >e x p ( B ) a r r a y ( [1 . , 2 . 7 1 8 2 8 1 8 3 , 7 . 3 8 9 0 5 6 1] ) > > >s q r t ( B ) a r r a y ( [0 . , 1 . , 1 . 4 1 4 2 1 3 5 6 ] ) > > >C=a r r a y ( [ 2 . ,1 . ,4 . ] ) > > >a d d ( B ,C ) a r r a y ( [2 . , 0 . , 6 . ] )

See also all, alltrue, any, apply along axis, 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, Slicing and Iterating


One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.
> > >a=a r a n g e ( 1 0 ) * * 3 > > >a a r r a y ( [ 0 , 1 , 8 , 2 7 , 6 4 ,1 2 5 ,2 1 6 ,3 4 3 ,5 1 2 ,7 2 9 ] ) > > >a [ 2 ] 8 > > >a [ 2 : 5 ] a r r a y ( [8 ,2 7 ,6 4 ] ) > > >a [ : 6 : 2 ]=1 0 0 0 #e q u i v a l e n tt oa [ 0 : 6 : 2 ]=1 0 0 0 ;f r o ms t a r t t op o s i t i o n6 ,e x c l u s i v e ,s e te v e r y2 n de l e m e n tt o1 0 0 0 > > >a
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 9/30

8/28/13

Tentative NumPy Tutorial -

a r r a y ( [ 1 0 0 0 , 1 ,1 0 0 0 , 2 7 ,1 0 0 0 , 1 2 5 , 2 1 6 , 3 4 3 , 7 2 9 ] ) > > >a [:: 1 ] #r e v e r s e da a r r a y ( [ 7 2 9 , 5 1 2 , 3 4 3 , 2 1 6 , 1 2 5 ,1 0 0 0 , 2 7 ,1 0 0 0 , 1 0 0 0 ] ) > > >f o rii na : . . . p r i n ti * * ( 1 / 3 . ) , . . . n a n1 . 0n a n3 . 0n a n5 . 06 . 07 . 08 . 09 . 0

5 1 2 ,

1 ,

Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas:
> > >d e ff ( x , y ) : . . . r e t u r n1 0 * x + y . . . > > >b=f r o m f u n c t i o n ( f , ( 5 , 4 ) , d t y p e = i n t ) > > >b a r r a y ( [ [0 , 1 , 2 , 3 ] , [ 1 0 ,1 1 ,1 2 ,1 3 ] , [ 2 0 ,2 1 ,2 2 ,2 3 ] , [ 3 0 ,3 1 ,3 2 ,3 3 ] , [ 4 0 ,4 1 ,4 2 ,4 3 ] ] ) > > >b [ 2 , 3 ] 2 3 > > >b [ 0 : 5 ,1 ] #e a c hr o wi nt h es e c o n dc o l u m n o fb a r r a y ( [1 ,1 1 ,2 1 ,3 1 ,4 1 ] ) > > >b [:, 1 ] #e q u i v a l e n tt ot h ep r e v i o u s e x a m p l e a r r a y ( [1 ,1 1 ,2 1 ,3 1 ,4 1 ] ) > > >b [ 1 : 3 ,:] #e a c hc o l u m ni nt h es e c o n da n d t h i r dr o wo fb a r r a y ( [ [ 1 0 ,1 1 ,1 2 ,1 3 ] , [ 2 0 ,2 1 ,2 2 ,2 3 ] ] )

When fewer indices are provided than the number of axes, the missing indices are considered complete slices:
> > >b [ 1 ] t ob [ 1 , : ] a r r a y ( [ 4 0 ,4 1 ,4 2 ,4 3 ] ) #t h el a s tr o w .E q u i v a l e n t

The expression within brackets in b [ i ]is treated as an ifollowed by as many instances of : as needed to represent the remaining axes. NumPy also allows you to write this using dots as b [ i , . . . ] . The dots (. . . ) represent as many colons as needed to produce a complete indexing tuple.
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 10/30

8/28/13

Tentative NumPy Tutorial -

For example, if xis a rank 5 array (i.e., it has 5 axes), then


x [ 1 , 2 , . . . ]is equivalent to x [ 1 , 2 , : , : , : ] , x [ . . . , 3 ]to x [ : , : , : , : , 3 ]and x [ 4 , . . . , 5 , : ]to x [ 4 , : , : , 5 , : ] .

> > >c=a r r a y ([[ [ 0 , 1 , 2 ] , s t a c k e d2 Da r r a y s ) . . . [1 0 ,1 2 ,1 3 ] ] , . . . . . . [ [ 1 0 0 , 1 0 1 , 1 0 2 ] , . . . [ 1 1 0 , 1 1 2 , 1 1 3 ] ]]) > > >c . s h a p e ( 2 ,2 ,3 ) > > >c [ 1 , . . . ] c [ 1 ] a r r a y ( [ [ 1 0 0 ,1 0 1 ,1 0 2 ] , [ 1 1 0 ,1 1 2 ,1 1 3 ] ] ) > > >c [ . . . , 2 ] a r r a y ( [ [ 2 , 1 3 ] , [ 1 0 2 ,1 1 3 ] ] )

#a3 Da r r a y( t w o

#s a m ea sc [ 1 , : , : ]o r

#s a m ea sc [ : , : , 2 ]

Iterating over multidimensional arrays is done with respect to the first axis:
> > >f o rr o wi nb : . . . p r i n tr o w . . . [ 0123 ] [ 1 01 11 21 3 ] [ 2 02 12 22 3 ] [ 3 03 13 23 3 ] [ 4 04 14 24 3 ]

However, if one wants to perform an operation on each element in the array, one can use the f l a tattribute which is an iterator over all the elements of the array:
> > >f o re l e m e n ti nb . f l a t : . . . p r i n te l e m e n t , . . . 01231 01 11 21 32 02 12 22 33 03 13 23 34 04 14 24 3

See also [], ..., newaxis, ndenumerate, indices, index exp

Shape Manipulation
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 11/30

8/28/13

Tentative NumPy Tutorial -

Changing the shape of an array


An array has a shape given by the number of elements along each axis:
> > >a=f l o o r ( 1 0 * r a n d o m . r a n d o m ( ( 3 , 4 ) ) ) > > >a a r r a y ( [ [7 . , 5 . , 9 . , 3 . ] , [7 . , 2 . , 7 . , 8 . ] , [6 . , 8 . , 3 . , 2 . ] ] ) > > >a . s h a p e ( 3 ,4 )

The shape of an array can be changed with various commands:


> > >a . r a v e l ( )#f l a t t e nt h ea r r a y a r r a y ( [7 . , 5 . , 9 . , 3 . , 7 . , 2 . , 7 . , 8 . , 6 . , 8 . , 3 . , 2 . ] ) > > >a . s h a p e=( 6 ,2 ) > > >a . t r a n s p o s e ( ) a r r a y ( [ [7 . , 9 . , 7 . , 7 . , 6 . , 3 . ] , [5 . , 3 . , 2 . , 8 . , 8 . , 2 . ] ] )

The order of the elements in the array resulting from ravel() is normally "C-style", that is, the rightmost index "changes the fastest", so the element after a[0,0] is a[0,1]. If the array is reshaped to some other shape, again the array is treated as "C-style". Numpy normally creates arrays stored in this order, so ravel() will usually not need to copy its argument, but if the array was made by taking slices of another array or created with unusual options, it may need to be copied. The functions ravel() and reshape() can also be instructed, using an optional argument, to use FORTRAN-style arrays, in which the leftmost index changes the fastest. The reshape function returns its argument with a modified shape, whereas the resize method modifies the array itself:
> > >a a r r a y ( [ [7 . , 5 . ] , [9 . , 3 . ] , [7 . , 2 . ] , [7 . , 8 . ] , [6 . , 8 . ] , [3 . , 2 . ] ] ) > > >a . r e s i z e ( ( 2 , 6 ) ) > > >a a r r a y ( [ [7 . , 5 . , 9 . , 3 . , 7 . , 2 . ] , [7 . , 8 . , 6 . , 8 . , 3 . , 2 . ] ] )

If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 12/30

8/28/13

Tentative NumPy Tutorial -

> > >a . r e s h a p e ( 3 , 1 ) a r r a y ( [ [7 . , 5 . , 9 . , 3 . ] , [7 . , 2 . , 7 . , 8 . ] , [6 . , 8 . , 3 . , 2 . ] ] )

See also:: shape example, reshape example, resize example, ravel example

Stacking together different arrays


Several arrays can be stacked together along different axes:
> > >a=f l o o r ( 1 0 * r a n d o m . r a n d o m ( ( 2 , 2 ) ) ) > > >a a r r a y ( [ [1 . , 1 . ] , [5 . , 8 . ] ] ) > > >b=f l o o r ( 1 0 * r a n d o m . r a n d o m ( ( 2 , 2 ) ) ) > > >b a r r a y ( [ [3 . , 3 . ] , [6 . , 0 . ] ] ) > > >v s t a c k ( ( a , b ) ) a r r a y ( [ [1 . , 1 . ] , [5 . , 8 . ] , [3 . , 3 . ] , [6 . , 0 . ] ] ) > > >h s t a c k ( ( a , b ) ) a r r a y ( [ [1 . , 1 . , 3 . , 3 . ] , [5 . , 8 . , 6 . , 0 . ] ] )

The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to vstack only for 1D arrays:
> > >c o l u m n _ s t a c k ( ( a , b ) ) #W i t h2 Da r r a y s a r r a y ( [ [1 . , 1 . , 3 . , 3 . ] , [5 . , 8 . , 6 . , 0 . ] ] ) > > >a = a r r a y ( [ 4 . , 2 . ] ) > > >b = a r r a y ( [ 2 . , 8 . ] ) > > >a [ : , n e w a x i s ] #T h i sa l l o w st oh a v ea2 Dc o l u m n sv e c t o r a r r a y ( [ [4 . ] , [2 . ] ] ) > > >c o l u m n _ s t a c k ( ( a [ : , n e w a x i s ] , b [ : , n e w a x i s ] ) ) a r r a y ( [ [4 . , 2 . ] , [2 . , 8 . ] ] ) > > >v s t a c k ( ( a [ : , n e w a x i s ] , b [ : , n e w a x i s ] ) )#T h eb e h a v i o ro fv s t a c ki s d i f f e r e n t a r r a y ( [ [4 . ] , [2 . ] , [2 . ] ,
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 13/30

8/28/13

Tentative NumPy Tutorial -

[8 . ] ] )

The function row_stack, on the other hand, stacks 1D arrays as rows into a 2D array. For arrays of with more than two dimensions, hstack stacks along their second axes, vstack stacks along their first axes, and concatenate allows for an optional arguments giving the number of the axis along which the concatenation should happen. Note In complex cases, r_[] and c_[] are useful for creating arrays by stacking numbers along one axis. They allow the use of range literals (":") :
> > >r _ [ 1 : 4 , 0 , 4 ] a r r a y ( [ 1 ,2 ,3 ,0 ,4 ] )

When used with arrays as arguments, r_[] and c_[] are similar to vstack and hstack in their default behavior, but allow for an optional argument giving the number of the axis along which to concatenate. See also: hstack example, vstack exammple, column_stack example, row_stack example, concatenate example, c_ example, r_ example

Splitting one array into several smaller ones


Using hsplit, you can split an array along its horizontal axis, either by specifying the number of equally shaped arrays to return, or by specifying the columns after which the division should occur:
> > >a=f l o o r ( 1 0 * r a n d o m . r a n d o m ( ( 2 , 1 2 ) ) ) > > >a a r r a y ( [ [8 . , 8 . , 3 . , 9 . , 0 . , 4 . , 3 . , 0 . , 0 . , 6 . , 4 . , 4 . ] , [0 . , 3 . , 2 . , 9 . , 6 . , 0 . , 4 . , 5 . , 7 . , 5 . , 1 . , 4 . ] ] ) > > >h s p l i t ( a , 3 ) #S p l i tai n t o3 [ a r r a y ( [ [8 . , 8 . , 3 . , 9 . ] , [0 . , 3 . , 2 . , 9 . ] ] ) ,a r r a y ( [ [0 . , 4 . , 3 . , 0 . ] , [6 . , 0 . , 4 . , 5 . ] ] ) ,a r r a y ( [ [0 . , 6 . , 4 . , 4 . ] , [7 . , 5 . , 1 . , 4 . ] ] ) ] > > >h s p l i t ( a , ( 3 , 4 ) ) #S p l i taa f t e rt h et h i r da n dt h ef o u r t hc o l u m n [ a r r a y ( [ [8 . , 8 . , 3 . ] , [0 . , 3 . , 2 . ] ] ) ,a r r a y ( [ [9 . ] , [9 . ] ] ) ,a r r a y ( [ [0 . , 4 . , 3 . , 0 . , 0 . , 6 . , 4 . , 4 . ] , [6 . , 0 . , 4 . , 5 . , 7 . , 5 . , 1 . , 4 . ] ] ) ]

vsplit splits along the vertical axis, and array split allows one to specify along which axis to split.

Copies and Views


wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 14/30

8/28/13

Tentative NumPy Tutorial -

When operating and manipulating arrays, their data is sometimes copied into a new array and sometimes not. This is often a source of confusion for beginners. There are three cases:

No Copy at All
Simple assignments make no copy of array objects or of their data.
> > >a=a r a n g e ( 1 2 ) > > >b=a > > >bi sa o b j e c t T r u e > > >b . s h a p e=3 , 4 > > >a . s h a p e ( 3 ,4 )

#n on e wo b j e c ti sc r e a t e d #aa n dba r et w on a m e sf o rt h es a m en d a r r a y

#c h a n g e st h es h a p eo fa

Python passes mutable objects as references, so function calls make no copy.


> > >d e ff ( x ) : . . . p r i n ti d ( x ) . . . > > >i d ( a ) o b j e c t 1 4 8 2 9 3 2 1 6 > > >f ( a ) 1 4 8 2 9 3 2 1 6

#i di sau n i q u ei d e n t i f i e ro fa n

View or Shallow Copy


Different array objects can share the same data. The v i e wmethod creates a new array object that looks at the same data.
> > >c=a . v i e w ( ) > > >ci sa F a l s e > > >c . b a s ei sa o w n e db ya T r u e > > >c . f l a g s . o w n d a t a F a l s e > > > > > >c . s h a p e=2 , 6 > > >a . s h a p e ( 3 ,4 ) > > >c [ 0 , 4 ]=1 2 3 4 > > >a
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

#ci sav i e wo ft h ed a t a

#a ' ss h a p ed o e s n ' tc h a n g e

#a ' sd a t ac h a n g e s

15/30

8/28/13

Tentative NumPy Tutorial -

a r r a y ( [ [

0 ,

1 , 5 , 9 ,

2 , 6 , 1 0 ,

3 ] , 7 ] , 1 1 ] ] )

[ 1 2 3 4 , [ 8 ,

Slicing an array returns a view of it:


> > >s=a [:,1 : 3 ] #s p a c e sa d d e df o rc l a r i t y ;c o u l da l s ob e w r i t t e n" s=a [ : , 1 : 3 ] " > > >s [ : ]=1 0 #s [ : ]i sav i e wo fs .N o t et h ed i f f e r e n c e b e t w e e ns = 1 0a n ds [ : ] = 1 0 > > >a a r r a y ( [ [ 0 , 1 0 , 1 0 , 3 ] , [ 1 2 3 4 , 1 0 , 1 0 , 7 ] , [ 8 , 1 0 , 1 0 , 1 1 ] ] )

Deep Copy
The c o p ymethod makes a complete copy of the array and its data.
> > >d=a . c o p y ( ) n e wd a t ai sc r e a t e d > > >di sa F a l s e > > >d . b a s ei sa w i t ha F a l s e > > >d [ 0 , 0 ]=9 9 9 9 > > >a a r r a y ( [ [ 0 , 1 0 , [ 1 2 3 4 , 1 0 , [ 8 , 1 0 , #an e wa r r a yo b j e c tw i t h

#dd o e s n ' ts h a r ea n y t h i n g

1 0 , 1 0 , 1 0 ,

3 ] , 7 ] , 1 1 ] ] )

Functions and Methods Overview


Here is a list of NumPy functions and methods names ordered in some categories. The names link to the Numpy Example List so that you can see the functions in action. Array Creation arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r , zeros, zeros_like Conversions astype, atleast 1d, atleast 2d, atleast 3d, mat Manipulations array split, column stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 16/30

8/28/13

Tentative NumPy Tutorial -

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 Basic Statistics cov, mean, std, var Basic Linear Algebra cross, dot, outer, svd, vdot

Less Basic
Broadcasting rules
Broadcasting allows universal functions to deal in a meaningful way with inputs that do not have exactly the same shape. The first rule of broadcasting is that if all input arrays do not have the same number of dimensions, a "1" will be repeatedly prepended to the shapes of the smaller arrays until all the arrays have the same number of dimensions. The second rule of broadcasting ensures that arrays with a size of 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is assumed to be the same along that dimension for the "broadcast" array. After application of the broadcasting rules, the sizes of all arrays must match. More details can be found in this documentation.

Fancy indexing and index tricks


NumPy offers more indexing facilities than regular Python sequences. In addition to indexing by integers and slices, as we saw before, arrays can be indexed by arrays of integers and arrays of booleans.

Indexing with Arrays of Indices


> > >a=a r a n g e ( 1 2 ) * * 2 n u m b e r s > > >i=a r r a y ([1 , 1 , 3 , 8 , 5]) > > >a [ i ] t h ep o s i t i o n si a r r a y ( [1 , 1 , 9 ,6 4 ,2 5 ] ) > > > > > >j=a r r a y ([[3 ,4 ] ,[9 ,7]]) a r r a yo fi n d i c e s
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

#t h ef i r s t1 2s q u a r e #a na r r a yo fi n d i c e s #t h ee l e m e n t so faa t

#ab i d i m e n s i o n a l

17/30

8/28/13

Tentative NumPy Tutorial -

> > >a [ j ] a r r a y ( [ [9 ,1 6 ] , [ 8 1 ,4 9 ] ] )

#t h es a m es h a p ea sj

When the indexed array ais multidimensional, a single array of indices refers to the first dimension of a . The following example shows this behavior by converting an image of labels into a color image using a palette.
> > >p a l e t t e=a r r a y ([[ 0 , 0 , 0 ] , . . . [ 2 5 5 , 0 , 0 ] , . . . [ 0 , 2 5 5 , 0 ] , . . . [ 0 , 0 , 2 5 5 ] , . . . [ 2 5 5 , 2 5 5 , 2 5 5 ]]) > > >i m a g e=a r r a y ([[0 ,1 ,2 ,0] , c o r r e s p o n d st oac o l o ri nt h ep a l e t t e . . . [0 ,3 ,4 ,0] ]) > > >p a l e t t e [ i m a g e ] i m a g e a r r a y ( [ [ [ 0 , 0 , 0 ] , [ 2 5 5 , 0 , 0 ] , [ 0 ,2 5 5 , 0 ] , [ 0 , 0 , 0 ] ] , [ [ 0 , 0 , 0 ] , [ 0 , 0 ,2 5 5 ] , [ 2 5 5 ,2 5 5 ,2 5 5 ] , [ 0 , 0 , 0 ] ] ] ) #b l a c k #r e d #g r e e n #b l u e #w h i t e #e a c hv a l u e

#t h e( 2 , 4 , 3 )c o l o r

We can also give indexes for more than one dimension. The arrays of indices for each dimension must have the same shape.
> > >a=a r a n g e ( 1 2 ) . r e s h a p e ( 3 , 4 ) > > >a a r r a y ( [ [0 , 1 , 2 , 3 ] , [4 , 5 , 6 , 7 ] , [8 , 9 ,1 0 ,1 1 ] ] ) > > >i=a r r a y ([[ 0 , 1 ] , f i r s td i mo fa . . . [ 1 , 2 ]]) > > >j=a r r a y ([[ 2 , 1 ] , s e c o n dd i m . . . [ 3 , 3 ]]) > > > > > >a [ i , j ] e q u a ls h a p e a r r a y ( [ [2 , 5 ] , [7 ,1 1 ] ] ) > > >
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

#i n d i c e sf o rt h e

#i n d i c e sf o rt h e

#ia n djm u s th a v e

18/30

8/28/13

Tentative NumPy Tutorial -

> > >a [ i , 2 ] a r r a y ( [ [2 , 6 ] , [6 ,1 0 ] ] ) > > > > > >a [ : , j ] a r r a y ( [ [ [2 , 1 ] , [3 , 3 ] ] , [ [6 , 5 ] , [7 , 7 ] ] , [ [ 1 0 , 9 ] , [ 1 1 ,1 1 ] ] ] )

#i . e . ,a [:,j ]

Naturally, we can put iand jin a sequence (say a list) and then do the indexing with the list.
> > >l=[ i , j ] > > >a [ l ] a r r a y ( [ [2 , 5 ] , [7 ,1 1 ] ] )

#e q u i v a l e n tt oa [ i , j ]

However, we can not do this by putting iand jinto an array, because this array will be interpreted as indexing the first dimension of a.
> > >s=a r r a y ([ i , j ]) > > >a [ s ] #n o tw h a tw ew a n t T r a c e b a c k( m o s tr e c e n tc a l ll a s t ) : F i l e" < s t d i n > " ,l i n e1 ,i n ? I n d e x E r r o r :i n d e x( 3 )o u to fr a n g e( 0 < = i n d e x < = 2 )i nd i m e n s i o n0 > > > > > >a [ t u p l e ( s ) ] #s a m ea sa [ i , j ] a r r a y ( [ [2 , 5 ] , [7 ,1 1 ] ] )

Another common use of indexing with arrays is the search of the maximum value of timedependent series :
> > >t i m e=l i n s p a c e ( 2 0 ,1 4 5 ,5 ) #t i m es c a l e > > >d a t a=s i n ( a r a n g e ( 2 0 ) ) . r e s h a p e ( 5 , 4 ) #4t i m e d e p e n d e n t s e r i e s > > >t i m e a r r a y ( [ 2 0 . , 5 1 . 2 5 , 8 2 . 5, 1 1 3 . 7 5 , 1 4 5 . ] ) > > >d a t a a r r a y ( [ [0 . , 0 . 8 4 1 4 7 0 9 8 , 0 . 9 0 9 2 9 7 4 3 , 0 . 1 4 1 1 2 0 0 1 ] , [ 0 . 7 5 6 8 0 2 5,0 . 9 5 8 9 2 4 2 7 ,0 . 2 7 9 4 1 5 5, 0 . 6 5 6 9 8 6 6] , [0 . 9 8 9 3 5 8 2 5 , 0 . 4 1 2 1 1 8 4 9 ,0 . 5 4 4 0 2 1 1 1 ,0 . 9 9 9 9 9 0 2 1 ] , [ 0 . 5 3 6 5 7 2 9 2 , 0 . 4 2 0 1 6 7 0 4 , 0 . 9 9 0 6 0 7 3 6 , 0 . 6 5 0 2 8 7 8 4 ] , [ 0 . 2 8 7 9 0 3 3 2 ,0 . 9 6 1 3 9 7 4 9 ,0 . 7 5 0 9 8 7 2 5 , 0 . 1 4 9 8 7 7 2 1 ] ] )
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 19/30

8/28/13

Tentative NumPy Tutorial -

> > > > > >i n d=d a t a . a r g m a x ( a x i s = 0 ) #i n d e xo ft h em a x i m a f o re a c hs e r i e s > > >i n d a r r a y ( [ 2 ,0 ,3 ,1 ] ) > > > > > >t i m e _ m a x=t i m e [i n d ] #t i m e sc o r r e s p o n d i n g t ot h em a x i m a > > > > > >d a t a _ m a x=d a t a [ i n d ,x r a n g e ( d a t a . s h a p e [ 1 ] ) ]#= >d a t a [ i n d [ 0 ] , 0 ] , d a t a [ i n d [ 1 ] , 1 ] . . . > > > > > >t i m e _ m a x a r r a y ( [ 8 2 . 5, 2 0 . , 1 1 3 . 7 5 , 5 1 . 2 5 ] ) > > >d a t a _ m a x a r r a y ( [0 . 9 8 9 3 5 8 2 5 , 0 . 8 4 1 4 7 0 9 8 , 0 . 9 9 0 6 0 7 3 6 , 0 . 6 5 6 9 8 6 6] ) > > > > > >a l l ( d a t a _ m a x= =d a t a . m a x ( a x i s = 0 ) ) T r u e

You can also use indexing with arrays as a target to assign to:
> > >a=a r a n g e ( 5 ) > > >a a r r a y ( [ 0 ,1 ,2 ,3 ,4 ] ) > > >a [ [ 1 , 3 , 4 ] ]=0 > > >a a r r a y ( [ 0 ,0 ,2 ,0 ,0 ] )

However, when the list of indices contains repetitions, the assignment is done several times, leaving behind the last value:
> > >a=a r a n g e ( 5 ) > > >a [ [ 0 , 0 , 2 ] ] = [ 1 , 2 , 3 ] > > >a a r r a y ( [ 2 ,1 ,3 ,3 ,4 ] )

This is reasonable enough, but watch out if you want to use Python's + =construct, as it may not do what you expect:
> > >a=a r a n g e ( 5 ) > > >a [ [ 0 , 0 , 2 ] ] + = 1 > > >a a r r a y ( [ 1 ,1 ,3 ,3 ,4 ] )

Even though 0 occurs twice in the list of indices, the 0th element is only incremented once. This is because Python requires "a+=1" to be equivalent to "a=a+1".
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 20/30

8/28/13

Tentative NumPy Tutorial -

Indexing with Boolean Arrays


When we index arrays with arrays of (integer) indices we are providing the list of indices to pick. With boolean indices the approach is different; we explicitly choose which items in the array we want and which ones we don't. The most natural way one can think of for boolean indexing is to use boolean arrays that have the same shape as the original array:
> > >a=a r a n g e ( 1 2 ) . r e s h a p e ( 3 , 4 ) > > >b=a>4 > > >b #bi sab o o l e a nw i t h a ' ss h a p e a r r a y ( [ [ F a l s e ,F a l s e ,F a l s e ,F a l s e ] , [ F a l s e ,T r u e ,T r u e ,T r u e ] , [ T r u e ,T r u e ,T r u e ,T r u e ] ] ,d t y p e = b o o l ) > > >a [ b ] #1 da r r a yw i t ht h e s e l e c t e de l e m e n t s a r r a y ( [5 , 6 , 7 , 8 , 9 ,1 0 ,1 1 ] )

This property can be very useful in assignments:


> > >a [ b ]=0 h i g h e rt h a n4b e c o m e0 > > >a a r r a y ( [ [ 0 ,1 ,2 ,3 ] , [ 4 ,0 ,0 ,0 ] , [ 0 ,0 ,0 ,0 ] ] ) #A l le l e m e n t so f' a '

You can look at the Mandelbrot set example to see how to use boolean indexing to generate an image of the Mandelbrot set. The second way of indexing with booleans is more similar to integer indexing; for each dimension of the array we give a 1D boolean array selecting the slices we want.
> > >a=a r a n g e ( 1 2 ) . r e s h a p e ( 3 , 4 ) > > >b 1=a r r a y ( [ F a l s e , T r u e , T r u e ] ) > > >b 2=a r r a y ( [ T r u e , F a l s e , T r u e , F a l s e ] ) > > > > > >a [ b 1 , : ] a r r a y ( [ [4 , 5 , 6 , 7 ] , [8 , 9 ,1 0 ,1 1 ] ] ) > > > > > >a [ b 1 ] a r r a y ( [ [4 , 5 , 6 , 7 ] , [8 , 9 ,1 0 ,1 1 ] ] ) > > > > > >a [ : , b 2 ] a r r a y ( [ [0 , 2 ] ,
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

#f i r s td i ms e l e c t i o n #s e c o n dd i ms e l e c t i o n #s e l e c t i n gr o w s

#s a m et h i n g

#s e l e c t i n gc o l u m n s
21/30

8/28/13

Tentative NumPy Tutorial -

[4 , 6 ] , [8 ,1 0 ] ] ) > > > > > >a [ b 1 , b 2 ] a r r a y ( [4 ,1 0 ] ) #aw e i r dt h i n gt od o

Note that the length of the 1D boolean array must coincide with the length of the dimension (or axis) you want to slice. In the previous example, b 1is a 1-rank array with length 3 (the number of rows in a ), and b 2(of length 4) is suitable to index the 2nd rank (columns) of a .

The ix_() function


The ix_ function can be used to combine different vectors so as to obtain the result for each n-uplet. For example, if you want to compute all the a+b*c for all the triplets taken from each of the vectors a, b and c:
> > >a=a r r a y ( [ 2 , 3 , 4 , 5 ] ) > > >b=a r r a y ( [ 8 , 5 , 4 ] ) > > >c=a r r a y ( [ 5 , 4 , 6 , 8 , 3 ] ) > > >a x , b x , c x=i x _ ( a , b , c ) > > >a x a r r a y ( [ [ [ 2 ] ] , [ [ 3 ] ] , [ [ 4 ] ] , [ [ 5 ] ] ] ) > > >b x a r r a y ( [ [ [ 8 ] , [ 5 ] , [ 4 ] ] ] ) > > >c x a r r a y ( [ [ [ 5 ,4 ,6 ,8 ,3 ] ] ] ) > > >a x . s h a p e ,b x . s h a p e ,c x . s h a p e ( ( 4 ,1 ,1 ) ,( 1 ,3 ,1 ) ,( 1 ,1 ,5 ) ) > > >r e s u l t=a x + b x * c x > > >r e s u l t a r r a y ( [ [ [ 4 2 ,3 4 ,5 0 ,6 6 ,2 6 ] , [ 2 7 ,2 2 ,3 2 ,4 2 ,1 7 ] , [ 2 2 ,1 8 ,2 6 ,3 4 ,1 4 ] ] , [ [ 4 3 ,3 5 ,5 1 ,6 7 ,2 7 ] , [ 2 8 ,2 3 ,3 3 ,4 3 ,1 8 ] , [ 2 3 ,1 9 ,2 7 ,3 5 ,1 5 ] ] , [ [ 4 4 ,3 6 ,5 2 ,6 8 ,2 8 ] , [ 2 9 ,2 4 ,3 4 ,4 4 ,1 9 ] , [ 2 4 ,2 0 ,2 8 ,3 6 ,1 6 ] ] , [ [ 4 5 ,3 7 ,5 3 ,6 9 ,2 9 ] ,
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 22/30

8/28/13

Tentative NumPy Tutorial -

[ 3 0 ,2 5 ,3 5 ,4 5 ,2 0 ] , [ 2 5 ,2 1 ,2 9 ,3 7 ,1 7 ] ] ] ) > > >r e s u l t [ 3 , 2 , 4 ] 1 7 > > >a [ 3 ] + b [ 2 ] * c [ 4 ] 1 7

You could also implement the reduce as follows:


d e fu f u n c _ r e d u c e ( u f c t ,* v e c t o r s ) : v s=i x _ ( * v e c t o r s ) r=u f c t . i d e n t i t y f o rvi nv s : r=u f c t ( r , v ) r e t u r nr

and then use it as:


> > >u f u n c _ r e d u c e ( a d d , a , b , c ) a r r a y ( [ [ [ 1 5 ,1 4 ,1 6 ,1 8 ,1 3 ] , [ 1 2 ,1 1 ,1 3 ,1 5 ,1 0 ] , [ 1 1 ,1 0 ,1 2 ,1 4 , 9 ] ] , [ [ 1 6 ,1 5 ,1 7 ,1 9 ,1 4 ] , [ 1 3 ,1 2 ,1 4 ,1 6 ,1 1 ] , [ 1 2 ,1 1 ,1 3 ,1 5 ,1 0 ] ] , [ [ 1 7 ,1 6 ,1 8 ,2 0 ,1 5 ] , [ 1 4 ,1 3 ,1 5 ,1 7 ,1 2 ] , [ 1 3 ,1 2 ,1 4 ,1 6 ,1 1 ] ] , [ [ 1 8 ,1 7 ,1 9 ,2 1 ,1 6 ] , [ 1 5 ,1 4 ,1 6 ,1 8 ,1 3 ] , [ 1 4 ,1 3 ,1 5 ,1 7 ,1 2 ] ] ] )

The advantage of this version of reduce compared to the normal ufunc.reduce is that it makes use of the Broadcasting Rules in order to avoid creating an argument array the size of the output times the number of vectors.

Indexing with strings


See RecordArrays.

Linear Algebra
Work in progress. Basic linear algebra to be included here.

Simple Array Operations


See linalg.py in numpy folder for more.
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 23/30

8/28/13

Tentative NumPy Tutorial -

> > >f r o mn u m p yi m p o r t* > > >f r o mn u m p y . l i n a l gi m p o r t* > > >a=a r r a y ( [ [ 1 . 0 ,2 . 0 ] ,[ 3 . 0 ,4 . 0 ] ] ) > > >p r i n ta [ [1 . 2 . ] [3 . 4 . ] ] > > >a . t r a n s p o s e ( ) a r r a y ( [ [1 . , 3 . ] , [2 . , 4 . ] ] ) > > >i n v ( a ) a r r a y ( [ [ 2 ., 1 .] , [1 . 5 ,0 . 5 ] ] ) > > >u=e y e ( 2 )#u n i t2 x 2m a t r i x ;" e y e "r e p r e s e n t s" I " > > >u a r r a y ( [ [1 . , 0 . ] , [0 . , 1 . ] ] ) > > >j=a r r a y ( [ [ 0 . 0 ,1 . 0 ] ,[ 1 . 0 ,0 . 0 ] ] ) > > >d o t( j ,j )#m a t r i xp r o d u c t a r r a y ( [ [ 1 . , 0 . ] , [0 . ,1 . ] ] ) > > >t r a c e ( u ) #t r a c e 2 . 0 > > >y=a r r a y ( [ [ 5 . ] ,[ 7 . ] ] ) > > >s o l v e ( a ,y ) a r r a y ( [ [ 3 . ] , [4 . ] ] ) > > >e i g ( j ) ( a r r a y ( [0 . + 1 . j , 0 . 1 . j ] ) , a r r a y ( [ [0 . 7 0 7 1 0 6 7 8 + 0 . j , 0 . 7 0 7 1 0 6 7 8 + 0 . j ] , [0 . 0 0 0 0 0 0 0 0 0 . 7 0 7 1 0 6 7 8 j , 0 . 0 0 0 0 0 0 0 0 + 0 . 7 0 7 1 0 6 7 8 j ] ] ) ) P a r a m e t e r s : s q u a r em a t r i x R e t u r n s T h ee i g e n v a l u e s ,e a c hr e p e a t e da c c o r d i n gt oi t sm u l t i p l i c i t y . T h en o r m a l i z e d( u n i t" l e n g t h " )e i g e n v e c t o r s ,s u c ht h a tt h e c o l u m n` ` v [ : , i ] ` `i st h ee i g e n v e c t o rc o r r e s p o n d i n gt ot h e e i g e n v a l u e` ` w [ i ] ` `.
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 24/30

8/28/13

Tentative NumPy Tutorial -

The Matrix Class


Here is a short intro to the Matrix class.
> > >A=m a t r i x ( ' 1 . 02 . 0 ;3 . 04 . 0 ' ) > > >A [ [1 . 2 . ] [3 . 4 . ] ] > > >t y p e ( A ) #f i l ew h e r ec l a s si sd e f i n e d < c l a s s' n u m p y . m a t r i x l i b . d e f m a t r i x . m a t r i x ' > > > >A . T #t r a n s p o s e [ [1 . 3 . ] [2 . 4 . ] ] > > >X=m a t r i x ( ' 5 . 07 . 0 ' ) > > >Y=X . T > > >Y [ [ 5 . ] [ 7 . ] ] > > >p r i n tA * Y #m a t r i xm u l t i p l i c a t i o n [ [ 1 9 . ] [ 4 3 . ] ] > > >p r i n tA . I #i n v e r s e [ [ 2 . 1 .] [1 . 50 . 5 ] ] > > >s o l v e ( A ,Y ) #s o l v i n gl i n e a re q u a t i o n m a t r i x ( [ [ 3 . ] , [4 . ] ] )

Indexing: Comparing Matrices and 2D Arrays


Note that there are some important differences between NumPy arrays and matrices. NumPy provides two fundamental objects: an N-dimensional array object and a universal function object. Other objects are built on top of these. In particular, matrices are 2-dimensional array objects that inherit from the NumPy array object. For both arrays and matrices, indices must consist of a proper combination of one or more of the following: integer scalars, ellipses, a list of integers or boolean values, a tuple of integers or boolean values, and a 1-dimensional array of integer or boolean values. A matrix can be used as an index for matrices, but commonly an array, list, or other form is needed to accomplish a given task. As usual in Python, indexing is zero-based. Traditionally we represent a 2D array or matrix as a rectangular array of rows and columns, where movement along axis 0 is movement across rows, while movement along axis 1 is movement across columns.
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 25/30

8/28/13

Tentative NumPy Tutorial -

Let's make an array and matrix to slice:


> > >A=a r a n g e ( 1 2 ) > > >A a r r a y ( [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,1 0 ,1 1 ] ) > > >A . s h a p e=( 3 , 4 ) > > >M=m a t ( A . c o p y ( ) ) > > >p r i n tt y p e ( A ) , " " , t y p e ( M ) < t y p e' n u m p y . n d a r r a y ' > < c l a s s' n u m p y . c o r e . d e f m a t r i x . m a t r i x ' > > > >p r i n tA [ [0 1 2 3 ] [4 5 6 7 ] [8 91 01 1 ] ] > > >p r i n tM [ [0 1 2 3 ] [4 5 6 7 ] [8 91 01 1 ] ]

Now, let's take some simple slices. Basic slicing uses slice objects or integers. For example, the evaluation of A[:] and M[:] will appear familiar from Python indexing, however it is important to note that slicing NumPy arrays does *not* make a copy of the data; slicing provides a new view of the same data.
> > >p r i n tA [ : ] ;p r i n tA [ : ] . s h a p e [ [0 1 2 3 ] [4 5 6 7 ] [8 91 01 1 ] ] ( 3 ,4 ) > > >p r i n tM [ : ] ;p r i n tM [ : ] . s h a p e [ [0 1 2 3 ] [4 5 6 7 ] [8 91 01 1 ] ] ( 3 ,4 )

Now for something that differs from Python indexing: you may use comma-separated indices to index along multiple axes at the same time.
> > >p r i n tA [ : , 1 ] ;p r i n tA [ : , 1 ] . s h a p e [ 159 ] ( 3 , ) > > >p r i n tM [ : , 1 ] ;p r i n tM [ : , 1 ] . s h a p e [ [ 1 ] [ 5 ] [ 9 ] ] ( 3 ,1 )

Notice the difference in the last two results. Use of a single colon for the 2D array produces
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 26/30

8/28/13

Tentative NumPy Tutorial -

a 1-dimensional array, while for a matrix it produces a 2-dimensional matrix. A slice of a matrix will always produce a matrix. For example, a slice M[2,:] produces a matrix of shape (1,4). In contrast, a slice of an array will always produce an array of the lowest possible dimension. For example, if C were a 3-dimensional array, C[...,1] produces a 2D array while C[1,:,1] produces a 1-dimensional array. From this point on, we will show results only for the array slice if the results for the corresponding matrix slice are identical. Lets say that we wanted the 1st and 3rd column of an array. One way is to slice using a list:
> > >A [ : , [ 1 , 3 ] ] a r r a y ( [ [1 , 3 ] , [5 , 7 ] , [9 ,1 1 ] ] )

A slightly more complicated way is to use the take() method:


> > >A [ : , ] . t a k e ( [ 1 , 3 ] , a x i s = 1 ) a r r a y ( [ [1 , 3 ] , [5 , 7 ] , [9 ,1 1 ] ] )

If we wanted to skip the first row, we could use:


> > >A [ 1 : , ] . t a k e ( [ 1 , 3 ] , a x i s = 1 ) a r r a y ( [ [5 , 7 ] , [9 ,1 1 ] ] )

Or we could simply use A[1:,[1,3]]. Yet another way to slice the above is to use a cross product:
> > >A [ i x _ ( ( 1 , 2 ) , ( 1 , 3 ) ) ] a r r a y ( [ [5 , 7 ] , [9 ,1 1 ] ] )

For the reader's convenience, here is our array again:


> > >p r i n tA [ [0 1 2 3 ] [4 5 6 7 ] [8 91 01 1 ] ]

Now let's do something a bit more complicated. Lets say that we want to retain all columns where the first row is greater than 1. One way is to create a boolean index:
> > >A [ 0 , : ] > 1 a r r a y ( [ F a l s e ,F a l s e ,T r u e ,T r u e ] ,d t y p e = b o o l ) > > >A [ : , A [ 0 , : ] > 1 ]
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 27/30

8/28/13

Tentative NumPy Tutorial -

a r r a y ( [ [2 , 3 ] , [6 , 7 ] , [ 1 0 ,1 1 ] ] )

Just what we wanted! But indexing the matrix is not so convenient.


> > >M [ 0 , : ] > 1 m a t r i x ( [ [ F a l s e ,F a l s e ,T r u e ,T r u e ] ] ,d t y p e = b o o l ) > > >M [ : , M [ 0 , : ] > 1 ] m a t r i x ( [ [ 2 ,3 ] ] )

The problem of course is that slicing the matrix slice produced a matrix. But matrices have a convenient 'A' attribute whose value is the array representation, so we can just do this instead:
> > >M [ : , M . A [ 0 , : ] > 1 ] m a t r i x ( [ [2 , 3 ] , [6 , 7 ] , [ 1 0 ,1 1 ] ] )

If we wanted to conditionally slice the matrix in two directions, we must adjust our strategy slightly. Instead of
> > >A [ A [ : , 0 ] > 2 , A [ 0 , : ] > 1 ] a r r a y ( [6 ,1 1 ] ) > > >M [ M . A [ : , 0 ] > 2 , M . A [ 0 , : ] > 1 ] m a t r i x ( [ [6 ,1 1 ] ] )

we need to use the cross product 'ix_':


> > >A [ n u m p y . i x _ ( A [ : , 0 ] > 2 , A [ 0 , : ] > 1 ) ] a r r a y ( [ [6 , 7 ] , [ 1 0 ,1 1 ] ] ) > > >M [ n u m p y . i x _ ( M . A [ : , 0 ] > 2 , M . A [ 0 , : ] > 1 ) ] m a t r i x ( [ [6 , 7 ] , [ 1 0 ,1 1 ] ] )

Tricks and Tips


Here we give a list of short and useful tips.

"Automatic" Reshaping
To change the dimensions of an array, you can omit one of the sizes which will then be deduced automatically:
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 28/30

8/28/13

Tentative NumPy Tutorial -

> > >a=a r a n g e ( 3 0 ) > > >a . s h a p e=2 , 1 , 3 #1m e a n s" w h a t e v e ri sn e e d e d " > > >a . s h a p e ( 2 ,5 ,3 ) > > >a a r r a y ( [ [ [0 , 1 , 2 ] , [3 , 4 , 5 ] , [6 , 7 , 8 ] , [9 ,1 0 ,1 1 ] , [ 1 2 ,1 3 ,1 4 ] ] , [ [ 1 5 ,1 6 ,1 7 ] , [ 1 8 ,1 9 ,2 0 ] , [ 2 1 ,2 2 ,2 3 ] , [ 2 4 ,2 5 ,2 6 ] , [ 2 7 ,2 8 ,2 9 ] ] ] )

Vector Stacking
How do we construct a 2D array from a list of equally-sized row vectors? In MATLAB this is quite easy: if xand yare two vectors of the same length you only need do m = [ x ; y ] . In NumPy this works via the functions c o l u m n _ s t a c k ,d s t a c k ,h s t a c kand v s t a c k , depending on the dimension in which the stacking is to be done. For example:
x=a r a n g e ( 0 , 1 0 , 2 ) y=a r a n g e ( 5 ) m=v s t a c k ( [ x , y ] ) x y=h s t a c k ( [ x , y ] ) #x = ( [ 0 , 2 , 4 , 6 , 8 ] ) #y = ( [ 0 , 1 , 2 , 3 , 4 ] ) #m = ( [ [ 0 , 2 , 4 , 6 , 8 ] , # [ 0 , 1 , 2 , 3 , 4 ] ] ) #x y= ( [ 0 , 2 , 4 , 6 , 8 , 0 , 1 , 2 , 3 , 4 ] )

The logic behind those functions in more than two dimensions can be strange. See also NumPy for Matlab Users and add your new findings there.

Histograms
The NumPy h i s t o g r a mfunction applied to an array returns a pair of vectors: the histogram of the array and the vector of bins. Beware: m a t p l o t l i balso has a function to build histograms (called h i s t , as in Matlab) that differs from the one in NumPy. The main difference is that p y l a b . h i s tplots the histogram automatically, while n u m p y . h i s t o g r a m only generates the data.
i m p o r tn u m p y i m p o r tp y l a b #B u i l dav e c t o ro f1 0 0 0 0n o r m a ld e v i a t e sw i t hv a r i a n c e0 . 5 ^ 2a n d m e a n2 m u ,s i g m a=2 ,0 . 5 v=n u m p y . r a n d o m . n o r m a l ( m u , s i g m a , 1 0 0 0 0 )
wiki.scipy.org/Tentative_NumPy_Tutorial?action=print 29/30

8/28/13

Tentative NumPy Tutorial -

#P l o tan o r m a l i z e dh i s t o g r a mw i t h5 0b i n s p y l a b . h i s t ( v ,b i n s = 5 0 ,n o r m e d = 1 ) #m a t p l o t l i bv e r s i o n( p l o t ) p y l a b . s h o w ( ) #C o m p u t et h eh i s t o g r a mw i t hn u m p ya n dt h e np l o ti t ( n ,b i n s )=n u m p y . h i s t o g r a m ( v ,b i n s = 5 0 ,n o r m e d = T r u e ) #N u m P yv e r s i o n ( n op l o t ) p y l a b . p l o t ( . 5 * ( b i n s [ 1 : ] + b i n s [ : 1 ] ) ,n ) p y l a b . s h o w ( )

References
The Python tutorial. The Numpy Example List. The nonexistent NumPy Tutorial at scipy.org, where we can find the old Numeric documentation. The Guide to NumPy book. The SciPy Tutorial and a SciPy course online NumPy for Matlab Users. A matlab, R, IDL, NumPy/SciPy dictionary.
Tentative NumPy Tutorial (last edited 2012-06-09 13:21:58 by ArunRangarajan)

wiki.scipy.org/Tentative_NumPy_Tutorial?action=print

30/30

S-ar putea să vă placă și