-
Notifications
You must be signed in to change notification settings - Fork 3
/
MvAGCimdb.py
305 lines (276 loc) · 10.2 KB
/
MvAGCimdb.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import scipy.io as sio
import time
import random
# import tensorflow as tf
import numpy as np
import scipy
import scipy.sparse as sp
from sklearn.cluster import KMeans
from metrics import clustering_metrics
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.preprocessing import normalize
from time import *
import warnings
warnings.filterwarnings("ignore")
# warnings.simplefilter('error',ComplexWarning)
def read_data(str):
data = sio.loadmat('{}.mat'.format(str))
if(str == 'large_cora'):
X = data['X']
A = data['G']
gnd = data['labels']
gnd = gnd[0, :]
else:
X = data['feature']
A = data['MAM']
B = data['MDM']
av=[]
av.append(A)
av.append(B)
gnd = data['label']
gnd = gnd.T
gnd = np.argmax(gnd, axis=0)
return X, av, gnd
def FGC_cora_modified(X, av, gnd, a, k, ind):
# Store some variables
gama=-1
final=[]
nada = [1, 1]
X_hat_list=[]
X_hat_anchor_list=[]
A_hat_list=[]
for i in range(2):
A=av[i]
N = X.shape[0]
# print("N = {}".format(N))
Im = np.eye(len(ind))
In = np.eye(N)
if sp.issparse(X):
X = X.todense()
# Normalize A
A = A + In
D = np.sum(A, axis=1)
D = np.power(D, -0.5)
D[np.isinf(D)] = 0
D = np.diagflat(D)
A = D.dot(A).dot(D)
# Get filter G
Ls = In - A
G = In - 0.5 * Ls
G_ = In
X_hat = X
for i in range(k):
# G_ = G_.dot(G)
X_hat = G.dot(X_hat)
X_hat_list.append(X_hat)
A_hat = (A)[ind] # (m,n)
A_hat_list.append(A_hat)
X_hat_anchor_list.append(X_hat[ind])
begin_time = time()
# Set the order of filter
for t in range(5):
tmp1=0
tmp2=0
for i in range(2):
tmp1 =tmp1+nada[i]*(X_hat_anchor_list[i].dot(X_hat_anchor_list[i].T) + a * Im)
for i in range(2):
tmp2 = tmp2+nada[i]*(X_hat_anchor_list[i].dot(X_hat_list[i].T) + a * A_hat_list[i])
S = np.linalg.inv(tmp1).dot(tmp2)
for i in range(2):
nada[i] = (-((np.linalg.norm(X_hat_list[i].T - (X_hat_anchor_list[i].T).dot(S))) ** 2 + a * (np.linalg.norm(S - A_hat_list[i])) ** 2) / (gama)) ** (1 / (gama - 1))
print("nada值")
print(nada[i])
# res = 0
# for j in range(2):
# res = res + nada[j] * ((np.linalg.norm(X_hat_list[i].T - (X_hat_anchor_list[i].T).dot(S))) ** 2 + a * (
# np.linalg.norm(S - A_hat_list[i])) ** 2) + (nada[j]) ** (gama)
# final.append(res)
# print(res)
# sio.savemat("a.mat", {'res': final})
return S, begin_time
def main(X, av, gnd, m, a, k, ind):
N = X.shape[0]
begin_time_filter = time()
types = len(np.unique(gnd))
S, begin_time = FGC_cora_modified(X, av, gnd, a, k, ind)
D = np.sum(S, axis=1)
D = np.power(D, -0.5)
D[np.isinf(D)] = 0
D[np.isnan(D)] = 0
D = np.diagflat(D) # (m,m)
S_hat = D.dot(S) # (m,n)
S_hat_tmp = S_hat.dot(S_hat.T) # (m,m)
S_hat_tmp[np.isinf(S_hat_tmp)] = 0
S_hat_tmp[np.isnan(S_hat_tmp)] = 0
# sigma, E = scipy.linalg.eig(S_hat_tmp)
E, sigma, v = sp.linalg.svds(S_hat_tmp, k=types, which='LM')
sigma = sigma.T
sigma = np.power(sigma, -0.5)
sigma[np.isinf(sigma)] = 0
sigma[np.isnan(sigma)] = 0
sigma = np.diagflat(sigma)
C_hat = (sigma.dot(E.T)).dot(S_hat)
C_hat[np.isinf(C_hat)] = 0
C_hat[np.isnan(C_hat)] = 0
C_hat = C_hat.astype(float)
kmeans = KMeans(n_clusters=types, random_state=37).fit(C_hat.T)
predict_labels = kmeans.predict(C_hat.T)
cm = clustering_metrics(gnd, predict_labels)
ac, nm, f1,adj = cm.evaluationClusterModelFromLabel(m,a,k)
end_time = time()
tot_time = end_time - begin_time
tot_time_filter = end_time - begin_time_filter
return ac, nm, f1,adj, tot_time, tot_time_filter
def lower_bound(p, rd):
l = 0
r = len(p) - 1
while(l < r):
# print("rd = {}, l = {}, r= {}".format(rd, l, r))
mid = (l + r) // 2
if(p[mid] > rd):
r = mid
else:
l = mid + 1
# print("rd = {}, l = {}, r= {}".format(rd, l, r))
return l
def node_sampling(A, m, alpha):
D = np.sum(A[0], axis=1).flatten()+np.sum(A[1], axis=1).flatten()
if(len(np.shape(D)) > 1):
D = D.A[0]
print(1)
D = D**alpha
D=D/10000
print(D)
tot = np.sum(D)
print(tot)
p = D / tot
print(p)
for i in range(len(p) - 1):
p[i + 1] = p[i + 1] + p[i]
print(p)
ind = []
vis = [0] * len(D)
while(m):
while(1):
rd = np.random.rand()
pos = lower_bound(p, rd)
if(vis[pos] == 1):
continue
else:
vis[pos] = 1
ind.append(pos)
m = m - 1
break
return ind
def func(X, A, gnd):
m_init_list = [80] #anchor numbers
a_list = [100] #second term
k_init_list = [2] #juanjijieshu
f_alpha_init_list = [3] #important node
k_list = []
aa_list = []
i_list = []
ac_list = []
nm_list = []
f1_list = []
adj_list=[]
tm_list = []
tm_list_filter = []
f_alpha_list = []
N = X.shape[0]
tot_test = 1
ac_max = 0.0
xia = 0
tot = 0
# print(node_sampling(A, 20))
for k in k_init_list:
for i in m_init_list:
# print("now k = {}, now m = {}".format(k, i))
for alpha in f_alpha_init_list:
ind = node_sampling(A, i, alpha)
ac_mean = 0
nm_mean = 0
f1_mean = 0
adj_mean=0
tm_mean = 0
for a in a_list:
# continue
acc, nmm, f11,adj, tm, tm_filter = main(
X, A, gnd, i, a, k, ind)
print("m = {},k = {}, f_alpha = {},a ={}, ac = {}, nmi = {}, f1 = {},adj={}, tm = {}, tm_filter = {}".format(
i, k, alpha, a, acc, nmm, f11,adj, tm, tm_filter))
if(ac_mean < acc):
ac_mean = acc
nm_mean = nmm
f1_mean = f11
adj_mean=adj
tm_mean = tm
tm_mean_filter = tm_filter
i_list.append(i)
k_list.append(k)
aa_list.append(a)
f_alpha_list.append(alpha)
ac_list.append(ac_mean)
nm_list.append(nm_mean)
f1_list.append(f1_mean)
adj_list.append(adj_mean)
tm_list.append(tm_mean)
tm_list_filter.append(tm_mean_filter)
print("m = {}, k ={},f_alpha = {}, ac_mean = {},nm_mean = {},f1_mean = {},adj_mean={},tm_mean = {},tm_mean_filter = {}\n".format(
i, k, alpha, ac_mean, nm_mean, f1_mean,adj_mean, tm_mean, tm_mean_filter))
if(ac_mean > ac_max):
xia = tot
ac_max = ac_mean
tot += 1
for i in range(len(i_list)):
print("m = {},k = {},f_alpha = {}, ac_mean = {}, nm_mean = {}, f1_mean = {},adj_mean={},tm_mean = {},tm_mean_filter ={}".format(
i_list[i], k_list[i], f_alpha_list[i], ac_list[i], nm_list[i], f1_list[i],adj_list[i], tm_list[i], tm_list_filter[i]))
print("the best result is ")
print("m = {},k = {},f_alpha = {}, ac_mean = {}, nm_mean = {}, f1_mean = {},adj_mean={},tm_mean = {},tm_mean_filter = {}".format(
i_list[xia], k_list[xia], f_alpha_list[xia], ac_list[xia], nm_list[xia], f1_list[xia],adj_list[xia], tm_list[xia], tm_list_filter[xia]))
return i_list[xia], k_list[xia], f_alpha_list[xia], ac_list[xia], nm_list[xia], f1_list[xia],adj_list[xia], tm_list[xia], tm_list_filter[xia]
if __name__ == '__main__':
dataset = 'imdb5k'
X, A, gnd = read_data(dataset)
# number of epoch
tt = 1
m_best_list = []
k_best_list = []
f_alpha_best_list = []
ac_best_list = []
nm_best_list = []
f1_best_list = []
adj_best_list = []
tm_best_list = []
tm_filter_best_list = []
for i in range(tt):
nowm, nowk, nowf, nowac, nownm, nowf1,nowadj, nowtm, nowtmf = func(X, A, gnd)
m_best_list.append(nowm)
k_best_list.append(nowk)
f_alpha_best_list.append(nowf)
ac_best_list.append(nowac)
nm_best_list.append(nownm)
f1_best_list.append(nowf1)
adj_best_list.append(nowadj)
tm_best_list.append(nowtm)
tm_filter_best_list.append(nowtmf)
print("iteration {}, m = {}, k = {}, f_alpha = {}, ac = {}, nm = {}, f1 = {},adj={}, tm = {}, tm_filter = {}".format(
i + 1, nowm, nowk, nowf, nowac, nownm, nowf1,nowadj, nowtm, nowtmf))
for i in range(len(ac_best_list)):
print("iteration {}, m = {}, k = {}, f_alpha = {}, ac = {}, nm = {}, f1 = {},adj={}, tm = {}, tm_filter = {}".format(
i + 1, m_best_list[i], k_best_list[i], f_alpha_best_list[i], ac_best_list[i], nm_best_list[i], f1_best_list[i],adj_best_list[i], tm_best_list[i], tm_filter_best_list[i]))
print("ac_mean = {}, ac_std = {}".format(
np.mean(ac_best_list), np.std(ac_best_list, ddof=1)))
print("nm_mean = {}, nm_std = {}".format(
np.mean(nm_best_list), np.std(nm_best_list, ddof=1)))
print("f1_mean = {}, f1_std = {}".format(
np.mean(f1_best_list), np.std(f1_best_list, ddof=1)))
print("adj_mean = {}, adj_std = {}".format(
np.mean(adj_best_list), np.std(adj_best_list, ddof=1)))
print("tm_mean = {}, tm_std = {}".format(
np.mean(tm_best_list), np.std(tm_best_list, ddof=1)))
print("tmf_mean = {}, tmf_std = {}".format(
np.mean(tm_filter_best_list), np.std(tm_filter_best_list, ddof=1)))