Plotting a Pretty Confusion Matrix in Python

Oct 21, 2023 | Data Science

Confusion matrices are pivotal tools in understanding the performance of classification models. While they can be rather monochromatic in output, the Pretty Confusion Matrix library in Python injects a splash of color and clarity into these readings. This guide walks you through the essential steps of installing and utilizing Pretty Confusion Matrix, ensuring that your results stand out beautifully.

Getting Started

Before diving in, you need to have the pretty-confusion-matrix library installed. The installation process is straightforward.

  • Open your command line interface.
  • Run the following command:
bash
pip install pretty-confusion-matrix

How to Plot a Confusion Matrix

1. Plotting from DataFrame

To plot a confusion matrix using a DataFrame, first, you’ll need to create a NumPy array representing the confusion matrix data.

python
import numpy as np
import pandas as pd
from pretty_confusion_matrix import pp_matrix

# Sample confusion matrix data
array = np.array([[13,  0,  1,  0,  2,  0],
                  [0, 50,  2,  0, 10,  0],
                  [0, 13, 16,  0,  0,  3],
                  [0,  0,  0, 13,  1,  0],
                  [0, 40,  0,  1, 15,  0],
                  [0,  0,  0,  0,  0, 20]])

# Create a pandas DataFrame
df_cm = pd.DataFrame(array, index=range(1, 7), columns=range(1, 7))

# Choose colormap
cmap = 'PuRd'

# Plotting the confusion matrix
pp_matrix(df_cm, cmap=cmap)

Imagine a food truck selling various dishes. The confusion matrix is like a menu where each dish represents a potential classification. The entries in the matrix indicate how often a customer orders each dish (actual class) versus what they actually received (predicted class). The greater the mismatch, the more critical it is to address customer preferences!

2. Plotting from Vectors

If you already have your actual and predicted labels in vector form, plotting is equally simple.

python
import numpy as np
from pretty_confusion_matrix import pp_matrix_from_data

# Actual labels
y_test = np.array([1, 2, 3, 4, 5] * 10)

# Predicted labels
predic = np.array([1, 2, 4, 3, 5, 1, 2, 4, 3, 5] * 10)

# Plotting confusion matrix
pp_matrix_from_data(y_test, predic)

3. Customizing Axis Labels

You can also customize the labels shown on the axes, adding clarity to your matrix.

From DataFrame

python
# Custom labels
col = ['Dog', 'Cat', 'Mouse', 'Fox', 'Bird', 'Chicken']
df_cm = pd.DataFrame(array, index=col, columns=col)
pp_matrix(df_cm, cmap=cmap)

From Vectors

python
columns = ['Dog', 'Cat', 'Mouse', 'Fox', 'Bird']
pp_matrix_from_data(y_test, predic, columns)

Choosing Colormaps

The beauty of your confusion matrix can be further enhanced by selecting different colormaps. You can explore different styles using the following command:

python
from matplotlib import colormaps
list(colormaps)

Troubleshooting

If you encounter any issues during installation or while plotting your confusion matrices:

  • Ensure you have the latest version of Python and pip installed.
  • Verify that the pretty-confusion-matrix library is correctly installed by trying to import it in your Python environment.
  • Ensure your NumPy arrays or DataFrames are properly formatted to match the expected input.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With the pretty-confusion-matrix library, you wield the power to represent your model’s performance with clarity. Adequate visualizations can lead to better communication and understanding of your classification results, turning raw numbers into insightful narratives.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox