how to plot XGBoost evaluation metrics?
Asked Answered
W

1

9

I've the following code

eval_set = [(X_train, y_train), (X_test, y_test)]
eval_metric = ["auc","error"]

In the following part, I'm training the XGBClassifier model

model = XGBClassifier()
%time model.fit(X_train, y_train, eval_set=eval_set, eval_metric=eval_metric, verbose=True)

This gives me the metrics in the following format

[0] validation_0-auc:0.840532   validation_0-error:0.187758 validation_1-auc:0.84765    validation_1-error:0.17672
[1] validation_0-auc:0.840536   validation_0-error:0.187758 validation_1-auc:0.847665   validation_1-error:0.17672
....
[99] validation_0-auc:0.917587  validation_0-error:0.13846  validation_1-auc:0.918747   validation_1-error:0.137714
Wall time: 5 s

I made a DataFrame out of this and plotted between time (0-99) and the other metrics. Is there any other way to plot directly feeding the output?

Waggish answered 17/8, 2018 at 18:11 Comment(2)
what do you mean "I made a DataFrame out of this"? The most straightforward option is to use model.evals_result()['sample name']['metric name'] list in plotting. See xgboost.readthedocs.io/en/latest/python/… for an exampleFumigant
@MykhailoLisovyi thanks, will check it out. By dataframe, I meant I took the output and created one.Waggish
A
17

I'll continue from your code to show the example of plotting your AUC score.

results = model.evals_result()
epochs = len(results['validation_0']['error'])
x_axis = range(0, epochs)

results is your y-axis values, and epochs is your 'n_estimators' value. Code below plots these results:

fig, ax = pyplot.subplots()
ax.plot(x_axis, results['validation_0']['auc'], label='Train')
ax.plot(x_axis, results['validation_1']['auc'], label='Test')
ax.legend()
pyplot.ylabel('AUC')
pyplot.title('XGBoost AUC')
pyplot.show()

This will then give the following output:

AUC XGBoost evaluation metric plot

If you want to look at classification error, change ['auc'] to ['error'] within ax.plot

Attainable answered 23/2, 2020 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.