-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
464 lines (447 loc) · 20.9 KB
/
Copy pathmain.py
File metadata and controls
464 lines (447 loc) · 20.9 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import time
import numpy as np
import argparse
import scipy
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import pynndescent
from graph_construction import graph_from_knn, generate_constraints_pairwise_fast, knn_k_estimating, \
generate_constraints_label_fast
from SSSE_hierarchical import TreeSSSE
from SSSE_partitioning import FlatSSSE
from hierarchical_single_graph import PartitionTree_SSE
from util import dendrogram_purity_expected, Graph
from sklearn.metrics import adjusted_rand_score, normalized_mutual_info_score
parser = argparse.ArgumentParser()
parser.add_argument('--method', required=True, choices=['SSSE_partitioning_pairwise', 'SSSE_partitioning_label',
'SSSE_partitioning_bio_pairwise', 'SSSE_partitioning_bio_label', 'SSSE_hierarchical'])
parser.add_argument('--dataset', required=True, type=str)
parser.add_argument('--constraint_ratio', type=float, required=True)
parser.add_argument('--constraint_weight', default=1.0, type=float)
parser.add_argument('--sigmasq', default=50, type=float, help='square of RBF kernel band width, i.e., sigma^2')
parser.add_argument('--exp_repeats', default=10, type=int)
parser.add_argument('--knn_constant', default=20, type=float)
parser.add_argument('--metric', default='euclidean')
parser.add_argument('--n_seeds', default=10, type=int, help='number of seeds when performing sampling_neighbor()')
parser.add_argument('--sampling', default='neighbor', type=str, choices=['random', 'neighbor'])
parser.add_argument('--knn_k', default=7, type=int)
parser.add_argument('--n_threads', default=30, type=int)
parser.add_argument('--n_montecalo', default=1000, type=int)
parser.add_argument('--sampling_size', default=1000, type=int)
parser.add_argument('--k_con',default=2, type=int)
parser.add_argument('--mergestop_SE', default=0, type=int, help='take SE into consideration or not when merge stops')
parser.add_argument('--normalization', default="MinMaxScaler", choices=['StandardScaler', 'MinMaxScaler'])
# for hierarchical clustering
parser.add_argument('--hie_knn_k', default=7)
args = parser.parse_args()
def SSSE_pairwise_clustering(path):
args.metric = 'euclidean'
args.normalization = 'MinMaxScaler'
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
y = np.array(data['gnd']).astype(float).squeeze()
n_instance = y.shape[0]
n_cluster = np.unique(y).shape[0]
n_instance_actual = n_instance
if n_instance > args.sampling_size * 2.5:
n_instance_actual = args.sampling_size
args.knn_k = knn_k_estimating(n_cluster=n_cluster, n_instance=n_instance_actual, knn_constant=args.knn_constant)
ARIs = []
NMIs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, :args.knn_k]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, :args.knn_k]
weight_knn = np.exp(- dist_knn ** 2 / (2 * args.sigmasq))
graph = graph_from_knn(ind_knn, weight_knn)
if args.constraint_ratio > 0:
graph_con = generate_constraints_pairwise_fast(y, args.constraint_ratio * n_instance,
args.constraint_ratio * n_instance, X, args, weight_knn)
else:
graph_con = Graph(n_instance)
flatSSSE = FlatSSSE(graph, graph_con, n_cluster, args.sampling_size, args)
y_pred = flatSSSE.run_parallel()
time2 = time.time()
ARI = adjusted_rand_score(y, y_pred)
NMI = normalized_mutual_info_score(y, y_pred)
ARIs.append(ARI)
NMIs.append(NMI)
times.append(time2 - time1)
print(ARI, NMI)
print("average: {}\t{}\t{}\n".format(args.dataset, np.mean(ARIs), np.mean(NMIs)))
def SSSE_label_clustering(path):
args.metric = 'euclidean'
args.normalization = 'MinMaxScaler'
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
y = np.array(data['gnd']).astype(float).squeeze()
n_instance = y.shape[0]
n_cluster = np.unique(y).shape[0]
n_instance_actual = n_instance
if n_instance > args.sampling_size * 2.5:
n_instance_actual = args.sampling_size
args.knn_k = knn_k_estimating(n_cluster=n_cluster, n_instance=n_instance_actual, knn_constant=args.knn_constant)
ARIs = []
NMIs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, :args.knn_k ]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, :args.knn_k ]
weight_knn = np.exp(- dist_knn ** 2 / (2 * args.sigmasq))
graph = graph_from_knn(ind_knn, weight_knn)
if args.constraint_ratio > 0:
graph_con = generate_constraints_label_fast(y, args.constraint_ratio * n_instance, args.constraint_ratio * n_instance, X, args, weight_knn)
else:
graph_con = Graph(n_instance)
flatSSSE = FlatSSSE(graph, graph_con, n_cluster, args.sampling_size, args)
y_pred = flatSSSE.run_parallel()
time2 = time.time()
ARI = adjusted_rand_score(y, y_pred)
NMI = normalized_mutual_info_score(y, y_pred)
ARIs.append(ARI)
NMIs.append(NMI)
times.append(time2-time1)
print(path, ARI, NMI)
print("average: {}\t{}\t{}\n".format(args.dataset, np.mean(ARIs), np.mean(NMIs)))
def SE_partitioning_clustering(path):
args.metric = 'euclidean'
args.normalization = 'MinMaxScaler'
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
y = np.array(data['gnd']).astype(float).squeeze()
n_instance = y.shape[0]
n_cluster = np.unique(y).shape[0]
n_instance_actual = n_instance
args.knn_k = knn_k_estimating(n_cluster=n_cluster, n_instance=n_instance_actual, knn_constant=args.knn_constant)
ARIs = []
NMIs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, :args.knn_k]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, :args.knn_k]
weight_knn = np.exp(- dist_knn ** 2 / (2 * args.sigmasq))
graph = graph_from_knn(ind_knn, weight_knn)
graph_con = Graph(n_instance)
from partitioning_single_graph import FlatSSE
flatSSE = FlatSSE(graph, graph_con)
y_pred = flatSSE.build_tree(moving=False)
time2 = time.time()
ARI = adjusted_rand_score(y, y_pred)
NMI = normalized_mutual_info_score(y, y_pred)
ARIs.append(ARI)
NMIs.append(NMI)
times.append(time2 - time1)
print(path, ARI, NMI)
print("average: {}\t{}\t{}\n".format(args.dataset, np.mean(ARIs), np.mean(NMIs)))
def SE_partitioning_clustering_scalable(path):
args.metric = 'euclidean'
args.normalization = 'MinMaxScaler'
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
y = np.array(data['gnd']).astype(float).squeeze()
n_instance = y.shape[0]
n_cluster = np.unique(y).shape[0]
n_instance_actual = n_instance
if n_instance > args.sampling_size * 2.5:
n_instance_actual = args.sampling_size
args.knn_k = knn_k_estimating(n_cluster=n_cluster, n_instance=n_instance_actual, knn_constant=args.knn_constant)
ARIs = []
NMIs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, :args.knn_k ]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, :args.knn_k ]
weight_knn = np.exp(- dist_knn ** 2 / (2 * args.sigmasq))
graph = graph_from_knn(ind_knn, weight_knn)
graph_con = Graph(n_instance)
flatSSSE = FlatSSSE(graph, graph_con, n_cluster, args.sampling_size, args)
y_pred = flatSSSE.run_parallel()
print(np.unique(y_pred).shape[0])
time2 = time.time()
ARI = adjusted_rand_score(y, y_pred)
NMI = normalized_mutual_info_score(y, y_pred)
ARIs.append(ARI)
NMIs.append(NMI)
times.append(time2-time1)
print(path, ARI, NMI)
print("average: {}\t{}\t{}\n".format(args.dataset, np.mean(ARIs), np.mean(NMIs)))
def SSSE_hierar_clustering(path):
args.metric = "cosine"
args.normalization = 'StandardScaler'
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
y = np.array(data['gnd']).astype(float).flatten()
n_instance = y.shape[0]
args.knn_k = args.hie_knn_k
DPs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:,1:args.knn_k]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:,1:args.knn_k]
weight_knn = 1 - dist_knn
graph = graph_from_knn(ind_knn, weight_knn)
if args.constraint_ratio > 0:
graph_con = generate_constraints_pairwise_fast(y, args.constraint_ratio * n_instance, args.constraint_ratio * n_instance, X, args, weight_knn)
else:
graph_con = Graph(n_instance)
treeSSSE = TreeSSSE(graph, graph_con, None, args.sampling_size, args)
t = treeSSSE.run_parallel()
time2 = time.time()
DP = dendrogram_purity_expected(t, y, args.n_montecalo)
DPs.append(DP)
times.append(time2-time1)
print(path, DP)
print("average: {}\t{}\n".format(args.dataset, np.mean(DPs)))
def SE_hierar_clustering(path):
args.metric = "cosine"
args.normalization = 'StandardScaler'
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
y = np.array(data['gnd']).astype(float).flatten()
n_instance = y.shape[0]
args.knn_k = args.hie_knn_k
DPs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:,1:args.knn_k]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:,1:args.knn_k]
weight_knn = 1 - dist_knn
graph = graph_from_knn(ind_knn, weight_knn)
graph_con = Graph(n_instance)
treeSSE = PartitionTree_SSE(graph, graph_con)
root_id, hierarchical_tree_node = treeSSE.build_tree()
t = treeSSE.subtree2etetree(root_id, hierarchical_tree_node)
time2 = time.time()
DP = dendrogram_purity_expected(t, y, args.n_montecalo)
DPs.append(DP)
times.append(time2-time1)
print(path, DP)
print("average: {}\t{}\n".format(args.dataset, np.mean(DPs)))
def SE_hierar_clustering_scalable(path):
args.metric = "cosine"
args.normalization = 'StandardScaler'
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
y = np.array(data['gnd']).astype(float).flatten()
n_instance = y.shape[0]
args.knn_k = args.hie_knn_k
DPs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, 1:args.knn_k]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, 1:args.knn_k]
weight_knn = 1 - dist_knn
graph = graph_from_knn(ind_knn, weight_knn)
graph_con = Graph(n_instance)
treeSSSE = TreeSSSE(graph, graph_con, None, args.sampling_size, args)
t = treeSSSE.run_parallel()
time2 = time.time()
DP = dendrogram_purity_expected(t, y, args.n_montecalo)
DPs.append(DP)
times.append(time2 - time1)
print(path, DP)
print("average: {}\t{}\n".format(args.dataset, np.mean(DPs)))
def SSSE_pairwise_clustering_bio(path):
args.metric = 'cosine'
args.normalization = "MinMaxScaler"
args.mergestop_SE = 1
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
y = np.array(data['gnd']).astype(float).squeeze()
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
n_instance = y.shape[0]
n_cluster = np.unique(y).shape[0]
n_instance_actual = n_instance
if n_instance > args.sampling_size * 2.5:
n_instance_actual = args.sampling_size
args.knn_k = knn_k_estimating(n_cluster=n_cluster, n_instance=n_instance_actual, knn_constant=args.knn_constant)
ARIs = []
NMIs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, :args.knn_k ]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, :args.knn_k ]
weight_knn = 1 - dist_knn
graph = graph_from_knn(ind_knn, weight_knn)
if args.constraint_ratio > 0:
graph_con = generate_constraints_pairwise_fast(y, args.constraint_ratio * n_instance, args.constraint_ratio * n_instance, X, args, weight_knn)
else:
graph_con = Graph(n_instance)
flatSSSE = FlatSSSE(graph, graph_con, n_cluster, args.sampling_size, args)
y_pred = flatSSSE.run_parallel()
time2 = time.time()
ARI = adjusted_rand_score(y, y_pred)
NMI = normalized_mutual_info_score(y, y_pred)
ARIs.append(ARI)
NMIs.append(NMI)
times.append(time2-time1)
print(path, ARI, NMI)
print("average: {}\t{}\t{}\n".format(args.dataset, np.mean(ARIs), np.mean(NMIs)))
def SSSE_label_clustering_bio(path):
args.metric = 'cosine'
args.normalization = "MinMaxScaler"
args.mergestop_SE = 1
if args.dataset == 'Karagiannis':
args.normalization = "StandardScaler"
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
y = np.array(data['gnd']).astype(float).squeeze()
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
n_instance = y.shape[0]
n_cluster = np.unique(y).shape[0]
n_instance_actual = n_instance
if n_instance > args.sampling_size * 2.5:
n_instance_actual = args.sampling_size
args.knn_k = knn_k_estimating(n_cluster=n_cluster, n_instance=n_instance_actual, knn_constant=args.knn_constant)
ARIs = []
NMIs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, :args.knn_k ]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, :args.knn_k ]
weight_knn = 1 - dist_knn
graph = graph_from_knn(ind_knn, weight_knn)
if args.constraint_ratio > 0:
graph_con = generate_constraints_label_fast(y, args.constraint_ratio * n_instance, args.constraint_ratio * n_instance, X, args, weight_knn)
else:
graph_con = Graph(n_instance)
flatSSSE = FlatSSSE(graph, graph_con, n_cluster, args.sampling_size, args)
y_pred = flatSSSE.run_parallel()
time2 = time.time()
ARI = adjusted_rand_score(y, y_pred)
NMI = normalized_mutual_info_score(y, y_pred)
ARIs.append(ARI)
NMIs.append(NMI)
times.append(time2-time1)
print(path, ARI, NMI)
print("average: {}\t{}\t{}\n".format(args.dataset, np.mean(ARIs), np.mean(NMIs)))
def SE_partitioning_clustering_scalable_bio(path):
args.metric = 'cosine'
args.normalization = "MinMaxScaler"
args.mergestop_SE = 1
if args.dataset == 'Karagiannis':
args.normalization = "StandardScaler"
data = scipy.io.loadmat(path)
X = np.array(data['fea']).astype(float)
y = np.array(data['gnd']).astype(float).squeeze()
if args.normalization == 'StandardScaler':
X = StandardScaler().fit_transform(X)
elif args.normalization == 'MinMaxScaler':
X = MinMaxScaler().fit_transform(X)
else:
raise Exception("not implemented")
n_instance = y.shape[0]
n_cluster = np.unique(y).shape[0]
n_instance_actual = n_instance
if n_instance > args.sampling_size * 2.5:
n_instance_actual = args.sampling_size
args.knn_k = knn_k_estimating(n_cluster=n_cluster, n_instance=n_instance_actual, knn_constant=args.knn_constant)
ARIs = []
NMIs = []
times = []
for _ in range(args.exp_repeats):
time1 = time.time()
index = pynndescent.NNDescent(X, metric=args.metric, n_neighbors=100, pruning_degree_multiplier=10.0)
ind_knn = np.array(index.neighbor_graph[0]).astype(int)[:, :args.knn_k ]
dist_knn = np.array(index.neighbor_graph[1]).astype(float)[:, :args.knn_k ]
weight_knn = 1 - dist_knn
graph = graph_from_knn(ind_knn, weight_knn)
graph_con = Graph(n_instance)
flatSSSE = FlatSSSE(graph, graph_con, n_cluster, args.sampling_size, args)
y_pred = flatSSSE.run_parallel()
print(np.unique(y_pred).shape[0])
time2 = time.time()
ARI = adjusted_rand_score(y, y_pred)
NMI = normalized_mutual_info_score(y, y_pred)
ARIs.append(ARI)
NMIs.append(NMI)
times.append(time2-time1)
print(path, ARI, NMI)
print("average: {}\t{}\t{}\n".format(args.dataset, np.mean(ARIs), np.mean(NMIs)))
if __name__=='__main__':
if args.dataset in ["ORL", "COIL100"]:
args.knn_constant = 40
if args.method == "SSSE_partitioning_pairwise":
path = "./datasets/clustering/{}.mat".format(args.dataset)
SSSE_pairwise_clustering(path)
elif args.method == "SSSE_partitioning_label":
path = "./datasets/clustering/{}.mat".format(args.dataset)
SSSE_label_clustering(path)
elif args.method == "SSSE_hierarchical":
path = "./datasets/clustering/{}.mat".format(args.dataset)
SSSE_hierar_clustering(path)
elif args.method == "SSSE_partitioning_bio_pairwise":
path = "./datasets/RNA-seq/{}.mat".format(args.dataset)
SSSE_pairwise_clustering_bio(path)
elif args.method == "SSSE_partitioning_bio_label":
path = "./datasets/RNA-seq/{}.mat".format(args.dataset)
SSSE_label_clustering_bio(path)