gpt4 book ai didi

python - XGBoost feature_importances_ 参数返回 nan

转载 作者:行者123 更新时间:2023-11-30 08:57:42 27 4
gpt4 key购买 nike

我有以下代码

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: invalid value encountered in true_divide return all_features / all_features.sum()

如何修复这个 nan 并获取系数?最后,该模型运行良好。

最佳答案

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

booster='gblinear'

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

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

使用booster = gbtree使用树进行训练

关于python - XGBoost feature_importances_ 参数返回 nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105189/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com