# Trying to make compelling plot for classification results with python

I've been working on a classification problem and have some good results, but now I struggle with trying to put together a good plot to illustrate the probabilities for each prediction. Here is my current data:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df_voting_output.head(n=5)
prob    actual  pred    correct
0   0.460200    0   0   1
1   0.548478    1   1   1
2   0.270609    0   0   1
3   0.686557    0   1   0
4   0.527935    0   1   0
5   0.098687    0   0   1


I've been able to create a bar chart with probabilites using this code:

plt.bar(np.arange(len(voting_predictions[:,1])), voting_predictions[:,1])
plt.xlabel("record number")
plt.ylabel("probability")
plt.title("Classification Probabilities")
plt.show()


However, I'm thinking there has to be a better way to include more information and a key. I'd like the "correct" labels to be clear to see, so from a visual standpoint you can see how the probabilities relate to a correct classification.

## 2 Answers

What piece of information are you trying to convey by presenting this plot? That determines what an appropriate plot type would be.

The bar chart you show in your question would be useful if the specific indices of the probabilities are important - for example, if there could be something special about e.g. indices 504-559 and a person would want to see what probabilities go with those indices - but I suspect that's not the case. It's more likely that you're interested in how the probabilities correlate with actual results. To show that, you could start with a simple scatter plot with probability on the horizontal axis and actual result on the vertical axis. It also helps if you use different colors (and perhaps slightly different symbols) for actual results of 0 and 1. Here's a primitive example:

Another option to show nearly the same information would be to group the probabilities into ranges and plot a histogram showing how many 1 results fall in each range. This better conveys how narrow the crossover from 0 to 1 predictions is.

If all you want to convey is how many predictions of each type were correct vs incorrect, then the confusion matrix recommended by StatsSorceress might be a better choice, since it more directly presents that information. You could "decorate" it as a heatmap, if you think that would enhance the impact (i.e. if this is targeted at less numerically-minded people or it fits with an overall graphical theme), but that probably doesn't make much of a difference.

It's really rare that you'd show a plot of the probabilities for each example in your set. Are you sure you want to do this? A better presentation might be a confusion matrix. Here's how it works:

1) The columns are the true class labels

2) The rows are the predicted classes

3) Along the right hand side of the plot you can show the probability of correctly assigning to a class (or the classification error, if you prefer).

For example, say I have three classes in my dataset. I have 10 examples of each class, so 30 examples total.

My classification results from my model are:

8 of the 10 examples in class 1 were correctly labelled; 1 was misclassified to class 2 and 1 was misclassified to class 3.

7 of the 10 examples in class 2 were correctly labelled; 3 were misclassified to class 3.

9 of the 10 examples in class 3 were correctly labelled; 1 was misclassified to class 2.

Then my confusion matrix looks like this:

$\begin{bmatrix} 0.8 & 0.1 & 0.1 \\ 0.0 & 0.7 & 0.3 \\ 0.0 & 0.1 & 0.9 \end{bmatrix}$

And along the right hand side of my confusion matrix I can include the classification error: 0.2 for class 1; 0.3 for class 2; 0.1 for class 3.

Notice the rows must add to 1.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.45265257358551025, "perplexity": 545.0666036967083}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": false}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-49/segments/1637964358673.74/warc/CC-MAIN-20211128224316-20211129014316-00472.warc.gz"}