numpy.recarray.transpose method   recarray.transpose(*axes)

Returns a view of the array with axes transposed. For a 1-D array this has no effect, as a transposed vector is simply the same vector. To convert a 1-D array into a 2D column vector, an additional dimension must be added. np.atleast2d(a).T achieves this, as does a[:, np.newaxis]. For a 2-D array, this is a standard matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided and a.shape = (i[0], i[1], ... i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0]).  Parameters

axesNone, tuple of ints, or n ints

 None or no argument: reverses the order of the axes. tuple of ints: i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.
n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form)     Returns

outndarray

View of a, with axes suitably permuted.      See also  transpose

Equivalent function  ndarray.T

Array property returning the array transposed.  ndarray.reshape

Give a new shape to an array without changing its data.    Examples >>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> a.transpose((1, 0))
array([[1, 3],
       [2, 4]])
>>> a.transpose(1, 0)
array([[1, 3],
       [2, 4]])


numpy.recarray.var method   recarray.var(axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True)

Returns the variance of the array elements, along given axis. Refer to numpy.var for full documentation.  See also  numpy.var

equivalent function


numpy.recarray.view method   recarray.view([dtype][, type])

New view of array with the same data.  Note Passing None for dtype is different from omitting the parameter, since the former invokes dtype(None) which is an alias for dtype('float_').   Parameters

dtypedata-type or ndarray sub-class, optional

Data-type descriptor of the returned view, e.g., float32 or int16. Omitting it results in the view having the same data-type as a. This argument can also be specified as an ndarray sub-class, which then specifies the type of the returned object (this is equivalent to setting the type parameter).
typePython type, optional

Type of the returned view, e.g., ndarray or matrix. Again, omission of the parameter results in type preservation.     Notes a.view() is used two different ways: a.view(some_dtype) or a.view(dtype=some_dtype) constructs a view of the array’s memory with a different data-type. This can cause a reinterpretation of the bytes of memory. a.view(ndarray_subclass) or a.view(type=ndarray_subclass) just returns an instance of ndarray_subclass that looks at the same array (same shape, dtype, etc.) This does not cause a reinterpretation of the memory. For a.view(some_dtype), if some_dtype has a different number of bytes per entry than the previous dtype (for example, converting a regular array to a structured array), then the behavior of the view cannot be predicted just from the superficial appearance of a (shown by print(a)). It also depends on exactly how a is stored in memory. Therefore if a is C-ordered versus fortran-ordered, versus defined as a slice or transpose, etc., the view may give different results. Examples >>> x = np.array([(1, 2)], dtype=[('a', np.int8), ('b', np.int8)])
 Viewing array data using a different type and dtype: >>> y = x.view(dtype=np.int16, type=np.matrix)
>>> y
matrix([[513]], dtype=int16)
>>> print(type(y))
<class 'numpy.matrix'>
 Creating a view on a structured array so it can be used in calculations >>> x = np.array([(1, 2),(3,4)], dtype=[('a', np.int8), ('b', np.int8)])
>>> xv = x.view(dtype=np.int8).reshape(-1,2)
>>> xv
array([[1, 2],
       [3, 4]], dtype=int8)
>>> xv.mean(0)
array([2.,  3.])
 Making changes to the view changes the underlying array >>> xv[0,1] = 20
>>> x
array([(1, 20), (3,  4)], dtype=[('a', 'i1'), ('b', 'i1')])
 Using a view to convert an array to a recarray: >>> z = x.view(np.recarray)
>>> z.a
array([1, 3], dtype=int8)
 Views share data: >>> x[0] = (9, 10)
>>> z[0]
(9, 10)
 Views that change the dtype size (bytes per entry) should normally be avoided on arrays defined by slices, transposes, fortran-ordering, etc.: >>> x = np.array([[1,2,3],[4,5,6]], dtype=np.int16)
>>> y = x[:, 0:2]
>>> y
array([[1, 2],
       [4, 5]], dtype=int16)
>>> y.view(dtype=[('width', np.int16), ('length', np.int16)])
Traceback (most recent call last):
    ...
ValueError: To change to a dtype of a different size, the array must be C-contiguous
>>> z = y.copy()
>>> z.view(dtype=[('width', np.int16), ('length', np.int16)])
array([[(1, 2)],
       [(4, 5)]], dtype=[('width', '<i2'), ('length', '<i2')])


numpy.record.all method   record.all()

Scalar method identical to the corresponding array attribute. Please see ndarray.all.


numpy.record.any method   record.any()

Scalar method identical to the corresponding array attribute. Please see ndarray.any.


numpy.record.argmax method   record.argmax()

Scalar method identical to the corresponding array attribute. Please see ndarray.argmax.


numpy.record.argmin method   record.argmin()

Scalar method identical to the corresponding array attribute. Please see ndarray.argmin.


numpy.record.argsort method   record.argsort()

Scalar method identical to the corresponding array attribute. Please see ndarray.argsort.


numpy.record.astype method   record.astype()

Scalar method identical to the corresponding array attribute. Please see ndarray.astype.


numpy.record.base attribute   record.base

base object


numpy.record.byteswap method   record.byteswap()

Scalar method identical to the corresponding array attribute. Please see ndarray.byteswap.


numpy.record.choose method   record.choose()

Scalar method identical to the corresponding array attribute. Please see ndarray.choose.


numpy.record.clip method   record.clip()

Scalar method identical to the corresponding array attribute. Please see ndarray.clip.


numpy.record.compress method   record.compress()

Scalar method identical to the corresponding array attribute. Please see ndarray.compress.


numpy.record.conjugate method   record.conjugate()

Scalar method identical to the corresponding array attribute. Please see ndarray.conjugate.


numpy.record.copy method   record.copy()

Scalar method identical to the corresponding array attribute. Please see ndarray.copy.


numpy.record.cumprod method   record.cumprod()

Scalar method identical to the corresponding array attribute. Please see ndarray.cumprod.


numpy.record.cumsum method   record.cumsum()

Scalar method identical to the corresponding array attribute. Please see ndarray.cumsum.


numpy.record.data attribute   record.data

Pointer to start of data.


numpy.record.diagonal method   record.diagonal()

Scalar method identical to the corresponding array attribute. Please see ndarray.diagonal.


numpy.record.dump method   record.dump()

Scalar method identical to the corresponding array attribute. Please see ndarray.dump.


numpy.record.dumps method   record.dumps()

Scalar method identical to the corresponding array attribute. Please see ndarray.dumps.


numpy.record.fill method   record.fill()

Scalar method identical to the corresponding array attribute. Please see ndarray.fill.


numpy.record.flags attribute   record.flags

integer value of flags


numpy.record.flat attribute   record.flat

A 1-D view of the scalar.


numpy.record.flatten method   record.flatten()

Scalar method identical to the corresponding array attribute. Please see ndarray.flatten.


numpy.record.getfield method   record.getfield()

Scalar method identical to the corresponding array attribute. Please see ndarray.getfield.


numpy.record.item method   record.item()

Scalar method identical to the corresponding array attribute. Please see ndarray.item.


numpy.record.itemset method   record.itemset()

Scalar method identical to the corresponding array attribute. Please see ndarray.itemset.


numpy.record.itemsize attribute   record.itemsize

The length of one element in bytes.


numpy.record.max method   record.max()

Scalar method identical to the corresponding array attribute. Please see ndarray.max.


numpy.record.mean method   record.mean()

Scalar method identical to the corresponding array attribute. Please see ndarray.mean.


numpy.record.min method   record.min()

Scalar method identical to the corresponding array attribute. Please see ndarray.min.


numpy.record.nbytes attribute   record.nbytes

The length of the scalar in bytes.


numpy.record.ndim attribute   record.ndim

The number of array dimensions.


numpy.record.newbyteorder method   record.newbyteorder(new_order='S', /)

Return a new dtype with a different byte order. Changes are also made in all fields and sub-arrays of the data type. The new_order code can be any from the following:  ‘S’ - swap dtype from current to opposite endian {‘<’, ‘little’} - little endian {‘>’, ‘big’} - big endian {‘=’, ‘native’} - native order {‘|’, ‘I’} - ignore (no change to byte order)   Parameters

new_orderstr, optional

Byte order to force; a value from the byte order specifications above. The default value (‘S’) results in swapping the current byte order.    Returns

new_dtypedtype

New dtype object with the given change to the byte order.


numpy.record.nonzero method   record.nonzero()

Scalar method identical to the corresponding array attribute. Please see ndarray.nonzero.


numpy.record.pprint method   record.pprint()[source]

Pretty-print all fields.


numpy.record.prod method   record.prod()

Scalar method identical to the corresponding array attribute. Please see ndarray.prod.


numpy.record.ptp method   record.ptp()

Scalar method identical to the corresponding array attribute. Please see ndarray.ptp.


numpy.record.put method   record.put()

Scalar method identical to the corresponding array attribute. Please see ndarray.put.


numpy.record.ravel method   record.ravel()

Scalar method identical to the corresponding array attribute. Please see ndarray.ravel.


numpy.record.repeat method   record.repeat()

Scalar method identical to the corresponding array attribute. Please see ndarray.repeat.


numpy.record.reshape method   record.reshape()

Scalar method identical to the corresponding array attribute. Please see ndarray.reshape.


numpy.record.resize method   record.resize()

Scalar method identical to the corresponding array attribute. Please see ndarray.resize.


numpy.record.round method   record.round()

Scalar method identical to the corresponding array attribute. Please see ndarray.round.


numpy.record.searchsorted method   record.searchsorted()

Scalar method identical to the corresponding array attribute. Please see ndarray.searchsorted.


numpy.record.setfield method   record.setfield()

Scalar method identical to the corresponding array attribute. Please see ndarray.setfield.


numpy.record.setflags method   record.setflags()

Scalar method identical to the corresponding array attribute. Please see ndarray.setflags.


numpy.record.size attribute   record.size

The number of elements in the gentype.


numpy.record.sort method   record.sort()

Scalar method identical to the corresponding array attribute. Please see ndarray.sort.


numpy.record.squeeze method   record.squeeze()

Scalar method identical to the corresponding array attribute. Please see ndarray.squeeze.


numpy.record.std method   record.std()

Scalar method identical to the corresponding array attribute. Please see ndarray.std.


numpy.record.strides attribute   record.strides

Tuple of bytes steps in each dimension.


numpy.record.sum method   record.sum()

Scalar method identical to the corresponding array attribute. Please see ndarray.sum.


numpy.record.swapaxes method   record.swapaxes()

Scalar method identical to the corresponding array attribute. Please see ndarray.swapaxes.


numpy.record.T attribute   record.T

Scalar attribute identical to the corresponding array attribute. Please see ndarray.T.


numpy.record.take method   record.take()

Scalar method identical to the corresponding array attribute. Please see ndarray.take.


numpy.record.tofile method   record.tofile()

Scalar method identical to the corresponding array attribute. Please see ndarray.tofile.


numpy.record.tolist method   record.tolist()

Scalar method identical to the corresponding array attribute. Please see ndarray.tolist.


numpy.record.tostring method   record.tostring()

Scalar method identical to the corresponding array attribute. Please see ndarray.tostring.


numpy.record.trace method   record.trace()

Scalar method identical to the corresponding array attribute. Please see ndarray.trace.


numpy.record.transpose method   record.transpose()

Scalar method identical to the corresponding array attribute. Please see ndarray.transpose.


numpy.record.var method   record.var()

Scalar method identical to the corresponding array attribute. Please see ndarray.var.


numpy.record.view method   record.view()

Scalar method identical to the corresponding array attribute. Please see ndarray.view.


NumPy Reference  Release
1.22  Date
December 31, 2021   This reference manual details functions, modules, and objects included in NumPy, describing what they are and what they do. For learning how to use NumPy, see the complete documentation.
Array objects The N-dimensional array (ndarray) Scalars Data type objects (dtype) Indexing routines Iterating Over Arrays Standard array subclasses Masked arrays The Array Interface Datetimes and Timedeltas   Constants
Universal functions (ufunc) ufunc Available ufuncs
Routines Array creation routines Array manipulation routines Binary operations String operations C-Types Foreign Function Interface (numpy.ctypeslib) Datetime Support Functions Data type routines Optionally SciPy-accelerated routines (numpy.dual) Mathematical functions with automatic domain (numpy.emath) Floating point error handling Discrete Fourier Transform (numpy.fft) Functional programming NumPy-specific help functions Input and output Linear algebra (numpy.linalg) Logic functions Masked array operations Mathematical functions Matrix library (numpy.matlib) Miscellaneous routines Padding Arrays Polynomials Random sampling (numpy.random) Set routines Sorting, searching, and counting Statistics Test Support (numpy.testing) Window functions
Typing (numpy.typing) Mypy plugin Differences from the runtime NumPy API API
Global State Performance-Related Options Interoperability-Related Options Debugging-Related Options
Packaging (numpy.distutils) Modules in numpy.distutils Configuration class Building Installable C libraries Conversion of .src files
NumPy Distutils - Users Guide SciPy structure Requirements for SciPy packages The setup.py file The __init__.py file Extra features in NumPy Distutils
NumPy C-API Python Types and C-Structures System configuration Data Type API Array API Array Iterator API UFunc API Generalized Universal Function API NumPy core libraries C API Deprecations Memory management in NumPy
SIMD Optimizations Build options for compilation Understanding CPU Dispatching, How the NumPy dispatcher works? Dive into the CPU dispatcher
NumPy and SWIG numpy.i: a SWIG Interface File for NumPy Testing the numpy.i Typemaps     Acknowledgements Large parts of this manual originate from Travis E. Oliphant’s book Guide to NumPy (which generously entered Public Domain in August 2008). The reference documentation for many of the functions are written by numerous contributors and developers of NumPy.


Routines In this chapter routine docstrings are presented, grouped by functionality. Many docstrings contain example code, which demonstrates basic usage of the routine. The examples assume that NumPy is imported with: >>> import numpy as np
 A convenient way to execute examples is the %doctest_mode mode of IPython, which allows for pasting of multi-line examples and preserves indentation.
Array creation routines From shape or value From existing data Creating record arrays (numpy.rec) Creating character arrays (numpy.char) Numerical ranges Building matrices The Matrix class
Array manipulation routines Basic operations Changing array shape Transpose-like operations Changing number of dimensions Changing kind of array Joining arrays Splitting arrays Tiling arrays Adding and removing elements Rearranging elements
Binary operations Elementwise bit operations Bit packing Output formatting
String operations String operations Comparison String information Convenience class   C-Types Foreign Function Interface (numpy.ctypeslib)
Datetime Support Functions numpy.datetime_as_string numpy.datetime_data Business Day Functions
Data type routines numpy.can_cast numpy.promote_types numpy.min_scalar_type numpy.result_type numpy.common_type numpy.obj2sctype Creating data types Data type information Data type testing Miscellaneous
Optionally SciPy-accelerated routines (numpy.dual) Linear algebra FFT Other
Mathematical functions with automatic domain (numpy.emath) Functions
Floating point error handling Setting and getting error handling Internal functions
Discrete Fourier Transform (numpy.fft) Standard FFTs Real FFTs Hermitian FFTs Helper routines Background information Implementation details Type Promotion Normalization Real and Hermitian transforms Higher dimensions References Examples
Functional programming numpy.apply_along_axis numpy.apply_over_axes numpy.vectorize numpy.frompyfunc numpy.piecewise
NumPy-specific help functions Finding help Reading help
Input and output NumPy binary files (NPY, NPZ) Text files Raw binary files String formatting Memory mapping files Text formatting options Base-n representations Data sources Binary Format Description
Linear algebra (numpy.linalg) The @ operator Matrix and vector products Decompositions Matrix eigenvalues Norms and other numbers Solving equations and inverting matrices Exceptions Linear algebra on several matrices at once
Logic functions Truth value testing Array contents Array type testing Logical operations Comparison
Masked array operations Constants Creation Inspecting the array Manipulating a MaskedArray Operations on masks Conversion operations Masked arrays arithmetic
Mathematical functions Trigonometric functions Hyperbolic functions Rounding Sums, products, differences Exponents and logarithms Other special functions Floating point routines Rational routines Arithmetic operations Handling complex numbers Extrema Finding Miscellaneous
Matrix library (numpy.matlib) numpy.matlib.empty numpy.matlib.zeros numpy.matlib.ones numpy.matlib.eye numpy.matlib.identity numpy.matlib.repmat numpy.matlib.rand numpy.matlib.randn
Miscellaneous routines Performance tuning Memory ranges Array mixins NumPy version comparison Utility Matlab-like Functions Exceptions
Padding Arrays numpy.pad
Polynomials Transitioning from numpy.poly1d to numpy.polynomial Documentation for the polynomial Package Documentation for Legacy Polynomials
Random sampling (numpy.random) Quick Start Introduction Concepts Features
Set routines numpy.lib.arraysetops Making proper sets Boolean operations
Sorting, searching, and counting Sorting Searching Counting
Statistics Order statistics Averages and variances Correlating Histograms
Test Support (numpy.testing) Asserts Asserts (not recommended) Decorators Test Running Guidelines
Window functions Various windows


Testing the numpy.i Typemaps  Introduction Writing tests for the numpy.i SWIG interface file is a combinatorial headache. At present, 12 different data types are supported, each with 74 different argument signatures, for a total of 888 typemaps supported “out of the box”. Each of these typemaps, in turn, might require several unit tests in order to verify expected behavior for both proper and improper inputs. Currently, this results in more than 1,000 individual unit tests executed when make test is run in the numpy/tools/swig subdirectory. To facilitate this many similar unit tests, some high-level programming techniques are employed, including C and SWIG macros, as well as Python inheritance. The purpose of this document is to describe the testing infrastructure employed to verify that the numpy.i typemaps are working as expected.   Testing Organization There are three independent testing frameworks supported, for one-, two-, and three-dimensional arrays respectively. For one-dimensional arrays, there are two C++ files, a header and a source, named: Vector.h
Vector.cxx
 that contain prototypes and code for a variety of functions that have one-dimensional arrays as function arguments. The file: Vector.i
 is a SWIG interface file that defines a python module Vector that wraps the functions in Vector.h while utilizing the typemaps in numpy.i to correctly handle the C arrays. The Makefile calls swig to generate Vector.py and Vector_wrap.cxx, and also executes the setup.py script that compiles Vector_wrap.cxx and links together the extension module _Vector.so or _Vector.dylib, depending on the platform. This extension module and the proxy file Vector.py are both placed in a subdirectory under the build directory. The actual testing takes place with a Python script named: testVector.py
 that uses the standard Python library module unittest, which performs several tests of each function defined in Vector.h for each data type supported. Two-dimensional arrays are tested in exactly the same manner. The above description applies, but with Matrix substituted for Vector. For three-dimensional tests, substitute Tensor for Vector. For four-dimensional tests, substitute SuperTensor for Vector. For flat in-place array tests, substitute Flat for Vector. For the descriptions that follow, we will reference the Vector tests, but the same information applies to Matrix, Tensor and SuperTensor tests. The command make test will ensure that all of the test software is built and then run all three test scripts.   Testing Header Files Vector.h is a C++ header file that defines a C macro called TEST_FUNC_PROTOS that takes two arguments: TYPE, which is a data type name such as unsigned int; and SNAME, which is a short name for the same data type with no spaces, e.g. uint. This macro defines several function prototypes that have the prefix SNAME and have at least one argument that is an array of type TYPE. Those functions that have return arguments return a TYPE value. TEST_FUNC_PROTOS is then implemented for all of the data types supported by numpy.i:  signed char unsigned char short unsigned short int unsigned int long unsigned long long long unsigned long long float double    Testing Source Files Vector.cxx is a C++ source file that implements compilable code for each of the function prototypes specified in Vector.h. It defines a C macro TEST_FUNCS that has the same arguments and works in the same way as TEST_FUNC_PROTOS does in Vector.h. TEST_FUNCS is implemented for each of the 12 data types as above.   Testing SWIG Interface Files Vector.i is a SWIG interface file that defines python module Vector. It follows the conventions for using numpy.i as described in this chapter. It defines a SWIG macro %apply_numpy_typemaps that has a single argument TYPE. It uses the SWIG directive %apply to apply the provided typemaps to the argument signatures found in Vector.h. This macro is then implemented for all of the data types supported by numpy.i. It then does a %include "Vector.h" to wrap all of the function prototypes in Vector.h using the typemaps in numpy.i.   Testing Python Scripts After make is used to build the testing extension modules, testVector.py can be run to execute the tests. As with other scripts that use unittest to facilitate unit testing, testVector.py defines a class that inherits from unittest.TestCase: class VectorTestCase(unittest.TestCase):
 However, this class is not run directly. Rather, it serves as a base class to several other python classes, each one specific to a particular data type. The VectorTestCase class stores two strings for typing information:  self.typeStr

