I'm trying to apply the RandomForest method to a dataset and I get this error:

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

Could someone tell me what I can modify in the function for the code to work:

def ranks_RF(x_train, y_train, features_train, RESULT_PATH='Results'):
    """Get ranks from Random Forest"""


    random_forest = RandomForestRegressor(n_estimators=10)
    random_forest.fit(x_train, y_train)

    # Get rank by doing two times a sort.
    imp_array = np.array(random_forest.feature_importances_)
    imp_order = imp_array.argsort()
    ranks = imp_order.argsort()

    # Plot Random Forest
    imp = pd.Series(random_forest.feature_importances_, index=x_train.columns)
    imp = imp.sort_values()

    plt.title("Feature importance using Random Forest")
    # plt.show()
    plt.savefig(RESULT_PATH + '/ranks_RF.png', bbox_inches='tight')

    return ranks
  • $\begingroup$ You should search for NaNs inyour data $\endgroup$
    – Aditya
    Feb 17, 2020 at 0:07

2 Answers 2


You are using np.nan_to_num(x_train) which would convert the null values to zeroes and also will take care of infinites. But you are not assigning back. can you try x_train = np.nan_to_num(x_train) and similar to y_train as well?

I just test this with one example:

a = np.array([[1,np.nan,3],[np.nan, 0, np.nan]])
a=np.insert(a, a.shape[0],[[1, np.nan, 1]], axis=0)

when I print a what I see is?

array([[ 1., nan,  3.],
       [nan,  0., nan],
       [ 1., nan,  1.]])

when I do this->


I get

array([[1., 0., 3.],
       [0., 0., 0.],
       [1., 0., 1.]])

But when I print a again, the nulls are still there. Hence do the assignment hope that solves your problem.


You should imput missing data in your dataset, or delete thos rows if they are not many.

Alternatively, if the values are too big to be represented by float32 datatype, try to convert them to float64 (takes more RAM).


Your Answer

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

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