For detailed reference documentation of the functions and classes included in the package, see the reference. As a simple example, consider the case of multiplying each element in a 1-D sequence by the corresponding element in another sequence of the same length.
QUICKSTART TUTORIAL
- Prerequisites
- The Basics
- An example
- Array Creation
- Printing Arrays
- Basic Operations
- Universal Functions
- Indexing, Slicing and Iterating
- Shape Manipulation
- Changing the shape of an array
- Stacking together different arrays
- Splitting one array into several smaller ones
- Copies and Views
- No Copy at All
- View or Shallow Copy
- Deep Copy
- Functions and Methods Overview
- Less Basic
- Broadcasting rules
- Fancy indexing and index tricks
- Indexing with Arrays of Indices
- Indexing with Boolean Arrays
- The ix_() function
- Indexing with strings
- Linear Algebra
- Simple Array Operations
- Tricks and Tips
The type of the resulting array is derived from the type of elements in the rows. The value of the array element is assumed to be the same along that dimension for the "broadcast" array.
2.8.1 “Automatic” Reshaping
Vector Stacking
Histograms
Further reading
THREE
NUMPY BASICS
Data types
- Array types and conversions between types
- Array Scalars
- Overflow Errors
- Extended Precision
Note that even though np.longdouble offers more precision than pythonfloat, it's easy to lose that extra precision, since python often forces values to pass through float. It might be useful to test your code with value1 + np.finfo(np. longdouble).eps.
Array creation
- Introduction
- Converting Python array_like Objects to NumPy Arrays
- Intrinsic NumPy Array Creation
- Reading Arrays From Disk
In some unusual situations, it may be useful to use floating-point numbers with higher precision. Which one is more efficient depends on the hardware and the development environment; they are typically padded to 96 bits on 32-bit systems, while they are typically padded to 128 bits on 64-bit systems.np.longdoubleis padded to the system default; np.float96innp.float128 are available for users who want special padding.
I/O with NumPy
- Importing data with genfromtxt
This argument accepts a single integer or a range of integers corresponding to the indices of the columns to input. In that case, the type of columns will be determined from the data itself (see below).
Indexing
- Assignment vs referencing
- Single element indexing
- Other indexing options
- Index arrays
- Indexing Multi-dimensional arrays
- Boolean or “mask” index arrays
- Combining index arrays with slices
- Structural indexing tools
- Assigning values to indexed arrays
- Dealing with variable numbers of indices within programs
Boolean arrays must have the same shape as the initial dimensions of the array being indexed. Unlike some references (such as array and mask indexes), assignments are always made to the original data in the array (nothing else would make sense indeed!).
Broadcasting
- General Broadcasting Rules
So the array value at x[1]+1 is assigned three times to x[1] instead of being incremented three times. If we pass a tuple to the index, it will be interpreted as a list of indices. We can think of a scalar being expanded into a matrix of the same form as a during an arithmetic operation.
The size of the resulting matrix is the maximum size along each dimension of the input matrices. For example, if you have a 256x256x3 array of RGB values and want to convert each color in the image to a different value, you can multiply the image with a one-dimensional array of 3 values.
Byte-swapping
- Introduction to byte ordering and ndarrays
- Changing byte ordering
Returning to ourbig_end_arr: In this case, our underlying data is big-endian (data-endianness) and we've set the dtype to match (the dtype is also big-endian). Change the byte order of the underlying data and leave the dtype interpretation as it was. Your data and dtype endianness do not match and you want to change the dtype to match the data.
Your data and dtype endianness do not match, and you want to swap the data to match the dtype 3. Your data and dtype endianness match, but you want the data to be swapped and the dtype to reflect that.
Structured arrays
- Introduction
- Structured Datatypes
- Indexing and Assignment to Structured arrays
- Record Arrays
- Recarray Helper Functions
The byte offsets of the fields within the structure and the total size of the structure items are determined automatically. The element size and byte offsets of the fields are determined automatically, and the field names are given the default names f0,f1, etc. Field names can be changed by assigning attributenames using a sequence of strings of the same length.
Warning: In Numpy 1.15, indexing an array with a multi-field index produced a copy of the result above, but with fields packed in memory as if passed through numpy.lib.recfunctions.repack_fields. The functionnumpy.lib.recfunctions.repack_field can always be used to reproduce the old behavior, as it will return a packed copy of the structured array.
Subclassing ndarray
- Introduction
- View casting
- Creating new from template
- Relationship of view casting and new-from-template
- Implications for subclassing
- Simple example - adding an extra attribute to ndarray
- Slightly more realistic example - attribute added to existing array
The first is using thendarray.__new__method for the main work of object initialization, rather than the more usual__init__method. Aside - why not callobj = subdtype.__new__(..then? Because we might not have a __new__method with the same call signature). This will call the usual sequence of MySubClass.__new__then (if it exists)MySubClass.__init__.
Our MySubClass.__new__ method is only called in the case of an explicit constructor call, so we can't rely on MySubClass.__new__orMySubClass.__init__ to handle view casting and new-from-it model. One sees that supercall, which goes tondarray.__new__, passing the __array_finalize__new object, of our class (self), as well as the object from which the view is derived (obj).
3.8.8 __array_ufunc__ for ufuncs
For this example, the result would be identical, but there is a difference if another operand also defines __array_ufunc__. If you use supereras in the example, ndarray.__array_ufunc__ will notice that bhar an override, meaning it cannot evaluate the result itself. If we instead replace our super call with getattr(ufunc, method), we effectively make np.add(a.
Our example class is not set up to handle this, but it could very well be the best approach if you e.g. had to re-implement MaskedArrayusing__array_ufunc__. Then any ufunc on an instance of C would pass to A.__array_ufunc__, the super call in A would pass to B.__array_ufunc__, and the super call in B would pass to ndarray.
3.8.9 __array_wrap__ for ufuncs and other functions
Subclassing and Downstream Compatibility
When subclassingndarray creates duck types that mimic the ndararay interface, it's your responsibility to decide how close your APIs will be to numpy's. This is exactly the same signature method fornp.sum, so now if a user calls np.sumon this object, numpy will call the object's selfsum method and pass these arguments listed above to the signature, and not there will be mistakes. This object is no longer compatible with np.sum because if you call np.sum, it will pass in unexpected outandkeepdims arguments, causing a TypeError to be raised.
If you want to maintain compatibility with numpy and its subsequent versions (which might add new keyword arguments), but don't want to display all of numpy's arguments, your function signature should accept **kwargs. This object is now compatible with np.sumigen, because any extraneous arguments (ie keywords that are not ax keyword type) will be tucked away in the **unused_kwargs parameter.
FOUR
MISCELLANEOUS
- IEEE 754 Floating Point Special Values
- How numpy handles numerical exceptions
- Examples
- Interfacing to C
- Interfacing to Fortran
- Interfacing to C++
- SWIG
But this can be changed and set individually for different types of exceptions. Can write code in a non-standard format that can become obsolete - Not as flexible as manual wrapping. Good for wrapping large (lots of functions) existing C libraries .. creates a lot of code between Python and C code .. can cause performance problems that are almost impossible to optimize - interface files can be hard to write .. not necessarily avoid problems with reference counting or the need to know APIs 5) scipy.weave.
Its future is very uncertain: it is the only part of Scipy that has not been ported to Python 3 and has actually been deprecated in favor of Cython.
FIVE
NUMPY FOR MATLAB USERS
- Introduction
- Some Key Differences
- Short answer
- Long answer
- Table of Rough MATLAB-NumPy Equivalents
- General Purpose Equivalents
- Linear Algebra Equivalents
- Notes
- Customizing Your Environment
- Links
Until Python 3.5, the only disadvantage of using the matrix type was that you had to use instead of * to multiply (reduce) two tensors (scalar product, matrix vector multiplication, etc.). If you were to use Matlab & or | you must use NumPy ufuncs logical_and/logical_or. Precedence: NumPy's & operator has higher precedence than logical operators such as < and >; Matlab is the other way around.
If you know you have boolean arguments, you can get away with using NumPy's bitwise operators, but be careful with parentheses, like this: z = (x > 1) & (x < 2). Make all matlib functions accessible at the top level via M.func() import numpy.matlib as M.
BUILDING FROM SOURCE
- Prerequisites
- Basic Installation
- Parallel builds
- FORTRAN ABI mismatch
- Choosing the fortran compiler
- How to check the ABI of blas/lapack/atlas
- Accelerated BLAS/LAPACK libraries
- BLAS
- MKL 2. BLIS
- LAPACK
- MKL 2. OpenBLAS
- Disabling ATLAS and other accelerated libraries
- Supplying additional compiler flags
- Building with ATLAS support
The number of build jobs can also be specified via the environment variable NPY_NUM_BUILD_JOBS. Unfortunately, they are not ABI compatible, which means that you should concretely avoid mixing libraries built with each other. In particular, if your blas/lapack/atlas is built with g77, use g77 when building numpy and scipy; on the contrary, if your atlas is built with gfortran, you must build numpy/scipy with gfortran.
One relatively simple and reliable way to check for the compiler used to build a library is to use ldd on the library. When providing options that should improve the performance of the code, be sure to also set DNDEBUG so that debugging code is not executed.
SEVEN
USING NUMPY C-API
How to extend NumPy
- Writing an extension module
- Required subroutine
- Defining functions
- Dealing with array objects
- Example
A function that borrows a reference does not change the reference count of the object and does not expect to "hold on" to the reference. The desired data type of the returned array is specified in typenum, which must be one of the enumerated types. One of the enumerated types or NPY_NOTYPE if the data type is to be determined from the object itself.
The memory model for an array accepts arbitrary steps in each dimension to advance to the next element of the array. Note: Whether or not an array is byte-swapped is determined by the array's data type.
Using Python as glue
- Calling other compiled libraries from Python
- Hand-generated wrappers
- Cython
- ctypes
- Additional tools you may find useful
This will produce an extension module called filter.so in the current directory with a method called dfilter2d that returns a filtered version of the input. This allows clean separation of Python with compiled code, while still allowing for separate distribution of the extension module. Alternatively, you might use the storage class specifier __declspec(dllexport) in the C definition of the function to avoid the need for this.deffile.
The functions in the shared library are available as attributes of the ctypes library object (returned from ctypeslib. load_library) or as items using lib['func_name'] syntax. Do not set the argtypes attribute of the function object and define an_as_parameter_method for the object you want to pass in.
Writing your own ufunc
- Creating a new universal function
- Example Non-ufunc extension
- Example NumPy ufunc for one dtype
- Example NumPy ufunc with multiple dtypes
- Example NumPy ufunc with multiple arguments/return values
- Example NumPy ufunc with structured array dtype arguments
The first is the C file that contains the actual code, and the second is the setup.py file used to create the module. To use the setup.py file, place setup.py and spammodule.c in the same folder. As in the previous section, we first provide the .c file and then the setup.py file used to create the module that contains the ufunc.
As in the previous sections, we first provide the .c file and then the corresponding setup.py file. Note that since this is a numpy extension, we use numpy.distutils instead of . distutils from the Python standard library. python setup.py build_ext --inplace. builds the extension library into the current file. continued from previous page) will build a file that looks like ./build/lib*, where.
Beyond the Basics
- Iterating over elements in the array
- User-defined data-types
- Subtyping the ndarray in C
You can also use PyArrayIter_Check(obj) to make sure you have an iterator object and PyArray_ITER_RESET(iter) to reset the iterator object back to the beginning of the array. Adding data types is one of the less well-tested areas for NumPy 1.0, so bugs may remain in the approach. The memory layout of the object structure for the new Python type must be PyObject_HEAD followed by the fixed size memory required for the data type.
Populate the tp_base member of the new object type structure with a pointer to the (main) object of the parent type. If two subtypes have the same __array_priority__ , then the subtype of the first argument determines the output.