0
$\begingroup$

How to fix TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''?

from sklearn.preprocessing import Imputer
imputer=Imputer(missing_values="np.nan",strategy="mean",axis=0)
imputer=imputer.fit(x[:,1:3])
x[:,1:3]=imputer.transform(x[:,1:3])

Traceback (most recent call last):

File "", line 3, in imputer=imputer.fit(x[:,1:3])

File "C:\Users\Nibir\Anaconda3\lib\site-packages\sklearn\preprocessing\imputation.py", line 167, in fit self.axis)

File "C:\Users\Nibir\Anaconda3\lib\site-packages\sklearn\preprocessing\imputation.py", line 254, in _dense_fit mask = _get_mask(X, missing_values)

File "C:\Users\Nibir\Anaconda3\lib\site-packages\sklearn\preprocessing\imputation.py", line 30, in _get_mask if value_to_mask == "NaN" or np.isnan(value_to_mask):

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

$\endgroup$
1
  • $\begingroup$ How does your data look like? $\endgroup$
    – TitoOrt
    Oct 16, 2019 at 10:06

1 Answer 1

3
$\begingroup$

You might try it without the double quotes around the np.NaN and do not forget to import numpy (import numpy as np)

As a sidenote, you should use SimpleImputer instead of Imputer:

Imputer was deprecated in version 0.20 and will be removed in 0.22. Import impute.SimpleImputer from sklearn instead.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.