mindspore.numpy.unique

mindspore.numpy.unique(x, return_inverse=False)[源代码]

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

说明

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

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

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

返回

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

Supported Platforms:

Ascend GPU CPU

异常

TypeError – If x is not tensor.

样例

>>> import mindspore.numpy as np
>>> import mindspore as ms
>>> ms.set_context(mode=ms.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]))