Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error ValueError: Input X contains NaN. SimpleImputer does not accept missing values encoded as NaN natively. For supervised learning, you might want to consider sklearn.ensemble.HistGradientBoostingClassifier and Regressor which accept missing values encoded as NaNs natively. Alternatively, it is possible to preprocess the data, for instance by using an imputer transformer in a pipeline or drop samples with missing values. See https://scikit-learn.org/stable/modules/impute.html You can find a list of all estimators that handle NaN values at the following page: https://scikit-learn.org/stable/modules/impute.html#estimators-that-handle-nan-values #4

Open
GlamarSK opened this issue Sep 24, 2022 · 3 comments

Comments

@GlamarSK
Copy link

At line model.fit(train[predictors], train["Target"])
ValueError: Input X contains NaN.
SimpleImputer does not accept missing values encoded as NaN natively. For supervised learning, you might want to consider sklearn.ensemble.HistGradientBoostingClassifier and Regressor which accept missing values encoded as NaNs natively. Alternatively, it is possible to preprocess the data, for instance by using an imputer transformer in a pipeline or drop samples with missing values. See https://scikit-learn.org/stable/modules/impute.html You can find a list of all estimators that handle NaN values at the following page: https://scikit-learn.org/stable/modules/impute.html#estimators-that-handle-nan-values

Then i try to do the following, but not able to resolve this,

Create our imputer to replace missing values with the mean e.g.

imp = SimpleImputer(missing_values=0, strategy='mean')
imp = imp.fit(train)

Impute our data, then train

X_train_imp = imp.transform(train)

model.fit(X_train_imp[predictors], X_train_imp["Target"])

Please share the solution

@meetttttt
Copy link

I had the same issue,
So I removed the null values and it worked,
PS: I know it's not best practice, but it worked, one solution is this, or we can also replace Nan using either mean or median.

@saidavanam
Copy link

I had the same issue.
I used imp = SimpleImputer(missing_values=np.nan, strategy='mean') and it worked

@Zhang-YaLiang
Copy link

you should not set missing_values=0 or something else
just imp = SimpleImputer(strategy='mean') may be ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants