How To Visualize Machine Learning Results Like a Pro | by Edwin Tan | Towards Data Science

Evaluating machine learning models is a essential step in the Machine Learning workflow. In this article, we examine how to easily visualize various common machine learning metrics with Scikit-plot. While it’s name may suggest that it is only compatible with Scikit-learn models, Scikit-plot can be used for any machine learning framework. Under the hood, Scikit-plot uses matplotlib as its graphing library.
The best part of Scikit-plot is it only requires one line of code to visualize each metric.
Install package
pip install scikit-plot==0.3.7
Import necessary packages
import pandas as pdimport numpy as npfrom sklearn import datasetsfrom sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifierfrom sklearn.model_selection import cross_val_predict, train_test_splitimport scikitplot as skplt
For the purpose of demonstrating Scikit-plot we will train a simple random forest and gradient boosting classifier with the breast cancer dataset.
X,y = datasets.load_breast_cancer(return_X_y = True)X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.3, stratify = y)gbc = GradientBoostingClassifier()gbc_model = gbc.fit(X_train, y_train)y_gbc_proba = gbc_model.predict_proba(X_test)y_gbc_pred = np.where(y_gbc_proba[:,1] > 0.5, 1, 0)rfc = RandomForestClassifier()rfc_model = rfc.fit(X_train, y_train)y_rfc_proba = rfc_model.predict_proba(X_test)y_rfc_pred = np.where(y_rfc_proba[:,1] > 0.5, 1, 0)
Lets take a look at how to use Scikit-plot for visualizing various metrics.
Confusion matrix compares the ground truth to the predicted label and classifies the results into True Positive, True Negative, False Positive and False Negative. To plot the confusion matrix, we simply call the plot_confusion_matrix method.
skplt.metrics.plot_confusion_matrix(y_test, y_gbc_pred, normalize=False, title = 'Confusion Matrix for GBC')
To display normalized values between 0 to 1 simply set normalize parameter to True
skplt.metrics.plot_confusion_matrix(y_test, y_gbc_pred, normalize=True, title = 'Confusion Matrix for GBC')
An ROC curve shows the performance (True Positive Rate aka Recall and False Positive Rate) of a classifier at all classification thresholds against a random baseline classifier.
skplt.metrics.plot_roc(y_test, y_gbc_proba, title = 'ROC Plot for GBC')
By default, ROC plot comes with macro and micro averages. We can hide these plot lines by setting the plot_micro or plot_macro parameters to False. plot_roc method displays all roc curves for all the classes and the chart might become cluttered in multi-class plots therefore we can choose to plot roc curves for specific classes by passing the classes_to_plot parameter.
A precision and recall curve shows precision and recall values at all classification thresholds. It summarizes the trade off between precision and recall.
skplt.metrics.plot_precision_recall(y_test, y_gbc_proba, title = 'PR Curve for GBC')
By default, precision recall curve comes with macro and micro averages. We can hide these plot lines by setting the plot_micro or plot_macro parameters to False. plot_precision_recall displays PR curves for all the classes and the chart might become cluttered in multi-class plots therefore we can choose to plot PR curves for specific classes by passing the classes_to_plot parameter.
Calibration plots also known as probability calibration curves is a diagnostic method to check if the predicted value can directly be interpreted as confidence level. For example, a well calibrated binary classifier should classify samples such that for samples with probability around 0.8, approximately 80% of them are from the positive class. This function only works for binary classification tasks.
probas_list = [y_gbc_proba, y_rfc_proba]clf_names = ['GBC', 'RF']skplt.metrics.plot_calibration_curve(y_test, probas_list = probas_list, clf_names = clf_names)
Cumulative gains curve shows the performance of a model and compares against a random baseline classifier. It shows the percentage of target achieved when considering a portion of the population with the highest probability. This function only work for binary classification.
skplt.metrics.plot_cumulative_gain(y_test, y_gbc_proba, title = 'Cumulative Gains Chart for GBC')
The lift curve shows the response rate for each class compared to a random baseline classifier when considering a portion of the population with the highest probability.
skplt.metrics.plot_lift_curve(y_test, y_gbc_proba, title = 'Lift Curve for GBC')
Scikit-plot allow users to adjust basic properties such as figure size, title font size and text font size using the figsize, title_fontsize and text_fontsize parameters respectively.
skplt.metrics.plot_lift_curve(y_test, y_gbc_proba, title = 'Lift Curve for GBC', figsize = (10,10), title_fontsize = 20, text_fontsize = 20)
In addition, the methods also accepts matplotlib.axes.Axes object through the ax parameter. This comes in useful as we can define the axis to be plotted on. For example, we would like to plot the cumulative gains chart and lift curve side-by-side on the same subplot. We can create a 1x2 subplot grid first and plot each of the chart in different grid within the subplot using the ax parameter.
fig, ax = plt.subplots(1,2)skplt.metrics.plot_cumulative_gain(y_test, y_gbc_proba, ax = ax[0], title = 'Cumulative Gains Chart for GBC')skplt.metrics.plot_lift_curve(y_test, y_gbc_proba, ax = ax[1],  title = 'Lift Curve for GBC')plt.show()
The output for these methods are matplotlib.axes._subplots.AxesSubplot objects which we can further manipulate using matplotlib methods. For example, we can do the following to adjust the range of the vertical axis.
import matplotlib.pyplot as pltax = skplt.metrics.plot_lift_curve(y_test, y_gbc_proba, title = 'Lift Curve for GBC')ax.set_ylim(0,3)
We examined how to plot various common machine learning metrics using just one line of code each and how we can use matplotlib to adjust the chart properties to fit our needs. There are many more functions in Scikit-plot that we did not cover in this article such as plotting elbow and silhouette chart for clustering problems. Check out Scikit-plot documentations for more details.