XGBoost feature_importances_参数返回nan

时间:2018-11-01 16:15:40

标签: python machine-learning statistics xgboost

我有以下代码

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("\nFeature Importances:")
for item in zip(feature_list_transform, xgb.feature_importances_):
    print("{1:10.4f} - {0}".format(item[0],item[1]))

print("\nR-squared, training set:")
print(xgb.score(X_train_sc,y_train))
print("R-squared, test set:")
print(xgb.score(X_test_sc,y_test))

print("\nRoot-mean squared error, from 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)

    Feature Importances:
           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-squared, training set:
    0.5364714955219572
    R-squared, test set:
    0.714197620258952

    Root-mean squared error, from metrics:
    5.248174086768801

错误:

  

c:\ python36 \ lib \ site-packages \ xgboost \ sklearn.py:420:RuntimeWarning:   true_divide中遇到的无效值返回all_features /   all_features.sum()

如何解决这个难点并获得系数?最后,该模型运行良好。

1 个答案:

答案 0 :(得分:2)

问题出在您的培训电话中...

booster='gblinear'

您正在使用此参数训练线性助推器,基本上只适合普通的线性回归...

所以没有功能的重要性(但是您可以查看系数)

使用booster = gbtree来训练树木