mindspore.dataset.transforms.Slice

class mindspore.dataset.transforms.Slice(*slices)[source]

Extract a slice from the input.

Currently, only 1-D input is supported.

Parameters

slices (Union[int, list[int], slice, Ellipsis]) –

The desired slice.

  • If the input type is int, it will slice the element with the specified index value. Negative index is also supported.

  • If the input type is list[int], it will slice all the elements with the specified index values. Negative index is also supported.

  • If the input type is slice , it will slice according to its specified start position, stop position and step size.

  • If the input type is Ellipsis , all elements will be sliced.

  • If the input is None, all elements will be sliced.

Raises

TypeError – If slices is not of type Union[int, list[int], slice, Ellipsis].

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
>>> # |   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