-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEfficientKernelGCSC.py
More file actions
162 lines (150 loc) · 5.81 KB
/
Copy pathEfficientKernelGCSC.py
File metadata and controls
162 lines (150 loc) · 5.81 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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.neighbors import kneighbors_graph
from sklearn.preprocessing import normalize
from sklearn.metrics import pairwise_kernels
class GCSC_Kernel:
def __init__(self, n_clusters, regu_coef=1., n_neighbors=10, kernel='rbf', gamma=1., ro=0.5, save_affinity=False):
"""
:param n_clusters: number of clusters
:param regu_coef: regularization coefficient i.e. labmda
:param n_neighbors: number of neighbors of knn graph
:param kernel: kernel functions, default 'rbf'. See sklearn for available kernels
:param gamma: only used in rbf kernel
:param ro: post-processing parameters
:param save_affinity: if True, save affinity matrix
"""
self.n_clusters = n_clusters
self.regu_coef = regu_coef
self.n_neighbors = n_neighbors
self.kernel = kernel
self.gamma = gamma
self.ro = ro
self.save_affinity = save_affinity
def __adjacent_mat(self, x, n_neighbors=10):
"""
Construct normalized adjacent matrix, N.B. consider only connection of k-nearest graph
:param x: array like: n_sample * n_feature
:return:
"""
A = kneighbors_graph(x, n_neighbors=n_neighbors, include_self=True).toarray()
A = A * np.transpose(A)
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(self, X):
A = self.__adjacent_mat(X, self.n_neighbors)
if self.kernel == 'linear':
K = pairwise_kernels(X, metric='linear')
elif self.kernel == 'polynomial':
K = pairwise_kernels(X, metric='polynomial', gamma=0.05, degree=3)
elif self.kernel == 'sigmoid':
K = pairwise_kernels(X, metric='sigmoid', gamma=0.5)
elif self.kernel == 'rbf':
K = pairwise_kernels(X, metric='rbf', gamma=self.gamma)
else:
raise Exception('Invalid kernel')
I = np.eye(X.shape[0])
T = np.dot(np.transpose(A), K)
inv = np.linalg.inv(np.dot(T, K) + self.regu_coef * I)
C = np.dot(inv, T)
Coef = self.thrC(C, self.ro)
y_pre, C_final = self.post_proC(Coef, self.n_clusters, 8, 18)
if self.save_affinity:
np.savez('./gcsc-kernel-affinity.npz', C=C_final, C1=0.5 * (np.abs(C) + np.abs(C.T)))
return y_pre
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 build_aff(self, C):
N = C.shape[0]
Cabs = np.abs(C)
ind = np.argsort(-Cabs, 0)
for i in range(N):
Cabs[:, i] = Cabs[:, i] / (Cabs[ind[0, i], i] + 1e-6)
Cksym = Cabs + Cabs.T
return Cksym
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) + 1
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)
ca = self.class_acc(y_true, y_best)
return acc, nmi, kappa, ca
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