1、NumPy ReferenceRelease 1.5.1Written by the NumPy communityNovember 18, 2010CONTENTS1 Array objects 31.1 The N-dimensional array (ndarray) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2、 . . . . . . . . . 731.3 Data type objects (dtype) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101.4 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1211.5 Standard array subclasses . . . . . . . . . . .
3、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1251.6 Masked arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2501.7 The Array Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4332 Universal f
4、unctions (ufunc) 4392.1 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4392.2 Output type determination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4402.3 Use of internal buffers . . . . . . . . . . . . .
5、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4402.4 Error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4402.5 Casting Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4432.6 ufunc
6、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4452.7 Available ufuncs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4523 Routines 4573.1 Array creation routines . . . . . . . . . . . . . . . . . . . .
7、. . . . . . . . . . . . . . . . . . . . . . 4573.2 Array manipulation routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4883.3 Indexing routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5223.4 Data type routines .
8、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5473.5 Input and output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5593.6 Discrete Fourier Transform (numpy.fft) . . . . . . . . . . . . . . . . . . . . . . . . . . .
9、. . . . 5793.7 Linear algebra (numpy.linalg) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5993.8 Random sampling (numpy.random) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6273.9 Sorting and searching . . . . . . . . . . . . . . . . . . . . . . . . . .
10、. . . . . . . . . . . . . . . . 6783.10 Logic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6913.11 Binary operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7073.12 Statistics . . . . . . . . .
11、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7153.13 Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7353.14 Functional programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12、 . . . 7943.15 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7993.16 Financial functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8123.17 Set routines . . . . . . . . . . . . . . . . . . . . .
13、. . . . . . . . . . . . . . . . . . . . . . . . . . . 8203.18 Window functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8253.19 Floating point error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8363.20 Masked ar
14、ray operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842i3.21 Numpy-specific help functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9623.22 Miscellaneous routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15、 . . . . . . . . . . . . . 9653.23 Test Support (numpy.testing) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9663.24 Asserts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9673.25 Mathematical functions with automatic do
16、main (numpy.emath) . . . . . . . . . . . . . . . . . . . 9773.26 Matrix library (numpy.matlib) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9773.27 Optionally Scipy-accelerated routines (numpy.dual) . . . . . . . . . . . . . . . . . . . . . . . . . 9773.28 Numarray compati
17、bility (numpy.numarray) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9783.29 Old Numeric compatibility (numpy.oldnumeric) . . . . . . . . . . . . . . . . . . . . . . . . . . 9783.30 C-Types Foreign Function Interface (numpy.ctypeslib) . . . . . . . . . . . . . . . . . . . . . . 9783.31
18、String operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9794 Packaging (numpy.distutils) 10154.1 Modules in numpy.distutils . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10154.2 Building Installable C libraries . . . . . .
19、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10264.3 Conversion of .src files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10275 Numpy C-API 10295.1 Python Types and C-Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20、. . . . 10295.2 System configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10435.3 Data Type API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10455.4 Array API . . . . . . . . . . . . . . . . . . . . . . .
21、 . . . . . . . . . . . . . . . . . . . . . . . . . 10475.5 UFunc API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10805.6 Generalized Universal Function API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10855.7 Numpy core lib
22、raries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10876 Numpy internals 10916.1 Numpy C Code Explanations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10916.2 Internal organization of numpy arrays . . . . . . . . . . . . . . .
23、 . . . . . . . . . . . . . . . . . . . 10986.3 Multidimensional Array Indexing Order Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10997 Acknowledgements 1101Bibliography 1103Python Module Index 1109Index 1111iiNumPy Reference, Release 1.5.1Release1.5DateNovember 18, 2010This refe
24、rence manual details functions, modules, and objects included in Numpy, describing what they are and whatthey do. For learning how to use NumPy, see also user.CONTENTS 1NumPy Reference, Release 1.5.12 CONTENTSCHAPTERONEARRAY OBJECTSNumPy provides an N-dimensional array type, the ndarray, which descr
25、ibes a collection of “items” of the same type.The items can be indexed using for example N integers.All ndarrays are homogenous: every item takes up the same size block of memory, and all blocks are interpreted inexactly the same way. How each item in the array is to be interpreted is specified by a
26、 separate data-type object, oneof which is associated with every array. In addition to basic types (integers, floats, etc.), the data type objects can alsorepresent data structures.An item extracted from an array, e.g., by indexing, is represented by a Python object whose type is one of the arraysca
27、lar types built in Numpy. The array scalars allow easy manipulation of also more complicated arrangements ofdata.Figure 1.1: Figure Conceptual diagram showing the relationship between the three fundamental objects used to de-scribe the data in an array: 1) the ndarray itself, 2) the data-type object
28、 that describes the layout of a single fixed-sizeelement of the array, 3) the array-scalar Python object that is returned when a single element of the array is accessed.1.1 The N-dimensional array (ndarray)An ndarray is a (usually fixed-size) multidimensional container of items of the same type and
29、size. The numberof dimensions and items in an array is defined by its shape, which is a tuple of N positive integers that specifythe sizes of each dimension. The type of items in the array is specified by a separate data-type object (dtype), one ofwhich is associated with each ndarray.As with other
30、container objects in Python, the contents of an ndarray can be accessed and modified by indexingor slicing the array (using, for example, N integers), and via the methods and attributes of the ndarray. Different3NumPy Reference, Release 1.5.1ndarrays can share the same data, so that changes made in
31、one ndarray may be visible in another. That is, anndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray.ndarrays can also be views to memory owned by Python strings or objects implementing the buffer or arrayinterfaces.ExampleA 2-dimension
32、al array of size 2 x 3, composed of 4-byte integer elements: x = np.array(1, 2, 3, 4, 5, 6, np.int32) type(x) x.shape(2, 3) x.dtypedtype(int32)The array can be indexed using Python container-like syntax: x1,2 # i.e., the element of x in the *second* row, *third*column, namely, 6.For example slicing
33、can produce views of the array: y = x:,1 yarray(2, 5) y0 = 9 # this also changes the corresponding element in x yarray(9, 5) xarray(1, 9, 3,4, 5, 6)1.1.1 Constructing arraysNew arrays can be constructed using the routines detailed in Array creation routines, and also by using the low-levelndarray co
34、nstructor:ndarray An array object represents a multidimensional, homogeneous array of fixed-size items.class numpy.ndarrayAn array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-typeobject describes the format of each element in the array (its byte-or
35、der, how many bytes it occupies in memory,whether it is an integer, a floating point number, or something else, etc.)Arrays should be constructed using array, zeros or empty (refer to the See Also section below). The parametersgiven here refer to a low-level method (ndarray(.) for instantiating an a
36、rray.For more information, refer to the numpy module and examine the the methods and attributes of an array.Parameters(for the _new_ method; see Notes below) :shape : tuple of intsShape of created array.dtype : data-type, optionalAny object that can be interpreted as a numpy data type.4 Chapter 1. A
37、rray objectsNumPy Reference, Release 1.5.1buffer : object exposing buffer interface, optionalUsed to fill the array with data.offset : int, optionalOffset of array data in buffer.strides : tuple of ints, optionalStrides of data in memory.order : C, F, optionalRow-major or column-major order.See Also
38、:arrayConstruct an array.zerosCreate an array, each element of which is zero.emptyCreate an array, but leave its allocated memory unchanged (i.e., it contains “garbage”).dtypeCreate a data-type.NotesThere are two modes of creating an array using _new_:1.If buffer is None, then only shape, dtype, and
39、 order are used.2.If buffer is an object exposing the buffer interface, then all keywords are interpreted.No _init_ method is needed because the array is fully initialized after the _new_ method.ExamplesThese examples illustrate the low-level ndarray constructor. Refer to the See Also section above
40、for easier waysof constructing an ndarray.First mode, buffer is None: np.ndarray(shape=(2,2), dtype=float, order=F)array( -1.13698227e+002, 4.25087011e-303, 2.88528414e-306, 3.27025015e-309) #randomSecond mode: np.ndarray(2,), buffer=np.array(1,2,3),. offset=np.int_().itemsize,. dtype=int) # offset
41、= 1*itemsize, i.e. skip first elementarray(2, 3)1.1. The N-dimensional array (ndarray) 5NumPy Reference, Release 1.5.1AttributesTdatadtype Create a data type object.flagsflatimag(val) Return the imaginary part of the elements of the array.real(val) Return the real part of the elements of the array.s
42、ize(a, axis) Return the number of elements along a given axis.itemsizenbytes Base object for a dictionary for look-up with any alias for an array dtype.ndim(a) Return the number of dimensions of an array.shape(a) Return the shape of an array.stridesctypes create and manipulate C data types in Python
43、baseclass numpy.dtypeCreate a data type object.A numpy array is homogeneous, and contains elements described by a dtype object. A dtype object can beconstructed from different combinations of fundamental numeric types.Parametersobj :Object to be converted to a data type object.align : bool, optional
44、Add padding to the fields to match what a C compiler would output for a similar C-struct. Can be True only if obj is a dictionary or a comma-separated string.copy : bool, optionalMake a new copy of the data-type object. If False, the result may just be a referenceto a built-in data-type object.Examp
45、lesUsing array-scalar type: np.dtype(np.int16)dtype(int16)Record, one field name f1, containing int16: np.dtype(f1, np.int16)dtype(f1, np.dtype(f1, (f1, np.int16)dtype(f1, (f1, np.dtype(f1, np.uint), (f2, np.int32)dtype(f1, np.dtype(a,f8),(b,S10)dtype(a, np.dtype(“i4, (2,3)f8“)dtype(f0, np.dtype(hel
46、lo,(np.int,3),(world,np.void,10)dtype(hello, np.dtype(np.int16, x:(np.int8,0), y:(np.int8,1)dtype( np.dtype(names:gender,age, formats:S1,np.uint8)dtype(gender, |S1), (age, |u1)Offsets in bytes, here 0 and 25: np.dtype(surname:(S25,0),age:(np.uint8,25)dtype(surname, |S25), (age, |u1)Methodsnewbyteord
47、ernumpy.imag(val)Return the imaginary part of the elements of the array.Parametersval : array_likeInput array.Returnsout : ndarrayOutput array. If val is real, the type of val is used for the output. If val has complexelements, the returned type is float.See Also:real, angle, real_if_closeExamples a
48、 = np.array(1+2j, 3+4j, 5+6j) a.imagarray( 2., 4., 6.) a.imag = np.array(8, 10, 12) aarray( 1. +8.j, 3.+10.j, 5.+12.j)numpy.real(val)Return the real part of the elements of the array.1.1. The N-dimensional array (ndarray) 7NumPy Reference, Release 1.5.1Parametersval : array_likeInput array.Returnsou
49、t : ndarrayOutput array. If val is real, the type of val is used for the output. If val has complexelements, the returned type is float.See Also:real_if_close, imag, angleExamples a = np.array(1+2j, 3+4j, 5+6j) a.realarray( 1., 3., 5.) a.real = 9 aarray( 9.+2.j, 9.+4.j, 9.+6.j) a.real = np.array(9, 8, 7) aarray( 9.+2.j, 8.+4.j, 7.+6.j)Methodsall(a, axis, out) Test whether al