plot a pretty confusion matrix (like Matlab) in python using seaborn and matplotlib
Project description
Confusion Matrix in Python
Plot a pretty confusion matrix (like Matlab) in python using seaborn and matplotlib.
This module get a pretty print confusion matrix from a NumPy matrix or from 2 NumPy arrays (y_test and predictions).
Become a sponsor
Please, consider contributing it to the project. Support the developers who power open source! Invest in open source. It powers your world. Become a sponsor now!
If my open source projects could bring you closer to your goals and you want to say thank you, and you can contribute, feel free to do Buy me a ☕ !
Or Become a sponsor directly on GitHub sponsors page ! You will receive a sponsor badge on your profile.
I will be really thankfull for anything even if it is a coffee or just a kind comment towards my work, because that helps me a lot.
See my projects and my contacts in my Linktree if you want to stay connected!
Installation
pip install pretty-confusion-matrix
Get Started
Plotting from DataFrame:
import numpy as np
import pandas as pd
from pretty_confusion_matrix import pp_matrix
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]])
# get pandas dataframe
df_cm = pd.DataFrame(array, index=range(1, 7), columns=range(1, 7))
# colormap: see this and choose your more dear
cmap = 'PuRd'
pp_matrix(df_cm, cmap=cmap)
Plotting from vectors
import numpy as np
from pretty_confusion_matrix import pp_matrix_from_data
y_test = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2,
3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
predic = np.array([1, 2, 4, 3, 5, 1, 2, 4, 3, 5, 1, 2, 3, 4, 4, 1, 4, 3, 4, 5, 1, 2, 4, 4, 5, 1, 2, 4, 4, 5, 1, 2, 4, 4, 5, 1, 2, 4, 4, 5, 1, 2, 3, 3, 5, 1, 2, 3, 3, 5, 1, 2,
3, 4, 4, 1, 2, 3, 4, 1, 1, 2, 3, 4, 1, 1, 2, 3, 4, 1, 1, 2, 4, 4, 5, 1, 2, 4, 4, 5, 1, 2, 4, 4, 5, 1, 2, 4, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
pp_matrix_from_data(y_test, predic)
Using custom labels in axis
You can customize the labels in axis, whether by DataFrame or vectors.
From DataFrame
To plot the matrix with text labels in axis rather than integer, change the params index and columns of your dataframe.
Getting the example one above, just change the line df_cm = pd.DataFrame(array, index=range(1, 7), columns=range(1, 7)) by
col = ['Dog', 'Cat', 'Mouse', 'Fox', 'Bird', 'Chicken']
df_cm = pd.DataFrame(array, index=col, columns=col)
It'll replace the integer labels (1...6) in the axis, by Dog, Cat, Mouse, and so on..
From vectors
It's very similar, in this case you just need to use the columns param like the example below.
This param is a positional array, i.e., the order must be the same of the data representation.
In this example Dog will be assigned to the class 0, Cat will be assigned to the class 1, and so on and so forth.
Getting the example two above, just change the line pp_matrix_from_data(y_test, predic), by
columns = ['Dog', 'Cat', 'Mouse', 'Fox', 'Bird']
pp_matrix_from_data(y_test, predic, columns)
It'll replace "class A, ..., class E" in the axis, by Dog, Cat, ..., Bird.
More information about "How to plot confusion matrix with string axis rather than integer in python" in this Stackoverflow answer.
Choosing Colormaps
You can choose the layout of the your matrix by a lot of colors options like PuRd, Oranges and more... To customizer your color scheme, use the param cmap of funcion pp_matrix. To see all the colormap available, please do this:
from matplotlib import colormaps
list(colormaps)
More information about Choosing Colormaps in Matplotlib is available here.
References:
1. MATLAB confusion matrix:
b) Plot Confusion Matrix Using Categorical Labels
2. Examples and more on Python:
a) How to plot confusion matrix with string axis rather than integer in python
b) Plot-scikit-learn-classification-report
c) Plot-confusion-matrix-with-string-axis-rather-than-integer-in-Python
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pretty_confusion_matrix-0.7.2.tar.gz.
File metadata
- Download URL: pretty_confusion_matrix-0.7.2.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f4b9198d1e2f25ad3111d89e1153b44480066eab89b1047f844809a1e1be195
|
|
| MD5 |
94a1081143dc6c6bddab5d7ca37879e0
|
|
| BLAKE2b-256 |
f2db6d76fd0a0cc045171b913cdaf380ace2fb6553f8b735c81b99d27a525143
|
Provenance
The following attestation bundles were made for pretty_confusion_matrix-0.7.2.tar.gz:
Publisher:
python-publish.yml on wcipriano/pretty-print-confusion-matrix
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pretty_confusion_matrix-0.7.2.tar.gz -
Subject digest:
9f4b9198d1e2f25ad3111d89e1153b44480066eab89b1047f844809a1e1be195 - Sigstore transparency entry: 1539741043
- Sigstore integration time:
-
Permalink:
wcipriano/pretty-print-confusion-matrix@64229c08ddb0ed56395df41424b754e3a2a90320 -
Branch / Tag:
refs/tags/0.7.3 - Owner: https://github.com/wcipriano
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@64229c08ddb0ed56395df41424b754e3a2a90320 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pretty_confusion_matrix-0.7.2-py3-none-any.whl.
File metadata
- Download URL: pretty_confusion_matrix-0.7.2-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fde25b2c299c21bfc0d0139532970a986601efbc0f69aa07bc284499434a2798
|
|
| MD5 |
3aeb6c924924f94f11049b5262709643
|
|
| BLAKE2b-256 |
8dfcbb82645425a36c02f2d63ad4216b05e7ff90737f0872d4b447ae20927a22
|
Provenance
The following attestation bundles were made for pretty_confusion_matrix-0.7.2-py3-none-any.whl:
Publisher:
python-publish.yml on wcipriano/pretty-print-confusion-matrix
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pretty_confusion_matrix-0.7.2-py3-none-any.whl -
Subject digest:
fde25b2c299c21bfc0d0139532970a986601efbc0f69aa07bc284499434a2798 - Sigstore transparency entry: 1539741145
- Sigstore integration time:
-
Permalink:
wcipriano/pretty-print-confusion-matrix@64229c08ddb0ed56395df41424b754e3a2a90320 -
Branch / Tag:
refs/tags/0.7.3 - Owner: https://github.com/wcipriano
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@64229c08ddb0ed56395df41424b754e3a2a90320 -
Trigger Event:
release
-
Statement type: