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 casted according to the rules specified in the following links: https://en.cppreference.com/w/cpp/string/basic_string/stof, https://en.cppreference.com/w/cpp/string/basic_string/stoul, except that any strings which represent negative numbers cannot be cast to an unsigned integer type.

Parameters

data_type (mindspore.dtype) – mindspore.dtype to be casted to. Must be a numeric type.

Raises

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

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)