【发布时间】:2017-12-19 22:30:36
【问题描述】:
我正在尝试通过XGBRegressor() 执行特征选择(用于回归任务)。
更准确地说,我想知道:
- 如果有类似的方法
feature_importances_,与XGBClassifier一起使用,我可以将其用于回归。 - 如果XGBoost的方法
plot_importance()与XGBRegressor()一起使用时是可靠的
【问题讨论】:
我正在尝试通过XGBRegressor() 执行特征选择(用于回归任务)。
更准确地说,我想知道:
feature_importances_,与 XGBClassifier 一起使用,我可以将其用于回归。plot_importance()与XGBRegressor()一起使用时是可靠的【问题讨论】:
最后我通过以下方式解决了这个问题:
model.booster().get_score(importance_type='weight')
【讨论】:
这是我的解决方案(Xnames 指的是功能名称):
def xgb_feature_importance(model_xgb, fnames=None):
b = model_xgb.booster()
fs = b.get_fscore()
all_features = [fs.get(f, 0.) for f in b.feature_names]
all_features = np.array(all_features, dtype=np.float32)
all_features_imp = all_features / all_features.sum()
if fnames is not None:
return pd.DataFrame({'X':fnames, 'IMP': all_features_imp})
else:
return all_features_imp
【讨论】: