mindspore.dataset.text.ToNumber
- class mindspore.dataset.text.ToNumber(data_type)[source]
Tensor operation that converts every element of a string tensor to a number.
Strings are cast according to the rules specified in the following links, except that any strings that represent negative numbers cannot be cast to an unsigned integer type. The links are as follows: https://en.cppreference.com/w/cpp/string/basic_string/stof, https://en.cppreference.com/w/cpp/string/basic_string/stoul.
- Parameters
data_type (mindspore.dtype) – The type to cast to, which must be a numeric type in mindspore.dtype.
- Raises
TypeError – If data_type is not of type mindspore.dtype.
RuntimeError – If the strings are invalid for casting or are out of range after being cast.
- Supported Platforms:
CPU
Examples
>>> import mindspore.dataset as ds >>> import mindspore.dataset.text as text >>> from mindspore import dtype as mstype >>> >>> # Use the transform in dataset pipeline mode >>> numpy_slices_dataset = ds.NumpySlicesDataset(data=[["1", "2", "3"]], column_names=["text"]) >>> to_number_op = text.ToNumber(mstype.int8) >>> numpy_slices_dataset = numpy_slices_dataset.map(operations=to_number_op) >>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True): ... print(item["text"]) [1 2 3] >>> >>> # Use the transform in eager mode >>> data = ["1", "2", "3"] >>> output = text.ToNumber(mstype.uint32)(data) >>> print(output) [1 2 3]
- Tutorial Examples: