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), y_fit)

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


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

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.



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.



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


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


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.
To Top