简体   繁体   English

XGBoost-使用XGBRegressor选择功能

[英]XGBoost - Feature selection using XGBRegressor

I am trying to perform features selection (for regression tasks) by XGBRegressor() . 我正在尝试通过XGBRegressor()执行功能选择(用于回归任务XGBRegressor()

More precisely, I would like to know: 更确切地说,我想知道:

  • If there is something like the method feature_importances_ , utilized with XGBClassifier , which I could use for regression. 如果有类似方法feature_importances_ ,可以与XGBClassifier一起XGBClassifier ,则可以将其用于回归分析。
  • If the XGBoost's method plot_importance() is reliable when it is used with XGBRegressor() 如果XGBoost的方法plot_importance()XGBRegressor()使用时是可靠的

最后,我通过以下方式解决了这个问题:

model.booster().get_score(importance_type='weight')

Here is my solution (Xnames refers to the feature names): 这是我的解决方案(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM