Returns True if two arrays are element-wise equal within a tolerance.
The tolerance values are positive, typically very small numbers. The
relative difference (rtol * abs(b)) and the absolute difference
atol are added together to compare against the absolute difference
between a and b.
NaNs are treated as equal if they are in the same place and if
equal_nan=True. Infs are treated as equal if they are in the same
place and of the same sign in both arrays.
Parameters:
a (array_like) – Input arrays to compare.
b (array_like) – Input arrays to compare.
rtol (float) – The relative tolerance parameter (see Notes).
atol (float) – The absolute tolerance parameter (see Notes).
If the following equation is element-wise True, then allclose returns
True.
absolute(a - b) <= (atol + rtol * absolute(b))
The above equation is not symmetric in a and b, so that
allclose(a,b) might be different from allclose(b,a) in
some rare cases.
The comparison of a and b uses standard broadcasting, which
means that a and b need not have the same shape in order for
allclose(a,b) to evaluate to True. The same is true for
equal but not array_equal.
allclose is not defined for non-numeric data types.
bool is considered a numeric data-type for this purpose.
out – The output array. The shape of out is identical to the shape of
arr, except along the axis dimension. This axis is removed, and
replaced with new dimensions equal to the shape of the return value
of func1d. So if func1d returns a scalar out will have one
fewer dimensions than arr.
>>> defmy_func(a):... """Average first and last element of a 1-D array"""... return(a[0]+a[-1])*0.5>>> b=np.array([[1,2,3],[4,5,6],[7,8,9]])>>> np.apply_along_axis(my_func,0,b)array([4., 5., 6.])>>> np.apply_along_axis(my_func,1,b)array([2., 5., 8.])
For a function that returns a 1D array, the number of dimensions in
outarr is the same as arr.
mxnet.numpy.fallback.apply_over_axes(func, a, axes)¶
Apply a function repeatedly over multiple axes.
func is called as res = func(a, axis), where axis is the first
element of axes. The result res of the function call must have
either the same dimensions as a or one less dimension. If res
has one less dimension than a, a dimension is inserted before
axis. The call to func is then repeated for each axis in axes,
with res as the first argument.
Parameters:
func (function) – This function must take two arguments, func(a, axis).
a (array_like) – Input array.
axes (array_like) – Axes over which func is applied; the elements must be integers.
Returns:
apply_over_axis – The output array. The number of dimensions is the same as a,
but the shape can be different. This depends on whether func
changes the shape of its output with respect to its input.
Apply a function to 1-D slices of an array along the given axis.
Notes
This function is equivalent to tuple axis arguments to reorderable ufuncs
with keepdims=True. Tuple axis arguments to ufuncs have been available since
version 1.7.0.
Perform an indirect partition along the given axis using the
algorithm specified by the kind keyword. It returns an array of
indices of the same shape as a that index data along the given
axis in partitioned order.
Element index to partition by. The k-th element will be in its
final sorted position and all smaller elements will be moved
before it and all larger elements behind it. The order of all
elements in the partitions is undefined. If provided with a
sequence of k-th it will partition all of them into their sorted
position at once.
Deprecated since version 1.22.0: Passing booleans as index is deprecated.
axis (int or None, optional) – Axis along which to sort. The default is -1 (the last axis). If
None, the flattened array is used.
kind ({'introselect'}, optional) – Selection algorithm. Default is ‘introselect’
order (str or list of str, optional) – When a is an array with fields defined, this argument
specifies which fields to compare first, second, etc. A single
field can be specified as a string, and not all fields need be
specified, but unspecified fields will still be used, in the
order in which they come up in the dtype, to break ties.
Returns:
index_array – Array of indices that partition a along the specified axis.
If a is one-dimensional, a[index_array] yields a partitioned a.
More generally, np.take_along_axis(a,index_array,axis=axis)
always yields the partitioned a, irrespective of dimensionality.
Find the indices of array elements that are non-zero, grouped by element.
Parameters:
a (array_like) – Input data.
Returns:
index_array – Indices of elements that are non-zero. Indices are grouped by element.
This array will have shape (N,a.ndim) where N is the number of
non-zero items.
Whether to compare NaN’s as equal. If the dtype of a1 and a2 is
complex, values will be considered equal if either the real or the
imaginary component of a given value is nan.
Construct an array from an index array and a list of arrays to choose from.
First of all, if confused or uncertain, definitely look at the Examples -
in its full generality, this function is less simple than it might
seem from the following code description (below ndi =
numpy.lib.index_tricks):
But this omits some subtleties. Here is a fully general summary:
Given an “index” array (a) of integers and a sequence of n arrays
(choices), a and each choice array are first broadcast, as necessary,
to arrays of a common shape; calling these Ba and Bchoices[i], i =
0,…,n-1 we have that, necessarily, Ba.shape==Bchoices[i].shape
for each i. Then, a new array with shape Ba.shape is created as
follows:
if mode='raise' (the default), then, first of all, each element of
a (and thus Ba) must be in the range [0,n-1]; now, suppose
that i (in that range) is the value at the (j0,j1,...,jm)
position in Ba - then the value at the same position in the new array
is the value in Bchoices[i] at that same position;
if mode='wrap', values in a (and thus Ba) may be any (signed)
integer; modular arithmetic is used to map integers outside the range
[0, n-1] back into that range; and then the new array is constructed
as above;
if mode='clip', values in a (and thus Ba) may be any (signed)
integer; negative integers are mapped to 0; values greater than n-1
are mapped to n-1; and then the new array is constructed as above.
Parameters:
a (int array) – This array must contain integers in [0,n-1], where n is the
number of choices, unless mode=wrap or mode=clip, in which
cases any integers are permissible.
choices (sequence of arrays) – Choice arrays. a and all of the choices must be broadcastable to the
same shape. If choices is itself an array (not recommended), then
its outermost dimension (i.e., the one corresponding to
choices.shape[0]) is taken as defining the “sequence”.
out (array, optional) – If provided, the result will be inserted into this array. It should
be of the appropriate shape and dtype. Note that out is always
buffered if mode='raise'; use other modes for better performance.
To reduce the chance of misinterpretation, even though the following
“abuse” is nominally supported, choices should neither be, nor be
thought of as, a single array, i.e., the outermost sequence-like container
should be either a list or a tuple.
Examples
>>> choices=[[0,1,2,3],[10,11,12,13],... [20,21,22,23],[30,31,32,33]]>>> np.choose([2,3,1,0],choices... # the first element of the result will be the first element of the... # third (2+1) "array" in choices, namely, 20; the second element... # will be the second element of the fourth (3+1) choice array, i.e.,... # 31, etc.... )array([20, 31, 12, 3])>>> np.choose([2,4,1,0],choices,mode='clip')# 4 goes to 3 (4-1)array([20, 31, 12, 3])>>> # because there are 4 choice arrays>>> np.choose([2,4,1,0],choices,mode='wrap')# 4 goes to (4 mod 4)array([20, 1, 12, 3])>>> # i.e., 0
A couple examples illustrating how choose broadcasts:
>>> # With thanks to Anne Archibald>>> a=np.array([0,1]).reshape((2,1,1))>>> c1=np.array([1,2,3]).reshape((1,3,1))>>> c2=np.array([-1,-2,-3,-4,-5]).reshape((1,1,5))>>> np.choose(a,(c1,c2))# result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2array([[[ 1, 1, 1, 1, 1], [ 2, 2, 2, 2, 2], [ 3, 3, 3, 3, 3]], [[-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5]]])
mxnet.numpy.fallback.compress(condition, a, axis=None, out=None)¶
Return selected slices of an array along given axis.
When working along a given axis, a slice along that axis is returned in
output for each index where condition evaluates to True. When
working on a 1-D array, compress is equivalent to extract.
Parameters:
condition (1-D array of bools) – Array that selects which entries to return. If len(condition)
is less than the size of a along the given axis, then output is
truncated to the length of the condition array.
a (array_like) – Array from which to extract a part.
axis (int, optional) – Axis along which to take slices. If None (default), work on the
flattened array.
out (ndarray, optional) – Output array. Its type is preserved and it must be of the right
shape to hold the output.
Returns:
compressed_array – A copy of a without the slices along axis for which condition
is false.
Please refer to the documentation for cov for more detail. The
relationship between the correlation coefficient matrix, R, and the
covariance matrix, C, is
x (array_like) – A 1-D or 2-D array containing multiple variables and observations.
Each row of x represents a variable, and each column a single
observation of all those variables. Also see rowvar below.
y (array_like, optional) – An additional set of variables and observations. y has the same
shape as x.
rowvar (bool, optional) – If rowvar is True (default), then each row represents a
variable, with observations in the columns. Otherwise, the relationship
is transposed: each column represents a variable, while the rows
contain observations.
bias (_NoValue, optional) –
Has no effect, do not use.
Deprecated since version 1.10.0.
ddof (_NoValue, optional) –
Has no effect, do not use.
Deprecated since version 1.10.0.
dtype (data-type, optional) –
Data-type of the result. By default, the return data-type will have
at least numpy.float64 precision.
Added in version 1.20.
Returns:
R – The correlation coefficient matrix of the variables.
Due to floating point rounding the resulting array may not be Hermitian,
the diagonal elements may not be 1, and the elements may not satisfy the
inequality abs(a) <= 1. The real and imaginary parts are clipped to the
interval [-1, 1] in an attempt to improve on that situation but is not
much help in the complex case.
This function accepts but discards arguments bias and ddof. This is
for backwards compatibility with previous versions of this function. These
arguments had no effect on the return values of the function and can be
safely ignored in this and previous versions of numpy.
Examples
In this example we generate two random arrays, xarr and yarr, and
compute the row-wise and column-wise Pearson correlation coefficients,
R. Since rowvar is true by default, we first find the row-wise
Pearson correlation coefficients between the variables of xarr.
If we add another set of variables and observations yarr, we can
compute the row-wise Pearson correlation coefficients between the
variables in xarr and yarr.
Finally if we use the option rowvar=False, the columns are now
being treated as the variables and we will find the column-wise Pearson
correlation coefficients between variables in xarr and yarr.
Discrete, linear convolution of two one-dimensional sequences.
multiarray.correlate
Old, no conjugate, version of correlate.
scipy.signal.correlate
uses FFT which has superior performance on large arrays.
Notes
The definition of correlation above is not unique and sometimes correlation
may be defined differently. Another common definition is:
\[c'_k = \sum_n a_{n} \cdot \overline{v_{n+k}}\]
which is related to \(c_k\) by \(c'_k = c_{-k}\).
numpy.correlate may perform slowly in large arrays (i.e. n = 1e5) because it does
not use the FFT to compute the convolution; in that case, scipy.signal.correlate might
be preferable.
Counts the number of non-zero values in the array a.
The word “non-zero” is in reference to the Python 2.x
built-in method __nonzero__() (renamed __bool__()
in Python 3.x) of Python objects that tests an object’s
“truthfulness”. For example, any number is considered
truthful if it is nonzero, whereas any string is considered
truthful if it is not the empty string. Thus, this function
(recursively) counts how many elements in a (and in
sub-arrays thereof) have their __nonzero__() or __bool__()
method evaluated to True.
Parameters:
a (array_like) – The array for which to count non-zeros.
If this is set to True, the axes that are counted are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the input array.
Added in version 1.19.0.
Returns:
count – Number of non-zero values in the array along a given axis.
Otherwise, the total number of non-zero values in the array
is returned.
Estimate a covariance matrix, given data and weights.
Covariance indicates the level to which two variables vary together.
If we examine N-dimensional samples, \(X = [x_1, x_2, ... x_N]^T\),
then the covariance matrix element \(C_{ij}\) is the covariance of
\(x_i\) and \(x_j\). The element \(C_{ii}\) is the variance
of \(x_i\).
See the notes for an outline of the algorithm.
Parameters:
m (array_like) – A 1-D or 2-D array containing multiple variables and observations.
Each row of m represents a variable, and each column a single
observation of all those variables. Also see rowvar below.
y (array_like, optional) – An additional set of variables and observations. y has the same form
as that of m.
rowvar (bool, optional) – If rowvar is True (default), then each row represents a
variable, with observations in the columns. Otherwise, the relationship
is transposed: each column represents a variable, while the rows
contain observations.
bias (bool, optional) – Default normalization (False) is by (N-1), where N is the
number of observations given (unbiased estimate). If bias is True,
then normalization is by N. These values can be overridden by using
the keyword ddof in numpy versions >= 1.5.
If not None the default value implied by bias is overridden.
Note that ddof=1 will return the unbiased estimate, even if both
fweights and aweights are specified, and ddof=0 will return
the simple average. See the notes for the details. The default value
is None.
1-D array of integer frequency weights; the number of times each
observation vector should be repeated.
Added in version 1.10.
aweights (array_like, optional) –
1-D array of observation vector weights. These relative weights are
typically large for observations considered “important” and smaller for
observations considered less “important”. If ddof=0 the array of
weights can be used to assign probabilities to observation vectors.
Added in version 1.10.
dtype (data-type, optional) –
Data-type of the result. By default, the return data-type will have
at least numpy.float64 precision.
Assume that the observations are in the columns of the observation
array m and let f=fweights and a=aweights for brevity. The
steps to compute the weighted covariance are as follows:
Return the cumulative product of elements along a given axis.
Parameters:
a (array_like) – Input array.
axis (int, optional) – Axis along which the cumulative product is computed. By default
the input is flattened.
dtype (dtype, optional) – Type of the returned array, as well as of the accumulator in which
the elements are multiplied. If dtype is not specified, it
defaults to the dtype of a, unless a has an integer dtype with
a precision less than that of the default platform integer. In
that case, the default platform integer is used instead.
out (ndarray, optional) – Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output
but the type of the resulting values will be cast if necessary.
Returns:
cumprod – A new array holding the result is returned unless out is
specified, in which case a reference to out is returned.
Return the indices of the bins to which each value in input array belongs.
right
order of bins
returned index i satisfies
False
increasing
bins[i-1]<=x<bins[i]
True
increasing
bins[i-1]<x<=bins[i]
False
decreasing
bins[i-1]>x>=bins[i]
True
decreasing
bins[i-1]>=x>bins[i]
If values in x are beyond the bounds of bins, 0 or len(bins) is
returned as appropriate.
Parameters:
x (array_like) – Input array to be binned. Prior to NumPy 1.10.0, this array had to
be 1-dimensional, but can now have any shape.
bins (array_like) – Array of bins. It has to be 1-dimensional and monotonic.
right (bool, optional) – Indicating whether the intervals include the right or the left bin
edge. Default behavior is (right==False) indicating that the interval
does not include the right edge. The left bin end is open in this
case, i.e., bins[i-1] <= x < bins[i] is the default behavior for
monotonically increasing bins.
Returns:
indices – Output array of indices, of same shape as x.
If values in x are such that they fall outside the bin range,
attempting to index bins with the indices that digitize returns
will result in an IndexError.
Added in version 1.10.0.
np.digitize is implemented in terms of np.searchsorted. This means
that a binary search is used to bin the values, which scales much better
for larger number of bins than the previous linear search. It also removes
the requirement for the input array to be 1-dimensional.
For monotonically _increasing_ bins, the following are equivalent:
Note that as the order of the arguments are reversed, the side must be too.
The searchsorted call is marginally faster, as it does not do any
monotonicity checks. Perhaps more importantly, it supports all dtypes.
Return element-wise quotient and remainder simultaneously.
Added in version 1.13.0.
np.divmod(x,y) is equivalent to (x//y,x%y), but faster
because it avoids redundant work. It is used to implement the Python
built-in function divmod on NumPy arrays.
Parameters:
x1 (array_like) – Dividend array.
x2 (array_like) – Divisor array.
If x1.shape!=x2.shape, they must be broadcastable to a common
shape (which becomes the shape of the output).
out (ndarray, None, or tuple of ndarray and None, optional) – A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where (array_like, optional) – This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None, locations within it where the condition is False will
remain uninitialized.
Returns:
out1 (ndarray) – Element-wise quotient resulting from floor division.
This is a scalar if both x1 and x2 are scalars.
out2 (ndarray) – Element-wise remainder from floor division.
This is a scalar if both x1 and x2 are scalars.
A numpy array is homogeneous, and contains elements described by a
dtype object. A dtype object can be constructed from different
combinations of fundamental numeric types.
Parameters:
dtype – Object to be converted to a data type object.
align (bool, optional) – Add padding to the fields to match what a C compiler would output
for a similar C-struct. Can be True only if obj is a dictionary
or a comma-separated string. If a struct dtype is being created,
this also sets a sticky alignment flag isalignedstruct.
copy (bool, optional) – Make a new copy of the data-type object. If False, the result
may just be a reference to a built-in data-type object.
metadata (dict, optional) – An optional dictionary with dtype metadata.
See also
result_type
Examples
Using array-scalar type:
>>> np.dtype(np.int16)dtype('int16')
Structured type, one field name ‘f1’, containing int16:
First array elements raised to powers from second array, element-wise.
Raise each base in x1 to the positionally-corresponding power in x2.
x1 and x2 must be broadcastable to the same shape. This differs from
the power function in that integers, float16, and float32 are promoted to
floats with a minimum precision of float64 so that the result is always
inexact. The intent is that the function will return a usable result for
negative powers and seldom overflow for positive powers.
Negative values raised to a non-integral value will return nan.
To get complex results, cast the input to complex, or specify the
dtype to be complex (see the example below).
Added in version 1.12.0.
Parameters:
x1 (array_like) – The bases.
x2 (array_like) – The exponents.
If x1.shape!=x2.shape, they must be broadcastable to a common
shape (which becomes the shape of the output).
out (ndarray, None, or tuple of ndarray and None, optional) – A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where (array_like, optional) – This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None, locations within it where the condition is False will
remain uninitialized.
Returns:
y – The bases in x1 raised to the exponents in x2.
This is a scalar if both x1 and x2 are scalars.
Decompose the elements of x into mantissa and twos exponent.
Returns (mantissa, exponent), where x=mantissa*2**exponent.
The mantissa lies in the open interval(-1, 1), while the twos
exponent is a signed integer.
Parameters:
x (array_like) – Array of numbers to be decomposed.
out1 (ndarray, optional) – Output array for the mantissa. Must have the same shape as x.
out2 (ndarray, optional) – Output array for the exponent. Must have the same shape as x.
out (ndarray, None, or tuple of ndarray and None, optional) – A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where (array_like, optional) – This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None, locations within it where the condition is False will
remain uninitialized.
Returns:
mantissa (ndarray) – Floating values between -1 and 1.
This is a scalar if x is a scalar.
exponent (ndarray) – Integer exponents of 2.
This is a scalar if x is a scalar.
See also
ldexp
Compute y=x1*2**x2, the inverse of frexp.
Notes
Complex dtypes are not supported, they will raise a TypeError.
where x2 is often taken to be 0.5, but 0 and 1 are also sometimes used.
Parameters:
x1 (array_like) – Input values.
x2 (array_like) – The value of the function when x1 is 0.
If x1.shape!=x2.shape, they must be broadcastable to a common
shape (which becomes the shape of the output).
out (ndarray, None, or tuple of ndarray and None, optional) – A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where (array_like, optional) – This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None, locations within it where the condition is False will
remain uninitialized.
Returns:
out – The output array, element-wise Heaviside step function of x1.
This is a scalar if both x1 and x2 are scalars.
mxnet.numpy.fallback.histogram2d(x, y, bins=10, range=None, density=None, weights=None)¶
Compute the bi-dimensional histogram of two data samples.
Parameters:
x (array_like, shape (N,)) – An array containing the x coordinates of the points to be
histogrammed.
y (array_like, shape (N,)) – An array containing the y coordinates of the points to be
histogrammed.
bins (int or array_like or [int, int] or [array, array], optional) –
The bin specification:
If int, the number of bins for the two dimensions (nx=ny=bins).
If array_like, the bin edges for the two dimensions
(x_edges=y_edges=bins).
If [int, int], the number of bins in each dimension
(nx, ny = bins).
If [array, array], the bin edges in each dimension
(x_edges, y_edges = bins).
A combination [int, array] or [array, int], where int
is the number of bins and array is the bin edges.
range (array_like, shape(2,2), optional) – The leftmost and rightmost edges of the bins along each dimension
(if not specified explicitly in the bins parameters):
[[xmin,xmax],[ymin,ymax]]. All values outside of this range
will be considered outliers and not tallied in the histogram.
density (bool, optional) – If False, the default, returns the number of samples in each bin.
If True, returns the probability density function at the bin,
bin_count/sample_count/bin_area.
weights (array_like, shape(N,), optional) – An array of values w_i weighing each sample (x_i,y_i).
Weights are normalized to 1 if density is True. If density is
False, the values of the returned histogram are equal to the sum of
the weights belonging to the samples falling into each bin.
Returns:
H (ndarray, shape(nx, ny)) – The bi-dimensional histogram of samples x and y. Values in x
are histogrammed along the first dimension and values in y are
histogrammed along the second dimension.
xedges (ndarray, shape(nx+1,)) – The bin edges along the first dimension.
yedges (ndarray, shape(ny+1,)) – The bin edges along the second dimension.
When density is True, then the returned histogram is the sample
density, defined such that the sum over bins of the product
bin_value*bin_area is 1.
Please note that the histogram does not follow the Cartesian convention
where x values are on the abscissa and y values on the ordinate
axis. Rather, x is histogrammed along the first dimension of the
array (vertical), and y along the second dimension of the array
(horizontal). This ensures compatibility with histogramdd.
Construct a 2-D histogram with variable bin width. First define the bin
edges:
>>> xedges=[0,1,3,5]>>> yedges=[0,2,3,4,6]
Next we create a histogram H with random bin content:
>>> x=np.random.normal(2,1,100)>>> y=np.random.normal(1,1,100)>>> H,xedges,yedges=np.histogram2d(x,y,bins=(xedges,yedges))>>> # Histogram does not follow Cartesian convention (see Notes),>>> # therefore transpose H for visualization purposes.>>> H=H.T
imshow can only display square bins:
>>> fig=plt.figure(figsize=(7,3))>>> ax=fig.add_subplot(131,title='imshow: square bins')>>> plt.imshow(H,interpolation='nearest',origin='lower',... extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]])<matplotlib.image.AxesImage object at 0x...>
pcolormesh can display actual edges:
>>> ax=fig.add_subplot(132,title='pcolormesh: actual edges',... aspect='equal')>>> X,Y=np.meshgrid(xedges,yedges)>>> ax.pcolormesh(X,Y,H)<matplotlib.collections.QuadMesh object at 0x...>
NonUniformImage can be used to
display actual bin edges with interpolation:
It is also possible to construct a 2-D histogram without specifying bin
edges:
>>> # Generate non-symmetric test data>>> n=10000>>> x=np.linspace(1,100,n)>>> y=2*np.log(x)+np.random.rand(n)-0.5>>> # Compute 2d histogram. Note the order of x/y and xedges/yedges>>> H,yedges,xedges=np.histogram2d(y,x,bins=20)
Now we can plot the histogram using
pcolormesh, and a
hexbin for comparison.
Function to calculate only the edges of the bins used by the histogram
function.
Parameters:
a (array_like) – Input data. The histogram is computed over the flattened array.
bins (int or sequence of scalars or str, optional) –
If bins is an int, it defines the number of equal-width
bins in the given range (10, by default). If bins is a
sequence, it defines the bin edges, including the rightmost
edge, allowing for non-uniform bin widths.
If bins is a string from the list below, histogram_bin_edges will use
the method chosen to calculate the optimal bin width and
consequently the number of bins (see Notes for more detail on
the estimators) from the data that falls within the requested
range. While the bin width will be optimal for the actual data
in the range, the number of bins will be computed to fill the
entire range, including the empty portions. For visualisation,
using the ‘auto’ option is suggested. Weighted data is not
supported for automated bin size selection.
’auto’
Maximum of the ‘sturges’ and ‘fd’ estimators. Provides good
all around performance.
’fd’ (Freedman Diaconis Estimator)
Robust (resilient to outliers) estimator that takes into
account data variability and data size.
’doane’
An improved version of Sturges’ estimator that works better
with non-normal datasets.
’scott’
Less robust estimator that takes into account data variability
and data size.
’stone’
Estimator based on leave-one-out cross-validation estimate of
the integrated squared error. Can be regarded as a generalization
of Scott’s rule.
’rice’
Estimator does not take variability into account, only data
size. Commonly overestimates number of bins required.
’sturges’
R’s default method, only accounts for data size. Only
optimal for gaussian data and underestimates number of bins
for large non-gaussian datasets.
’sqrt’
Square root (of data size) estimator, used by Excel and
other programs for its speed and simplicity.
range ((float, float), optional) – The lower and upper range of the bins. If not provided, range
is simply (a.min(),a.max()). Values outside the range are
ignored. The first element of the range must be less than or
equal to the second. range affects the automatic bin
computation as well. While bin width is computed to be optimal
based on the actual data within range, the bin count will fill
the entire range including portions containing no data.
weights (array_like, optional) – An array of weights, of the same shape as a. Each value in
a only contributes its associated weight towards the bin count
(instead of 1). This is currently not used by any of the bin estimators,
but may be in the future.
Returns:
bin_edges – The edges to pass into histogram
Return type:
array of dtype float
See also
histogram
Notes
The methods to estimate the optimal number of bins are well founded
in literature, and are inspired by the choices R provides for
histogram visualisation. Note that having the number of bins
proportional to \(n^{1/3}\) is asymptotically optimal, which is
why it appears in most estimators. These are simply plug-in methods
that give good starting points for number of bins. In the equations
below, \(h\) is the binwidth and \(n_h\) is the number of
bins. All estimators that compute bin counts are recast to bin width
using the ptp of the data. The final bin count is obtained from
np.round(np.ceil(range/h)). The final bin width is often less
than what is returned by the estimators below.
‘auto’ (maximum of the ‘sturges’ and ‘fd’ estimators)
A compromise to get a good value. For small datasets the Sturges
value will usually be chosen, while larger datasets will usually
default to FD. Avoids the overly conservative behaviour of FD
and Sturges for small and large datasets respectively.
Switchover point is usually \(a.size \approx 1000\).
‘fd’ (Freedman Diaconis Estimator)
\[h = 2 \frac{IQR}{n^{1/3}}\]
The binwidth is proportional to the interquartile range (IQR)
and inversely proportional to cube root of a.size. Can be too
conservative for small datasets, but is quite good for large
datasets. The IQR is very robust to outliers.
‘scott’
\[h = \sigma \sqrt[3]{\frac{24 \sqrt{\pi}}{n}}\]
The binwidth is proportional to the standard deviation of the
data and inversely proportional to cube root of x.size. Can
be too conservative for small datasets, but is quite good for
large datasets. The standard deviation is not very robust to
outliers. Values are very similar to the Freedman-Diaconis
estimator in the absence of outliers.
‘rice’
\[n_h = 2n^{1/3}\]
The number of bins is only proportional to cube root of
a.size. It tends to overestimate the number of bins and it
does not take into account data variability.
‘sturges’
\[n_h = \log _{2}(n) + 1\]
The number of bins is the base 2 log of a.size. This
estimator assumes normality of data and is too conservative for
larger, non-normal datasets. This is the default method in R’s
hist method.
An improved version of Sturges’ formula that produces better
estimates for non-normal datasets. This estimator attempts to
account for the skew of the data.
‘sqrt’
\[n_h = \sqrt n\]
The simplest and fastest estimator. Only takes into account the
data size.
A sequence of arrays describing the monotonically increasing bin
edges along each dimension.
The number of bins for each dimension (nx, ny, … =bins)
The number of bins for all dimensions (nx=ny=…=bins).
range (sequence, optional) – A sequence of length D, each an optional (lower, upper) tuple giving
the outer bin edges to be used if the edges are not given explicitly in
bins.
An entry of None in the sequence results in the minimum and maximum
values being used for the corresponding dimension.
The default, None, is equivalent to passing a tuple of D None values.
density (bool, optional) – If False, the default, returns the number of samples in each bin.
If True, returns the probability density function at the bin,
bin_count/sample_count/bin_volume.
weights ((N,) array_like, optional) – An array of values w_i weighing each sample (x_i, y_i, z_i, …).
Weights are normalized to 1 if density is True. If density is False,
the values of the returned histogram are equal to the sum of the
weights belonging to the samples falling into each bin.
Returns:
H (ndarray) – The multidimensional histogram of sample x. See density and weights
for the different possible semantics.
edges (list) – A list of D arrays describing the bin edges for each dimension.
The scipy implementation is recommended over this function: it is a
proper ufunc written in C, and more than an order of magnitude faster.
We use the algorithm published by Clenshaw [1]_ and referenced by
Abramowitz and Stegun [2]_, for which the function domain is
partitioned into the two intervals [0,8] and (8,inf), and Chebyshev
polynomial expansions are employed in each interval. Relative error on
the domain [0,30] using IEEE arithmetic is documented [3] as having a
peak of 5.8e-16 with an rms of 1.4e-16 (n = 30000).
Test whether each element of a 1-D array is also present in a second array.
Returns a boolean array the same length as ar1 that is True
where an element of ar1 is in ar2 and False otherwise.
We recommend using isin() instead of in1d for new code.
Parameters:
ar1 ((M,) array_like) – Input array.
ar2 (array_like) – The values against which to test each value of ar1.
assume_unique (bool, optional) – If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
invert (bool, optional) – If True, the values in the returned array are inverted (that is,
False where an element of ar1 is in ar2 and True otherwise).
Default is False. np.in1d(a,b,invert=True) is equivalent
to (but is faster than) np.invert(in1d(a,b)).
kind ({None, 'sort', 'table'}, optional) –
The algorithm to use. This will not affect the final result,
but will affect the speed and memory use. The default, None,
will select automatically based on memory considerations.
If ‘sort’, will use a mergesort-based approach. This will have
a memory usage of roughly 6 times the sum of the sizes of
ar1 and ar2, not accounting for size of dtypes.
If ‘table’, will use a lookup table approach similar
to a counting sort. This is only available for boolean and
integer arrays. This will have a memory usage of the
size of ar1 plus the max-min value of ar2. assume_unique
has no effect when the ‘table’ option is used.
If None, will automatically choose ‘table’ if
the required memory allocation is less than or equal to
6 times the sum of the sizes of ar1 and ar2,
otherwise will use ‘sort’. This is done to not use
a large amount of memory by default, even though
‘table’ may be faster in most cases. If ‘table’ is chosen,
assume_unique will have no effect.
Version of this function that preserves the shape of ar1.
numpy.lib.arraysetops
Module with a number of other functions for performing set operations on arrays.
Notes
in1d can be considered as an element-wise function version of the
python keyword in, for 1-D sequences. in1d(a,b) is roughly
equivalent to np.array([iteminbforitemina]).
However, this idea fails if ar2 is a set, or similar (non-sequence)
container: As ar2 is converted to an array, in those cases
asarray(ar2) is an object array rather than the expected array of
contained values.
Using kind='table' tends to be faster than kind=’sort’ if the
following relationship is true:
log10(len(ar2))>(log10(max(ar2)-min(ar2))-2.27)/0.927,
but may use greater memory. The default value for kind will
be automatically selected based only on memory usage, so one may
manually set kind='table' if memory constraints can be relaxed.
Return the sorted, unique values that are in both of the input arrays.
Parameters:
ar1 (array_like) – Input arrays. Will be flattened if not already 1D.
ar2 (array_like) – Input arrays. Will be flattened if not already 1D.
assume_unique (bool) – If True, the input arrays are both assumed to be unique, which
can speed up the calculation. If True but ar1 or ar2 are not
unique, incorrect results and out-of-bounds indices could result.
Default is False.
If True, the indices which correspond to the intersection of the two
arrays are returned. The first instance of a value is used if there are
multiple. Default is False.
Added in version 1.15.0.
Returns:
intersect1d (ndarray) – Sorted 1D array of common and unique elements.
comm1 (ndarray) – The indices of the first occurrences of the common values in ar1.
Only provided if return_indices is True.
comm2 (ndarray) – The indices of the first occurrences of the common values in ar2.
Only provided if return_indices is True.
See also
numpy.lib.arraysetops
Module with a number of other functions for performing set operations on arrays.
Returns a boolean array where two arrays are element-wise equal within a
tolerance.
The tolerance values are positive, typically very small numbers. The
relative difference (rtol * abs(b)) and the absolute difference
atol are added together to compare against the absolute difference
between a and b.
Warning
The default atol is not appropriate for comparing numbers
that are much smaller than one (see Notes).
Parameters:
a (array_like) – Input arrays to compare.
b (array_like) – Input arrays to compare.
rtol (float) – The relative tolerance parameter (see Notes).
atol (float) – The absolute tolerance parameter (see Notes).
equal_nan (bool) – Whether to compare NaN’s as equal. If True, NaN’s in a will be
considered equal to NaN’s in b in the output array.
Returns:
y – Returns a boolean array of where a and b are equal within the
given tolerance. If both a and b are scalars, returns a single
boolean value.
For finite values, isclose uses the following equation to test whether
two floating point values are equivalent.
absolute(a - b) <= (atol + rtol * absolute(b))
Unlike the built-in math.isclose, the above equation is not symmetric
in a and b – it assumes b is the reference value – so that
isclose(a, b) might be different from isclose(b, a). Furthermore,
the default value of atol is not zero, and is used to determine what
small values should be considered close to zero. The default value is
appropriate for expected values of order unity: if the expected values
are significantly smaller than one, it can result in false positives.
atol should be carefully selected for the use case at hand. A zero value
for atol will result in False if either a or b is zero.
isclose is not defined for non-numeric data types.
bool is considered a numeric data-type for this purpose.
Calculates elementintest_elements, broadcasting over element only.
Returns a boolean array of the same shape as element that is True
where an element of element is in test_elements and False otherwise.
Parameters:
element (array_like) – Input array.
test_elements (array_like) – The values against which to test each value of element.
This argument is flattened if it is an array or array_like.
See notes for behavior with non-array-like parameters.
assume_unique (bool, optional) – If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
invert (bool, optional) – If True, the values in the returned array are inverted, as if
calculating element not in test_elements. Default is False.
np.isin(a,b,invert=True) is equivalent to (but faster
than) np.invert(np.isin(a,b)).
kind ({None, 'sort', 'table'}, optional) –
The algorithm to use. This will not affect the final result,
but will affect the speed and memory use. The default, None,
will select automatically based on memory considerations.
If ‘sort’, will use a mergesort-based approach. This will have
a memory usage of roughly 6 times the sum of the sizes of
ar1 and ar2, not accounting for size of dtypes.
If ‘table’, will use a lookup table approach similar
to a counting sort. This is only available for boolean and
integer arrays. This will have a memory usage of the
size of ar1 plus the max-min value of ar2. assume_unique
has no effect when the ‘table’ option is used.
If None, will automatically choose ‘table’ if
the required memory allocation is less than or equal to
6 times the sum of the sizes of ar1 and ar2,
otherwise will use ‘sort’. This is done to not use
a large amount of memory by default, even though
‘table’ may be faster in most cases. If ‘table’ is chosen,
assume_unique will have no effect.
Returns:
isin – Has the same shape as element. The values element[isin]
are in test_elements.
Module with a number of other functions for performing set operations on arrays.
Notes
isin is an element-wise function version of the python keyword in.
isin(a,b) is roughly equivalent to
np.array([iteminbforitemina]) if a and b are 1-D sequences.
element and test_elements are converted to arrays if they are not
already. If test_elements is a set (or other non-sequence collection)
it will be converted to an object array with one element, rather than an
array of the values contained in test_elements. This is a consequence
of the array constructor’s way of handling non-sequence collections.
Converting the set to a list usually gives the desired behavior.
Using kind='table' tends to be faster than kind=’sort’ if the
following relationship is true:
log10(len(ar2))>(log10(max(ar2)-min(ar2))-2.27)/0.927,
but may use greater memory. The default value for kind will
be automatically selected based only on memory usage, so one may
manually set kind='table' if memory constraints can be relaxed.
This function takes N 1-D sequences and returns N outputs with N
dimensions each, such that the shape is 1 in all but one dimension
and the dimension with the non-unit shape value cycles through all
N dimensions.
Using ix_ one can quickly construct index arrays that will index
the cross product. a[np.ix_([1,3],[2,5])] returns the array
[[a[1,2]a[1,5]],[a[3,2]a[3,5]]].
Parameters:
args (1-D sequences) – Each sequence should be of integer or boolean type.
Boolean sequences will be interpreted as boolean masks for the
corresponding dimension (equivalent to passing in
np.nonzero(boolean_sequence)).
Returns:
out – N arrays with N dimensions each, with N the number of input
sequences. Together these arrays form an open mesh.
Perform an indirect stable sort using a sequence of keys.
Given multiple sorting keys, which can be interpreted as columns in a
spreadsheet, lexsort returns an array of integer indices that describes
the sort order by multiple columns. The last key in the sequence is used
for the primary sort order, the second-to-last key for the secondary sort
order, and so on. The keys argument must be a sequence of objects that
can be converted to arrays of the same shape. If a 2D array is provided
for the keys argument, its rows are interpreted as the sorting keys and
sorting is according to the last row, second last row etc.
Parameters:
keys ((k, N) array or tuple containing k (N,)-shaped sequences) – The k different “columns” to be sorted. The last column (or row if
keys is a 2D array) is the primary sort key.
axis (int, optional) – Axis to be indirectly sorted. By default, sort over the last axis.
Returns:
indices – Array of indices that sort the keys along the specified axis.
>>> a=[1,5,1,4,3,4,4]# First column>>> b=[9,4,0,4,0,2,1]# Second column>>> ind=np.lexsort((b,a))# Sort by a, then by b>>> indarray([2, 0, 4, 6, 5, 3, 1])
For scalar a, returns the data type with the smallest size
and smallest scalar kind which can hold its value. For non-scalar
array a, returns the vector’s dtype unmodified.
Floating point values are not demoted to integers,
and complex values are not demoted to floats.
Parameters:
a (scalar or array_like) – The value whose minimal data type is to be found.
Return the fractional and integral parts of an array, element-wise.
The fractional and integral parts are negative if the given number is
negative.
Parameters:
x (array_like) – Input array.
out (ndarray, None, or tuple of ndarray and None, optional) – A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where (array_like, optional) – This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None, locations within it where the condition is False will
remain uninitialized.
Returns:
y1 (ndarray) – Fractional part of x.
This is a scalar if x is a scalar.
y2 (ndarray) – Integral part of x.
This is a scalar if x is a scalar.
Return the indices of the maximum values in the specified axis ignoring
NaNs. For all-NaN slices ValueError is raised. Warning: the
results cannot be trusted if a slice contains only NaNs and -Infs.
Parameters:
a (array_like) – Input data.
axis (int, optional) – Axis along which to operate. By default flattened input is used.
out (array, optional) –
If provided, the result will be inserted into this array. It should
be of the appropriate shape and dtype.
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 array.
Added in version 1.22.0.
Returns:
index_array – An array of indices or a single index value.
Return the indices of the minimum values in the specified axis ignoring
NaNs. For all-NaN slices ValueError is raised. Warning: the results
cannot be trusted if a slice contains only NaNs and Infs.
Parameters:
a (array_like) – Input data.
axis (int, optional) – Axis along which to operate. By default flattened input is used.
out (array, optional) –
If provided, the result will be inserted into this array. It should
be of the appropriate shape and dtype.
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 array.
Added in version 1.22.0.
Returns:
index_array – An array of indices or a single index value.
Return the cumulative product of array elements over a given axis treating Not a
Numbers (NaNs) as one. The cumulative product does not change when NaNs are
encountered and leading NaNs are replaced by ones.
Ones are returned for slices that are all-NaN or empty.
Added in version 1.12.0.
Parameters:
a (array_like) – Input array.
axis (int, optional) – Axis along which the cumulative product is computed. By default
the input is flattened.
dtype (dtype, optional) – Type of the returned array, as well as of the accumulator in which
the elements are multiplied. If dtype is not specified, it
defaults to the dtype of a, unless a has an integer dtype with
a precision less than that of the default platform integer. In
that case, the default platform integer is used instead.
out (ndarray, optional) – Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output
but the type of the resulting values will be cast if necessary.
Returns:
nancumprod – A new array holding the result is returned unless out is
specified, in which case it is returned.
Return the cumulative sum of array elements over a given axis treating Not a
Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are
encountered and leading NaNs are replaced by zeros.
Zeros are returned for slices that are all-NaN or empty.
Added in version 1.12.0.
Parameters:
a (array_like) – Input array.
axis (int, optional) – Axis along which the cumulative sum is computed. The default
(None) is to compute the cumsum over the flattened array.
dtype (dtype, optional) – Type of the returned array and of the accumulator in which the
elements are summed. If dtype is not specified, it defaults
to the dtype of a, unless a has an integer dtype with a
precision less than that of the default platform integer. In
that case, the default platform integer is used.
out (ndarray, optional) – Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output
but the type will be cast if necessary. See Output type determination for
more details.
Returns:
nancumsum – A new array holding the result is returned unless out is
specified, in which it is returned. The result has the same
size as a, and the same shape as a if axis is not None
or a is a 1-d array.
Return the maximum of an array or maximum along an axis, ignoring any
NaNs. When all-NaN slices are encountered a RuntimeWarning is
raised and NaN is returned for that slice.
Parameters:
a (array_like) – Array containing numbers whose maximum is desired. If a is not an
array, a conversion is attempted.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the maximum is computed. The default is to compute
the maximum of the flattened array.
Alternate output array in which to place the result. The default
is None; if provided, it must have the same shape as the
expected output, but the type will be cast if necessary. See
Output type determination for more details.
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 a.
If the value is anything but the default, then
keepdims will be passed through to the max method
of sub-classes of ndarray. If the sub-classes methods
does not implement keepdims any exceptions will be raised.
Added in version 1.8.0.
initial (scalar, optional) –
The minimum value of an output element. Must be present to allow
computation on empty slice. See ~numpy.ufunc.reduce for details.
Elements to compare for the maximum. See ~numpy.ufunc.reduce
for details.
Added in version 1.22.0.
Returns:
nanmax – An array with the same shape as a, with the specified axis removed.
If a is a 0-d array, or if axis is None, an ndarray scalar is
returned. The same dtype as a is returned.
The minimum value of an array along a given axis, ignoring any NaNs.
amax
The maximum value of an array along a given axis, propagating any NaNs.
fmax
Element-wise maximum of two arrays, ignoring any NaNs.
maximum
Element-wise maximum of two arrays, propagating any NaNs.
isnan
Shows which elements are Not a Number (NaN).
isfinite
Shows which elements are neither NaN nor infinity.
amin, fmin, minimum
Notes
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
(IEEE 754). This means that Not a Number is not equivalent to infinity.
Positive infinity is treated as a very large number and negative
infinity is treated as a very small (i.e. negative) number.
If the input has a integer type the function is equivalent to np.max.
Compute the median along the specified axis, while ignoring NaNs.
Returns the median of the array elements.
Added in version 1.9.0.
Parameters:
a (array_like) – Input array or object that can be converted to an array.
axis ({int, sequence of int, None}, optional) – Axis or axes along which the medians are computed. The default
is to compute the median along a flattened version of the array.
A sequence of axes is supported since version 1.9.0.
out (ndarray, optional) – Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output,
but the type (of the output) will be cast if necessary.
overwrite_input (bool, optional) – If True, then allow use of memory of input array a for
calculations. The input array will be modified by the call to
median. This will save memory when you do not need to preserve
the contents of the input array. Treat the input as undefined,
but it will probably be fully or partially sorted. Default is
False. If overwrite_input is True and a is not already an
ndarray, an error will be raised.
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 a.
If this is anything but the default value it will be passed
through (in the special case of an empty array) to the
mean function of the underlying array. If the array is
a sub-class and mean does not have the kwarg keepdims this
will raise a RuntimeError.
Returns:
median – A new array holding the result. If the input contains integers
or floats smaller than float64, then the output data-type is
np.float64. Otherwise, the data-type of the output is the
same as that of the input. If out is specified, that array is
returned instead.
Given a vector V of length N, the median of V is the
middle value of a sorted copy of V, V_sorted - i.e.,
V_sorted[(N-1)/2], when N is odd and the average of the two
middle values of V_sorted when N is even.
Return minimum of an array or minimum along an axis, ignoring any NaNs.
When all-NaN slices are encountered a RuntimeWarning is raised and
Nan is returned for that slice.
Parameters:
a (array_like) – Array containing numbers whose minimum is desired. If a is not an
array, a conversion is attempted.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the minimum is computed. The default is to compute
the minimum of the flattened array.
Alternate output array in which to place the result. The default
is None; if provided, it must have the same shape as the
expected output, but the type will be cast if necessary. See
Output type determination for more details.
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 a.
If the value is anything but the default, then
keepdims will be passed through to the min method
of sub-classes of ndarray. If the sub-classes methods
does not implement keepdims any exceptions will be raised.
Added in version 1.8.0.
initial (scalar, optional) –
The maximum value of an output element. Must be present to allow
computation on empty slice. See ~numpy.ufunc.reduce for details.
Elements to compare for the minimum. See ~numpy.ufunc.reduce
for details.
Added in version 1.22.0.
Returns:
nanmin – An array with the same shape as a, with the specified axis
removed. If a is a 0-d array, or if axis is None, an ndarray
scalar is returned. The same dtype as a is returned.
The maximum value of an array along a given axis, ignoring any NaNs.
amin
The minimum value of an array along a given axis, propagating any NaNs.
fmin
Element-wise minimum of two arrays, ignoring any NaNs.
minimum
Element-wise minimum of two arrays, propagating any NaNs.
isnan
Shows which elements are Not a Number (NaN).
isfinite
Shows which elements are neither NaN nor infinity.
amax, fmax, maximum
Notes
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
(IEEE 754). This means that Not a Number is not equivalent to infinity.
Positive infinity is treated as a very large number and negative
infinity is treated as a very small (i.e. negative) number.
If the input has a integer type the function is equivalent to np.min.
mxnet.numpy.fallback.nanpercentile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=<novalue>, *, interpolation=None)¶
Compute the qth percentile of the data along the specified axis,
while ignoring nan values.
Returns the qth percentile(s) of the array elements.
Added in version 1.9.0.
Parameters:
a (array_like) – Input array or object that can be converted to an array, containing
nan values to be ignored.
q (array_like of float) – Percentile or sequence of percentiles to compute, which must be
between 0 and 100 inclusive.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the percentiles are computed. The default
is to compute the percentile(s) along a flattened version of the
array.
out (ndarray, optional) – Alternative output array in which to place the result. It must have
the same shape and buffer length as the expected output, but the
type (of the output) will be cast if necessary.
overwrite_input (bool, optional) – If True, then allow the input array a to be modified by
intermediate calculations, to save memory. In this case, the
contents of the input a after this function completes is
undefined.
This parameter specifies the method to use for estimating the
percentile. There are many different methods, some unique to NumPy.
See the notes for explanation. The options sorted by their R type
as summarized in the H&F paper [1]_ are:
’inverted_cdf’
’averaged_inverted_cdf’
’closest_observation’
’interpolated_inverted_cdf’
’hazen’
’weibull’
’linear’ (default)
’median_unbiased’
’normal_unbiased’
The first three methods are discontinuous. NumPy further defines the
following discontinuous variations of the default ‘linear’ (7.) option:
’lower’
’higher’,
’midpoint’
’nearest’
Changed in version 1.22.0: This argument was previously called “interpolation” and only
offered the “linear” default and last four options.
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 a.
If this is anything but the default value it will be passed
through (in the special case of an empty array) to the
mean function of the underlying array. If the array is
a sub-class and mean does not have the kwarg keepdims this
will raise a RuntimeError.
percentile – If q is a single percentile and axis=None, then the result
is a scalar. If multiple percentiles are given, first axis of
the result corresponds to the percentiles. The other axes are
the axes that remain after the reduction of a. If the input
contains integers or floats smaller than float64, the output
data-type is float64. Otherwise, the output data-type is the
same as that of the input. If out is specified, that array is
returned instead.
Return the product of array elements over a given axis treating Not a
Numbers (NaNs) as ones.
One is returned for slices that are all-NaN or empty.
Added in version 1.10.0.
Parameters:
a (array_like) – Array containing numbers whose product is desired. If a is not an
array, a conversion is attempted.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the product is computed. The default is to compute
the product of the flattened array.
dtype (data-type, optional) – The type of the returned array and of the accumulator in which the
elements are summed. By default, the dtype of a is used. An
exception is when a has an integer type with less precision than
the platform (u)intp. In that case, the default will be either
(u)int32 or (u)int64 depending on whether the platform is 32 or 64
bits. For inexact inputs, dtype must be inexact.
out (ndarray, optional) – Alternate output array in which to place the result. The default
is None. If provided, it must have the same shape as the
expected output, but the type will be cast if necessary. See
Output type determination for more details. The casting of NaN to integer
can yield unexpected results.
keepdims (bool, optional) – If 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 arr.
initial (scalar, optional) –
The starting value for this product. See ~numpy.ufunc.reduce
for details.
mxnet.numpy.fallback.nanquantile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=<novalue>, *, interpolation=None)¶
Compute the qth quantile of the data along the specified axis,
while ignoring nan values.
Returns the qth quantile(s) of the array elements.
Added in version 1.15.0.
Parameters:
a (array_like) – Input array or object that can be converted to an array, containing
nan values to be ignored
q (array_like of float) – Probability or sequence of probabilities for the quantiles to compute.
Values must be between 0 and 1 inclusive.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the quantiles are computed. The
default is to compute the quantile(s) along a flattened
version of the array.
out (ndarray, optional) – Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output,
but the type (of the output) will be cast if necessary.
overwrite_input (bool, optional) – If True, then allow the input array a to be modified by intermediate
calculations, to save memory. In this case, the contents of the input
a after this function completes is undefined.
This parameter specifies the method to use for estimating the
quantile. There are many different methods, some unique to NumPy.
See the notes for explanation. The options sorted by their R type
as summarized in the H&F paper [1]_ are:
’inverted_cdf’
’averaged_inverted_cdf’
’closest_observation’
’interpolated_inverted_cdf’
’hazen’
’weibull’
’linear’ (default)
’median_unbiased’
’normal_unbiased’
The first three methods are discontinuous. NumPy further defines the
following discontinuous variations of the default ‘linear’ (7.) option:
’lower’
’higher’,
’midpoint’
’nearest’
Changed in version 1.22.0: This argument was previously called “interpolation” and only
offered the “linear” default and last four options.
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 a.
If this is anything but the default value it will be passed
through (in the special case of an empty array) to the
mean function of the underlying array. If the array is
a sub-class and mean does not have the kwarg keepdims this
will raise a RuntimeError.
quantile – If q is a single probability and axis=None, then the result
is a scalar. If multiple probability levels are given, first axis of
the result corresponds to the quantiles. The other axes are
the axes that remain after the reduction of a. If the input
contains integers or floats smaller than float64, the output
data-type is float64. Otherwise, the output data-type is the
same as that of the input. If out is specified, that array is
returned instead.
Compute the standard deviation along the specified axis, while
ignoring NaNs.
Returns the standard deviation, a measure of the spread of a
distribution, of the non-NaN array elements. The standard deviation is
computed for the flattened array by default, otherwise over the
specified axis.
For all-NaN slices or slices with zero degrees of freedom, NaN is
returned and a RuntimeWarning is raised.
Added in version 1.8.0.
Parameters:
a (array_like) – Calculate the standard deviation of the non-NaN values.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the standard deviation is computed. The default is
to compute the standard deviation of the flattened array.
dtype (dtype, optional) – Type to use in computing the standard deviation. For arrays of
integer type the default is float64, for arrays of float types it
is the same as the array type.
out (ndarray, optional) – Alternative output array in which to place the result. It must have
the same shape as the expected output but the type (of the
calculated values) will be cast if necessary.
ddof (int, optional) – Means Delta Degrees of Freedom. The divisor used in calculations
is N-ddof, where N represents the number of non-NaN
elements. By default ddof is zero.
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 a.
If this value is anything but the default it is passed through
as-is to the relevant functions of the sub-classes. If these
functions do not have a keepdims kwarg, a RuntimeError will
be raised.
Elements to include in the standard deviation.
See ~numpy.ufunc.reduce for details.
Added in version 1.22.0.
Returns:
standard_deviation – If out is None, return a new array containing the standard
deviation, otherwise return a reference to the output array. If
ddof is >= the number of non-NaN elements in a slice or the slice
contains only NaNs, then the result for that slice is NaN.
The standard deviation is the square root of the average of the squared
deviations from the mean: std=sqrt(mean(abs(x-x.mean())**2)).
The average squared deviation is normally calculated as
x.sum()/N, where N=len(x). If, however, ddof is
specified, the divisor N-ddof is used instead. In standard
statistical practice, ddof=1 provides an unbiased estimator of the
variance of the infinite population. ddof=0 provides a maximum
likelihood estimate of the variance for normally distributed variables.
The standard deviation computed in this function is the square root of
the estimated variance, so even with ddof=1, it will not be an
unbiased estimate of the standard deviation per se.
Note that, for complex numbers, std takes the absolute value before
squaring, so that the result is always real and nonnegative.
For floating-point input, the std is computed using the same
precision the input has. Depending on the input data, this can cause
the results to be inaccurate, especially for float32 (see example
below). Specifying a higher-accuracy accumulator using the dtype
keyword can alleviate this issue.
Examples
>>> a=np.array([[1,np.nan],[3,4]])>>> np.nanstd(a)1.247219128924647>>> np.nanstd(a,axis=0)array([1., 0.])>>> np.nanstd(a,axis=1)array([0., 0.5]) # may vary
Return the sum of array elements over a given axis treating Not a
Numbers (NaNs) as zero.
In NumPy versions <= 1.9.0 Nan is returned for slices that are all-NaN or
empty. In later versions zero is returned.
Parameters:
a (array_like) – Array containing numbers whose sum is desired. If a is not an
array, a conversion is attempted.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the sum is computed. The default is to compute the
sum of the flattened array.
dtype (data-type, optional) –
The type of the returned array and of the accumulator in which the
elements are summed. By default, the dtype of a is used. An
exception is when a has an integer type with less precision than
the platform (u)intp. In that case, the default will be either
(u)int32 or (u)int64 depending on whether the platform is 32 or 64
bits. For inexact inputs, dtype must be inexact.
Alternate output array in which to place the result. The default
is None. If provided, it must have the same shape as the
expected output, but the type will be cast if necessary. See
Output type determination for more details. The casting of NaN to integer
can yield unexpected results.
Added in version 1.8.0.
keepdims (bool, 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 a.
initialscalar, optional
Starting value for the sum. See ~numpy.ufunc.reduce for details.
Added in version 1.22.0.
wherearray_like of bool, optional
Elements to include in the sum. See ~numpy.ufunc.reduce for details.
Added in version 1.22.0.
Returns:
nansum – A new array holding the result is returned unless out is
specified, in which it is returned. The result has the same
size as a, and the same shape as a if axis is not None
or a is a 1-d array.
Compute the variance along the specified axis, while ignoring NaNs.
Returns the variance of the array elements, a measure of the spread of
a distribution. The variance is computed for the flattened array by
default, otherwise over the specified axis.
For all-NaN slices or slices with zero degrees of freedom, NaN is
returned and a RuntimeWarning is raised.
Added in version 1.8.0.
Parameters:
a (array_like) – Array containing numbers whose variance is desired. If a is not an
array, a conversion is attempted.
axis ({int, tuple of int, None}, optional) – Axis or axes along which the variance is computed. The default is to compute
the variance of the flattened array.
dtype (data-type, optional) – Type to use in computing the variance. For arrays of integer type
the default is float64; for arrays of float types it is the same as
the array type.
out (ndarray, optional) – Alternate output array in which to place the result. It must have
the same shape as the expected output, but the type is cast if
necessary.
ddof (int, optional) – “Delta Degrees of Freedom”: the divisor used in the calculation is
N-ddof, where N represents the number of non-NaN
elements. By default ddof is zero.
keepdims (bool, 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 a.
Elements to include in the variance. See ~numpy.ufunc.reduce for
details.
Added in version 1.22.0.
Returns:
variance – If out is None, return a new array containing the variance,
otherwise return a reference to the output array. If ddof is >= the
number of non-NaN elements in a slice or the slice contains only
NaNs, then the result for that slice is NaN.
The variance is the average of the squared deviations from the mean,
i.e., var=mean(abs(x-x.mean())**2).
The mean is normally calculated as x.sum()/N, where N=len(x).
If, however, ddof is specified, the divisor N-ddof is used
instead. In standard statistical practice, ddof=1 provides an
unbiased estimator of the variance of a hypothetical infinite
population. ddof=0 provides a maximum likelihood estimate of the
variance for normally distributed variables.
Note that for complex numbers, the absolute value is taken before
squaring, so that the result is always real and nonnegative.
For floating-point input, the variance is computed using the same
precision the input has. Depending on the input data, this can cause
the results to be inaccurate, especially for float32 (see example
below). Specifying a higher-accuracy accumulator using the dtype
keyword can alleviate this issue.
For this function to work on sub-classes of ndarray, they must define
sum with the kwarg keepdims
Examples
>>> a=np.array([[1,np.nan],[3,4]])>>> np.nanvar(a)1.5555555555555554>>> np.nanvar(a,axis=0)array([1., 0.])>>> np.nanvar(a,axis=1)array([0., 0.25]) # may vary
Packs the elements of a binary-valued array into bits in a uint8 array.
The result is padded to full bytes by inserting zero bits at the end.
Parameters:
a (array_like) – An array of integers or booleans whose elements should be packed to
bits.
axis (int, optional) – The dimension over which bit-packing is done.
None implies packing the flattened array.
bitorder ({'big', 'little'}, optional) –
The order of the input bits. ‘big’ will mimic bin(val),
[0,0,0,0,0,0,1,1]=>3=0b00000011, ‘little’ will
reverse the order so [1,1,0,0,0,0,0,0]=>3.
Defaults to ‘big’.
Added in version 1.17.0.
Returns:
packed – Array of type uint8 whose elements represent bits corresponding to the
logical (0 or nonzero) value of the input elements. The shape of
packed has the same number of dimensions as the input (unless axis
is None, in which case the output is 1-D).
Creates a copy of the array with its elements rearranged in such a
way that the value of the element in k-th position is in the position
the value would be in a sorted array. In the partitioned array, all
elements before the k-th element are less than or equal to that
element, and all the elements after the k-th element are greater than
or equal to that element. The ordering of the elements in the two
partitions is undefined.
Element index to partition by. The k-th value of the element
will be in its final sorted position and all smaller elements
will be moved before it and all equal or greater elements behind
it. The order of all elements in the partitions is undefined. If
provided with a sequence of k-th it will partition all elements
indexed by k-th of them into their sorted position at once.
Deprecated since version 1.22.0: Passing booleans as index is deprecated.
axis (int or None, optional) – Axis along which to sort. If None, the array is flattened before
sorting. The default is -1, which sorts along the last axis.
kind ({'introselect'}, optional) – Selection algorithm. Default is ‘introselect’.
order (str or list of str, optional) – When a is an array with fields defined, this argument
specifies which fields to compare first, second, etc. A single
field can be specified as a string. Not all fields need be
specified, but unspecified fields will still be used, in the
order in which they come up in the dtype, to break ties.
Returns:
partitioned_array – Array of the same type and shape as a.
The various selection algorithms are characterized by their average
speed, worst case performance, work space size, and whether they are
stable. A stable sort keeps items with the same key in the same
relative order. The available algorithms have the following
properties:
kind
speed
worst case
work space
stable
‘introselect’
1
O(n)
0
no
All the partition algorithms make temporary copies of the data when
partitioning along any but the last axis. Consequently,
partitioning along the last axis is faster and uses less space than
partitioning along any other axis.
The sort order for complex numbers is lexicographic. If both the
real and imaginary parts are non-nan then the order is determined by
the real parts except when they are equal, in which case the order
is determined by the imaginary parts.
p2[4] is 2 and p2[8] is 5. All elements in p2[:4]
are less than or equal to p2[4], all elements in p2[5:8]
are greater than or equal to p2[4] and less than or equal to
p2[8], and all elements in p2[9:] are greater than or
equal to p2[8]. The partition is:
Each boolean array corresponds to a function in funclist. Wherever
condlist[i] is True, funclist[i](x) is used as the output value.
Each boolean array in condlist selects a piece of x,
and should therefore be of the same shape as x.
The length of condlist must correspond to that of funclist.
If one extra function is given, i.e. if
len(funclist)==len(condlist)+1, then that extra function
is the default value, used wherever all conditions are false.
funclist (list of callables, f(x,*args,**kw), or scalars) – Each function is evaluated over x wherever its corresponding
condition is True. It should take a 1d array as input and give an 1d
array or a scalar value as output. If, instead of a callable,
a scalar is provided then a constant function (lambdax:scalar) is
assumed.
args (tuple, optional) – Any further arguments given to piecewise are passed to the functions
upon execution, i.e., if called piecewise(...,...,1,'a'), then
each function is called as f(x,1,'a').
kw (dict, optional) – Keyword arguments used in calling piecewise are passed to the
functions upon execution, i.e., if called
piecewise(...,...,alpha=1), then each function is called as
f(x,alpha=1).
Returns:
out – The output is the same shape and type as x and is found by
calling the functions in funclist on the appropriate portions of x,
as defined by the boolean arrays in condlist. Portions not covered
by any condition have a default value of 0.
Find the coefficients of a polynomial with the given sequence of roots.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
Returns the coefficients of the polynomial whose leading coefficient
is one for the given sequence of zeros (multiple roots must be included
in the sequence as many times as their multiplicity; see Examples).
A square matrix (or array, which will be treated as a matrix) can also
be given, in which case the coefficients of the characteristic polynomial
of the matrix are returned.
Parameters:
seq_of_zeros (array_like, shape (N,) or (N, N)) – A sequence of polynomial roots, or a square array or matrix object.
Returns:
c – 1D array of polynomial coefficients from highest to lowest degree:
c[0]*x**(N)+c[1]*x**(N-1)+...+c[N-1]*x+c[N]
where c[0] always equals 1.
Specifying the roots of a polynomial still leaves one degree of
freedom, typically represented by an undetermined leading
coefficient. [1]_ In the case of this function, that coefficient -
the first one in the returned array - is always taken as one. (If
for some reason you have one other point, the only automatic way
presently to leverage that information is to use polyfit.)
The characteristic polynomial, \(p_a(t)\), of an n-by-n
matrix A is given by
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
Returns the polynomial resulting from the sum of two input polynomials.
Each input must be either a poly1d object or a 1D sequence of polynomial
coefficients, from highest to lowest degree.
Parameters:
a1 (array_like or poly1d object) – Input polynomials.
a2 (array_like or poly1d object) – Input polynomials.
Returns:
out – The sum of the inputs. If either input is a poly1d object, then the
output is also a poly1d object. Otherwise, it is a 1D array of
polynomial coefficients from highest to lowest degree.
Returns the quotient and remainder of polynomial division.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
The input arrays are the coefficients (including any coefficients
equal to zero) of the “numerator” (dividend) and “denominator”
(divisor) polynomials, respectively.
Parameters:
u (array_like or poly1d) – Dividend polynomial’s coefficients.
v (array_like or poly1d) – Divisor polynomial’s coefficients.
Returns:
q (ndarray) – Coefficients, including those equal to zero, of the quotient.
r (ndarray) – Coefficients, including those equal to zero, of the remainder.
Both u and v must be 0-d or 1-d (ndim = 0 or 1), but u.ndim need
not equal v.ndim. In other words, all four possible combinations -
u.ndim=v.ndim=0, u.ndim=v.ndim=1,
u.ndim=1,v.ndim=0, and u.ndim=0,v.ndim=1 - work.
mxnet.numpy.fallback.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)¶
Least squares polynomial fit.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
Fit a polynomial p(x)=p[0]*x**deg+...+p[deg] of degree deg
to points (x, y). Returns a vector of coefficients p that minimises
the squared error in the order deg, deg-1, … 0.
The Polynomial.fit <numpy.polynomial.polynomial.Polynomial.fit> class
method is recommended for new code as it is more stable numerically. See
the documentation of the method for more information.
Parameters:
x (array_like, shape (M,)) – x-coordinates of the M sample points (x[i],y[i]).
y (array_like, shape (M,) or (M, K)) – y-coordinates of the sample points. Several data sets of sample
points sharing the same x-coordinates can be fitted at once by
passing in a 2D-array that contains one dataset per column.
rcond (float, optional) – Relative condition number of the fit. Singular values smaller than
this relative to the largest singular value will be ignored. The
default value is len(x)*eps, where eps is the relative precision of
the float type, about 2e-16 in most cases.
full (bool, optional) – Switch determining nature of return value. When it is False (the
default) just the coefficients are returned, when True diagnostic
information from the singular value decomposition is also returned.
w (array_like, shape (M,), optional) – Weights. If not None, the weight w[i] applies to the unsquared
residual y[i]-y_hat[i] at x[i]. Ideally the weights are
chosen so that the errors of the products w[i]*y[i] all have the
same variance. When using inverse-variance weighting, use
w[i]=1/sigma(y[i]). The default value is None.
cov (bool or str, optional) – If given and not False, return not just the estimate but also its
covariance matrix. By default, the covariance are scaled by
chi2/dof, where dof = M - (deg + 1), i.e., the weights are presumed
to be unreliable except in a relative sense and everything is scaled
such that the reduced chi2 is unity. This scaling is omitted if
cov='unscaled', as is relevant for the case that the weights are
w = 1/sigma, with sigma known to be a reliable estimate of the
uncertainty.
Returns:
p (ndarray, shape (deg + 1,) or (deg + 1, K)) – Polynomial coefficients, highest power first. If y was 2-D, the
coefficients for k-th data set are in p[:,k].
residuals, rank, singular_values, rcond – These values are only returned if full==True
residuals – sum of squared residuals of the least squares fit
rank – the effective rank of the scaled Vandermonde
coefficient matrix
singular_values – singular values of the scaled Vandermonde
coefficient matrix
rcond – value of rcond.
For more details, see numpy.linalg.lstsq.
V (ndarray, shape (M,M) or (M,M,K)) – Present only if full==False and cov==True. The covariance
matrix of the polynomial coefficient estimates. The diagonal of
this matrix are the variance estimates for each coefficient. If y
is a 2-D array, then the covariance matrix for the k-th data set
are in V[:,:,k]
Warns:
RankWarning – The rank of the coefficient matrix in the least-squares fit is
deficient. The warning is only raised if full==False.
The coefficient matrix of the coefficients p is a Vandermonde matrix.
polyfit issues a RankWarning when the least-squares fit is badly
conditioned. This implies that the best fit is not well-defined due
to numerical error. The results may be improved by lowering the polynomial
degree or by replacing x by x - x.mean(). The rcond parameter
can also be set to a value smaller than its default, but the resulting
fit may be spurious: including contributions from the small singular
values can add numerical noise to the result.
Note that fitting polynomial coefficients is inherently badly conditioned
when the degree of the polynomial is large or the interval of sample points
is badly centered. The quality of the fit should always be checked in these
cases. When polynomial fits are not satisfactory, splines may be a good
alternative.
Return an antiderivative (indefinite integral) of a polynomial.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
The returned order m antiderivative P of polynomial p satisfies
\(\frac{d^m}{dx^m}P(x) = p(x)\) and is defined up to m - 1
integration constants k. The constants determine the low-order
polynomial part
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
Finds the polynomial resulting from the multiplication of the two input
polynomials. Each input must be either a poly1d object or a 1D sequence
of polynomial coefficients, from highest to lowest degree.
Parameters:
a1 (array_like or poly1d object) – Input polynomials.
a2 (array_like or poly1d object) – Input polynomials.
Returns:
out – The polynomial resulting from the multiplication of the inputs. If
either inputs is a poly1d object, then the output is also a poly1d
object. Otherwise, it is a 1D array of polynomial coefficients from
highest to lowest degree.
>>> p1=np.poly1d([1,2,3])>>> p2=np.poly1d([9,5,1])>>> print(p1) 21 x + 2 x + 3>>> print(p2) 29 x + 5 x + 1>>> print(np.polymul(p1,p2)) 4 3 29 x + 23 x + 38 x + 17 x + 3
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
Given two polynomials a1 and a2, returns a1-a2.
a1 and a2 can be either array_like sequences of the polynomials’
coefficients (including coefficients equal to zero), or poly1d objects.
Parameters:
a1 (array_like or poly1d) – Minuend and subtrahend polynomials, respectively.
a2 (array_like or poly1d) – Minuend and subtrahend polynomials, respectively.
Returns:
out – Array or poly1d object of the difference polynomial’s coefficients.
Returns the data type with the smallest size and smallest scalar
kind to which both type1 and type2 may be safely cast.
The returned data type is always considered “canonical”, this mainly
means that the promoted dtype will always be in native byte order.
This function is symmetric, but rarely associative.
Parameters:
type1 (dtype or dtype specifier) – First data type.
type2 (dtype or dtype specifier) – Second data type.
Returns:
out – The promoted data type.
Return type:
dtype
Notes
Please see numpy.result_type for additional information about promotion.
Added in version 1.6.0.
Starting in NumPy 1.9, promote_types function now returns a valid string
length when given an integer or float dtype as one argument and a string
dtype as another argument. Previously it always returned the input string
dtype, even if it wasn’t long enough to store the max integer/float value
converted to a string.
Changed in version 1.23.0.
NumPy now supports promotion for more structured dtypes. It will now
remove unnecessary padding from a structure dtype and promote included
fields individually.
Range of values (maximum - minimum) along an axis.
The name of the function comes from the acronym for ‘peak to peak’.
Warning
ptp preserves the data type of the array. This means the
return value for an input of signed integers with n bits
(e.g. np.int8, np.int16, etc) is also a signed integer
with n bits. In that case, peak-to-peak values greater than
2**(n-1)-1 will be returned as negative values. An example
with a work-around is shown below.
Axis along which to find the peaks. By default, flatten the
array. axis may be negative, in
which case it counts from the last to the first axis.
Added in version 1.15.0.
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.
out (array_like) – Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output,
but the type of the output values will be cast if necessary.
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 input array.
If the default value is passed, then keepdims will not be
passed through to the ptp method of sub-classes of
ndarray, however any non-default value will be. If the
sub-class’ method does not implement keepdims any
exceptions will be raised.
Returns:
ptp – The range of a given array - scalar if array is one-dimensional
or a new array holding the result along the given axis
out – The real component of the complex argument. If val is real, the type
of val is used for the output. If val has complex elements, the
returned type is float.
Return the roots of a polynomial with coefficients given in p.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial is preferred.
A summary of the differences can be found in the
transition guide.
The values in the rank-1 array p are coefficients of a polynomial.
If the length of p is n+1 then the polynomial is described by:
p[0]*x**n+p[1]*x**(n-1)+...+p[n-1]*x+p[n]
Parameters:
p (array_like) – Rank-1 array of polynomial coefficients.
Returns:
out – An array containing the roots of the polynomial.
Find indices where elements should be inserted to maintain order.
Find the indices into a sorted array a such that, if the
corresponding elements in v were inserted before the indices, the
order of a would be preserved.
Assuming that a is sorted:
side
returned index i satisfies
left
a[i-1]<v<=a[i]
right
a[i-1]<=v<a[i]
Parameters:
a (1-D array_like) – Input array. If sorter is None, then it must be sorted in
ascending order, otherwise sorter must be an array of indices
that sort it.
v (array_like) – Values to insert into a.
side ({'left', 'right'}, optional) – If ‘left’, the index of the first suitable location found is given.
If ‘right’, return the last such index. If there is no suitable
index, return either 0 or N (where N is the length of a).
sorter (1-D array_like, optional) –
Optional array of integer indices that sort array a into ascending
order. They are typically the result of argsort.
Added in version 1.7.0.
Returns:
indices – Array of insertion points with the same shape as v,
or an integer if v is a scalar.
Binary search is used to find the required insertion points.
As of NumPy 1.4.0 searchsorted works with real/complex arrays containing
nan values. The enhanced sort order is documented in sort.
This function uses the same algorithm as the builtin python bisect.bisect_left
(side='left') and bisect.bisect_right (side='right') functions,
which is also vectorized in the v argument.
Return an array drawn from elements in choicelist, depending on conditions.
Parameters:
condlist (list of bool ndarrays) – The list of conditions which determine from which array in choicelist
the output elements are taken. When multiple conditions are satisfied,
the first one encountered in condlist is used.
choicelist (list of ndarrays) – The list of arrays from which the output elements are taken. It has
to be of the same length as condlist.
default (scalar, optional) – The element inserted in output when all conditions evaluate to False.
Returns:
output – The output at position m is the m-th element of the array in
choicelist where the m-th element of the corresponding array in
condlist is True.
Return the unique values in ar1 that are not in ar2.
Parameters:
ar1 (array_like) – Input array.
ar2 (array_like) – Input comparison array.
assume_unique (bool) – If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
Returns:
setdiff1d – 1D array of values in ar1 that are not in ar2. The result
is sorted when assume_unique=False, but otherwise only sorted
if the input is sorted.
Returns element-wise True where signbit is set (less than zero).
Parameters:
x (array_like) – The input value(s).
out (ndarray, None, or tuple of ndarray and None, optional) – A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where (array_like, optional) – This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None, locations within it where the condition is False will
remain uninitialized.
Returns:
result – Output array, or reference to out if that was supplied.
This is a scalar if x is a scalar.
Return the distance between x and the nearest adjacent number.
Parameters:
x (array_like) – Values to find the spacing of.
out (ndarray, None, or tuple of ndarray and None, optional) – A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where (array_like, optional) – This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None, locations within it where the condition is False will
remain uninitialized.
Returns:
out – The spacing of values of x.
This is a scalar if x is a scalar.
It can be considered as a generalization of EPS:
spacing(np.float64(1))==np.finfo(np.float64).eps, and there
should not be any representable number between x+spacing(x) and
x for any finite x.
Take values from the input array by matching 1d index and data slices.
This iterates over matching 1d slices oriented along the specified axis in
the index and data arrays, and uses the former to look up values in the
latter. These slices can be different lengths.
Functions returning an index along an axis, like argsort and
argpartition, produce suitable indices for this function.
indices (ndarray (Ni..., J, Nk...)) – Indices to take along each 1d slice of arr. This must match the
dimension of arr, but dimensions Ni and Nj only need to broadcast
against arr.
axis (int) – The axis to take 1d slices along. If axis is None, the input array is
treated as if it had first been flattened to 1d, for consistency with
sort and argsort.
This is equivalent to (but faster than) the following use of ndindex and
s_, which sets each of ii and kk to a tuple of indices:
Ni,M,Nk=a.shape[:axis],a.shape[axis],a.shape[axis+1:]J=indices.shape[axis]# Need not equal Mout=np.empty(Ni+(J,)+Nk)foriiinndindex(Ni):forkkinndindex(Nk):a_1d=a[ii+s_[:,]+kk]indices_1d=indices[ii+s_[:,]+kk]out_1d=out[ii+s_[:,]+kk]forjinrange(J):out_1d[j]=a_1d[indices_1d[j]]
Equivalently, eliminating the inner loop, the last two lines would be:
out_1d[:]=a_1d[indices_1d]
See also
take
Take along an axis, using the same indices for every 1d slice
put_along_axis
Put values into the destination array by matching 1d index and data slices
Examples
For this sample array
>>> a=np.array([[10,30,20],[60,40,50]])
We can sort either by using sort directly, or argsort and this function
Integrate along the given axis using the composite trapezoidal rule.
If x is provided, the integration happens in sequence along its
elements - they are not sorted.
Integrate y (x) along each 1d slice on the given axis, compute
\(\int y(x) dx\).
When x is specified, this integrates along the parametric curve,
computing \(\int_t y(t) dt =
\int_t y(t) \left.\frac{dx}{dt}\right|_{x=x(t)} dt\).
Parameters:
y (array_like) – Input array to integrate.
x (array_like, optional) – The sample points corresponding to the y values. If x is None,
the sample points are assumed to be evenly spaced dx apart. The
default is None.
dx (scalar, optional) – The spacing between sample points when x is None. The default is 1.
axis (int, optional) – The axis along which to integrate.
Returns:
trapz – Definite integral of y = n-dimensional array as approximated along
a single axis by the trapezoidal rule. If y is a 1-dimensional array,
then the result is a float. If n is greater than 1, then the result
is an n-1 dimensional array.
Image [2]_ illustrates trapezoidal rule – y-axis locations of points
will be taken from y array, by default x-axis distances between
points will be 1.0, alternatively they can be provided with x array
or with dx scalar. Return value will be equal to combined area under
the red lines.
References
Examples
Use the trapezoidal rule on evenly spaced points:
>>> np.trapz([1,2,3])4.0
The spacing between sample points can be selected by either the
x or dx arguments:
Trim the leading and/or trailing zeros from a 1-D array or sequence.
Parameters:
filt (1-D array or sequence) – Input array.
trim (str, optional) – A string with ‘f’ representing trim from front and ‘b’ to trim from
back. Default is ‘fb’, trim zeros from both front and back of the
array.
Returns:
trimmed – The result of trimming the input. The input data type is preserved.
Unpacks elements of a uint8 array into a binary-valued output array.
Each element of a represents a bit-field that should be unpacked
into a binary-valued output array. The shape of the output array is
either 1-D (if axis is None) or the same shape as the input
array with unpacking done along the axis specified.
The number of elements to unpack along axis, provided as a way
of undoing the effect of packing a size that is not a multiple
of eight. A non-negative number means to only unpack count
bits. A negative number means to trim off that many bits from
the end. None means to unpack the entire array (the
default). Counts larger than the available number of bits will
add zero padding to the output. Negative counts must not
exceed the available number of bits.
Added in version 1.17.0.
bitorder ({'big', 'little'}, optional) –
The order of the returned bits. ‘big’ will mimic bin(val),
3=0b00000011=>[0,0,0,0,0,0,1,1], ‘little’ will reverse
the order to [1,1,0,0,0,0,0,0].
Defaults to ‘big’.
Added in version 1.17.0.
Returns:
unpacked – The elements are binary-valued (0 or 1).
Unwrap by taking the complement of large deltas with respect to the period.
This unwraps a signal p by changing elements which have an absolute
difference from their predecessor of more than max(discont,period/2)
to their period-complementary values.
For the default case where period is \(2\pi\) and discont is
\(\pi\), this unwraps a radian phase p such that adjacent differences
are never greater than \(\pi\) by adding \(2k\pi\) for some
integer \(k\).
Parameters:
p (array_like) – Input array.
discont (float, optional) – Maximum discontinuity between values, default is period/2.
Values below period/2 are treated as if they were period/2.
To have an effect different from the default, discont should be
larger than period/2.
axis (int, optional) – Axis along which unwrap will operate, default is the last axis.
If the discontinuity in p is smaller than period/2,
but larger than discont, no unwrapping is done because taking
the complement would only make the discontinuity larger.
The columns of the output matrix are powers of the input vector. The
order of the powers is determined by the increasing boolean argument.
Specifically, when increasing is False, the i-th output column is
the input vector raised element-wise to the power of N-i-1. Such
a matrix with a geometric progression in each row is named for Alexandre-
Theophile Vandermonde.
Parameters:
x (array_like) – 1-D input array.
N (int, optional) – Number of columns in the output. If N is not specified, a square
array is returned (N=len(x)).
Order of the powers of the columns. If True, the powers increase
from left to right, if False (the default) they are reversed.
Added in version 1.9.0.
Returns:
out – Vandermonde matrix. If increasing is False, the first column is
x^(N-1), the second x^(N-2) and so forth. If increasing is
True, the columns are x^0,x^1,...,x^(N-1).