mindspore.numpy.unique

mindspore.numpy.unique(x, return_inverse=False)[source]

Finds the unique elements of a tensor. The input tensor will be flattened first when it has more than one dimension.

Note

Numpy arguments axis, return_index and return_counts are not supported. On CPU, this operator must be executed in graph mode.

Parameters
  • x (Tensor) – The input tensor to be processed.

  • return_inverse (bool) – If True, also return the indices of the unique tensor. Default: False.

Returns

Tensor or tuple of Tensors. - If return_inverse is False, just return the unique tensor. - If return_inverse is True, return tuple of tensors.

Supported Platforms:

Ascend GPU CPU

Raises

TypeError – If x is not tensor.

Examples

>>> import mindspore.numpy as np
>>> from mindspore import context
>>> context.set_context(mode=context.GRAPH_MODE)
>>> input_x = np.asarray([1, 2, 2, 2, 3, 4, 5]).astype('int32')
>>> output_x = np.unique(input_x)
>>> print(output_x)
[1 2 3 4 5]
>>> output_x = np.unique(input_x, return_inverse=True)
>>> print(output_x)
(Tensor(shape=[5], dtype=Int32, value= [ 1, 2, 3, 4, 5]), Tensor(shape=[7], dtype=Int32,
    value= [0, 1, 1, 1, 2, 3, 4]))