Guide To NumPy

Transcription

Guide to NumPyTravis E. Oliphant, PhDDec 7, 2006This book is under restricted distribution using a Market-Determined, Temporary, Distribution-Restriction (MDTDR) system (see http://www.trelgol.com) untilOctober 31, 2010 at the latest. If you receive this book, you are asked not to copy itin any form (electronic or paper) until the temporary distribution-restriction lapses.If you have multiple users at an institution, you should either share a single copyusing some form of digital library check-out, or buy multiple copies. The morecopies purchased, the sooner the documentation can be released from this inconvenient distribution restriction. After October 31, 2010 this book may be freelycopied in any format and used as source material for other books as long as acknowledgement of the original author is given. Your support of this temporarydistribution restriction plays an essential role in allowing the author and others likehim to produce more quality books and software.1

ContentsINumPy from Python121 Origins of NumPy132 Object Essentials182.12.2Data-Type Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . .Basic indexing (slicing) . . . . . . . . . . . . . . . . . . . . . . . . . .19232.3Memory Layout of ndarray . . . . . . . . . . . . . . . . . . . . . .2.3.1 Contiguous Memory Layout . . . . . . . . . . . . . . . . . . .26262.42.3.2 Non-contiguous memory layout . . . . . . . . . . . . . . . . .Universal Functions for arrays . . . . . . . . . . . . . . . . . . . . . .28302.52.6Summary of new features . . . . . . . . . . . . . . . . . . . . . . . .Summary of differences with Numeric . . . . . . . . . . . . . . . . .32342.6.1First-step changes . . . . . . . . . . . . . . . . . . . . . . . .342.6.22.6.3Second-step changes . . . . . . . . . . . . . . . . . . . . . . .Updating code that uses Numeric using alter codeN . . . . .37382.6.4 Changes to think about . . . . . . . . . . . . . . . . . . . . .Summary of differences with Numarray . . . . . . . . . . . . . . . .39402.7.1First-step changes . . . . . . . . . . . . . . . . . . . . . . . .2.7.1.1 Import changes . . . . . . . . . . . . . . . . . . . . .41412.7.22.7.1.2 Attribute and method changes . . . . . . . . . . . .Second-step changes . . . . . . . . . . . . . . . . . . . . . . .42432.7.3Additional Extension modules . . . . . . . . . . . . . . . . . .432.73 The Array Object3.145ndarray Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.1 Memory Layout attributes . . . . . . . . . . . . . . . . . . . .45453.1.249Data Type attributes. . . . . . . . . . . . . . . . . . . . . .2

3.1.33.23.33.4Other attributes . . . . . . . . . . . . . . . . . . . . . . . . .503.1.4 Array Interface attributes . . . . . . . . . . . . . . . . . . . .ndarray Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .52543.2.13.2.2Array conversion . . . . . . . . . . . . . . . . . . . . . . . . .Array shape manipulation . . . . . . . . . . . . . . . . . . . .54593.2.33.2.4Array item selection and manipulation . . . . . . . . . . . . .Array calculation . . . . . . . . . . . . . . . . . . . . . . . . .6165Array Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.1 Methods for standard library functions . . . . . . . . . . . . .71713.3.23.3.3Basic customization . . . . . . . . . . . . . . . . . . . . . . .Container customization . . . . . . . . . . . . . . . . . . . . .72743.3.4Arithmetic customization . . . . . . . . . . . . . . . . . . . .3.3.4.1 Binary . . . . . . . . . . . . . . . . . . . . . . . . .75753.3.4.23.3.4.3In-place . . . . . . . . . . . . . . . . . . . . . . . . .Unary operations . . . . . . . . . . . . . . . . . . .7778Array indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .793.4.13.4.2Basic Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . .Advanced selection . . . . . . . . . . . . . . . . . . . . . . . .79813.4.2.13.4.2.2Integer . . . . . . . . . . . . . . . . . . . . . . . . .Boolean . . . . . . . . . . . . . . . . . . . . . . . . .8183Flat Iterator indexing . . . . . . . . . . . . . . . . . . . . . .843.4.34 Basic Routines854.14.2Creating arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Operations on two or more arrays . . . . . . . . . . . . . . . . . . . .85904.34.4Printing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functions redundant with methods . . . . . . . . . . . . . . . . . .93944.5Dealing with data types . . . . . . . . . . . . . . . . . . . . . . . . .945 Additional Convenience Routines965.1Shape functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5.25.3Basic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Polynomial functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1085.45.5Set Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Array construction using index tricks . . . . . . . . . . . . . . . . . . 1125.65.7Other indexing devices . . . . . . . . . . . . . . . . . . . . . . . . . . 115Two-dimensional functions . . . . . . . . . . . . . . . . . . . . . . . . 116396

5.8More data type functions . . . . . . . . . . . . . . . . . . . . . . . . 1185.9 Functions that behave like ufuncs . . . . . . . . . . . . . . . . . . . . 1215.10 Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . 1215.11 Utility functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1246 Scalar objects1266.16.2Attributes of array scalars . . . . . . . . . . . . . . . . . . . . . . . . 127Methods of array scalars . . . . . . . . . . . . . . . . . . . . . . . . . 1296.3Defining New Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 1307 Data-type (dtype) Objects1317.17.2Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337.3Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1378 Standard Classes1388.1Special attributes and methods recognized by NumPy . . . . . . . . 1398.28.3Matrix Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Memory-mapped-file arrays . . . . . . . . . . . . . . . . . . . . . . . 1428.48.5Character arrays (numpy.char) . . . . . . . . . . . . . . . . . . . . . 143Record Arrays (numpy.rec) . . . . . . . . . . . . . . . . . . . . . . . 1448.68.7Masked Arrays (numpy.ma) . . . . . . . . . . . . . . . . . . . . . . . 148Standard container class . . . . . . . . . . . . . . . . . . . . . . . . . 1498.8Array Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1498.8.1 Default iteration . . . . . . . . . . . . . . . . . . . . . . . . . 1508.8.28.8.3Flat iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . 150N-dimensional enumeration . . . . . . . . . . . . . . . . . . . 1518.8.4Iterator for broadcasting . . . . . . . . . . . . . . . . . . . . . 1519 Universal Functions9.1153Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1539.1.19.1.2Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Output type determination . . . . . . . . . . . . . . . . . . . 1549.1.39.1.4Use of internal buffers . . . . . . . . . . . . . . . . . . . . . . 155Error handling . . . . . . . . . . . . . . . . . . . . . . . . . . 1559.29.1.5 Optional keyword arguments . . . . . . . . . . . . . . . . . . 156Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1579.3Casting Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1584

9.49.5Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1599.4.19.4.2Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1619.4.39.4.4Reduceat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Outer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Available ufuncs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1649.5.1 Math operations . . . . . . . . . . . . . . . . . . . . . . . . . 1649.5.29.5.3Trigonometric functions . . . . . . . . . . . . . . . . . . . . . 167Bit-twiddling functions . . . . . . . . . . . . . . . . . . . . . . 1689.5.49.5.5Comparison functions . . . . . . . . . . . . . . . . . . . . . . 169Floating functions . . . . . . . . . . . . . . . . . . . . . . . . 17110 Basic Modules17410.1 Linear Algebra (linalg) . . . . . . . . . . . . . . . . . . . . . . . . 17410.2 Discrete Fourier Transforms (fft) . . . . . . . . . . . . . . . . . . . 17710.3 Random Numbers (random) . . . . . . . . . . . . . . . . . . . . . . 18110.3.1 Discrete Distributions . . . . . . . . . . . . . . . . . . . . . . 18110.3.2 Continuous Distributions . . . . . . . . . . . . . . . . . . . . 18410.3.3 Miscellaneous utilities . . . . . . . . . . . . . . . . . . . . . . 19010.4 Matrix-specific functions (matlib) . . . . . . . . . . . . . . . . . . . . 19110.5 Ctypes utiltity functions (ctypeslib) . . . . . . . . . . . . . . . . . . 19111 Testing and Packaging19211.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19211.2 NumPy Distutils . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19411.2.1 misc util . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19411.2.2 Other modules . . . . . . . . . . . . . . . . . . . . . . . . . . 20211.3 Conversion of .src files . . . . . . . . . . . . . . . . . . . . . . . . . . 20311.3.1 Fortran files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20311.3.1.1 Named repeat rule . . . . . . . . . . . . . . . . . . . 20411.3.1.2 Short repeat rule . . . . . . . . . . . . . . . . . . . . 20411.3.1.3 Pre-defined names . . . . . . . . . . . . . . . . . . . 20411.3.2 Other files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205IIC-API20612 New Python Types and C-Structures5207

12.1 New Python Types Defined . . . . . . . . . . . . . . . . . . . . . . . 20812.1.1 PyArray Type . . . . . . . . . . . . . . . . . . . . . . . . . . 20912.1.2 PyArrayDescr Type . . . . . . . . . . . . . . . . . . . . . . . 21012.1.3 PyUFunc Type . . . . . . . . . . . . . . . . . . . . . . . . . . 21812.1.4 PyArrayIter Type . . . . . . . . . . . . . . . . . . . . . . . . 22012.1.5 PyArrayMultiIter Type . . . . . . . . . . . . . . . . . . . . . 22212.1.6 PyArrayFlags Type . . . . . . . . . . . . . . . . . . . . . . . 22312.1.7 ScalarArrayTypes . . . . . . . . . . . . . . . . . . . . . . . . 22312.2 Other C-Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22412.2.1 PyArray Dims . . . . . . . . . . . . . . . . . . . . . . . . . . 22412.2.2 PyArray Chunk . . . . . . . . . . . . . . . . . . . . . . . . . . 22412.2.3 PyArrayInterface . . . . . . . . . . . . . . . . . . . . . . . . . 22512.2.4 Internally used structures . . . . . . . . . . . . . . . . . . . . 22612.2.4.1 PyUFuncLoopObject . . . . . . . . . . . . . . . . . 22712.2.4.2 PyUFuncReduceObject . . . . . . . . . . . . . . . . 22712.2.4.3 PyUFunc Loop1d . . . . . . . . . . . . . . . . . . . 22712.2.4.4 PyArrayMapIter Type13 Complete API. . . . . . . . . . . . . . . . 22722813.1 Configuration defines . . . . . . . . . . . . . . . . . . . . . . . . . . . 22813.1.1 Guaranteed to be defined . . . . . . . . . . . . . . . . . . . . 22813.1.2 Possible defines . . . . . . . . . . . . . . . . . . . . . . . . . . 22913.2 Array Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22913.2.1 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . 23013.2.2 Defines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23113.2.2.1 Max and min values for integers . . . . . . . . . . . 23113.2.2.2 Number of bits in data types . . . . . . . . . . . . . 23113.2.2.3 Bit-width references to enumerated typenums . . . . 23113.2.2.4 Integer that can hold a pointer . . . . . . . . . . . . 23213.2.3 C-type names . . . . . . . . . . . . . . . . . . . . . . . . . . . 23213.2.3.1 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . 23213.2.3.2 (Un)Signed Integer . . . . . . . . . . . . . . . . . . 23213.2.3.3 (Complex) Floating point . . . . . . . . . . . . . . . 23313.2.3.4 Bit-width names . . . . . . . . . . . . . . . . . . . . 23313.2.4 Printf Formatting . . . . . . . . . . . . . . . . . . . . . . . . 23313.3 Array API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23313.3.1 Array structure and data access6. . . . . . . . . . . . . . . . 233

13.3.1.1 Data access . . . . . . . . . . . . . . . . . . . . . . . 23513.3.2 Creating arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 23613.3.2.1 From scratch . . . . . . . . . . . . . . . . . . . . . . 23613.3.2.2 From other objects . . . . . . . . . . . . . . . . . . . 23813.3.3 Dealing with types . . . . . . . . . . . . . . . . . . . . . . . . 24413.3.3.1 General check of Python Type . . . . . . . . . . . . 24413.3.3.2 Data-type checking . . . . . . . . . . . . . . . . . . 24613.3.3.3 Converting data types . . . . . . . . . . . . . . . . . 24913.3.3.4 New data types . . . . . . . . . . . . . . . . . . . . 25113.3.3.5 Special functions for PyArray OBJECT . . . . . . . 25213.3.4 Array flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25313.3.4.1 Basic Array Flags . . . . . . . . . . . . . . . . . . . 25313.3.4.2 Combinations of array flags . . . . . . . . . . . . . . 25413.3.4.3 Flag-like constants . . . . . . . . . . . . . . . . . . 25413.3.4.4 Flag checking . . . . . . . . . . . . . . . . . . . . . . 25513.3.5 Array method alternative API . . . . . . . . . . . . . . . . . 25613.3.5.1 Conversion . . . . . . . . . . . . . . . . . . . . . . . 25613.3.5.2 Shape Manipulation . . . . . . . . . . . . . . . . . . 25813.3.5.3 Item selection and manipulation . . . . . . . . . . . 26013.3.5.4 Calculation . . . . . . . . . . . . . . . . . . . . . . . 26313.3.6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26513.3.6.1 Array Functions . . . . . . . . . . . . . . . . . . . . 26513.3.6.2 Other functions . . . . . . . . . . . . . . . . . . . . 26713.3.7 Array Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . 26813.3.8 Broadcasting (multi-iterators) . . . . . . . . . . . . . . . . . . 26913.3.9 Array Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . 27113.3.10 Data-type descriptors . . . . . . . . . . . . . . . . . . . . . . 27313.3.11 Conversion Utilities . . . . . . . . . . . . . . . . . . . . . . . 27513.3.11.1 For use with PyArg ParseTuple . . . . . . . . . . 27513.3.11.2 Other conversions . . . . . . . . . . . . . . . . . . . 27713.3.12 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . 27813.3.12.1 Importing the API . . . . . . . . . . . . . . . . . . . 27813.3.12.2 Internal Flexibility . . . . . . . . . . . . . . . . . . . 27913.3.12.3 Memory management . . . . . . . . . . . . . . . . . 28013.3.12.4 Threading support . . . . . . . . . . . . . . . . . . . 28013.3.12.5 Priority . . . . . . . . . . . . . . . . . . . . . . . . . 28213.3.12.6 Default buffers . . . . . . . . . . . . . . . . . . . . . 2827

13.3.12.7 Other constants . . . . . . . . . . . . . . . . . . . . 28213.3.12.8 Miscellaneous Macros . . . . . . . . . . . . . . . . . 28313.3.12.9 Enumerated Types . . . . . . . . . . . . . . . . . . . 28413.4 UFunc API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28413.4.1 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28413.4.2 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28513.4.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28513.4.4 Generic functions . . . . . . . . . . . . . . . . . . . . . . . . . 28813.5 Importing the API . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29014 How to extend NumPy29214.1 Writing an extension module . . . . . . . . . . . . . . . . . . . . . . 29214.2 Required subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . 29314.3 Defining functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29414.3.1 Functions without keyword arguments . . . . . . . . . . . . . 29514.3.2 Functions with keyword arguments . . . . . . . . . . . . . . . 29614.3.3 Reference counting . . . . . . . . . . . . . . . . . . . . . . . . 29714.4 Dealing with array objects . . . . . . . . . . . . . . . . . . . . . . . . 29814.4.1 Converting an arbitrary sequence object . . . . . . . . . . . . 29914.4.2 Creating a brand-new ndarray . . . . . . . . . . . . . . . . . 30114.4.3 Getting at ndarray memory and accessing elements of thendarray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30314.5 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30315 Beyond the Basics30515.1 Iterating over elements in the array . . . . . . . . . . . . . . . . . . . 30515.1.1 Basic Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . 30515.1.2 Iterating over all but one axis . . . . . . . . . . . . . . . . . . 30715.1.3 Iterating over multiple arrays . . . . . . . . . . . . . . . . . . 30715.1.4 Broadcasting over multiple arrays . . . . . . . . . . . . . . . 30815.2 Creating a new universal function . . . . . . . . . . . . . . . . . . . . 30915.3 User-defined data-types . . . . . . . . . . . . . . . . . . . . . . . . . 31215.3.1 Adding the new data-type . . . . . . . . . . . . . . . . . . . . 31215.3.2 Registering a casting function . . . . . . . . . . . . . . . . . . 31315.3.3 Registering coercion rules . . . . . . . . . . . . . . . . . . . . 31415.3.4 Registering a ufunc loop . . . . . . . . . . . . . . . . . . . . . 31515.4 Subtyping the ndarray in C . . . . . . . . . . . . . . . . . . . . . . . 3158

15.4.1 Creating sub-types . . . . . . . . . . . . . . . . . . . . . . . . 31615.4.2 Specific features of ndarray sub-typing . . . . . . . . . . . . . 31715.4.2.1 The array finalize method . . . . . . . . . . . . . 31715.4.2.2 The15.4.2.3 Thearray priority attribute . . . . . . . . . . . . 318array wrap method . . . . . . . . . . . . . . 31816 Using Python as glue31916.1 Calling other compiled libraries from Python . . . . . . . . . . . . . 32016.2 Hand-generated wrappers . . . . . . . . . . . . . . . . . . . . . . . . 32016.3 f2py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32116.3.1 Creating source for a basic extension module . . . . . . . . . 32116.3.2 Creating a compiled extension module . . . . . . . . . . . . . 32216.3.3 Improving the basic interface . . . . . . . . . . . . . . . . . . 32316.3.4 Inserting directives in Fortran source . . . . . . . . . . . . . . 32416.3.5 A filtering example . . . . . . . . . . . . . . . . . . . . . . . . 32516.3.6 Calling f2py from Python . . . . . . . . . . . . . . . . . . . . 32616.3.7 Automatic extension module generation . . . . . . . . . . . . 32616.3.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32716.4 weave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32816.4.1 Speed up code involving arrays (also see scipy.numexpr) . . . 32816.4.2 Inline C-code . . . . . . . . . . . . . . . . . . . . . . . . . . . 32916.4.3 Simplify creation of an extension module . . . . . . . . . . . 33116.4.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33216.5 Pyrex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33216.5.1 Pyrex-add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33316.5.2 Pyrex-filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33516.5.3 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33616.6 ctypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33716.6.1 Having a shared library . . . . . . . . . . . . . . . . . . . . . 33816.6.2 Loading the shared library. . . . . . . . . . . . . . . . . . . 33916.6.3 Converting arguments . . . . . . . . . . . . . . . . . . . . . . 34016.6.4 Calling the function . . . . . . . . . . . . . . . . . . . . . . . 34116.6.5 Complete example . . . . . . . . . . . . . . . . . . . . . . . . 34216.6.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34616.7 Additional tools you may find useful . . . . . . . . . . . . . . . . . . 34716.7.1 SWIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34716.7.2 SIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3489

16.7.3 Boost Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 34816.7.4 Instant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34916.7.5 PyInline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35016.7.6 PyFort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35017 Code Explanations35117.1 Memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35117.2 Data-type encapsulation . . . . . . . . . . . . . . . . . . . . . . . . 35217.3 N-D Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35217.4 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35317.5 Array Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35417.6 Advanced (“Fancy”) Indexing . . . . . . . . . . . . . . . . . . . . . . 35417.6.1 Fancy-indexing check . . . . . . . . . . . . . . . . . . . . . . . 35517.6.2 Fancy-indexing implementation . . . . . . . . . . . . . . . . . 35617.6.2.1 Creating the mapping object . . . . . . . . . . . . . 35617.6.2.2 Binding the mapping object . . . . . . . . . . . . . 35617.6.2.3 Getting (or Setting) . . . . . . . . . . . . . . . . . . 35717.7 Universal Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 35717.7.1 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35717.7.2 Function call . . . . . . . . . . . . . . . . . . . . . . . . . . . 35917.7.2.1 One Loop . . . . . . . . . . . . . . . . . . . . . . . . 35917.7.2.2 Strided Loop . . . . . . . . . . . . . . . . . . . . . . 36017.7.2.3 Buffered Loop . . . . . . . . . . . . . . . . . . . . . 36017.7.3 Final output manipulation . . . . . . . . . . . . . . . . . . . . 36117.7.4 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36117.7.4.1 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . 36117.7.4.2 Reduce . . . . . . . . . . . . . . . . . . . . . . . . . 36217.7.4.3 Accumulate . . . . . . . . . . . . . . . . . . . . . . . 36317.7.4.4 Reduceat . . . . . . . . . . . . . . . . . . . . . . . . 36310

List of Tables2.1Built-in array-scalar types corresponding to data-types for an ndarray. The bold-face types correspond to standard Python types. Theobject type is special because arrays with dtype ’O’ do not returnan array scalar on item access but instead return the actual objectreferenced in the array. . . . . . . . . . . . . . . . . . . . . . . . . . .223.13.2Attributes of the ndarray. . . . . . . . . . . . . . . . . . . . . . . .Array conversion methods . . . . . . . . . . . . . . . . . . . . . . . .46583.3Array item selection and shape manipulation methods. If axis is anargument, then the calculation is performed along that axis. An axis3.4value of None means the array is flattened before calculation proceeds. 66Array object calculation methods. If axis is an argument, then thecalculation is performed along that axis. An axis value of None meansthe array is flattened before calculation proceeds. All of these methods can take an optional out argument which can specify the outputarray to write the results into.6.1. . . . . . . . . . . . . . . . . . . . .70Array scalar types that inherit from basic Python types. The intcarray data type might also inherit from the IntType if it has the samenumber of bits as the int array data type on your platform. . . . . . 1279.1Universal function (ufunc) attributes. . . . . . . . . . . . . . . . . 15810.1 Functions in numpy.dual (both in NumPy and SciPy) . . . . . . . . 17411

Part INumPy from Python12

Chapter 1Origins of NumPyNumPy builds on (and is a successor to) the successful Numeric array object. Itsgoal is to create the corner-stone for a useful environment for scientific computing.In order to better understand the people surrounding NumPy and (its librarypackage) SciPy, I will explain a little about how SciPy and (current) NumPy originated. In 1998, as a graduate student studying biomedical imaging at the MayoClinic in Rochester, MN, I came across Python and its numerical extension (Numeric) while I was looking for ways to analyze large data sets for Magnetic Resonance Imaging and Ultrasound using a high-level language. I quickly fell in lovewith Python programming which is a remarkable statement to make about a programming language. If I had not seen others with the same view, I might haveseriously doubted my sanity. I became rather involved in the Numeric Python community, adding the C-API chapter to the Numeric documentation (for which PaulDubois graciously made me a co-author).As I progressed with my thesis work, programming in Python was so enjoyablethat I felt inhibited when I worked with other programming frameworks. As a result,when a task I needed to perform was not available in the core language, or in theNumeric extension, I looked around and found C or Fortran code that performedthe needed task, wrapped it into Python (either by hand or using SWIG), and usedthe new functionality in my programs.Along the way, I learned a great deal about the underlying structure of Numericand grew to admire it’s simple but elegant structures that grew out of the mechanismby which Python allows itself to be extended.13

NOTENumeric was originally written in 1995 largely by Jim Huguninwhile he was a graduate student at MIT. He received help frommany people including Jim Fulton, David Ascher, Paul Dubois,and Konrad Hinsen. These individuals and many others addedcomments, criticisms, and code which helped the Numeric extension reach stability. Jim Hugunin did not stay long as an activemember of the community — moving on to write Jython and, later,Iron Python.By operating in this need-it-make-it fashion I ended up with a substantial library of extension modules that helped Python Numeric become easier to usein a scientific setting. These early modules included raw input-output functions,a special function library, an integration library, an ordinary differential equationsolver, some least-squares optimizers, and sparse matrix solvers. While I was doingthis laborious work, Pearu Peterson noticed that a lot of the routines I was wrapping were written in Fortran and there was no simplified wrapping mechanism forFortran subroutines (like SWIG for C). He began the task of writing f2py whichmade it possible to easily wrap Fortran programs into Python. I helped him a littlebit, mostly with testing and contributing early function-call-back code, but he putforth the brunt of the work. His result was simply amazing to me. I’ve always beenimpressed with f2py, especially because I knew how much effort writing and maintaining extension modules could be. Anybody serious about scientific computingwith Python will appreciate that f2py is distributed along with NumPy.When I finished my Ph.D. in 2001, Eric Jones (who had recently completed hisPh.D. at Duke) contacted me because he had a collection of Python modules he haddeveloped as part of his thesis work as well. He wanted to combine his modules withmine into one super package. Together with Pearu Peterson we joined our efforts,and SciPy was born in 2001. Since then, many people have contributed modulecode to SciPy including Ed Schofield, Robert Cimrman, David M. Cooke, Charles(Chuck) Harris, Prabhu Ramachandran, Gary Strangman, Jean-Sebastien Roy, andFernando Perez. Others such as Travis Vaught, David Morrill, Jeff Whitaker, andLouis Luangkesorn have contributed testing and build support.At the start of 2005, SciPy was at release 0.3 and relatively stable for an earlyversion number. Part of the reason it was difficult to stabilize SciPy was that thearray object upon which SciPy builds was undergoing a bit of an upheaval. At about14

the same time as SciPy was being built, some Numeric users were hitting up againstthe limited capabilities of Numeric. In particular, the ability to deal with memorymapped files (and associated alignment and swapping issues), record arrays, andaltered error checking modes were important but limited or non-existent in Numeric.As a result, numarray was created by Perry Greenfield, Todd Miller, and Rick Whiteat the Space Science Telescope Institute as a replacement for Numeric. Numarrayused a very different implementation scheme as a mix of Python classes and Ccode (which led to slow downs in certain common uses). While improving somecapabilities, it was slow to pick up on the more advanced features of Numeric’suniversal functions (ufuncs) — never re-creating the C-API that SciPy dependedon. This made it difficult for SciPy to “convert” to numarray.Many newcomers to scientific computing with Python were told that numarraywas the future and started developing for it. Very useful tools were developedthat could not be used with Numeric (because of numarray’s change in C-API),and therefore could not be used easily in SciPy. This state of affairs was verydiscouraging for me personally as it left the community fragmented. Some developedfor numarray, others developed as part of SciPy. A few people even rejected adoptingPython for scientific computing entirely because of the split. In addition, I estimatethat quite a few Python users simply stayed away from both SciPy and numarray,leaving the community smaller than it could have been given the number of peoplethat use Python for science and engineering purposes.It should be recognized that the split was not intentional, but simply an outgrowth of the different and exacting demands of scientific computing users. Mydescribing these events should not be construed as assigning blame to anyone. Ivery much admi

venient distribution restriction. After October 31, 2010 this book may be freely copied in any format and used as source material for other books as long as ac-knowledgement of the original author is given. Your support of this temporary distribution restriction plays