• Register
0 votes

Problem :

Currently I am trying to learn NumPy. I am trying to execute my code but

I am facing following error while trying to use my code.

TypeError: Cannot cast array data from dtype('float64')

           to dtype('S32') according to the rule 'safe'

Please Note : My NumPy version is 1.11.0.

How can I fix the above error ?

8 4 2
2,300 points

1 Answer

0 votes

Solution :

You are facing this error because of the very interesting property of the numpy arrays it says all the elements of your NumPy array must be of a same type

Even if all the elements in your list are all of the type int (more specifically 'numpy.int64'), then all of those elements in the array will be converted to the floats because of some of the element in the some of the row is float.

On the similar lines if we include even the single string at some place in your array then all of the elements of that array are converted to strings:

The original result array has the elements of the type 'numpy.int64', but the if the result/4 operation is returning the array of the type 'numpy.float64' (as 82 / 4 = 20.5, etc.). So when you want to replace your values in the result, then I can say it is not at all 'safe' as you are by mistake trying to place the floats into the array of ints.

5 2 1
4,980 points

Related questions

0 votes
1 answer 3.2K views
Problem : I have the pandas data frame with some of the categorical predictors or variables as 0 & 1, and some of the numeric variables. When I fit that to a stasmodel like below : est = sm.OLS(y, X).fit() It throws the below error : Pandas data ... hundreds of variables. For that I have concatenated the 3 pandas DataFrames to come up with the final DataFrame to be used in the model building.
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 92 views
Problem : I have below error for trying to load the saved SVM model. I have tried uninstalling the sklearn, NumPy and SciPy, and reinstalling a latest versions all-together again (using pip). I am still facing below error. “Runtimewarning : Numpy.dtype size changed, may indicate binary incompatibility” How to get rid of the above mentioned issue?
asked Jan 21 jwilliam 3.9k points
0 votes
1 answer 101 views
Problem : Help needed with this error runtimewarning: numpy.dtype size changed, may indicate binary incompatibility. expected 96, got 88
asked Nov 8, 2019 peterlaw 6.9k points
1 vote
1 answer 117 views
Problem : How to concatenate following numpy arrays? First np.array with the shape (5,4) as below : [[ 7487 500 389580 0] [ 7488 501 392994 0] [ 7491 508 389247 0] [ 7491 508 389247 0] [ 7492 502 399013 0]] Second np.array with the shape (1, ... ([array1, array2]) but i get below error ValueError: all the input arrays must have same number of dimensions How can I get the required output?
asked Dec 12, 2019 alecxe 7.5k points
0 votes
2 answers 1.3K views
Problem : I got following rather cryptic error message: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
asked Nov 18, 2019 peterlaw 6.9k points