dpnp.bincount

dpnp.bincount(x, /, weights=None, minlength=None)[source]

Count number of occurrences of each value in array of non-negative ints.

For full documentation refer to numpy.bincount.

Parameters:
  • x ({dpnp.ndarray, usm_ndarray}) -- Input 1-dimensional array with non-negative integer values.

  • weights ({None, dpnp.ndarray, usm_ndarray}, optional) -- Weights, array of the same shape as x. Default: None

  • minlength ({None, int}, optional) -- A minimum number of bins for the output array. Default: None

Returns:

out -- The result of binning the input array. The length of out is equal to dpnp.max(x) + 1.

Return type:

dpnp.ndarray of ints

See also

dpnp.histogram

Compute the histogram of a data set.

dpnp.digitize

Return the indices of the bins to which each value

dpnp.unique

Find the unique elements of an array.

Examples

>>> import dpnp as np
>>> np.bincount(np.arange(5))
array([1, 1, 1, 1, 1])
>>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
array([1, 3, 1, 1, 0, 0, 0, 1])
>>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23])
>>> np.bincount(x).size == np.amax(x) + 1
array(True)

The input array needs to be of integer dtype, otherwise a TypeError is raised:

>>> np.bincount(np.arange(5, dtype=np.float32))
Traceback (most recent call last):
  ...
TypeError: x must be an integer array

A possible use of dpnp.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], dtype=np.float32) # weights
>>> x = np.array([0, 1, 1, 2, 2, 2])
>>> np.bincount(x, weights=w)
array([0.3, 0.7, 1.1], dtype=float32)