【问题标题】:How to plot feature importance with feature names from GridSearchCV XGBoost results in Python如何在 Python 中使用 GridSearchCV XGBoost 结果中的特征名称绘制特征重要性
【发布时间】:2022-06-20 19:07:46
【问题描述】:

所以我有一些代码如下:

best_score_param_estimator_gs = []
# XGBoost Model
xg_model = xg.XGBClassifier(use_label_encoder=False,
objective='binary:logistic',
verbosity=1,
seed=42
)

params = {
    'learning_rate': [.01],
    'n_estimators': [550],
    'gamma': [5],
    'subsample': [0.65],
    'colsample_bytree': [1.0],
    'max_depth': [6]
}


skf = StratifiedKFold(n_splits=2, shuffle = True, random_state = 1001)
    
# AUC and accuracy as score
scoring = {'AUC':'roc_auc', 'Accuracy':metrics.make_scorer(metrics.accuracy_score)}

# Run grid search
global grid
grid = GridSearchCV(xg_model, param_grid=params, scoring=scoring, refit='AUC', n_jobs=6, 
    cv=skf.split(X_train,y_train), verbose=1)
model = grid.fit(X_train, y_train)


有了这个,我得到了一个模型;我想按特征重要性的降序绘制此模型的特征重要性。

model.best_estimator_.feature_importances_

使用这个,我可以获得所有重要性的数组,但是我如何制作一个情节(matplotlib)以及它们的特征名称?

这个“有效”,但我不知道哪个是哪个

plt.bar(range(len(best_estimator_xgbc.feature_importances_)), best_estimator_xgbc.feature_importances_)

【问题讨论】:

    标签: python xgboost feature-selection grid-search


    【解决方案1】:

    试试这个:

    from xgboost import plot_importance
    
    plot_importance(model.best_estimator_)
    plt.show()
    

    (假设你已经完成了import matplotlib.pyplot as plt

    xgboost 包中的内置 plot_importance 函数看起来就像您要找的一样。

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 2022-01-19
      • 1970-01-01
      • 2018-12-06
      • 2019-08-08
      • 2019-04-15
      • 2019-12-13
      • 2018-06-30
      相关资源
      最近更新 更多