bottleneck.slow package

Submodules

bottleneck.slow.move module

Alternative methods of calculating moving window statistics.

bottleneck.slow.move.move_argmax(a, window, min_count=None, axis=-1)

Slow move_argmax for unaccelerated dtype

bottleneck.slow.move.move_argmin(a, window, min_count=None, axis=-1)

Slow move_argmin for unaccelerated dtype

bottleneck.slow.move.move_max(a, window, min_count=None, axis=-1)

Slow move_max for unaccelerated dtype

bottleneck.slow.move.move_mean(a, window, min_count=None, axis=-1)

Slow move_mean for unaccelerated dtype

bottleneck.slow.move.move_median(a, window, min_count=None, axis=-1)

Slow move_median for unaccelerated dtype

bottleneck.slow.move.move_min(a, window, min_count=None, axis=-1)

Slow move_min for unaccelerated dtype

bottleneck.slow.move.move_rank(a, window, min_count=None, axis=-1)

Slow move_rank for unaccelerated dtype

bottleneck.slow.move.move_std(a, window, min_count=None, axis=-1, ddof=0)

Slow move_std for unaccelerated dtype

bottleneck.slow.move.move_sum(a, window, min_count=None, axis=-1)

Slow move_sum for unaccelerated dtype

bottleneck.slow.move.move_var(a, window, min_count=None, axis=-1, ddof=0)

Slow move_var for unaccelerated dtype

bottleneck.slow.nonreduce module

bottleneck.slow.nonreduce.replace(a, old, new)

Slow replace (inplace) used for unaccelerated dtypes.

bottleneck.slow.nonreduce_axis module

bottleneck.slow.nonreduce_axis.argpartition(a, kth, axis=-1, kind='introselect', order=None)

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.

New in version 1.8.0.

Parameters
aarray_like

Array to sort.

kthint or sequence of ints

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.

axisint 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’

orderstr 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_arrayndarray, int

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.

See also

partition

Describes partition algorithms used.

ndarray.partition

Inplace partition.

argsort

Full indirect sort.

take_along_axis

Apply index_array from argpartition to an array as if by calling partition.

Notes

See partition for notes on the different selection algorithms.

Examples

One dimensional array:

>>> x = np.array([3, 4, 2, 1])
>>> x[np.argpartition(x, 3)]
array([2, 1, 3, 4])
>>> x[np.argpartition(x, (1, 3))]
array([1, 2, 3, 4])
>>> x = [3, 4, 2, 1]
>>> np.array(x)[np.argpartition(x, 3)]
array([2, 1, 3, 4])

Multi-dimensional array:

>>> x = np.array([[3, 4, 2], [1, 3, 1]])
>>> index_array = np.argpartition(x, kth=1, axis=-1)
>>> np.take_along_axis(x, index_array, axis=-1)  # same as np.partition(x, kth=1)
array([[2, 3, 4],
       [1, 1, 3]])
bottleneck.slow.nonreduce_axis.nanrankdata(a, axis=None)

Slow nanrankdata function used for unaccelerated dtypes.

bottleneck.slow.nonreduce_axis.partition(a, kth, axis=-1, kind='introselect', order=None)

Return a partitioned copy of an array.

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.

New in version 1.8.0.

Parameters
aarray_like

Array to be sorted.

kthint or sequence of ints

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.

axisint 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’.

orderstr 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_arrayndarray

Array of the same type and shape as a.

See also

ndarray.partition

Method to sort an array in-place.

argpartition

Indirect partition.

sort

Full sorting

Notes

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.

Examples

>>> a = np.array([7, 1, 7, 7, 1, 5, 7, 2, 3, 2, 6, 2, 3, 0])
>>> p = np.partition(a, 4)
>>> p
array([0, 1, 2, 1, 2, 5, 2, 3, 3, 6, 7, 7, 7, 7])

p[4] is 2; all elements in p[:4] are less than or equal to p[4], and all elements in p[5:] are greater than or equal to p[4]. The partition is:

[0, 1, 2, 1], [2], [5, 2, 3, 3, 6, 7, 7, 7, 7]

The next example shows the use of multiple values passed to kth.

>>> p2 = np.partition(a, (4, 8))
>>> p2
array([0, 1, 2, 1, 2, 3, 3, 2, 5, 6, 7, 7, 7, 7])

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:

[0, 1, 2, 1], [2], [3, 3, 2], [5], [6, 7, 7, 7, 7]
bottleneck.slow.nonreduce_axis.push(a, n=None, axis=-1)

Slow push used for unaccelerated dtypes.

bottleneck.slow.nonreduce_axis.rankdata(a, axis=None)

Slow rankdata function used for unaccelerated dtypes.

bottleneck.slow.reduce module

