-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_link.py
314 lines (223 loc) · 9.1 KB
/
random_link.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
306
307
308
309
310
311
312
313
314
import os
import numpy as np
import pandas as pd
from enum import Enum
from scipy.spatial import distance
from scipy.sparse import csgraph, csr_matrix
from sklearn.neighbors import kneighbors_graph
from sklearn.cluster import KMeans
from sklearn.metrics import normalized_mutual_info_score
from disjoint_set import DisjointSet
NMI_AVERAGE = 'geometric'
class DistanceSimilarity(Enum):
"""
Enum for different identification of different similarity/distance measures that can be used for the adjacency matrix
"""
MANHATTAN = 1
EUCLIDEAN = 2
GAUSSIAN = 3
COSINE = 4
def delete_random_edge(A, row_sums, n):
"""Deletes a random edge in the adjacency matrix A
creates a random value that corresponds to one edge in A and deletes it
Parameters
----------
A: numpy.array
the adjacency matrix
row_sums: numpy.array
matrix of the row sums for a quicker identification which edge to delete
n: int
size of the dataset
Returns
-------
target_row: int
the row in A where the edge was deleted
target_col: int
the column in A where the edge was deleted
value: float
the weight of the edge
"""
total_dist = row_sums[len(row_sums) - 1, 1]
random_number = np.random.random() * total_dist
target_row = np.argmax(row_sums[:, 1] > random_number)
dist = row_sums[target_row, 1]
target_col = 0
for i in range(n - 1, -1, -1):
if (dist < random_number):
target_col = (i + 1)
break
dist -= A[target_row, i]
value = A[target_row, target_col]
A[target_row, target_col] = 0
return target_row, target_col, value
def create_knn_adjacency(data, k, verbose=False):
# NOTE! The Knn Adjacency Matrix is not symmetric
if verbose:
print("Creating knn Adjacency Matrix with k =", k)
A = kneighbors_graph(data, n_neighbors=k, include_self=False, mode='distance')
return A.toarray()
def create_specific_adjacency_matrix(data, distance_similarity=DistanceSimilarity.EUCLIDEAN, sigma=1, verbose=False):
"""
A = Adjacency Matrix with euclidean distance, 0 on Diagonal Entries to not perturb distance sums
"""
if verbose:
print("Creating Adjacency with:", DistanceSimilarity(distance_similarity))
n, dim = data.shape
A = np.zeros(shape=(n, n), dtype=np.float32)
for i in range(0, n):
for j in range(i, n):
if distance_similarity == DistanceSimilarity.MANHATTAN:
dist = distance.cityblock(data[i, :], data[j, :])
elif distance_similarity == DistanceSimilarity.GAUSSIAN:
dist = np.exp(distance.euclidean(data[i, :], data[j, :]) / (2 * sigma ** 2)) - sigma
elif distance_similarity == DistanceSimilarity.COSINE:
dist = distance.cosine(data[i, :], data[j, :]) / 2
else:
dist = distance.euclidean(data[i, :], data[j, :])
A[i, j] = dist
return A
def create_row_sums(A, n):
"""Create a matrix with row sums for edge deletion
Creates a (2,n) matrix that holds the edge weight sums of the adjacency matrix per row, this matrix is used later
to identify which random edge is deleted
Parameters
----------
A: numpy.array
the adjacency matrix, has to be the upper triangular matrix
n:
the size of the dataset
Returns
-------
: numpy.array
A numpy array holding cumulated and single sums of the adjacency matrix
"""
rowSum = np.zeros(shape=(n, 2), dtype=np.float64)
rowSum[:, 0] = np.sum(A, axis=1)
cumm = 0
for i in range(0, n):
cumm += rowSum[i, 0]
rowSum[i, 1] = cumm
return rowSum
def load_csv_dataset(data_path, name, verbose=False):
"""helper function to make dataloading more convenient
Parameters
----------
data_path: str
path to the data folder
name: str
name of the dataset to load from the data_path
Returns
-------
: tuple
tuple consisting of the dataset and the labels
"""
abs_path = os.path.join(data_path, name)
if verbose:
print(abs_path)
import_data = pd.read_csv(abs_path, header=None)
rows, cols = import_data.shape
labels = import_data.iloc[:, cols - 1]
data = import_data.iloc[:, 0:cols - 1]
if data.dtypes[0] == 'object':
print(' >> string', name, data.dtypes[0])
data = data.drop(data.columns[0], axis=1)
data_loaded = (np.array(data), np.array(labels))
return data_loaded
def random_link(dataset,
noise=3,
knn=0,
distance_similarity=DistanceSimilarity.EUCLIDEAN,
verbose=False,
stopping_k=0):
n = dataset.shape[0]
max_stopping_score = 0
deleted_edges_stack = []
# Either create initial kNN Graph or a fully connected Graph
if knn and knn > 0:
distance_matrix = create_knn_adjacency(data=dataset, k=knn)
start_components, _ = csgraph.connected_components(
csr_matrix(distance_matrix + distance_matrix.transpose()),
directed=False,
return_labels=True)
else:
distance_matrix = create_specific_adjacency_matrix(data=dataset,
distance_similarity=distance_similarity)
start_components = 1
a_mean = distance_matrix.mean()
row_sums = create_row_sums(distance_matrix, n)
if verbose:
print(start_components, 'Connected Components at the beginning')
##################################################################
# Delete Edges to define order
cur_edges = 0
while row_sums[n - 1, 1] > a_mean * 20: # Delete Edges
d_row, d_col, d_val = delete_random_edge(distance_matrix, row_sums, n)
cur_edges += 1
row_sums[d_row][0] -= d_val
deleted_edges_stack.append((d_row, d_col))
cumm = 0
if d_row > 0:
cumm = row_sums[d_row - 1, 1]
for i in range(d_row, n):
cumm += row_sums[i, 0]
row_sums[i, 1] = cumm
# Now we have deleted most of the edges and we want to add them in reverse order!
n_components, n_labels = csgraph.connected_components(
csr_matrix(distance_matrix + distance_matrix.transpose()),
directed=False,
return_labels=True)
n_components = n
ds = DisjointSet(n)
ds.init_with_cluster(n=n, labels=list(n_labels))
last_n_components = n_components
comparison_clustering_results = dict()
edge_index = len(deleted_edges_stack) - 1
edges_added = 0
while n_components > start_components: # Add edges until connected components = 1
if verbose:
print(n_components, 'components')
ds.add_edge(edge=deleted_edges_stack[edge_index])
edge_index -= 1
edges_added += 1
if verbose:
print(len(deleted_edges_stack), 'STACK SIZE')
n_components = ds.get_components()
if n_components != last_n_components:
last_n_components = n_components
n_labels = ds.get_labels()
################################################################
# NOISE, Count labels that occur more than once to estimate how many non noise points we have
if noise > 0:
_, counts = np.unique(n_labels, return_counts=True)
n_components_no_noise = len(counts[counts > noise])
if n_components_no_noise < 1:
continue
else:
n_components_no_noise = n_components
##################################################################
# STOPPING AT K COMPONENTS
if stopping_k:
if n_components_no_noise <= stopping_k:
best_labels_with_vote = n_labels
break
##################################################################
# STOPPING CRITERION
else:
if n_components_no_noise not in comparison_clustering_results.keys():
pred = KMeans(n_clusters=n_components_no_noise, n_init=3, max_iter=30).fit(dataset)
comparison_clustering_results[n_components_no_noise] = pred
else:
pred = comparison_clustering_results[n_components_no_noise]
if n_components_no_noise > 1:
if verbose:
print(n_components_no_noise, 'No Noise')
stopping_score = normalized_mutual_info_score(n_labels, pred.labels_, average_method=NMI_AVERAGE)
if stopping_score >= max_stopping_score:
max_stopping_score = stopping_score
best_labels_with_vote = n_labels
return best_labels_with_vote
if __name__ == '__main__':
data, labels = load_csv_dataset('data', 'arrhythmia.txt')
r_labels = random_link(data, distance_similarity=DistanceSimilarity.EUCLIDEAN, verbose=False)
nmi = normalized_mutual_info_score(r_labels, labels, average_method=NMI_AVERAGE)
print("NMI: {:.4f}".format(nmi))