Last active
May 31, 2022 17:39
-
-
Save zachguo/10296432 to your computer and use it in GitHub Desktop.
Pretty print for sklearn confusion matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.metrics import confusion_matrix | |
def print_cm(cm, labels, hide_zeroes=False, hide_diagonal=False, hide_threshold=None): | |
"""pretty print for confusion matrixes""" | |
columnwidth = max([len(x) for x in labels]+[5]) # 5 is value length | |
empty_cell = " " * columnwidth | |
# Print header | |
print " " + empty_cell, | |
for label in labels: | |
print "%{0}s".format(columnwidth) % label, | |
# Print rows | |
for i, label1 in enumerate(labels): | |
print " %{0}s".format(columnwidth) % label1, | |
for j in range(len(labels)): | |
cell = "%{0}.1f".format(columnwidth) % cm[i, j] | |
if hide_zeroes: | |
cell = cell if float(cm[i, j]) != 0 else empty_cell | |
if hide_diagonal: | |
cell = cell if i != j else empty_cell | |
if hide_threshold: | |
cell = cell if cm[i, j] > hide_threshold else empty_cell | |
print cell, | |
# first generate with specified labels | |
labels = [ ... ] | |
cm = confusion_matrix(ypred, y, labels) | |
# then print it in a pretty way | |
print_cm(cm, labels) |
@hrieke Not really, use it however you want.
Thank you for the fast reply, and the "okay to use statement", but it would be nice to have something from the more standard license.
May I suggest then any of the public domain licenses? CC0 1.0 Universal or Public Domain are two good choices.
If not, MIT License, BSD 3-clause, or Apache License are also very nice.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this is old code, but do you have a license you'd like to release this under?
Thanks!