mindspore.numpy.unique
- mindspore.numpy.unique(x, return_inverse=False)[源代码]
- 返回去重后的Tensor元素。当输入Tensor有多个维度时,将首先将其展平。 - 说明 - 不支持Numpy的 - axis、- return_index和- return_counts参数。在CPU上,该操作必须在图模式下执行。- 参数:
- x (Tensor) - 要处理的输入Tensor。 
- return_inverse (bool,可选) - 如果为 - True,还返回唯一Tensor的索引。默认值:- False。
 
- 返回:
- Tensor或Tensor的tuple。如果 - return_inverse为- False,返回唯一Tensor;否则,返回Tensor的tuple。
- 异常:
- TypeError - 如果 - x不是Tensor。
 
- 支持平台:
- Ascend- GPU- CPU
 - 样例: - >>> 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]))