forked from Moonpuck/chameleon_cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustertools.py
28 lines (25 loc) · 957 Bytes
/
clustertools.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
import numpy as np
from scipy.special import comb
def external_index(v1, v2):
TP, FN, FP, TN = confusion_index(v1, v2)
RI = (TP + TN) / (TP + FN + FP + TN);
ARI = 2 * (TP * TN - FN * FP) / ((TP + FN) * (FN + TN) + (TP + FP) * (FP + TN));
JI = TP / (TP + FN + FP);
FM = TP / np.sqrt((TP + FN) * (TP + FP));
return RI, ARI, JI, FM
def confusion_index(v1, v2):
cmatrix = contingency(v1, v2)
size = np.size(v1)
sum_rows = np.sum(cmatrix, 0)
sum_cols = np.sum(cmatrix, 1)
N = comb(size, 2)
TP = np.sum(list(map(lambda x: comb(x, 2), cmatrix)))
FN = np.sum(list(map(lambda x: comb(x, 2), sum_rows))) - TP
FP = np.sum(list(map(lambda x: comb(x, 2), sum_cols))) - TP
TN = N - TP - FN - FP
return TP, FN, FP, TN
def contingency(v1, v2):
res = np.zeros((np.max(v1), np.max(v2)))
for i in range(0, np.size(v1)):
res[v1[i] - 1, v2[i] - 1] = res[v1[i] - 1, v2[i] - 1] + 1
return res