mindspore.dataset.transforms.Slice
- class mindspore.dataset.transforms.Slice(*slices)[源代码]
对输入进行切片。
当前仅支持1-D输入。
- 参数:
- 异常:
TypeError - 当 slices 不为Union[int, list[int], slice, Ellipsis]类型。
- 支持平台:
CPU
样例:
>>> import numpy as np >>> import mindspore.dataset as ds >>> import mindspore.dataset.transforms as transforms >>> >>> # Use the transform in dataset pipeline mode >>> # Data before >>> # | col | >>> # +---------+ >>> # | [1,2,3] | >>> # +---------| >>> data = [[1, 2, 3]] >>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"]) >>> # slice indices 1 and 2 only >>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms.Slice(slice(1,3))) >>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True): ... print(item["col"].shape, item["col"].dtype) ... break (2,) int64 >>> # Data after >>> # | col | >>> # +---------+ >>> # | [2,3] | >>> # +---------| >>> >>> # Use the transform in eager mode >>> data = np.array([1, 2, 3]) >>> output = transforms.Slice(slice(1, 3))(data) >>> print(output.shape, output.dtype) (2,) int64