A string that matches one of the SNAME prefixes used in Vector.h and Vector.cxx. For example, "double".  self.typeCode

A short (typically single-character) string that represents a data type in numpy and corresponds to self.typeStr. For example, if self.typeStr is "double", then self.typeCode should be "d".   Each test defined by the VectorTestCase class extracts the python function it is trying to test by accessing the Vector module’s dictionary: length = Vector.__dict__[self.typeStr + "Length"]
 In the case of double precision tests, this will return the python function Vector.doubleLength. We then define a new test case class for each supported data type with a short definition such as: class doubleTestCase(VectorTestCase):
    def __init__(self, methodName="runTest"):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = "double"
        self.typeCode = "d"
 Each of these 12 classes is collected into a unittest.TestSuite, which is then executed. Errors and failures are summed together and returned as the exit argument. Any non-zero result indicates that at least one test did not pass.


setup.py #!/usr/bin/env python3
"""
Build the Cython demonstrations of low-level access to NumPy random

Usage: python setup.py build_ext -i
"""
import setuptools  # triggers monkeypatching distutils
from distutils.core import setup
from os.path import dirname, join, abspath

import numpy as np
from Cython.Build import cythonize
from numpy.distutils.misc_util import get_info
from setuptools.extension import Extension

path = dirname(__file__)
src_dir = join(dirname(path), '..', 'src')
defs = [('NPY_NO_DEPRECATED_API', 0)]
inc_path = np.get_include()
lib_path = [abspath(join(np.get_include(), '..', '..', 'random', 'lib'))]
lib_path += get_info('npymath')['library_dirs']

extending = Extension("extending",
                      sources=[join('.', 'extending.pyx')],
                      include_dirs=[
                            np.get_include(),
                            join(path, '..', '..')
                        ],
                      define_macros=defs,
                      )
distributions = Extension("extending_distributions",
                          sources=[join('.', 'extending_distributions.pyx')],
                          include_dirs=[inc_path],
                          library_dirs=lib_path,
                          libraries=['npyrandom', 'npymath'],
                          define_macros=defs,
                          )

extensions = [extending, distributions]

setup(
    ext_modules=cythonize(extensions)
)


Statistics  Order statistics
ptp(a[, axis, out, keepdims]) Range of values (maximum - minimum) along an axis.
percentile(a, q[, axis, out, ...]) Compute the q-th percentile of the data along the specified axis.
nanpercentile(a, q[, axis, out, ...]) Compute the qth percentile of the data along the specified axis, while ignoring nan values.
quantile(a, q[, axis, out, overwrite_input, ...]) Compute the q-th quantile of the data along the specified axis.
nanquantile(a, q[, axis, out, ...]) Compute the qth quantile of the data along the specified axis, while ignoring nan values.     Averages and variances
median(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis.
average(a[, axis, weights, returned]) Compute the weighted average along the specified axis.
mean(a[, axis, dtype, out, keepdims, where]) Compute the arithmetic mean along the specified axis.
std(a[, axis, dtype, out, ddof, keepdims, where]) Compute the standard deviation along the specified axis.
var(a[, axis, dtype, out, ddof, keepdims, where]) Compute the variance along the specified axis.
nanmedian(a[, axis, out, overwrite_input, ...]) Compute the median along the specified axis, while ignoring NaNs.
nanmean(a[, axis, dtype, out, keepdims, where]) Compute the arithmetic mean along the specified axis, ignoring NaNs.
nanstd(a[, axis, dtype, out, ddof, ...]) Compute the standard deviation along the specified axis, while ignoring NaNs.
nanvar(a[, axis, dtype, out, ddof, ...]) Compute the variance along the specified axis, while ignoring NaNs.     Correlating
corrcoef(x[, y, rowvar, bias, ddof, dtype]) Return Pearson product-moment correlation coefficients.
correlate(a, v[, mode]) Cross-correlation of two 1-dimensional sequences.
cov(m[, y, rowvar, bias, ddof, fweights, ...]) Estimate a covariance matrix, given data and weights.     Histograms
histogram(a[, bins, range, normed, weights, ...]) Compute the histogram of a dataset.
histogram2d(x, y[, bins, range, normed, ...]) Compute the bi-dimensional histogram of two data samples.
histogramdd(sample[, bins, range, normed, ...]) Compute the multidimensional histogram of some data.
bincount(x, /[, weights, minlength]) Count number of occurrences of each value in array of non-negative ints.
histogram_bin_edges(a[, bins, range, weights]) Function to calculate only the edges of the bins used by the histogram function.
digitize(x, bins[, right]) Return the indices of the bins to which each value in input array belongs.


numpy.testing.assert_allclose   testing.assert_allclose(actual, desired, rtol=1e-07, atol=0, equal_nan=True, err_msg='', verbose=True)[source]

Raises an AssertionError if two objects are not equal up to desired tolerance. The test is equivalent to allclose(actual, desired, rtol, atol) (note that allclose has different default values). It compares the difference between actual and desired to atol + rtol * abs(desired).  New in version 1.5.0.   Parameters

actualarray_like

Array obtained.
desiredarray_like

Array desired.
rtolfloat, optional

Relative tolerance.
atolfloat, optional

Absolute tolerance.
equal_nanbool, optional.

If True, NaNs will compare equal.
err_msgstr, optional

The error message to be printed in case of failure.
verbosebool, optional

If True, the conflicting values are appended to the error message.    Raises
 AssertionError

If actual and desired are not equal up to specified precision.      See also
assert_array_almost_equal_nulp, assert_array_max_ulp

  Examples >>> x = [1e-5, 1e-3, 1e-1]
>>> y = np.arccos(np.cos(x))
>>> np.testing.assert_allclose(x, y, rtol=1e-5, atol=0)


numpy.testing.assert_almost_equal   testing.assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True)[source]

Raises an AssertionError if two items are not equal up to desired precision.  Note It is recommended to use one of assert_allclose, assert_array_almost_equal_nulp or assert_array_max_ulp instead of this function for more consistent floating point comparisons.  The test verifies that the elements of actual and desired satisfy. abs(desired-actual) < 1.5 * 10**(-decimal) That is a looser test than originally documented, but agrees with what the actual implementation in assert_array_almost_equal did up to rounding vagaries. An exception is raised at conflicting values. For ndarrays this delegates to assert_array_almost_equal  Parameters

actualarray_like

The object to check.
desiredarray_like

The expected object.
decimalint, optional

Desired precision, default is 7.
err_msgstr, optional

The error message to be printed in case of failure.
verbosebool, optional

If True, the conflicting values are appended to the error message.    Raises
 AssertionError

If actual and desired are not equal up to specified precision.      See also  assert_allclose

Compare two array_like objects for equality with desired relative and/or absolute precision.
assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal

  Examples >>> from numpy.testing import assert_almost_equal
>>> assert_almost_equal(2.3333333333333, 2.33333334)
>>> assert_almost_equal(2.3333333333333, 2.33333334, decimal=10)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not almost equal to 10 decimals
 ACTUAL: 2.3333333333333
 DESIRED: 2.33333334
 >>> assert_almost_equal(np.array([1.0,2.3333333333333]),
...                     np.array([1.0,2.33333334]), decimal=9)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not almost equal to 9 decimals

Mismatched elements: 1 / 2 (50%)
Max absolute difference: 6.66669964e-09
Max relative difference: 2.85715698e-09
 x: array([1.         , 2.333333333])
 y: array([1.        , 2.33333334])


numpy.testing.assert_approx_equal   testing.assert_approx_equal(actual, desired, significant=7, err_msg='', verbose=True)[source]

Raises an AssertionError if two items are not equal up to significant digits.  Note It is recommended to use one of assert_allclose, assert_array_almost_equal_nulp or assert_array_max_ulp instead of this function for more consistent floating point comparisons.  Given two numbers, check that they are approximately equal. Approximately equal is defined as the number of significant digits that agree.  Parameters

actualscalar

The object to check.
desiredscalar

The expected object.
significantint, optional

Desired precision, default is 7.
err_msgstr, optional

The error message to be printed in case of failure.
verbosebool, optional

If True, the conflicting values are appended to the error message.    Raises
 AssertionError

If actual and desired are not equal up to specified precision.      See also  assert_allclose

Compare two array_like objects for equality with desired relative and/or absolute precision.
assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal

  Examples >>> np.testing.assert_approx_equal(0.12345677777777e-20, 0.1234567e-20)
>>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345671e-20,
...                                significant=8)
>>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345672e-20,
...                                significant=8)
Traceback (most recent call last):
    ...
AssertionError:
Items are not equal to 8 significant digits:
 ACTUAL: 1.234567e-21
 DESIRED: 1.2345672e-21
 the evaluated condition that raises the exception is >>> abs(0.12345670e-20/1e-21 - 0.12345672e-20/1e-21) >= 10**-(8-1)
True


numpy.testing.assert_array_almost_equal   testing.assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True)[source]

