2

I have a 40 - dimension numpy vector with values like -0.399917 ,0.441786 ...

The data type of the vector by default is |S8 (String)

When I try to change the dtype using the astype method, I get the following error --

ValueError: could not convert string to float:

My partial ode

 value=np.array(value) 
 value.astype(float)
Abhishek Kannan
  • 988
  • 8
  • 16
  • 1
    did you split the string? you could probably just use `np.fromstring` – M4rtini Jun 10 '14 at 15:22
  • Try `[float(x) for x in value]`. If that works the wrap it with `np.array`. `astype` does not convert strings to floats. – hpaulj Jun 10 '14 at 16:47

1 Answers1

5

I don't have your exact dataset, but the following code worked for me:

a = np.array(['-0.399917', '0.441786'])  # (Line 1)
dim = (2, 2, 2, 2)
a = np.tile(a, dim)
b = a.astype(float)

I know the dimensions aren't the same as you have, but that shouldn't make a difference. What is likely happening is that some of the values in your vector are not of the form you specified. For example, the following both raise your ValueError when they are used in (Line 1):

a = np.array(['-0.399917', '0.441786', ''])
a = np.array(['-0.399917', '0.441786', 'spam'])

It is likely that you have either empty values, string values, or something similar in your array somewhere.

If the values are empty, you can do something like was suggested here:

a[a==''] = '0'

or whatever value you want it to be. You can do a similar thing with other string values as long as they have a pattern. If they don't have a pattern, you can still do this, but it may not be feasible to go through looking for all the possibilities.

EDIT: If you don't care what the strings are, you just want them turned into nan, you can use np.genfromtxt as explained here. That might or might not be dangerous, depending on your application. Often, codes are given to indicate something about the array element and you might not want to treat them all the same.

Community
  • 1
  • 1
philE
  • 1,655
  • 15
  • 20