mindspore.dataset.text.transforms.ToNumber

class mindspore.dataset.text.transforms.ToNumber(data_type)[source]

Tensor operation to convert 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 which represent negative numbers cannot be cast to an unsigned integer type, rules 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) – Type to be cast to. Must be a numeric type in mindspore.dtype.

Raises

RuntimeError – If strings are invalid to cast, or are out of range after being cast.

Examples

>>> import mindspore.common.dtype as mstype
>>> data = [["1", "2", "3"]]
>>> dataset = ds.NumpySlicesDataset(data)
>>> to_number_op = text.ToNumber(mstype.int8)
>>> dataset = dataset.map(operations=to_number_op)