-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_metrics.py
More file actions
129 lines (91 loc) · 3.58 KB
/
cluster_metrics.py
File metadata and controls
129 lines (91 loc) · 3.58 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
import sklearn.metrics
def recovered_upper_limit(clustering, model_kcs):
# Size K
counts = np.bincount(clustering)
ix = np.argsort(-counts)
counts = counts[ix]
# get top M-1
ul = np.sum(counts[:model_kcs-1]) / clustering.shape[0]
return ul
def recovered(ref_clustering, pred_clustering, recall_thres, precision_thres):
r_clusters = np.unique(ref_clustering)
p_clusters = np.unique(pred_clustering)
R = np.zeros((r_clusters.shape[0], p_clusters.shape[0]))
print("Processing ", R.shape)
counts = np.zeros(r_clusters.shape[0])
for i in range(R.shape[0]):
u = ref_clustering == r_clusters[i]
counts[i] = np.sum(u)
for j in range(R.shape[1]):
R[i, j] = is_match(u, pred_clustering == p_clusters[j], recall_thres=recall_thres, precision_thres=precision_thres)
print("Done ", R.shape)
R = np.sum(counts * (np.sum(R, axis=1) > 0)) / ref_clustering.shape[0]
return R
def is_match(ytrue, ypred, recall_thres, precision_thres):
count11 = np.sum((ytrue == 1) & (ypred == 1))
recall11 = count11 / np.sum(ytrue == 1)
precision11 = count11 / (1e-6+np.sum(ypred == 1))
#print("Recall: %0.2f, precision: %0.2f" % (recall11, precision11))
return (recall11 >= recall_thres) and (precision11 >= precision_thres)
def make_membership_profile(clustering):
r = np.unique(clustering)
M = np.zeros((r.shape[0], len(clustering)))
for i in range(M.shape[0]):
M[i, :] = clustering == r[i]
return M
def make_contingency_table(clustering_a, clustering_b):
r = np.unique(clustering_a)
s = np.unique(clustering_b)
C = np.zeros((r.shape[0], s.shape[0]))
for i in range(C.shape[0]):
r_assigned = clustering_a == r[i]
for j in range(C.shape[1]):
s_assigned = clustering_b == s[j]
intersect = np.sum(r_assigned & s_assigned)
C[i, j] = intersect
return C
def fmeasure(clustering_a, clustering_b):
C = make_contingency_table(clustering_a, clustering_b)
return _fmeasure(C)
def _fmeasure(C):
"""
Given contingency table of reference x predicted (RxP),
Computes f-measure per reference cluster.
"""
O = np.zeros_like(C)
for i in range(C.shape[0]):
for j in range(C.shape[1]):
pres = C[i, j] / np.sum(C[:, j])
recall = C[i, j] / np.sum(C[i, :])
f = (2 * pres * recall) / (pres + recall)
O[i, j] = f
N = np.sum(C, axis=1) # R
f = np.sum(N * np.nanmax(O, axis=1)) / np.sum(N)
return f
def main():
# C = make_contingency_table([0, 0, 1, 2, 3, 3], [1, 1, 2, 0, 5, 10])
# print(C)
# f = fmeasure([0, 0, 1, 2, 3, 3], [1, 1, 2, 0, 5, 10])
# print(f)
# R = recovered(np.array([0, 0, 1, 2, 3, 3]), np.array([1, 1, 2, 0, 5, 10]), thres=0.9)
# print(R)
# a, b = np.array([0, 0, 1, 1, 0, 0, 0, 0, 1]), np.array([0, 1, 0, 0, 0, 0, 0, 0, 1])
# x = sklearn.metrics.balanced_accuracy_score(a, b)
# y = fast_bacc(a, b)
# print(x, y)
# x = sklearn.metrics.balanced_accuracy_score(a, np.zeros_like(a))
# y = fast_bacc(a, np.zeros_like(a))
# print(x, y)
#R = recovered(np.array([0, 0, 1, 2, 3, 3]), np.array([1, 1, 2, 0, 5, 10]))
#print(R)
ytrue = np.zeros(500)
ytrue[:10] = 1
ypred = np.zeros(500)
ypred[:5] = 1
print(is_match(ytrue, ypred, recall_thres=0.9, precision_thres=0.9))
ytrue = np.tile(np.arange(50), 10)
print(ytrue)
print(recovered_upper_limit(ytrue, 20))
if __name__ == "__main__":
main()