mindspore.ops.UniqueConsecutive

class mindspore.ops.UniqueConsecutive(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.

Warning

This is an experimental API that is subject to change or deletion.

Refer to mindspore.ops.unique_consecutive() for more details.

Parameters
  • 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.

Inputs:
  • x (Tensor) - The input tensor.

Outputs:

A tensor or a tuple of tensors containing tensor objects (output, idx, counts).

  • output has the same type as x 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 x 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.

Supported Platforms:

Ascend GPU CPU

Examples

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