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)
Answer from Joe Kington on Stack OverflowWell, 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)
Another 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'>
ValueError: could not convert string to float: '' in python numpy
python - Numpy Convert String to Float when Possible - Stack Overflow
python - Numpy converting array from float to strings - Stack Overflow
Convert numpy, list or float to string in python - Stack Overflow
Didn't find a function to make it work, so I wrote something that works for you.
def myArrayConverter(arr):
convertArr = []
for s in arr.ravel():
try:
value = float32(s)
except ValueError:
value = s
convertArr.append(value)
return array(convertArr,dtype=object).reshape(arr.shape)
Cheers
For arrays of mixed datatypes set dtype=object:
>>> mix = numpy.array(['1.', '2.', 'a'])
>>> mixed=[]
>>> for a in list(mix):
try:
mixed.append(float(a))
except:
mixed.append(a)
>>> mixed=numpy.array(mixed, dtype=object)
>>> mixed
array([1.0, 2.0, 'a'], dtype=object)
>>> type(mixed[0]),type(mixed[1]),type(mixed[2])
(<type 'float'>, <type 'float'>, <type 'numpy.string_'>)
Hope it hepls.
You seem a bit confused as to how numpy arrays work behind the scenes. Each item in an array must be the same size.
The string representation of a float doesn't work this way. For example, repr(1.3) yields '1.3', but repr(1.33) yields '1.3300000000000001'.
A accurate string representation of a floating point number produces a variable length string.
Because numpy arrays consist of elements that are all the same size, numpy requires you to specify the length of the strings within the array when you're using string arrays.
If you use x.astype('str'), it will always convert things to an array of strings of length 1.
For example, using x = np.array(1.344566), x.astype('str') yields '1'!
You need to be more explict and use the '|Sx' dtype syntax, where x is the length of the string for each element of the array.
For example, use x.astype('|S10') to convert the array to strings of length 10.
Even better, just avoid using numpy arrays of strings altogether. It's usually a bad idea, and there's no reason I can see from your description of your problem to use them in the first place...
If you have an array of numbers and you want an array of strings, you can write:
strings = ["%.2f" % number for number in numbers]
If your numbers are floats, the array would be an array with the same numbers as strings with two decimals.
>>> a = [1,2,3,4,5]
>>> min_a, max_a = min(a), max(a)
>>> a_normalized = [float(x-min_a)/(max_a-min_a) for x in a]
>>> a_normalized
[0.0, 0.25, 0.5, 0.75, 1.0]
>>> a_strings = ["%.2f" % x for x in a_normalized]
>>> a_strings
['0.00', '0.25', '0.50', '0.75', '1.00']
Notice that it also works with numpy arrays:
>>> a = numpy.array([0.0, 0.25, 0.75, 1.0])
>>> print ["%.2f" % x for x in a]
['0.00', '0.25', '0.50', '0.75', '1.00']
A similar methodology can be used if you have a multi-dimensional array:
new_array = numpy.array(["%.2f" % x for x in old_array.reshape(old_array.size)])
new_array = new_array.reshape(old_array.shape)
Example:
>>> x = numpy.array([[0,0.1,0.2],[0.3,0.4,0.5],[0.6, 0.7, 0.8]])
>>> y = numpy.array(["%.2f" % w for w in x.reshape(x.size)])
>>> y = y.reshape(x.shape)
>>> print y
[['0.00' '0.10' '0.20']
['0.30' '0.40' '0.50']
['0.60' '0.70' '0.80']]
If you check the Matplotlib example for the function you are using, you will notice they use a similar methodology: build empty matrix and fill it with strings built with the interpolation method. The relevant part of the referenced code is:
colortuple = ('y', 'b')
colors = np.empty(X.shape, dtype=str)
for y in range(ylen):
for x in range(xlen):
colors[x, y] = colortuple[(x + y) % len(colortuple)]
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
linewidth=0, antialiased=False)
Type checking is not the only option to do what you want, but definitely one of the easiest:
import numpy as np
def to_str(var):
if type(var) is list:
return str(var)[1:-1] # list
if type(var) is np.ndarray:
try:
return str(list(var[0]))[1:-1] # numpy 1D array
except TypeError:
return str(list(var))[1:-1] # numpy sequence
return str(var) # everything else
EDIT: Another easy way, which does not use type checking (thanks to jtaylor for giving me that idea), is to convert everything into the same type (np.array) and then convert it to a string:
import numpy as np
def to_str(var):
return str(list(np.reshape(np.asarray(var), (1, np.size(var)))[0]))[1:-1]
Example use (both methods give same results):
>>> to_str(1.) #float
'1.0'
>>> to_str([1., 1., 1.]) #list
'1.0, 1.0, 1.0'
>>> to_str(np.ones((1,3))) #np.array
'1.0, 1.0, 1.0'
str is able to convert any type into string. It can be numpy.array / list / float
# using numpy array
new_array = numpy.array([1,2,3])
str(new_array)
>> '[1 2 3]'
# using list
new_list = [1, 2, 3]
str(new_list)
>> '[1, 2, 3]'
# using float
new_float = 1.1
str(new_float)
>> '1.1'