-
Notifications
You must be signed in to change notification settings - Fork 58
/
metrics.py
77 lines (63 loc) · 2.35 KB
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Implementation of the Deep Temporal Clustering model
Performance metric functions
@author Florent Forest (FlorentF9)
"""
import numpy as np
from sklearn import metrics
from scipy.optimize import linear_sum_assignment
from sklearn.preprocessing import label_binarize
def cluster_acc(y_true, y_pred):
"""
Calculate unsupervised clustering accuracy. Requires scikit-learn installed
# Arguments
y_true: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
row_ind, col_ind = linear_sum_assignment(w.max() - w)
return w[row_ind, col_ind].sum() * 1.0 / y_pred.size
def cluster_purity(y_true, y_pred):
"""
Calculate clustering purity
# Arguments
y_true: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
purity, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
label_mapping = w.argmax(axis=1)
y_pred_voted = y_pred.copy()
for i in range(y_pred.size):
y_pred_voted[i] = label_mapping[y_pred[i]]
return metrics.accuracy_score(y_pred_voted, y_true)
def roc_auc(y_true, q_pred, n_classes):
"""
Calculate area under ROC curve (ROC AUC)
WARNING: DO NOT USE, MAY CONTAIN ERRORS
TODO: CHECK IT!
# Arguments
y_true: true labels, numpy.array with shape `(n_samples,)`
q_pred: predicted probabilities, numpy.array with shape `(n_samples,)`
# Return
ROC AUC score, in [0,1]
"""
if n_classes == 2: # binary ROC AUC
auc = max(metrics.roc_auc_score(y_true, q_pred[:, 1]), metrics.roc_auc_score(y_true, q_pred[:, 0]))
else: # micro-averaged ROC AUC (multiclass)
fpr, tpr, _ = metrics.roc_curve(label_binarize(y_true, classes=np.unique(y_true)).ravel(), q_pred.ravel())
auc = metrics.auc(fpr, tpr)
return auc