Created
July 22, 2014 02:28
-
-
Save jnothman/27a47a930b6c8cf7f1d6 to your computer and use it in GitHub Desktop.
Illustration of P/R/F1 averaging methods
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 __future__ import print_function | |
import numpy as np | |
from sklearn.metrics import precision_recall_fscore_support as prfs, confusion_matrix | |
from sklearn.preprocessing import label_binarize | |
true = [0, 0, 0, 1, 1, 2] | |
preds = [('under-generate 1', [0, 0, 0, 0, 1, 2]), | |
('under-generate 2', [0, 0, 0, 1, 1, 0]), | |
('over-generate 1', [0, 1, 1, 1, 1, 2]), | |
('confuse 1 and 2', [0, 0, 0, 1, 2, 1])] | |
fmt = lambda x: '%0.2f' % x | |
classes = np.unique(true) | |
for name, pred in preds: | |
print(name) | |
print(confusion_matrix(true, pred)) | |
p, r, f, _ = prfs(true, pred, average=None) | |
for i in classes: | |
print(fmt(p[i]), fmt(r[i]), fmt(f[i]), 'class %d' % i, sep='\t') | |
for average in ['macro', 'micro', 'weighted']: | |
p, r, f, _ = prfs(true, pred, average=average) | |
print(fmt(p), fmt(r), fmt(f), average, sep='\t') | |
true_ml = label_binarize(true, classes)[:, 1:] | |
pred_ml = label_binarize(pred, classes)[:, 1:] | |
for average in ['macro', 'micro', 'weighted']: | |
p, r, f, _ = prfs(true_ml, pred_ml, average=average) | |
print(fmt(p), fmt(r), fmt(f), average + ' minor', sep='\t') | |
print() |
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
under-generate 1 | |
[[3 0 0] | |
[1 1 0] | |
[0 0 1]] | |
0.75 1.00 0.86 class 0 | |
1.00 0.50 0.67 class 1 | |
1.00 1.00 1.00 class 2 | |
0.92 0.83 0.84 macro | |
0.83 0.83 0.83 micro | |
0.88 0.83 0.82 weighted | |
1.00 0.75 0.83 macro minor | |
1.00 0.67 0.80 micro minor | |
1.00 0.67 0.78 weighted minor | |
under-generate 2 | |
[[3 0 0] | |
[0 2 0] | |
[1 0 0]] | |
0.75 1.00 0.86 class 0 | |
1.00 1.00 1.00 class 1 | |
0.00 0.00 0.00 class 2 | |
0.58 0.67 0.62 macro | |
0.83 0.83 0.83 micro | |
0.71 0.83 0.76 weighted | |
0.50 0.50 0.50 macro minor | |
1.00 0.67 0.80 micro minor | |
0.67 0.67 0.67 weighted minor | |
over-generate 1 | |
[[1 2 0] | |
[0 2 0] | |
[0 0 1]] | |
1.00 0.33 0.50 class 0 | |
0.50 1.00 0.67 class 1 | |
1.00 1.00 1.00 class 2 | |
0.83 0.78 0.72 macro | |
0.67 0.67 0.67 micro | |
0.83 0.67 0.64 weighted | |
0.75 1.00 0.83 macro minor | |
0.60 1.00 0.75 micro minor | |
0.67 1.00 0.78 weighted minor | |
confuse 1 and 2 | |
[[3 0 0] | |
[0 1 1] | |
[0 1 0]] | |
1.00 1.00 1.00 class 0 | |
0.50 0.50 0.50 class 1 | |
0.00 0.00 0.00 class 2 | |
0.50 0.50 0.50 macro | |
0.67 0.67 0.67 micro | |
0.67 0.67 0.67 weighted | |
0.25 0.25 0.25 macro minor | |
0.33 0.33 0.33 micro minor | |
0.33 0.33 0.33 weighted minor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment