-
Notifications
You must be signed in to change notification settings - Fork 3
/
adjrand.pyx
37 lines (26 loc) · 1.01 KB
/
adjrand.pyx
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
import numpy as np
cimport numpy as np
DTYPE = np.double
ctypedef np.long_t DTYPE_L
ctypedef np.double_t DTYPE_D
def computeAdjustedRandIndex(np.ndarray[DTYPE_D, ndim = 1] pred, np.ndarray[DTYPE_L, ndim = 1] expect):
cdef double ARI = 0.0
if pred.shape[0] != expect.shape[0]:
raise Exception("pred and expect must have the same length.")
cdef int N = pred.shape[0]
cdef int M = N * (N - 1) / 2
cdef int a = 0 # number of pairs that belong to the same class and the same cluster
cdef b = 0 # number of pairs that belong to the same class but different clusters
cdef c = 0 # number of pairs that belong to different classes but the same cluster
cdef int i = 0
cdef int j = 0
for i in range(N - 1):
for j in range(i + 1, N):
if expect[i] == expect[j] and pred[i] == pred[j]:
a += 1
elif expect[i] == expect[j] and pred[i] != pred[j]:
b += 1
elif expect[i] != expect[j] and pred[i] == pred[j]:
c += 1
ARI = (a - ((a+c)*(a+b) / M)) / ( (((a+c)+(a+b)) / 2.0) - ((a+c)*(a+b) / M) )
return ARI