Skip to content

Instantly share code, notes, and snippets.

@ariffyasri
Forked from ClementC/print_cm.py
Last active September 5, 2017 07:10
Show Gist options
  • Save ariffyasri/fb6c76df29a1112c023625f682a8cd97 to your computer and use it in GitHub Desktop.
Save ariffyasri/fb6c76df29a1112c023625f682a8cd97 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"""
# using str in len because of some label
# may using integer as label
columnwidth = max([len(str(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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment