'How to plot feature importance with feature names from GridSearchCV XGBoost results in Python
So I have some code as follows:
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)
And with this, I get a model ; I would like to plot the feature importance of this model in descending order of the feature importance.
model.best_estimator_.feature_importances_
USing this, I can get an array of all the importances, but how to I make a plot (matplotlib) ALONG WITH their feature names?
This "works" but I don't know which is which
plt.bar(range(len(best_estimator_xgbc.feature_importances_)), best_estimator_xgbc.feature_importances_)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
