mindspore.ops.bincount

mindspore.ops.bincount(input, weights=None, minlength=0)[source]

Counts the number of occurrences of each value in input.

If you don’t specify minlength, the length of output Tensor will be the maximum value of the input input plus one.

If minlength is specified, the length of output Tensor is the value of maximum of input plus 1 and minlength.

Each value in the output Tensor marks the number of occurrences of that index in input. If ‘weights’ is specified, the output results are weighted, i.e out[n] += weight[i] instead of out[n] += 1.

Note

If input contains negative value, the result will be undefined.

Parameters
  • input (Tensor) – 1-d input tensor.

  • weights (Tensor, optional) – Weights, a tensor of the same shape as input. Default: None .

  • minlength (int, optional) – A minimum number of bins for the output tensor. Default: 0 .

Returns

Tensor, a tensor of shape [max(input)+1] if input is non-empty, otherwise, the shape is [0].

Raises
  • TypeError – If input or weights is not a tensor.

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

  • ValueError – If minlength is a negative integer.

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import Tensor, ops
>>> from mindspore import dtype as mstype
>>> x = Tensor([2, 4, 1, 0, 0], dtype=mstype.int64)
>>> print(ops.bincount(x, minlength=7))
[2. 1. 1. 0. 1. 0. 0.]
>>> weights = Tensor([0, 0.25, 0.5, 0.75, 1], dtype=mstype.float32)
>>> print(ops.bincount(x, weights=weights))
[1.75 0.5  0.   0.   0.25]