Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy.dot TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'

Why am I getting this error when using np.dot(a,b.T):

TypeError: Cannot cast array data from dtype('float64') 
               to dtype('S32') according to the rule 'safe'

a and b are of type numpy.ndarray. My NumPy version is 1.11.0.

like image 745
user2212461 Avatar asked Dec 09 '15 07:12

user2212461


2 Answers

Just taking the input from BrenBarn and Warren Weckesser to provide a code snippet which should run (by converting your strings to float):

a = map(lambda x: float(x),a)
b = map(lambda x: float(x),b)
np.dot(a,b.T)

or simpler as suggested by @JLT

a = map(float,a)
b = map(float,b)
np.dot(a,b.T)

But as Warren Weckesser already said, you should check the types of the array, most likely one already contains floats.

like image 61
Romeo Kienzler Avatar answered Nov 12 '22 06:11

Romeo Kienzler


Try converting whole numpy array into float Example:

train = train.astype(float)
train_target = train_target.astype(float)
like image 24
VishnuVardhanA Avatar answered Nov 12 '22 08:11

VishnuVardhanA