mindspore.ops.unique_consecutive

mindspore.ops.unique_consecutive(input, return_idx=False, return_counts=False, axis=None)[source]

Returns the elements that are unique in each consecutive group of equivalent elements in the input tensor.

Parameters
  • input (Tensor) – The input tensor.

  • return_idx (bool, optional) – Whether to return the index of where the element in the original input maps to the position in the output. Default: False.

  • return_counts (bool, optional) – Whether to return the counts of each unique element. Default: False.

  • axis (int, optional) – The dimension to apply unique. If None, the unique of the flattened input is returned. If specified, it must be int32 or int64. Default: None.

Returns

A tensor or a tuple of tensors containing tensor objects (output, idx, counts). output has the same type as input and is used to represent the output list of unique scalar elements. If return_idx is True, there will be an additional returned tensor, idx, which has the same shape as input and represents the index of where the element in the original input maps to the position in the output. If return_counts is True, there will be an additional returned tensor, counts, which represents the number of occurrences for each unique value or tensor.

Raises
  • TypeError – If input is not a Tensor.

  • TypeError – If dtype of input is not supported.

  • TypeError – If return_idx is not a bool.

  • TypeError – If return_counts is not a bool.

  • TypeError – If axis is not an int.

  • ValueError – If axis is not in the range of \([-ndim, ndim-1]\).

Supported Platforms:

Ascend GPU CPU

Examples

>>> x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]), mstype.int32)
>>> output, idx, counts = ops.unique_consecutive(x, True, True, None)
>>> print(output)
[1 2 3 1 2]
>>> print(idx)
[0 0 1 1 2 3 3 4]
>>> print(counts)
[2 2 1 2 1]