ValueError: Input contains NaN, infinity or a value too large for dtype('float32')

Machine Learning

I got ValueError when predicting test data using a RandomForest model.

clf = RandomForestClassifier(n_estimators=10, max_depth=6, n_jobs=1, verbose=2)
clf.fit(X_fit, y_fit)

df_test.fillna(df_test.mean())
X_test = df_test.values
y_pred = clf.predict(X_test)

error:

ValueError: Input contains NaN, infinity or a value too large for dtype('float32').

2
Answers

Replies

Here, in this scenario, we need to assume that X_test is a pandas dataframe. Hence, in this case, you can use DataFrame.fillna in order to replace the NAN values. Below is the representation.


 X_test.fillna(X_test.mean())

 

You can replace the NaN values with mean by using the following command,



X_test.fillna(X_test.mean())



If you want to remove the NaN values, use dropna().

 
 

If you want to unleash your potential in this competitive field, please visit the Machine Learning course page for more information, where you can find the Machine Learning tutorials and Machine Learning frequently asked interview questions and answers as well.

 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.