Raises an AssertionError if two objects are not equal up to desired precision.  Note It is recommended to use one of assert_allclose, assert_array_almost_equal_nulp or assert_array_max_ulp instead of this function for more consistent floating point comparisons.  The test verifies identical shapes and that the elements of actual and desired satisfy. abs(desired-actual) < 1.5 * 10**(-decimal) That is a looser test than originally documented, but agrees with what the actual implementation did up to rounding vagaries. An exception is raised at shape mismatch or conflicting values. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions.  Parameters

xarray_like

The actual object to check.
yarray_like

The desired, expected object.
decimalint, optional

Desired precision, default is 6.
err_msgstr, optional

The error message to be printed in case of failure.
verbosebool, optional

If True, the conflicting values are appended to the error message.    Raises
 AssertionError

If actual and desired are not equal up to specified precision.      See also  assert_allclose

Compare two array_like objects for equality with desired relative and/or absolute precision.
assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal

  Examples the first assert does not raise an exception >>> np.testing.assert_array_almost_equal([1.0,2.333,np.nan],
...                                      [1.0,2.333,np.nan])
 >>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan],
...                                      [1.0,2.33339,np.nan], decimal=5)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not almost equal to 5 decimals

Mismatched elements: 1 / 3 (33.3%)
Max absolute difference: 6.e-05
Max relative difference: 2.57136612e-05
 x: array([1.     , 2.33333,     nan])
 y: array([1.     , 2.33339,     nan])
 >>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan],
...                                      [1.0,2.33333, 5], decimal=5)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not almost equal to 5 decimals

x and y nan location mismatch:
 x: array([1.     , 2.33333,     nan])
 y: array([1.     , 2.33333, 5.     ])


numpy.testing.assert_array_almost_equal_nulp   testing.assert_array_almost_equal_nulp(x, y, nulp=1)[source]

Compare two arrays relatively to their spacing. This is a relatively robust method to compare two arrays whose amplitude is variable.  Parameters

x, yarray_like

Input arrays.
nulpint, optional

The maximum number of unit in the last place for tolerance (see Notes). Default is 1.    Returns
 None
  Raises
 AssertionError

If the spacing between x and y for one or more elements is larger than nulp.      See also  assert_array_max_ulp

Check that all items of arrays differ in at most N Units in the Last Place.  spacing

Return the distance between x and the nearest adjacent number.    Notes An assertion is raised if the following condition is not met: abs(x - y) <= nulps * spacing(maximum(abs(x), abs(y)))
 Examples >>> x = np.array([1., 1e-10, 1e-20])
>>> eps = np.finfo(x.dtype).eps
>>> np.testing.assert_array_almost_equal_nulp(x, x*eps/2 + x)
 >>> np.testing.assert_array_almost_equal_nulp(x, x*eps + x)
Traceback (most recent call last):
  ...
AssertionError: X and Y are not equal to 1 ULP (max is 2)


numpy.testing.assert_array_equal   testing.assert_array_equal(x, y, err_msg='', verbose=True)[source]

Raises an AssertionError if two array_like objects are not equal. Given two array_like objects, check that the shape is equal and all elements of these objects are equal (but see the Notes for the special handling of a scalar). An exception is raised at shape mismatch or conflicting values. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions. The usual caution for verifying equality with floating point numbers is advised.  Parameters

xarray_like

The actual object to check.
yarray_like

The desired, expected object.
err_msgstr, optional

The error message to be printed in case of failure.
verbosebool, optional

If True, the conflicting values are appended to the error message.    Raises
 AssertionError

If actual and desired objects are not equal.      See also  assert_allclose

Compare two array_like objects for equality with desired relative and/or absolute precision.
assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal

  Notes When one of x and y is a scalar and the other is array_like, the function checks that each element of the array_like object is equal to the scalar. Examples The first assert does not raise an exception: >>> np.testing.assert_array_equal([1.0,2.33333,np.nan],
...                               [np.exp(0),2.33333, np.nan])
 Assert fails with numerical imprecision with floats: >>> np.testing.assert_array_equal([1.0,np.pi,np.nan],
...                               [1, np.sqrt(np.pi)**2, np.nan])
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not equal

Mismatched elements: 1 / 3 (33.3%)
Max absolute difference: 4.4408921e-16
Max relative difference: 1.41357986e-16
 x: array([1.      , 3.141593,      nan])
 y: array([1.      , 3.141593,      nan])
 Use assert_allclose or one of the nulp (number of floating point values) functions for these cases instead: >>> np.testing.assert_allclose([1.0,np.pi,np.nan],
...                            [1, np.sqrt(np.pi)**2, np.nan],
...                            rtol=1e-10, atol=0)
 As mentioned in the Notes section, assert_array_equal has special handling for scalars. Here the test checks that each value in x is 3: >>> x = np.full((2, 5), fill_value=3)
>>> np.testing.assert_array_equal(x, 3)


numpy.testing.assert_array_less   testing.assert_array_less(x, y, err_msg='', verbose=True)[source]

Raises an AssertionError if two array_like objects are not ordered by less than. Given two array_like objects, check that the shape is equal and all elements of the first object are strictly smaller than those of the second object. An exception is raised at shape mismatch or incorrectly ordered values. Shape mismatch does not raise if an object has zero dimension. In contrast to the standard usage in numpy, NaNs are compared, no assertion is raised if both objects have NaNs in the same positions.  Parameters

xarray_like

The smaller object to check.
yarray_like

The larger object to compare.
err_msgstring

The error message to be printed in case of failure.
verbosebool

If True, the conflicting values are appended to the error message.    Raises
 AssertionError

If actual and desired objects are not equal.      See also  assert_array_equal

tests objects for equality  assert_array_almost_equal

test objects for equality up to precision    Examples >>> np.testing.assert_array_less([1.0, 1.0, np.nan], [1.1, 2.0, np.nan])
>>> np.testing.assert_array_less([1.0, 1.0, np.nan], [1, 2.0, np.nan])
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not less-ordered

Mismatched elements: 1 / 3 (33.3%)
Max absolute difference: 1.
Max relative difference: 0.5
 x: array([ 1.,  1., nan])
 y: array([ 1.,  2., nan])
 >>> np.testing.assert_array_less([1.0, 4.0], 3)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not less-ordered

Mismatched elements: 1 / 2 (50%)
Max absolute difference: 2.
Max relative difference: 0.66666667
 x: array([1., 4.])
 y: array(3)
 >>> np.testing.assert_array_less([1.0, 2.0, 3.0], [4])
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not less-ordered

(shapes (3,), (1,) mismatch)
 x: array([1., 2., 3.])
 y: array([4])


numpy.testing.assert_array_max_ulp   testing.assert_array_max_ulp(a, b, maxulp=1, dtype=None)[source]

Check that all items of arrays differ in at most N Units in the Last Place.  Parameters

a, barray_like

Input arrays to be compared.
maxulpint, optional

The maximum number of units in the last place that elements of a and b can differ. Default is 1.
dtypedtype, optional

Data-type to convert a and b to if given. Default is None.    Returns

retndarray

Array containing number of representable floating point numbers between items in a and b.    Raises
 AssertionError

If one or more elements differ by more than maxulp.      See also  assert_array_almost_equal_nulp

Compare two arrays relatively to their spacing.    Notes For computing the ULP difference, this API does not differentiate between various representations of NAN (ULP difference between 0x7fc00000 and 0xffc00000 is zero). Examples >>> a = np.linspace(0., 1., 100)
>>> res = np.testing.assert_array_max_ulp(a, np.arcsin(np.sin(a)))


numpy.testing.assert_equal   testing.assert_equal(actual, desired, err_msg='', verbose=True)[source]

Raises an AssertionError if two objects are not equal. Given two objects (scalars, lists, tuples, dictionaries or numpy arrays), check that all elements of these objects are equal. An exception is raised at the first conflicting values. When one of actual and desired is a scalar and the other is array_like, the function checks that each element of the array_like object is equal to the scalar. This function handles NaN comparisons as if NaN was a “normal” number. That is, AssertionError is not raised if both objects have NaNs in the same positions. This is in contrast to the IEEE standard on NaNs, which says that NaN compared to anything must return False.  Parameters

actualarray_like

The object to check.
desiredarray_like

The expected object.
err_msgstr, optional

The error message to be printed in case of failure.
verbosebool, optional

If True, the conflicting values are appended to the error message.    Raises
 AssertionError

If actual and desired are not equal.     Examples >>> np.testing.assert_equal([4,5], [4,6])
Traceback (most recent call last):
    ...
AssertionError:
Items are not equal:
item=1
 ACTUAL: 5
 DESIRED: 6
 The following comparison does not raise an exception. There are NaNs in the inputs, but they are in the same positions. >>> np.testing.assert_equal(np.array([1.0, 2.0, np.nan]), [1, 2, np.nan])


numpy.testing.assert_raises   testing.assert_raises(exception_class, callable, *args, **kwargs) assert_raises(exception_class)[source]
  testing.assert_raises(exception_class) → None

Fail unless an exception of class exception_class is thrown by callable when invoked with arguments args and keyword arguments kwargs. If a different type of exception is thrown, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception. Alternatively, assert_raises can be used as a context manager: >>> from numpy.testing import assert_raises
>>> with assert_raises(ZeroDivisionError):
...     1 / 0
 is equivalent to >>> def div(x, y):
...     return x / y
>>> assert_raises(ZeroDivisionError, div, 1, 0)


numpy.testing.assert_raises_regex   testing.assert_raises_regex(exception_class, expected_regexp, callable, *args, **kwargs) assert_raises_regex(exception_class, expected_regexp)[source]

Fail unless an exception of class exception_class and with message that matches expected_regexp is thrown by callable when invoked with arguments args and keyword arguments kwargs. Alternatively, can be used as a context manager like assert_raises. Name of this function adheres to Python 3.2+ reference, but should work in all versions down to 2.6. Notes  New in version 1.9.0.


numpy.testing.assert_string_equal   testing.assert_string_equal(actual, desired)[source]

Test if two strings are equal. If the given strings are equal, assert_string_equal does nothing. If they are not equal, an AssertionError is raised, and the diff between the strings is shown.  Parameters

actualstr

