Skip to content

Instantly share code, notes, and snippets.

@ClementC
Forked from zachguo/print_cm.py
Last active September 5, 2017 07:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ClementC/acf8d5f21fd91c674808 to your computer and use it in GitHub Desktop.
Save ClementC/acf8d5f21fd91c674808 to your computer and use it in GitHub Desktop.
from sklearn.metrics import confusion_matrix
def print_cm(cm, labels):
"""pretty print for confusion matrixes"""
columnwidth = max([len(x) for x in labels])
# Print header
print(" " * columnwidth, end="\t")
for label in labels:
print("%{0}s".format(columnwidth) % label, end="\t")
print()
# Print rows
for i, label1 in enumerate(labels):
print("%{0}s".format(columnwidth) % label1, end="\t")
for j in range(len(labels)):
print("%{0}d".format(columnwidth) % cm[i, j], end="\t")
print()
# first generate with specified labels
labels = [ ... ]
cm = confusion_matrix(ypred, y, labels)
# then print it in a pretty way
print_cm(cm, labels)
@ClementC
Copy link
Author

Adapted for Python 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment