随机森林分类器ValueError:输入包含NaN,无穷大或对于dtype('float32')而言太大的值

问题描述 投票:0回答:1

我正在尝试将RandomForest方法应用于数据集,但出现此错误:

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

有人可以告诉我我可以在函数中进行哪些修改以使代码起作用:

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

    print("\nMétodo_Random_Forest")

    random_forest = RandomForestRegressor(n_estimators=10)
    np.nan_to_num(x_train)
    np.nan_to_num(y_train)
    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()

    imp.plot(kind="barh")
    plt.xlabel("Importance")
    plt.ylabel("Features")
    plt.title("Feature importance using Random Forest")
    # plt.show()
    plt.savefig(RESULT_PATH + '/ranks_RF.png', bbox_inches='tight')

    return ranks
python python-3.x random-forest
1个回答
0
投票

使用np.isnan(X),对于包含NaN的位置,您会得到一个带True的布尔掩码。

© www.soinside.com 2019 - 2024. All rights reserved.