-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGCOT.py
executable file
·144 lines (133 loc) · 4.7 KB
/
GCOT.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import numpy as np
from munkres import Munkres
from scipy.sparse.linalg import svds
from sklearn.cluster import SpectralClustering
from sklearn.metrics import normalized_mutual_info_score, cohen_kappa_score, accuracy_score
from sklearn.preprocessing import normalize,minmax_scale
import ot
class GCOT:
def __init__(self, n_clusters):
"""
:param n_clusters: number of clusters
"""
self.n_clusters = n_clusters
def __adj_mat(self, C, k):
n = C.shape[0]
ridx = np.argpartition(C, -k, axis = 1)[:, -k:]
cidx = np.argpartition(C, -k, axis = 0)[-k:, :]
row = np.zeros((n,n))
col = np.zeros((n,n))
I = np.identity(n)
for i in range(ridx.shape[0]):
row[i, ridx[i, :]] = 1
col[cidx[:, i], i] = 1
A = I + row * col
D = np.diag(np.reshape(np.sum(A, axis=1) ** -0.5, -1))
normlized_A = np.dot(np.dot(D, A), D)
return normlized_A
def fit_base(self, X, eps):
n = X.shape[0]
a, b = np.ones((n,)) / n, np.ones((n,)) / n
M = ot.dist(X, X)
M /= M.max()
C = ot.sinkhorn(a, b, M, eps, verbose=True)
return C
def fit_gcot(self, X, eps, C, k):
n = X.shape[0]
a, b = np.ones((n,)) / n, np.ones((n,)) / n
A = self.__adj_mat(C, k)
E = np.dot(A, X)
M = ot.dist(E, E)
M /= M.max()
C = ot.sinkhorn(a, b, M, eps, verbose=True)
return C
def thrC(self, C, ro):
if ro < 1:
N = C.shape[1]
Cp = np.zeros((N, N))
S = np.abs(np.sort(-np.abs(C), axis=0))
Ind = np.argsort(-np.abs(C), axis=0)
for i in range(N):
cL1 = np.sum(S[:, i]).astype(float)
stop = False
csum = 0
t = 0
while (stop == False):
csum = csum + S[t, i]
if csum > ro * cL1:
stop = True
Cp[Ind[0:t + 1, i], i] = C[Ind[0:t + 1, i], i]
t = t + 1
else:
Cp = C
return Cp
def post_proC(self, C, K, d, alpha):
# C: coefficient matrix, K: number of clusters, d: dimension of each subspace
C = 0.5 * (C + C.T)
r = d * K + 1
U, S, _ = svds(C, r, v0=np.ones(C.shape[0]))
U = U[:, ::-1]
S = np.sqrt(S[::-1])
S = np.diag(S)
U = U.dot(S)
U = normalize(U, norm='l2', axis=1)
Z = U.dot(U.T)
Z = Z * (Z > 0)
L = np.abs(Z ** alpha)
L = L / L.max()
L = 0.5 * (L + L.T)
spectral = SpectralClustering(n_clusters=K, eigen_solver='arpack', affinity='precomputed',
assign_labels='discretize')
spectral.fit(L)
grp = spectral.fit_predict(L)
return grp, L
def cluster_accuracy(self, y_true, y_pre):
Label1 = np.unique(y_true)
nClass1 = len(Label1)
Label2 = np.unique(y_pre)
nClass2 = len(Label2)
nClass = np.maximum(nClass1, nClass2)
G = np.zeros((nClass, nClass))
for i in range(nClass1):
ind_cla1 = y_true == Label1[i]
ind_cla1 = ind_cla1.astype(float)
for j in range(nClass2):
ind_cla2 = y_pre == Label2[j]
ind_cla2 = ind_cla2.astype(float)
G[i, j] = np.sum(ind_cla2 * ind_cla1)
m = Munkres()
index = m.compute(-G.T)
index = np.array(index)
c = index[:, 1]
y_best = np.zeros(y_pre.shape)
for i in range(nClass2):
y_best[y_pre == Label2[i]] = Label1[c[i]]
# # calculate accuracy
err_x = np.sum(y_true[:] != y_best[:])
missrate = err_x.astype(float) / (y_true.shape[0])
acc = 1. - missrate
nmi = normalized_mutual_info_score(y_true, y_pre)
kappa = cohen_kappa_score(y_true, y_best)
print(y_true.shape, y_best.shape)
ca = self.class_acc(y_true, y_best)
return (acc, nmi, kappa, ca), y_best
def class_acc(self, y_true, y_pre):
"""
calculate each class's acc
:param y_true:
:param y_pre:
:return:
"""
ca = []
for c in np.unique(y_true):
y_c = y_true[np.nonzero(y_true == c)] # find indices of each classes
y_c_p = y_pre[np.nonzero(y_true == c)]
acurracy = accuracy_score(y_c, y_c_p)
ca.append(acurracy)
ca = np.array(ca)
return ca
def call_acc(self, C, y, ro):
C = self.thrC(C, ro)
y_pre, C_final = self.post_proC(C, self.n_clusters, 8, 18)
acc, y_best = self.cluster_accuracy(y, y_pre)
return acc, y_pre, y_best