bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / _core / _multiarray_umath / bincount
built-in
numpy._core._multiarray_umath:bincount
Signature
bincount ( x , / , weights = None , minlength = 0 ) Summary
Count number of occurrences of each value in array of non-negative ints.
Extended Summary
The number of bins (of size 1) is one larger than the largest value in x. If minlength is specified, there will be at least this number of bins in the output array (though it will be longer if necessary, depending on the contents of x). Each bin gives the number of occurrences of its index value in x. If weights is specified the input array is weighted by it, i.e. if a value n is found at position i, out[n] += weight[i] instead of out[n] += 1.
Parameters
x: array_like, 1 dimension, nonnegative intsInput array.
weights: array_like, optionalWeights, array of the same shape as
x.minlength: int, optionalA minimum number of bins for the output array.
Returns
out: ndarray of intsThe result of binning the input array. The length of out is equal to
np.amax(x)+1.
Raises
: ValueErrorIf the input is not 1-dimensional, or contains elements with negative values, or if
minlengthis negative.: TypeErrorIf the type of the input is float or complex.
Examples
import numpy as np np.bincount(np.arange(5)) np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
x = np.array([0, 1, 1, 3, 2, 1, 7, 23]) np.bincount(x).size == np.amax(x)+1The input array needs to be of integer dtype, otherwise a TypeError is raised:
np.bincount(np.arange(5, dtype=float))
A possible use of ``bincount`` is to perform sums over
variable-size chunks of an array, using the ``weights`` keyword.
w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights x = np.array([0, 1, 1, 2, 2, 2]) np.bincount(x, weights=w)
See also
Aliases
-
numpy._core._multiarray_umath.bincount