How do I make the following conversion in NumPy?
["1.1", "2.2", "3.2"] ⟶ [1.1, 2.2, 3.2]
Well, if you're reading the data in as a list, just do np.array(map(float, list_of_strings)) (or equivalently, use a list comprehension). (In Python 3, you'll need to call list on the map return value if you use map, since map returns an iterator now.)
However, if it's already a numpy array of strings, there's a better way. Use astype().
import numpy as np
x = np.array(['1.1', '2.2', '3.3'])
y = x.astype(np.float)
list. Numpy arrays are deliberately homogenously typed. If you really want, you can use an object array (e.g. np.array(['apple', 1.2, 1, {'b'=None, 'c'=object()}], dtype=object)). However, object arrays don't have any significant advantages over using a list.print(list(y)) gives [1.1, 2.2, 3.3] [Program finished] as requested by OPAnother option might be numpy.asarray:
import numpy as np
a = ["1.1", "2.2", "3.2"]
b = np.asarray(a, dtype=float)
print(a, type(a), type(a[0]))
print(b, type(b), type(b[0]))
resulting in:
['1.1', '2.2', '3.2'] <class 'list'> <class 'str'>
[1.1 2.2 3.2] <class 'numpy.ndarray'> <class 'numpy.float64'>
.tolist() at the end, like that b = np.asarray(d, dtype=np.float64).tolist() to get comma separated listIf you have (or create) a single string, you can use np.fromstring:
import numpy as np
x = ["1.1", "2.2", "3.2"]
x = ','.join(x)
x = np.fromstring( x, dtype=np.float, sep=',' )
Note, x = ','.join(x) transforms the x array to string '1.1, 2.2, 3.2'. If you read a line from a txt file, each line will be already a string.
If you have an array that contains invalid values such as an empty string (''), then a straightforward casting would raise an error. If you just want to convert this "problematic" array into a numpy float array and handle the invalid values later, then pandas package has a function (pandas.to_numeric) that sets invalid values to NaN and converts the rest to float.
import pandas as pd
a = np.array(["1.1", "2.2"], float) # OK
lst = ["1.1", "2.2", "3.2.", ""]
a = np.array(lst, float) # ValueError: could not convert string to float: '3.2.'
a = pd.to_numeric(lst, errors='coerce') # array([1.1, 2.2, nan, nan])