Publish AI, ML & data-science insights to a global community of data professionals.

A different way to visualize classification results

I love good data visualizations. Here is one on classification results.

Upgrade your machine learning report with an uncommon classification plot.

I love good data visualizations. Back in the days when I did my PhD in particle physics, I was stunned by the histograms my colleagues built and how much information was accumulated in one single plot.


Information in Plots

It is really challenging to improve existing visualization methods or to transport methods from other research fields. You have to think about the dimensions in your plot and the ways to add more of them. A good example is the path from a boxplot to a violinplot to a swarmplot. It is a continuous process of adding dimensions and thus information.

The possibilities of adding information or dimensions to a plot are almost endless. Categories can be added with different marker shapes, color maps like in a heat map can serve as another dimension and the size of a marker can give insight to further parameters.

Plots of Classifier Performance

When it comes to machine learning, there are many ways to plot the performance of a classifier. There is an overwhelming amount of metrics to compare different estimators like accuracy, precision, recall or the helpful MMC.

All of the common classification metrics are calculated from true positive, true negative, false positive and false negative incidents. The most popular plots are definitely ROC curve, PRC, CAP curve and the confusion matrix.

I won’t get into detail of the three curves, but there are many different ways to handle the confusion matrix, like adding a heat map.

A seaborn heatmap of a confusion matrix.
A seaborn heatmap of a confusion matrix.

A Classification Mosaic Diagram

For many cases, this is probably sufficient and easy to pick up all relevant information, but for a multi class problem, it can get much harder to do so.

While reading some papers, I stumbled across:

Jakob Raymaekers, Peter J. Rousseeuw, Mia Hubert. Visualizing classification results. arXiv:2007.14495 [stat.ML]

and from there to

Friendly, Michael. "Mosaic Displays for Multi-Way Contingency Tables." Journal of the American Statistical Association, vol. 89, no. 425, 1994, pp. 190–200. JSTOR, www.jstor.org/stable/2291215. Accessed 13 Aug. 2020.

The authors propose a mosaic diagram to plot discrete values. We can transport this idea to the field of machine learning with the predicted classes as the discrete values.

In a multi class environment, such a plot would look like the following:

Mosaic plot of a classification result with four classes.
Mosaic plot of a classification result with four classes.

It has several advantages over a classical confusion matrix. One can easily see the predicted classes on the y-axis and the number proportion of each class on the x-axis. The big difference from a simple bar plot is the width of the bars, which are giving an idea of the class imbalance.

You can find the code for such a plot fed with a confusion matrix here:

Have fun plotting your next classification results!


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles