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 ofout[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
- 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]