mindspore.numpy.bincount

mindspore.numpy.bincount(x, weights=None, minlength=0, length=None)[source]

Count number of occurrences of each value in array of non-negative ints. 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.

Note

The additional argument length specifies the number of bins (overriding x.max() + 1), which must be provided in graph mode. If x contains negative values, no error will be raised, and negative values are treated as zeros instead.

Parameters
  • x (Union[list, tuple, Tensor]) – 1-d input array.

  • weights (Union[int, float, bool, list, tuple, Tensor], optional) – Weights, array of the same shape as x.

  • minlength (int, optional) – A minimum number of bins for the output array.

  • length (int, optional) – Number of bins.

Returns

Tensor, the result of binning the input array. The length of out is equal to np.amax(x)+1.

Raises

ValueError – if x is not one-dimensional, or if x and weights do not have the same shape.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.numpy as np
>>> print(np.bincount(np.arange(5)))
[1 1 1 1 1]
>>> print(np.bincount(np.array([0, 1, 1, 3, 2, 1, 7])))
[1 3 1 1 0 0 0 1]
>>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
>>> x = np.array([0, 1, 1, 2, 2, 2])
>>> print(np.bincount(x,  weights=w))
[0.3 0.7 1.1]