The string to test for equality against the expected string.
desiredstr

The expected string.     Examples >>> np.testing.assert_string_equal('abc', 'abc')
>>> np.testing.assert_string_equal('abc', 'abcd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
...
AssertionError: Differences in strings:
- abc+ abcd?    +


numpy.testing.assert_warns   testing.assert_warns(warning_class, *args, **kwargs)[source]

Fail unless the given callable throws the specified warning. A warning of class warning_class should be thrown by the callable when invoked with arguments args and keyword arguments kwargs. If a different type of warning is thrown, it will not be caught. If called with all arguments other than the warning class omitted, may be used as a context manager:  with assert_warns(SomeWarning):

do_something()   The ability to be used as a context manager is new in NumPy v1.11.0.  New in version 1.4.0.   Parameters

warning_classclass

The class defining the warning that func is expected to throw.
funccallable, optional

Callable to test
*argsArguments

Arguments for func.
**kwargsKwargs

Keyword arguments for func.    Returns
 The value returned by func.
   Examples >>> import warnings
>>> def deprecated_func(num):
...     warnings.warn("Please upgrade", DeprecationWarning)
...     return num*num
>>> with np.testing.assert_warns(DeprecationWarning):
...     assert deprecated_func(4) == 16
>>> # or passing a func
>>> ret = np.testing.assert_warns(DeprecationWarning, deprecated_func, 4)
>>> assert ret == 16


numpy.testing.dec.deprecated   testing.dec.deprecated(conditional=True)[source]

 Deprecated since version 1.21: This decorator is retained for compatibility with the nose testing framework, which is being phased out. Please use the nose2 or pytest frameworks instead.  Filter deprecation warnings while running the test suite. This decorator can be used to filter DeprecationWarning’s, to avoid printing them during the test suite run, while checking that the test actually raises a DeprecationWarning.  Parameters

conditionalbool or callable, optional

Flag to determine whether to mark test as deprecated or not. If the condition is a callable, it is used at runtime to dynamically make the decision. Default is True.    Returns

decoratorfunction

The deprecated decorator itself.     Notes  New in version 1.4.0.


numpy.testing.dec.knownfailureif   testing.dec.knownfailureif(fail_condition, msg=None)[source]

 Deprecated since version 1.21: This decorator is retained for compatibility with the nose testing framework, which is being phased out. Please use the nose2 or pytest frameworks instead.  Make function raise KnownFailureException exception if given condition is true. If the condition is a callable, it is used at runtime to dynamically make the decision. This is useful for tests that may require costly imports, to delay the cost until the test suite is actually executed.  Parameters

fail_conditionbool or callable

Flag to determine whether to mark the decorated test as a known failure (if True) or not (if False).
msgstr, optional

Message to give on raising a KnownFailureException exception. Default is None.    Returns

decoratorfunction

Decorator, which, when applied to a function, causes KnownFailureException to be raised when fail_condition is True, and the function to be called normally otherwise.     Notes The decorator itself is decorated with the nose.tools.make_decorator function in order to transmit function name, and various other metadata.


numpy.testing.dec.setastest   testing.dec.setastest(tf=True)[source]

 Deprecated since version 1.21: This decorator is retained for compatibility with the nose testing framework, which is being phased out. Please use the nose2 or pytest frameworks instead.  Signals to nose that this function is or is not a test.  Parameters

tfbool

If True, specifies that the decorated callable is a test. If False, specifies that the decorated callable is not a test. Default is True.     Notes This decorator can’t use the nose namespace, because it can be called from a non-test module. See also istest and nottest in nose.tools. Examples setastest can be used in the following way: from numpy.testing import dec

@dec.setastest(False)
def func_with_test_in_name(arg1, arg2):
    pass


numpy.testing.dec.skipif   testing.dec.skipif(skip_condition, msg=None)[source]

 Deprecated since version 1.21: This decorator is retained for compatibility with the nose testing framework, which is being phased out. Please use the nose2 or pytest frameworks instead.  Make function raise SkipTest exception if a given condition is true. If the condition is a callable, it is used at runtime to dynamically make the decision. This is useful for tests that may require costly imports, to delay the cost until the test suite is actually executed.  Parameters

skip_conditionbool or callable

Flag to determine whether to skip the decorated test.
msgstr, optional

Message to give on raising a SkipTest exception. Default is None.    Returns

decoratorfunction

Decorator which, when applied to a function, causes SkipTest to be raised when skip_condition is True, and the function to be called normally otherwise.     Notes The decorator itself is decorated with the nose.tools.make_decorator function in order to transmit function name, and various other metadata.


numpy.testing.dec.slow   testing.dec.slow(t)[source]

 Deprecated since version 1.21: This decorator is retained for compatibility with the nose testing framework, which is being phased out. Please use the nose2 or pytest frameworks instead.  Label a test as ‘slow’. The exact definition of a slow test is obviously both subjective and hardware-dependent, but in general any individual test that requires more than a second or two should be labeled as slow (the whole suite consists of thousands of tests, so even a second is significant).  Parameters

tcallable

The test to label as slow.    Returns

tcallable

The decorated test t.     Examples The numpy.testing module includes import decorators as dec. A test can be decorated as slow like this: from numpy.testing import *

@dec.slow
def test_big(self):
    print('Big, slow test')


numpy.testing.decorate_methods   testing.decorate_methods(cls, decorator, testmatch=None)[source]

Apply a decorator to all methods in a class matching a regular expression. The given decorator is applied to all public methods of cls that are matched by the regular expression testmatch (testmatch.search(methodname)). Methods that are private, i.e. start with an underscore, are ignored.  Parameters

clsclass

Class whose methods to decorate.
decoratorfunction

Decorator to apply to methods
testmatchcompiled regexp or str, optional

The regular expression. Default value is None, in which case the nose default (re.compile(r'(?:^|[\b_\.%s-])[Tt]est' % os.sep)) is used. If testmatch is a string, it is compiled to a regular expression first.


numpy.testing.run_module_suite   testing.run_module_suite(file_to_run=None, argv=None)[source]

Run a test module. Equivalent to calling $ nosetests <argv> <file_to_run> from the command line  Parameters

file_to_runstr, optional

Path to test module, or None. By default, run the module from which this function is called.
argvlist of strings

Arguments to be passed to the nose test runner. argv[0] is ignored. All command line arguments accepted by nosetests will work. If it is the default value None, sys.argv is used.  New in version 1.9.0.      Examples Adding the following: if __name__ == "__main__" :
    run_module_suite(argv=sys.argv)
 at the end of a test module will run the tests when that module is called in the python interpreter. Alternatively, calling: >>> run_module_suite(file_to_run="numpy/tests/test_matlib.py")
 from an interpreter will run all the test routine in ‘test_matlib.py’.


numpy.testing.rundocs   testing.rundocs(filename=None, raise_on_error=True)[source]

Run doctests found in the given file. By default rundocs raises an AssertionError on failure.  Parameters

filenamestr

The path to the file for which the doctests are run.
raise_on_errorbool

Whether to raise an AssertionError when a doctest fails. Default is True.     Notes The doctests can be run by the user/developer by adding the doctests argument to the test() call. For example, to run all tests (including doctests) for numpy.lib: >>> np.lib.test(doctests=True)


numpy.testing.suppress_warnings.__call__ method   testing.suppress_warnings.__call__(func)[source]

Function decorator to apply certain suppressions to a whole function.


numpy.testing.suppress_warnings.filter method   testing.suppress_warnings.filter(category=<class 'Warning'>, message='', module=None)[source]

Add a new suppressing filter or apply it if the state is entered.  Parameters

categoryclass, optional

Warning class to filter
messagestring, optional

Regular expression matching the warning message.
modulemodule, optional

Module to filter for. Note that the module (and its file) must match exactly and cannot be a submodule. This may make it unreliable for external modules.     Notes When added within a context, filters are only added inside the context and will be forgotten when the context is exited.


numpy.testing.suppress_warnings.record method   testing.suppress_warnings.record(category=<class 'Warning'>, message='', module=None)[source]

Append a new recording filter or apply it if the state is entered. All warnings matching will be appended to the log attribute.  Parameters

categoryclass, optional

Warning class to filter
messagestring, optional

Regular expression matching the warning message.
modulemodule, optional

Module to filter for. Note that the module (and its file) must match exactly and cannot be a submodule. This may make it unreliable for external modules.    Returns

loglist

A list which will be filled with all matched warnings.     Notes When added within a context, filters are only added inside the context and will be forgotten when the context is exited.


numpy.ufunc.__call__ method   ufunc.__call__(*args, **kwargs)

Call self as a function.


numpy.ufunc.accumulate method   ufunc.accumulate(array, axis=0, dtype=None, out=None)

Accumulate the result of applying the operator to all elements. For a one-dimensional array, accumulate produces results equivalent to: r = np.empty(len(A))
t = op.identity        # op = the ufunc being applied to A's  elements
for i in range(len(A)):
    t = op(t, A[i])
    r[i] = t
return r
 For example, add.accumulate() is equivalent to np.cumsum(). For a multi-dimensional array, accumulate is applied along only one axis (axis zero by default; see Examples below) so repeated use is necessary if one wants to accumulate over multiple axes.  Parameters

arrayarray_like

The array to act on.
axisint, optional

The axis along which to apply the accumulation; default is zero.
dtypedata-type code, optional

The data-type used to represent the intermediate results. Defaults to the data-type of the output array if such is provided, or the the data-type of the input array if no output array is provided.
outndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If not provided or None, a freshly-allocated array is returned. For consistency with ufunc.__call__, if given as a keyword, this may be wrapped in a 1-element tuple.  Changed in version 1.13.0: Tuples are allowed for keyword argument.     Returns

rndarray

The accumulated values. If out was supplied, r is a reference to out.     Examples 1-D array examples: >>> np.add.accumulate([2, 3, 5])
array([ 2,  5, 10])
>>> np.multiply.accumulate([2, 3, 5])
array([ 2,  6, 30])
 2-D array examples: >>> I = np.eye(2)
>>> I
array([[1.,  0.],
       [0.,  1.]])
 Accumulate along axis 0 (rows), down columns: >>> np.add.accumulate(I, 0)
array([[1.,  0.],
       [1.,  1.]])
>>> np.add.accumulate(I) # no axis specified = axis zero
array([[1.,  0.],
       [1.,  1.]])
 Accumulate along axis 1 (columns), through rows: >>> np.add.accumulate(I, 1)
array([[1.,  1.],
       [0.,  1.]])


numpy.ufunc.at method   ufunc.at(a, indices, b=None, /)

Performs unbuffered in place operation on operand ‘a’ for elements specified by ‘indices’. For addition ufunc, this method is equivalent to a[indices] += b, except that results are accumulated for elements that are indexed more than once. For example, a[[0,0]] += 1 will only increment the first element once because of buffering, whereas add.at(a, [0,0], 1) will increment the first element twice.  New in version 1.8.0.   Parameters

aarray_like

The array to perform in place operation on.
indicesarray_like or tuple

Array like index object or slice object for indexing into first operand. If first operand has multiple dimensions, indices can be a tuple of array like index objects or slice objects.
barray_like

Second operand for ufuncs requiring two operands. Operand must be broadcastable over first operand after indexing or slicing.     Examples Set items 0 and 1 to their negative values: >>> a = np.array([1, 2, 3, 4])
>>> np.negative.at(a, [0, 1])
>>> a
array([-1, -2,  3,  4])
 Increment items 0 and 1, and increment item 2 twice: >>> a = np.array([1, 2, 3, 4])
>>> np.add.at(a, [0, 1, 2, 2], 1)
>>> a
array([2, 3, 5, 4])
 Add items 0 and 1 in first array to second array, and store results in first array: >>> a = np.array([1, 2, 3, 4])
>>> b = np.array([1, 2])
>>> np.add.at(a, [0, 1], b)
>>> a
array([2, 4, 3, 4])


numpy.ufunc.identity attribute   ufunc.identity

The identity value. Data attribute containing the identity element for the ufunc, if it has one. If it does not, the attribute value is None. Examples >>> np.add.identity
0
>>> np.multiply.identity
1
>>> np.power.identity
1
>>> print(np.exp.identity)
None


numpy.ufunc.nargs attribute   ufunc.nargs

The number of arguments. Data attribute containing the number of arguments the ufunc takes, including optional ones. Notes Typically this value will be one more than what you might expect because all ufuncs take the optional “out” argument. Examples >>> np.add.nargs
3
>>> np.multiply.nargs
3
>>> np.power.nargs
3
>>> np.exp.nargs
2


numpy.ufunc.nin attribute   ufunc.nin

The number of inputs. Data attribute containing the number of arguments the ufunc treats as input. Examples >>> np.add.nin
2
>>> np.multiply.nin
2
>>> np.power.nin
2
>>> np.exp.nin
1


numpy.ufunc.nout attribute   ufunc.nout

The number of outputs. Data attribute containing the number of arguments the ufunc treats as output. Notes Since all ufuncs can take output arguments, this will always be (at least) 1. Examples >>> np.add.nout
1
>>> np.multiply.nout
1
>>> np.power.nout
1
>>> np.exp.nout
1


numpy.ufunc.ntypes attribute   ufunc.ntypes

The number of types. The number of numerical NumPy types - of which there are 18 total - on which the ufunc can operate.  See also  numpy.ufunc.types
  Examples >>> np.add.ntypes
18
>>> np.multiply.ntypes
18
>>> np.power.ntypes
17
>>> np.exp.ntypes
7
>>> np.remainder.ntypes
14


numpy.ufunc.outer method   ufunc.outer(A, B, /, **kwargs)

Apply the ufunc op to all pairs (a, b) with a in A and b in B. Let M = A.ndim, N = B.ndim. Then the result, C, of op.outer(A, B) is an array of dimension M + N such that:  \[C[i_0, ..., i_{M-1}, j_0, ..., j_{N-1}] = op(A[i_0, ..., i_{M-1}], B[j_0, ..., j_{N-1}])\] For A and B one-dimensional, this is equivalent to: r = empty(len(A),len(B))
for i in range(len(A)):
    for j in range(len(B)):
        r[i,j] = op(A[i], B[j])  # op = ufunc in question
  Parameters

Aarray_like

First array
Barray_like

Second array
kwargsany

Arguments to pass on to the ufunc. Typically dtype or out. See ufunc for a comprehensive overview of all available arguments.    Returns

rndarray

Output array      See also  numpy.outer

A less powerful version of np.multiply.outer that ravels all inputs to 1D. This exists primarily for compatibility with old code.  tensordot

np.tensordot(a, b, axes=((), ())) and np.multiply.outer(a, b) behave same for all dimensions of a and b.    Examples >>> np.multiply.outer([1, 2, 3], [4, 5, 6])
array([[ 4,  5,  6],
       [ 8, 10, 12],
       [12, 15, 18]])
 A multi-dimensional example: >>> A = np.array([[1, 2, 3], [4, 5, 6]])
>>> A.shape
(2, 3)
>>> B = np.array([[1, 2, 3, 4]])
>>> B.shape
(1, 4)
>>> C = np.multiply.outer(A, B)
>>> C.shape; C
(2, 3, 1, 4)
array([[[[ 1,  2,  3,  4]],
        [[ 2,  4,  6,  8]],
        [[ 3,  6,  9, 12]]],
       [[[ 4,  8, 12, 16]],
        [[ 5, 10, 15, 20]],
        [[ 6, 12, 18, 24]]]])


numpy.ufunc.reduce method   ufunc.reduce(array, axis=0, dtype=None, out=None, keepdims=False, initial=<no value>, where=True)

Reduces array’s dimension by one, by applying ufunc along one axis. Let \(array.shape = (N_0, ..., N_i, ..., N_{M-1})\). Then \(ufunc.reduce(array, axis=i)[k_0, ..,k_{i-1}, k_{i+1}, .., k_{M-1}]\) = the result of iterating j over \(range(N_i)\), cumulatively applying ufunc to each \(array[k_0, ..,k_{i-1}, j, k_{i+1}, .., k_{M-1}]\). For a one-dimensional array, reduce produces results equivalent to: r = op.identity # op = ufunc
for i in range(len(A)):
  r = op(r, A[i])
return r
 For example, add.reduce() is equivalent to sum().  Parameters

arrayarray_like

The array to act on.
axisNone or int or tuple of ints, optional

Axis or axes along which a reduction is performed. The default (axis = 0) is perform a reduction over the first dimension of the input array. axis may be negative, in which case it counts from the last to the first axis.  New in version 1.7.0.  If this is None, a reduction is performed over all the axes. If this is a tuple of ints, a reduction is performed on multiple axes, instead of a single axis or all the axes as before. For operations which are either not commutative or not associative, doing a reduction over multiple axes is not well-defined. The ufuncs do not currently raise an exception in this case, but will likely do so in the future.
dtypedata-type code, optional

The type used to represent the intermediate results. Defaults to the data-type of the output array if this is provided, or the data-type of the input array if no output array is provided.
outndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If not provided or None, a freshly-allocated array is returned. For consistency with ufunc.__call__, if given as a keyword, this may be wrapped in a 1-element tuple.  Changed in version 1.13.0: Tuples are allowed for keyword argument.
keepdimsbool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array.  New in version 1.7.0.
initialscalar, optional

The value with which to start the reduction. If the ufunc has no identity or the dtype is object, this defaults to None - otherwise it defaults to ufunc.identity. If None is given, the first element of the reduction is used, and an error is thrown if the reduction is empty.  New in version 1.15.0.
wherearray_like of bool, optional

A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. Note that for ufuncs like minimum that do not have an identity defined, one has to pass in also initial.  New in version 1.17.0.     Returns

rndarray

The reduced array. If out was supplied, r is a reference to it.     Examples >>> np.multiply.reduce([2,3,5])
30
 A multi-dimensional array example: >>> X = np.arange(8).reshape((2,2,2))
>>> X
array([[[0, 1],
        [2, 3]],
       [[4, 5],
        [6, 7]]])
>>> np.add.reduce(X, 0)
array([[ 4,  6],
       [ 8, 10]])
>>> np.add.reduce(X) # confirm: default axis value is 0
array([[ 4,  6],
       [ 8, 10]])
>>> np.add.reduce(X, 1)
array([[ 2,  4],
       [10, 12]])
>>> np.add.reduce(X, 2)
array([[ 1,  5],
       [ 9, 13]])
 You can use the initial keyword argument to initialize the reduction with a different value, and where to select specific elements to include: >>> np.add.reduce([10], initial=5)
15
>>> np.add.reduce(np.ones((2, 2, 2)), axis=(0, 2), initial=10)
array([14., 14.])
>>> a = np.array([10., np.nan, 10])
>>> np.add.reduce(a, where=~np.isnan(a))
20.0
 Allows reductions of empty arrays where they would normally fail, i.e. for ufuncs without an identity. >>> np.minimum.reduce([], initial=np.inf)
inf
>>> np.minimum.reduce([[1., 2.], [3., 4.]], initial=10., where=[True, False])
array([ 1., 10.])
>>> np.minimum.reduce([])
Traceback (most recent call last):
    ...
ValueError: zero-size array to reduction operation minimum which has no identity


numpy.ufunc.reduceat method   ufunc.reduceat(array, indices, axis=0, dtype=None, out=None)

Performs a (local) reduce with specified slices over a single axis. For i in range(len(indices)), reduceat computes ufunc.reduce(array[indices[i]:indices[i+1]]), which becomes the i-th generalized “row” parallel to axis in the final result (i.e., in a 2-D array, for example, if axis = 0, it becomes the i-th row, but if axis = 1, it becomes the i-th column). There are three exceptions to this:  when i = len(indices) - 1 (so for the last index), indices[i+1] = array.shape[axis]. if indices[i] >= indices[i + 1], the i-th generalized “row” is simply array[indices[i]]. if indices[i] >= len(array) or indices[i] < 0, an error is raised.  The shape of the output depends on the size of indices, and may be larger than array (this happens if len(indices) > array.shape[axis]).  Parameters

arrayarray_like

The array to act on.
indicesarray_like

Paired indices, comma separated (not colon), specifying slices to reduce.
axisint, optional

The axis along which to apply the reduceat.
dtypedata-type code, optional

The type used to represent the intermediate results. Defaults to the data type of the output array if this is provided, or the data type of the input array if no output array is provided.
outndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If not provided or None, a freshly-allocated array is returned. For consistency with ufunc.__call__, if given as a keyword, this may be wrapped in a 1-element tuple.  Changed in version 1.13.0: Tuples are allowed for keyword argument.     Returns

rndarray

The reduced values. If out was supplied, r is a reference to out.     Notes A descriptive example: If array is 1-D, the function ufunc.accumulate(array) is the same as ufunc.reduceat(array, indices)[::2] where indices is range(len(array) - 1) with a zero placed in every other element: indices = zeros(2 * len(array) - 1), indices[1::2] = range(1, len(array)). Don’t be fooled by this attribute’s name: reduceat(array) is not necessarily smaller than array. Examples To take the running sum of four successive values: >>> np.add.reduceat(np.arange(8),[0,4, 1,5, 2,6, 3,7])[::2]
array([ 6, 10, 14, 18])
 A 2-D example: >>> x = np.linspace(0, 15, 16).reshape(4,4)
>>> x
array([[ 0.,   1.,   2.,   3.],
       [ 4.,   5.,   6.,   7.],
       [ 8.,   9.,  10.,  11.],
       [12.,  13.,  14.,  15.]])
 # reduce such that the result has the following five rows:
# [row1 + row2 + row3]
# [row4]
# [row2]
# [row3]
# [row1 + row2 + row3 + row4]
 >>> np.add.reduceat(x, [0, 3, 1, 2, 0])
array([[12.,  15.,  18.,  21.],
       [12.,  13.,  14.,  15.],
       [ 4.,   5.,   6.,   7.],
       [ 8.,   9.,  10.,  11.],
       [24.,  28.,  32.,  36.]])
 # reduce such that result has the following two columns:
# [col1 * col2 * col3, col4]
 >>> np.multiply.reduceat(x, [0, 3], 1)
array([[   0.,     3.],
       [ 120.,     7.],
       [ 720.,    11.],
       [2184.,    15.]])


numpy.ufunc.signature attribute   ufunc.signature

Definition of the core elements a generalized ufunc operates on. The signature determines how the dimensions of each input/output array are split into core and loop dimensions:  Each dimension in the signature is matched to a dimension of the corresponding passed-in array, starting from the end of the shape tuple. Core dimensions assigned to the same label in the signature must have exactly matching sizes, no broadcasting is performed. The core dimensions are removed from all inputs and the remaining dimensions are broadcast together, defining the loop dimensions.  Notes Generalized ufuncs are used internally in many linalg functions, and in the testing suite; the examples below are taken from these. For ufuncs that operate on scalars, the signature is None, which is equivalent to ‘()’ for every argument. Examples >>> np.core.umath_tests.matrix_multiply.signature
'(m,n),(n,p)->(m,p)'
>>> np.linalg._umath_linalg.det.signature
'(m,m)->()'
>>> np.add.signature is None
True  # equivalent to '(),()->()'


numpy.ufunc.types attribute   ufunc.types

Returns a list with types grouped input->output. Data attribute listing the data-type “Domain-Range” groupings the ufunc can deliver. The data-types are given using the character codes.  See also  numpy.ufunc.ntypes
  Examples >>> np.add.types
['??->?', 'bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l',
'LL->L', 'qq->q', 'QQ->Q', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D',
'GG->G', 'OO->O']
 >>> np.multiply.types
['??->?', 'bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l',
'LL->L', 'qq->q', 'QQ->Q', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D',
'GG->G', 'OO->O']
 >>> np.power.types
['bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 'LL->L',
'qq->q', 'QQ->Q', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D', 'GG->G',
'OO->O']
 >>> np.exp.types
['f->f', 'd->d', 'g->g', 'F->F', 'D->D', 'G->G', 'O->O']
 >>> np.remainder.types
['bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 'LL->L',
'qq->q', 'QQ->Q', 'ff->f', 'dd->d', 'gg->g', 'OO->O']


numpy.vectorize.__call__ method   vectorize.__call__(*args, **kwargs)[source]

Return arrays with the results of pyfunc broadcast (vectorized) over args and kwargs not in excluded.


The Array Interface  Note This page describes the numpy-specific API for accessing the contents of a numpy array from other C extensions. PEP 3118 – The Revised Buffer Protocol introduces similar, standardized API to Python 2.6 and 3.0 for any extension module to use. Cython’s buffer array support uses the PEP 3118 API; see the Cython numpy tutorial. Cython provides a way to write code that supports the buffer protocol with Python versions older than 2.6 because it has a backward-compatible implementation utilizing the array interface described here.   version
3   The array interface (sometimes called array protocol) was created in 2005 as a means for array-like Python objects to re-use each other’s data buffers intelligently whenever possible. The homogeneous N-dimensional array interface is a default mechanism for objects to share N-dimensional array memory and information. The interface consists of a Python-side and a C-side using two attributes. Objects wishing to be considered an N-dimensional array in application code should support at least one of these attributes. Objects wishing to support an N-dimensional array in application code should look for at least one of these attributes and use the information provided appropriately. This interface describes homogeneous arrays in the sense that each item of the array has the same “type”. This type can be very simple or it can be a quite arbitrary and complicated C-like structure. There are two ways to use the interface: A Python side and a C-side. Both are separate attributes.  Python side This approach to the interface consists of the object having an __array_interface__ attribute.   object.__array_interface__

A dictionary of items (3 required and 5 optional). The optional keys in the dictionary have implied defaults if they are not provided. The keys are:
shape (required)

Tuple whose elements are the array size in each dimension. Each entry is an integer (a Python int). Note that these integers could be larger than the platform int or long could hold (a Python int is a C long). It is up to the code using this attribute to handle this appropriately; either by raising an error when overflow is possible, or by using long long as the C type for the shapes.
typestr (required)

A string providing the basic type of the homogeneous array The basic string format consists of 3 parts: a character describing the byteorder of the data (<: little-endian, >: big-endian, |: not-relevant), a character code giving the basic type of the array, and an integer providing the number of bytes the type uses. The basic type character codes are:
t Bit field (following integer gives the number of bits in the bit field).
b Boolean (integer type where all values are only True or False)
i Integer
u Unsigned integer
f Floating point
c Complex floating point
m Timedelta
M Datetime
O Object (i.e. the memory contains a pointer to PyObject)
S String (fixed-length sequence of char)
U Unicode (fixed-length sequence of Py_UNICODE)
V Other (void * – each item is a fixed-size chunk of memory)
descr (optional)

A list of tuples providing a more detailed description of the memory layout for each item in the homogeneous array. Each tuple in the list has two or three elements. Normally, this attribute would be used when typestr is V[0-9]+, but this is not a requirement. The only requirement is that the number of bytes represented in the typestr key is the same as the total number of bytes represented here. The idea is to support descriptions of C-like structs that make up array elements. The elements of each tuple in the list are  A string providing a name associated with this portion of the datatype. This could also be a tuple of ('full name',
'basic_name') where basic name would be a valid Python variable name representing the full name of the field. Either a basic-type description string as in typestr or another list (for nested structured types) An optional shape tuple providing how many times this part of the structure should be repeated. No repeats are assumed if this is not given. Very complicated structures can be described using this generic interface. Notice, however, that each element of the array is still of the same data-type. Some examples of using this interface are given below.  Default: [('', typestr)]
data (optional)

A 2-tuple whose first argument is an integer (a long integer if necessary) that points to the data-area storing the array contents. This pointer must point to the first element of data (in other words any offset is always ignored in this case). The second entry in the tuple is a read-only flag (true means the data area is read-only). This attribute can also be an object exposing the buffer interface which will be used to share the data. If this key is not present (or returns None), then memory sharing will be done through the buffer interface of the object itself. In this case, the offset key can be used to indicate the start of the buffer. A reference to the object exposing the array interface must be stored by the new object if the memory area is to be secured. Default: None
strides (optional)

Either None to indicate a C-style contiguous array or a Tuple of strides which provides the number of bytes needed to jump to the next array element in the corresponding dimension. Each entry must be an integer (a Python int). As with shape, the values may be larger than can be represented by a C int or long; the calling code should handle this appropriately, either by raising an error, or by using long long in C. The default is None which implies a C-style contiguous memory buffer. In this model, the last dimension of the array varies the fastest. For example, the default strides tuple for an object whose array entries are 8 bytes long and whose shape is (10, 20, 30) would be (4800, 240, 8) Default: None (C-style contiguous)
mask (optional)

None or an object exposing the array interface. All elements of the mask array should be interpreted only as true or not true indicating which elements of this array are valid. The shape of this object should be “broadcastable” to the shape of the original array. Default: None (All array values are valid)
offset (optional)

An integer offset into the array data region. This can only be used when data is None or returns a buffer object. Default: 0.
version (required)

An integer showing the version of the interface (i.e. 3 for this version). Be careful not to use this to invalidate objects exposing future versions of the interface.
   C-struct access This approach to the array interface allows for faster access to an array using only one attribute lookup and a well-defined C-structure.   object.__array_struct__

A PyCapsule whose pointer member contains a pointer to a filled PyArrayInterface structure. Memory for the structure is dynamically created and the PyCapsule is also created with an appropriate destructor so the retriever of this attribute simply has to apply Py_DECREF to the object returned by this attribute when it is finished. Also, either the data needs to be copied out, or a reference to the object exposing this attribute must be held to ensure the data is not freed. Objects exposing the __array_struct__ interface must also not reallocate their memory if other objects are referencing them.
 The PyArrayInterface structure is defined in numpy/ndarrayobject.h as: typedef struct {
  int two;              /* contains the integer 2 -- simple sanity check */
  int nd;               /* number of dimensions */
  char typekind;        /* kind in array --- character code of typestr */
  int itemsize;         /* size of each element */
  int flags;            /* flags indicating how the data should be interpreted */
                        /*   must set ARR_HAS_DESCR bit to validate descr */
  Py_intptr_t *shape;   /* A length-nd array of shape information */
  Py_intptr_t *strides; /* A length-nd array of stride information */
  void *data;           /* A pointer to the first element of the array */
  PyObject *descr;      /* NULL or data-description (same as descr key
                                of __array_interface__) -- must set ARR_HAS_DESCR
                                flag or this will be ignored. */
} PyArrayInterface;
 The flags member may consist of 5 bits showing how the data should be interpreted and one bit showing how the Interface should be interpreted. The data-bits are NPY_ARRAY_C_CONTIGUOUS (0x1), NPY_ARRAY_F_CONTIGUOUS (0x2), NPY_ARRAY_ALIGNED (0x100), NPY_ARRAY_NOTSWAPPED (0x200), and NPY_ARRAY_WRITEABLE (0x400). A final flag NPY_ARR_HAS_DESCR (0x800) indicates whether or not this structure has the arrdescr field. The field should not be accessed unless this flag is present.   NPY_ARR_HAS_DESCR

  New since June 16, 2006: In the past most implementations used the desc member of the PyCObject (now PyCapsule) itself (do not confuse this with the “descr” member of the PyArrayInterface structure above — they are two separate things) to hold the pointer to the object exposing the interface. This is now an explicit part of the interface. Be sure to take a reference to the object and call PyCapsule_SetContext before returning the PyCapsule, and configure a destructor to decref this reference.    Type description examples For clarity it is useful to provide some examples of the type description and corresponding __array_interface__ ‘descr’ entries. Thanks to Scott Gilbert for these examples: In every case, the ‘descr’ key is optional, but of course provides more information which may be important for various applications: * Float data
    typestr == '>f4'
    descr == [('','>f4')]

* Complex double
    typestr == '>c8'
    descr == [('real','>f4'), ('imag','>f4')]

* RGB Pixel data
    typestr == '|V3'
    descr == [('r','|u1'), ('g','|u1'), ('b','|u1')]

* Mixed endian (weird but could happen).
    typestr == '|V8' (or '>u8')
    descr == [('big','>i4'), ('little','<i4')]

* Nested structure
    struct {
        int ival;
        struct {
            unsigned short sval;
            unsigned char bval;
            unsigned char cval;
        } sub;
    }
    typestr == '|V8' (or '<u8' if you want)
    descr == [('ival','<i4'), ('sub', [('sval','<u2'), ('bval','|u1'), ('cval','|u1') ]) ]

* Nested array
    struct {
        int ival;
        double data[16*4];
    }
    typestr == '|V516'
    descr == [('ival','>i4'), ('data','>f8',(16,4))]

* Padded structure
    struct {
        int ival;
        double dval;
    }
    typestr == '|V16'
    descr == [('ival','>i4'),('','|V4'),('dval','>f8')]
 It should be clear that any structured type could be described using this interface.   Differences with Array interface (Version 2) The version 2 interface was very similar. The differences were largely aesthetic. In particular:  The PyArrayInterface structure had no descr member at the end (and therefore no flag ARR_HAS_DESCR)
The context member of the PyCapsule (formally the desc member of the PyCObject) returned from __array_struct__ was not specified. Usually, it was the object exposing the array (so that a reference to it could be kept and destroyed when the C-object was destroyed). It is now an explicit requirement that this field be used in some way to hold a reference to the owning object.  Note Until August 2020, this said: Now it must be a tuple whose first element is a string with “PyArrayInterface Version #” and whose second element is the object exposing the array. This design was retracted almost immediately after it was proposed, in <https://mail.python.org/pipermail/numpy-discussion/2006-June/020995.html>. Despite 14 years of documentation to the contrary, at no point was it valid to assume that __array_interface__ capsules held this tuple content.   The tuple returned from __array_interface__['data'] used to be a hex-string (now it is an integer or a long integer).
There was no __array_interface__ attribute instead all of the keys (except for version) in the __array_interface__ dictionary were their own attribute: Thus to obtain the Python-side information you had to access separately the attributes:  __array_data__ __array_shape__ __array_strides__ __array_typestr__ __array_descr__ __array_offset__ __array_mask__


numpy.lib.format Binary serialization  NPY format A simple format for saving numpy arrays to disk with the full information about them. The .npy format is the standard binary file format in NumPy for persisting a single arbitrary NumPy array on disk. The format stores all of the shape and dtype information necessary to reconstruct the array correctly even on another machine with a different architecture. The format is designed to be as simple as possible while achieving its limited goals. The .npz format is the standard format for persisting multiple NumPy arrays on disk. A .npz file is a zip file containing multiple .npy files, one for each array.  Capabilities  Can represent all NumPy arrays including nested record arrays and object arrays. Represents the data in its native binary form. Supports Fortran-contiguous arrays directly. Stores all of the necessary information to reconstruct the array including shape and dtype on a machine of a different architecture. Both little-endian and big-endian arrays are supported, and a file with little-endian numbers will yield a little-endian array on any machine reading the file. The types are described in terms of their actual sizes. For example, if a machine with a 64-bit C “long int” writes out an array with “long ints”, a reading machine with 32-bit C “long ints” will yield an array with 64-bit integers. Is straightforward to reverse engineer. Datasets often live longer than the programs that created them. A competent developer should be able to create a solution in their preferred programming language to read most .npy files that they have been given without much documentation. Allows memory-mapping of the data. See open_memmap. Can be read from a filelike stream object instead of an actual file. Stores object arrays, i.e. arrays containing elements that are arbitrary Python objects. Files with object arrays are not to be mmapable, but can be read and written to disk.    Limitations  Arbitrary subclasses of numpy.ndarray are not completely preserved. Subclasses will be accepted for writing, but only the array data will be written out. A regular numpy.ndarray object will be created upon reading the file.   Warning Due to limitations in the interpretation of structured dtypes, dtypes with fields with empty names will have the names replaced by ‘f0’, ‘f1’, etc. Such arrays will not round-trip through the format entirely accurately. The data is intact; only the field names will differ. We are working on a fix for this. This fix will not require a change in the file format. The arrays with such structures can still be saved and restored, and the correct dtype may be restored by using the loadedarray.view(correct_dtype) method.    File extensions We recommend using the .npy and .npz extensions for files saved in this format. This is by no means a requirement; applications may wish to use these file formats but use an extension specific to the application. In the absence of an obvious alternative, however, we suggest using .npy and .npz.   Version numbering The version numbering of these formats is independent of NumPy version numbering. If the format is upgraded, the code in numpy.io will still be able to read and write Version 1.0 files.   Format Version 1.0 The first 6 bytes are a magic string: exactly \x93NUMPY. The next 1 byte is an unsigned byte: the major version number of the file format, e.g. \x01. The next 1 byte is an unsigned byte: the minor version number of the file format, e.g. \x00. Note: the version of the file format is not tied to the version of the numpy package. The next 2 bytes form a little-endian unsigned short int: the length of the header data HEADER_LEN. The next HEADER_LEN bytes form the header data describing the array’s format. It is an ASCII string which contains a Python literal expression of a dictionary. It is terminated by a newline (\n) and padded with spaces (\x20) to make the total of len(magic string) + 2 + len(length) + HEADER_LEN be evenly divisible by 64 for alignment purposes. The dictionary contains three keys:  “descr”dtype.descr

An object that can be passed as an argument to the numpy.dtype constructor to create the array’s dtype.  “fortran_order”bool

Whether the array data is Fortran-contiguous or not. Since Fortran-contiguous arrays are a common form of non-C-contiguity, we allow them to be written directly to disk for efficiency.  “shape”tuple of int

The shape of the array.   For repeatability and readability, the dictionary keys are sorted in alphabetic order. This is for convenience only. A writer SHOULD implement this if possible. A reader MUST NOT depend on this. Following the header comes the array data. If the dtype contains Python objects (i.e. dtype.hasobject is True), then the data is a Python pickle of the array. Otherwise the data is the contiguous (either C- or Fortran-, depending on fortran_order) bytes of the array. Consumers can figure out the number of bytes by multiplying the number of elements given by the shape (noting that shape=() means there is 1 element) by dtype.itemsize.   Format Version 2.0 The version 1.0 format only allowed the array header to have a total size of 65535 bytes. This can be exceeded by structured arrays with a large number of columns. The version 2.0 format extends the header size to 4 GiB. numpy.save will automatically save in 2.0 format if the data requires it, else it will always use the more compatible 1.0 format. The description of the fourth element of the header therefore has become: “The next 4 bytes form a little-endian unsigned int: the length of the header data HEADER_LEN.”   Format Version 3.0 This version replaces the ASCII string (which in practice was latin1) with a utf8-encoded string, so supports structured types with any unicode field names.   Notes The .npy format, including motivation for creating it and a comparison of alternatives, is described in the “npy-format” NEP, however details have evolved with time and this document is more current. Functions
descr_to_dtype(descr) Returns a dtype based off the given description.
dtype_to_descr(dtype) Get a serializable descriptor from the dtype.
header_data_from_array_1_0(array) Get the dictionary of header metadata from a numpy.ndarray.
magic(major, minor) Return the magic string for the given file format version.
open_memmap(filename[, mode, dtype, shape, ...]) Open a .npy file as a memory-mapped array.
read_array(fp[, allow_pickle, pickle_kwargs]) Read an array from an NPY file.
read_array_header_1_0(fp) Read an array header from a filelike object using the 1.0 file format version.
read_array_header_2_0(fp) Read an array header from a filelike object using the 2.0 file format version.
read_magic(fp) Read the magic string to get the version of the file format.
write_array(fp, array[, version, ...]) Write an array to an NPY file, including a header.
write_array_header_1_0(fp, d) Write the header for an array using the 1.0 format.
write_array_header_2_0(fp, d) Write the header for an array using the 2.0 format.