当使用XGB想得到特征重要性时报错,代码及报错如下,
model = XGBRegressor(
learning_rate = 0.1,
n_estimators = 300,
max_depth = 7,
min_child_weight = 3,
subsample = 0.8,
colsample_bytree = 0.8,
seed = 0
)
model.fit( X_train, y_train, eval_metric='mae',
eval_set=[(X_train, y_train), (X_valid, y_valid)],
early_stopping_rounds=20 )
y_pred = model.predict( X_valid )
feat_imp = pd.Series(model.booster().get_fscore())
feat_imp = feat_imp.sort_values( ascending=False )
feat_imp[:10].plot( kind='bar', title='Feature Importances' )
plt.ylabel( 'Feature Importance Score' )
plt.show()
遇到这个问题,开始我是佷懵的。
参考https://stackoverflow.com/questions/38212649/feature-importance-with-xgbclassifier上的内容后找到解决方法。
此时我们只需要将model.booster()改为model.get_booster()即可。