How to get from ["1.1", "2.2", "3.2"] to [1.1, 2.2, 3.2] in numpy?
+4
A:
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).
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'], dtype='|S4')
y = x.astype(np.float)
Joe Kington
2010-10-06 22:04:11