XGBoost的feature_importances_参数返回NaN

我有以下代码

xgb = XGBRegressor(booster='gblinear', reg_lambda=0, learning_rate=0.028) print(xgb)xgb.fit(X_train_sc, y_train)y_pred = xgb.predict(X_test_sc)print("\n特征重要性:")for item in zip(feature_list_transform, xgb.feature_importances_):    print("{1:10.4f} - {0}".format(item[0],item[1]))print("\n训练集的R平方:")print(xgb.score(X_train_sc,y_train))print("测试集的R平方:")print(xgb.score(X_test_sc,y_test))print("\n来自metrics的均方根误差:")mse = mean_squared_error(y_test, y_pred)rmse = np.sqrt(mse)print(rmse)

输出结果是:

    XGBRegressor(base_score=0.5, booster='gblinear', colsample_bylevel=1,           colsample_bytree=1, gamma=0, learning_rate=0.028, max_delta_step=0,           max_depth=3, min_child_weight=1, missing=None, n_estimators=100,           n_jobs=1, nthread=None, objective='reg:linear', random_state=0,           reg_alpha=0, reg_lambda=0, scale_pos_weight=1, seed=None,           silent=True, subsample=1)    特征重要性:           nan - fertility_rate_log           nan - life_expectancy_log           nan - avg_supply_of_protein_of_animal_origin_log           nan - access_to_improved_sanitation_log           nan - access_to_improved_water_sources_log           nan - obesity_prevalence_log           nan - open_defecation_log           nan - access_to_electricity_log           nan - cereal_yield_log           nan - population_growth_log           nan - avg_value_of_food_production_log           nan - gross_domestic_product_per_capita_ppp_log           nan - net_oda_received_percent_gni_log           nan - adult_literacy_rate           nan - school_enrollment_rate_female           nan - school_enrollment_rate_total           nan - caloric_energy_from_cereals_roots_tubers           nan - anemia_prevalence           nan - political_stability    训练集的R平方:    0.5364714955219572    测试集的R平方:    0.714197620258952    来自metrics的均方根误差:    5.248174086768801

还有错误:

c:\python36\lib\site-packages\xgboost\sklearn.py:420: RuntimeWarning: invalid value encountered in true_divide return all_features / all_features.sum()

如何修复这些NaN值并获取系数?最后,模型运行良好。


回答:

问题出在你的训练调用上…

booster='gblinear'

你使用这个参数训练了一个线性提升器,基本上只是拟合了一个普通的线性回归…

所以不会有特征重要性(但你可以查看系数)

使用 booster = gbtree 来训练树模型

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注