bottleneck.slow.reduce.allnan(a, axis=None)

Slow check for all Nans used for unaccelerated dtypes.

bottleneck.slow.reduce.anynan(a, axis=None)

Slow check for Nans used for unaccelerated dtypes.

bottleneck.slow.reduce.median(a, axis=None)

Slow median function used for unaccelerated dtypes.

bottleneck.slow.reduce.nanargmax(a, axis=None)

Slow nanargmax function used for unaccelerated dtypes.

bottleneck.slow.reduce.nanargmin(a, axis=None)

Slow nanargmin function used for unaccelerated dtypes.

bottleneck.slow.reduce.nanmax(a, axis=None)

Slow nanmax function used for unaccelerated dtypes.

bottleneck.slow.reduce.nanmean(a, axis=None, dtype=None, out=None, keepdims=<no value>, *, where=<no value>)

Compute the arithmetic mean along the specified axis, ignoring NaNs.

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.

For all-NaN slices, NaN is returned and a RuntimeWarning is raised.

New in version 1.8.0.

Parameters
aarray_like

Array containing numbers whose mean 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 means are computed. The default is to compute the mean of the flattened array.

dtypedata-type, optional

Type to use in computing the mean. For integer inputs, the default is float64; for inexact inputs, it is the same as the input dtype.

outndarray, 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 ufuncs-output-type for more details.

keepdimsbool, optional

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

If the value is anything but the default, then keepdims will be passed through to the mean or sum methods of sub-classes of ndarray. If the sub-classes methods does not implement keepdims any exceptions will be raised.

wherearray_like of bool, optional

Elements to include in the mean. See ~numpy.ufunc.reduce for details.

New in version 1.22.0.

Returns
mndarray, see dtype parameter above

If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned. Nan is returned for slices that contain only NaNs.

See also

average

Weighted average

mean

Arithmetic mean taken while not ignoring NaNs

var, nanvar

Notes

The arithmetic mean is the sum of the non-NaN elements along the axis divided by the number of non-NaN elements.

Note that for floating-point input, the mean 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. Specifying a higher-precision accumulator using the dtype keyword can alleviate this issue.

Examples

>>> a = np.array([[1, np.nan], [3, 4]])
>>> np.nanmean(a)
2.6666666666666665
>>> np.nanmean(a, axis=0)
array([2.,  4.])
>>> np.nanmean(a, axis=1)
array([1.,  3.5]) # may vary
bottleneck.slow.reduce.nanmedian(a, axis=None)

Slow nanmedian function used for unaccelerated dtypes.

bottleneck.slow.reduce.nanmin(a, axis=None)

Slow nanmin function used for unaccelerated dtypes.

bottleneck.slow.reduce.nanstd(a, axis=None, ddof=0)

Slow nanstd function used for unaccelerated dtypes.

bottleneck.slow.reduce.nansum(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)

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
aarray_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.

dtypedata-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.

New in version 1.8.0.

outndarray, 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 ufuncs-output-type for more details. The casting of NaN to integer can yield unexpected results.

New in version 1.8.0.

keepdimsbool, optional

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

If the value is anything but the default, then keepdims will be passed through to the mean or sum methods of sub-classes of ndarray. If the sub-classes methods does not implement keepdims any exceptions will be raised.

New in version 1.8.0.

initialscalar, optional

Starting value for the sum. See ~numpy.ufunc.reduce for details.

New in version 1.22.0.

wherearray_like of bool, optional

Elements to include in the sum. See ~numpy.ufunc.reduce for details.

New in version 1.22.0.

Returns
nansumndarray.

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.

See also

numpy.sum

Sum across array propagating NaNs.

isnan

Show which elements are NaN.

isfinite

Show which elements are not NaN or +/-inf.

Notes

If both positive and negative infinity are present, the sum will be Not A Number (NaN).

Examples

>>> np.nansum(1)
1
>>> np.nansum([1])
1
>>> np.nansum([1, np.nan])
1.0
>>> a = np.array([[1, 1], [1, np.nan]])
>>> np.nansum(a)
3.0
>>> np.nansum(a, axis=0)
array([2.,  1.])
>>> np.nansum([1, np.nan, np.inf])
inf
>>> np.nansum([1, np.nan, np.NINF])
-inf
>>> from numpy.testing import suppress_warnings
>>> with suppress_warnings() as sup:
...     sup.filter(RuntimeWarning)
...     np.nansum([1, np.nan, np.inf, -np.inf]) # both +/- infinity present
nan
bottleneck.slow.reduce.nanvar(a, axis=None, ddof=0)

Slow nanvar function used for unaccelerated dtypes.

bottleneck.slow.reduce.ss(a, axis=None)

Slow sum of squares used for unaccelerated dtypes.

Module contents