Kittens&DragonsNumPyTutorialpresentedatSciPy2010Stéfan van der WaltStellenbosch University, South Africa28 June 2010 Setup• Tutorial layout• Setup import numpy as npThe NumPy ndarrayprint np.__version__ # version 1.3 or greaterBroadcastingIndexingStructured arrays Point your browser to the problem set atUniversal functionshttp://mentat.za.net/numpy/kittensThe __array_interface__OptimisationUpdate, wrap-up & questions9th Python in Science Conference (SciPy) 2010 3 / 48• Tutorial layout• SetupThe NumPy ndarray• ndarray• Rating: Kitten• Data buffers• Dimensions• Data-type• Strides• Flags• Base Pointer• TheNumPyndarrayBroadcastingIndexingStructured arraysUniversal functionsThe __array_interface__OptimisationUpdate, wrap-up & questions9th Python in Science Conference (SciPy) 2010 4 / 48Revision: Structureofanndarray• Tutorial layout Taking a look atnumpy/core/include/numpy/ndarraytypes.h:• SetupThe NumPy ndarray• ndarray• Rating: Kitten typedef struct PyArrayObject {• Data buffersPyObject_HEAD• Dimensions• Data-type char *data; /* pointer to data buffer */• Stridesint nd; /* number of dimensions */• Flags• Base Pointer npy_intp *dimensions; /* size in each dimension */•npy_intp *strides; /* bytes to jump to getBroadcasting* to the next element inIndexing* each ...
Taking a look atmunc/yptypyseh./ydnraarude/numpore/incl
fereneec*s}/P
P y A r r a y D e s c r * d e s c r ; _ intf l a g s ; P y O b e c t * w e a k r e f l i s t ; j y A r r a y O b e c t ; j
t y p e d e f s tj r u c t {P y A r r a y O b c t e j P y O b e c t _ H E A D c h a r* d a t a ; intnd ; n y i n t * d i m e n s i o n s ; npy_i n tp* s t r i d e s ;
9th Python in Science Conference (SciPy) 2010
y O b j e c t * b a s e ;
:
//*krearwFopysetrto*t/rtcuags*/*Fl
A homoge eous container n
c h a r* d a t a ;
/*pointertodatabuffer*/
Data is just a pointer to bytes in memory:
IIII
n[16]:x=np.array([1,2,3])
n [ 2 2 ] : x. d t y p e Out t y p e ( d[ 2 2 ] :’ i n t 3 2 ’) b y t e s# 4
n [ 1 8 ] : x . _ _ a r r a y _ i n t e r f a c e _ _ [’ d a t a ’] Out , 2 6 3 1 6 6 2 4 a l s e ) F ([ 1 8 ] :
n [ 2 1 ] : str ( x . d a t a ) Out[ 2 1 ] : ’’ \ x01 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00
9th Python in Science Conference (SciPy) 2010
7 / 48
²Tutorial layout ²ptuSe The NumPy ndarray ²naydarr ²Rating: Kitten ²ata buffers D ²neisDminos ²eyp-taDat ²Stsderi ²Flags ²Base Pointer ²
n [ 3 ] : x = np . a r r a y ( [ ] ) n [ 4 ] : x. s h a p e Out[4(0]:,)
n [ 5 ] : np . a r r a y ( 0 ) . s h a p e Out[5]:()
/*numberofdimensions*/ /*sizeineachdimension*/
n[8]:x=np.random.random((3,2,3,3)) In [ 9 ] : x. s h a p e Out)3,3,3(2,9[:] In 1 0 ] : x . n d i m [ Out[ 1 0 ] : 4
9th Python in Science Conference (SciPy) 2010
8 / 48
Datatypedescriptors
P y A r r a y _ D e s c r * d e s c r ;/*Poi*/eptsurtctnreotyt
Common types in include int, oat, bool:
III
n [ 1 9 ] : np . a r r a y ([ -1 , 0 , 1] , d t y p e = int ) Out1[a:]9ayrr-1([,1,0]) n [ 2 0 ] : np . a r r a y ([ -1 , 0 , 1] , d t y p e = f l o a t ) Out , 0. . ] ) 1 r r a y ([ -1. ,[ 2 0 ] : a n [ 2 1 ] : np . a r r a y ([ -1 , 0 , 1] , d t y p e = b o o l ) Oute = b o o l ) t y p d[ 2 1 ] : a False , T r u e ] , r r a y ([ True ,
Eachiteminthearrayhastohavethesametype(occupyaxednrofbytesin memory), but that does not mean a type has to consist of a single item:
I
. n[2]:dt=np.dtype([(’ v a l u e ’, np int ) , (’ s t a t u s ’, n[3]:np.array([(0,True),(1,False)],dtype=dt) Out[ 3 ] : array([(0,True),(1,False)], dtype=[(’ v a l u e ’,’ < i4 ’) , (’ s t a t u s ’,’ | b1 ’)])
n [ 3 7 ] : x = np . a r a n g e ( 1 2 ) . r e s h a p e ( ( 3 , 4 ) )
n [ 3 8 ] : x Out[ 3 8 ] : a r r a y ([[ 0 , 1 , 2 , 3] , [ 4 , 5 , 6 , 7] , [ 8 , 9 , 10 , 1 1 ] ] )
n [ 3 9 ] : x. d t y p e Out t y p e ( d[ 3 9 ] :’ i n t 3 2 ’)
n [ 4 0 ] : x . d t y p e . i t e m s i z e Out[ 4 0 ] : 4
n [ 4 1 ] : x . s t r i d e s Out,416)4[(:]1 t e m s i z e ) , it e m s i z e(4* i # k i p _ b y t e s _ c o l s ,_ b y t e s _ r o w( s k i p
9th Python in Science Conference (SciPy) 2010
)
10 / 48
Flags
intf l a g s ;
In [ 6 6 ] : x = np . a r r a y ([1 , 2 , 3
In [ 6 7 ] : x . f l a g s Out[ 6 7 ] : C C O N T I O S T r u e F_O N T IG UOUS:T r u e C O N D A T A T r u e W : W R I T E A B L E : T r u e A L I G N E D : T r u e U P D A T E I F C O P Y : F a l s e
In [ 6 8 ] : z . f l a g s Out[ 6 8 ] : C C O N T I O S F a l s e F_C O N T IG UOUS:F a l s e O W N D A T A : F a l s e W R I T E A B L E : T r u e A L I G N E : T r u e D U P D A T E I F C O P Y : F a l s e
9th Python in Science Conference (SciPy) 2010
#])
/*Flags*/
C - c o n t i g u o u s F o r t r a n - c o n t i g u o u s are we r e s p o n s i b l e for m e m o r y h a n d l i n g ? ay we c h a n g e the d a t a ? m a p p r o p r i a t e h a r d w a r e a l i g n m e n t u p d a t e b a s e on d e a l l o c a t i o n ?