mindspore.mint.unique
- mindspore.mint.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None)[source]
Return the unique elements of input tensor.
- Parameters
input (Tensor) – The input tensor.
sorted (bool, optional) – Whether to sort the unique elements in ascending order before returning as output. Default
True.return_inverse (bool, optional) – Whether to additionally return a tensor indicating the indices of input corresponding to output. Default
False.return_counts (bool, optional) – Whether to additionally return a tensor indicating the count of each element in output within the input. Default
False.dim (int, optional) – Specify the dimension for computation.
- Returns
A tensor or a tuple of tensors.
output (Tensor) - The unique elements of input tensor.
inverse_indices (Tensor) - Return when
return_inverseis True. It represents the indices for where elements in input map to in output. WhendimisNone, it has same shape as input, otherwise, the shape is input.shape[dim].counts (Tensor) - Return when
return_countsis True. It represents the number of occurrences for each unique element from input within output. WhendimisNone, it has same shape as output, otherwise, the shape is output.shape[dim].
- Supported Platforms:
Ascend
Examples
>>> import mindspore >>> import numpy as np >>> x = mindspore.tensor(np.array([1, 2, 5, 2]), mindspore.int32) >>> output = mindspore.mint.unique(x, return_inverse=True, return_counts=True) >>> print(output) (Tensor(shape=[3], dtype=Int32, value= [1, 2, 5]), Tensor(shape=[4], dtype=Int64, value= [0, 1, 2, 1]), Tensor(shape=[3], dtype=Int64, value= [1, 2, 1])) >>> y = output[0] >>> print(y) [1 2 5] >>> inverse_indices = output[1] >>> print(inverse_indices) [0 1 2 1] >>> counts = output[2] >>> print(counts) [1 2 1]