mindspore.dataset.transforms.Unique
- class mindspore.dataset.transforms.Unique[source]
Perform the unique operation on the input tensor. Only one column can be transformed at a time.
Returns 3 tensors: unique output tensor, index tensor, and count tensor.
The output tensor contains all the unique elements of the input tensor in the same order that they occur in the input tensor.
The index tensor contains the index of each element of the input tensor in the unique output tensor.
The count tensor contains the count of each element of the output tensor in the input tensor.
Note
Call batch op before calling this function.
- Raises
RuntimeError – If the given tensor has two columns.
- Supported Platforms:
CPU
Examples
>>> import numpy as np >>> import mindspore.dataset as ds >>> import mindspore.dataset.transforms as transforms >>> >>> # Use the transform in dataset pipeline mode >>> # Data before >>> # | x | >>> # +--------------------+ >>> # | [[0,1,2], [1,2,3]] | >>> # +--------------------+ >>> data = [[[0,1,2], [1,2,3]]] >>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["x"]) >>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms.Unique(), ... input_columns=["x"], ... output_columns=["x", "y", "z"]) >>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True): ... print(item["x"].shape, item["y"].shape, item["z"].shape) ... print(item["x"].dtype, item["y"].dtype, item["z"].dtype) (4,) (6,) (4,) int64 int32 int32 >>> # Data after >>> # | x | y |z | >>> # +---------+-----------------+---------+ >>> # | [0,1,2,3] | [0,1,2,1,2,3] | [1,2,2,1] >>> # +---------+-----------------+---------+ >>> >>> # Use the transform in eager mode >>> data = [[0, -1, -2, -1, 2], [2, -0, 2, 1, -3]] >>> output = transforms.Unique()(data) >>> print(output[0].shape, output[1].shape, output[2].shape) (6,) (10,) (6,) >>> print(output[0].dtype, output[1].dtype, output[2].dtype) int64 int32 int32