Search code examples
pythonpandasmatplotlibxgboostxgbclassifier

how to plot XGBoost evaluation metrics?


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?


Solution

  • 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