-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclusterRoutines.py
432 lines (353 loc) · 14.2 KB
/
clusterRoutines.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 2 19:20:01 2022
@author: seolubuntu
"""
import numpy as np
from sklearn.cluster import KMeans
from sklearn import metrics
import matplotlib.pyplot as plt
# %%
class ClusterEngine:
def __init__(
self, guesses, minClusterSize=None, minClusterFraction=None, scoretypes=["sil"]
):
self.guesses = guesses
self.minClusterSize = minClusterSize
self.minClusterFraction = minClusterFraction
self.scoretypes = scoretypes
self.scoretitles = {
"sil": "Silhouette",
"ch": "Calinski Harabasz",
"db": "Davies Bouldin",
}
def _cluster(self, x):
self.scores = {key: np.zeros(len(self.guesses)) for key in self.scoretypes}
# Compute all requested scores
for i in range(len(self.guesses)):
model = KMeans(n_clusters=self.guesses[i]).fit(x)
if "sil" in self.scoretypes:
self.scores["sil"][i] = metrics.silhouette_score(
x, model.labels_, metric="euclidean"
)
if "ch" in self.scoretypes:
self.scores["ch"][i] = metrics.calinski_harabasz_score(x, model.labels_)
if "db" in self.scoretypes:
self.scores["db"][i] = metrics.davies_bouldin_score(x, model.labels_)
# Only use the first scoretype to decide
if self.scoretypes[0] == "sil":
sel = np.argmax(self.scores[self.scoretypes[0]]) # This one is max
elif self.scoretypes[0] == "db":
sel = np.argmin(self.scores[self.scoretypes[0]]) # but this is min
elif self.scoretypes[0] == "ch":
raise NotImplementedError(
"Calinski Harabasz Score maximisation not available."
) # will have to look for the kink, but seems unreliable
return self.guesses[sel]
def cluster(self, x: np.ndarray, ensure_sorted: bool = True, verbose: bool = False):
"""
Parameters
----------
x : np.ndarray
Input array to be clustered.
ensure_sorted : bool, optional
Option to sort the cluster centers in ascending order. The default is True.
verbose : bool, optional
Verbose debug prints. The default is False.
Returns
-------
bestguess : int
Best fit number of clusters based on configured guesses (see init).
bestmodel : KMeans
The fitted model. Common use cases are to see the labels via bestmodel.labels_.
See scikit-learn's documentation for more info.
idxRemoved: np.ndarray
The indices removed as outliers based on the configured options (see init).
idxUsed : np.ndarray
The remaining indices that are used in the 'bestmodel'.
"""
if x.ndim == 1:
x = x.reshape((-1, 1)) # does not overwrite external argument array
# We loop until all conditions are met
idxUsed = np.arange(len(x)) # Initialize using the entire array
idxRemoved = []
while True:
# Cluster first
bestguess = self._cluster(x[idxUsed])
# Make the model with best guess of no. of clusters
bestmodel = KMeans(n_clusters=bestguess).fit(x[idxUsed])
# Count the cluster sizes
uniqueLabels = np.unique(bestmodel.labels_)
numPerCluster = np.array(
[np.argwhere(bestmodel.labels_ == label).size for label in uniqueLabels]
)
if verbose:
print("\nFound %d clusters with members:" % uniqueLabels.size)
print(numPerCluster)
# Check for conditionals
if self.minClusterSize is not None and np.any(
numPerCluster < self.minClusterSize
):
# We remove the smallest cluster first
clusterToRemove = np.argmin(numPerCluster)
idxToRemove = np.argwhere(
bestmodel.labels_ == clusterToRemove
).flatten()
if verbose:
print("Removed indices")
print(idxUsed[idxToRemove])
print("with corresponding values")
print(x[idxUsed[idxToRemove]])
elif (
self.minClusterFraction is not None
and np.min(numPerCluster) / np.max(numPerCluster)
< self.minClusterFraction
):
# We again remove the smallest cluster
clusterToRemove = np.argmin(numPerCluster)
idxToRemove = np.argwhere(
bestmodel.labels_ == clusterToRemove
).flatten()
if verbose:
print("Removed indices")
print(idxUsed[idxToRemove])
print("with corresponding values")
print(x[idxUsed[idxToRemove]])
else:
break
# Remove those slated for removal
# Append to removal list, using global indices
idxRemoved.extend(idxUsed[idxToRemove])
# Then delete it
idxUsed = np.delete(idxUsed, idxToRemove)
# Sort if desired
if ensure_sorted:
sortedIdx = np.argsort(bestmodel.cluster_centers_.flatten())
bestmodel.labels_ = sortedIdx[bestmodel.labels_]
bestmodel.cluster_centers_ = bestmodel.cluster_centers_[sortedIdx]
return bestguess, bestmodel, np.array(idxRemoved), idxUsed
@staticmethod
def getLargestCluster(model):
clusterSizes = np.array(
[np.argwhere(model.labels_ == i).size for i in range(model.n_clusters)]
)
maxci = np.argmax(clusterSizes)
maxCenter = model.cluster_centers_[maxci]
maxClusterSize = clusterSizes[maxci]
return maxCenter, maxClusterSize
@staticmethod
def plotClusters(
x,
bestmodel,
idxRemoved,
idxUsed,
t=None,
colours=["r", "b"],
ax=None,
title="Clusters",
):
if t is None:
t = np.arange(x.size) # just numbers them
if ax is None: # Make a new plot with the title
fig, ax = plt.subplots(1, 1, num=title)
else: # Else just plot on existing axes object
fig = None
# Plot all the points
ax.plot(t, x, "x")
# Plot the outliers removed
if idxRemoved.size > 0:
ax.plot(
t[idxRemoved],
x[idxRemoved],
"ko",
label="Outliers (%d)" % idxRemoved.size,
)
# Plot the clusters via labels
for i in range(bestmodel.n_clusters):
si = idxUsed[np.argwhere(bestmodel.labels_ == i)]
ax.plot(
t[si],
x[si],
colours[i % len(colours)] + "s",
markerfacecolor="none",
label="Cluster %d (%d)" % (i, si.size),
)
ax.legend()
return fig, ax
def plotScores(self, title: str = "Cluster Scores"):
"""
Parameters
----------
title : str, optional
Figure title. The default is 'Cluster Scores'.
Returns
-------
fig : pyplot figure
ax : pyplot axes
"""
if title is None:
title = "Cluster Scores"
fig, ax = plt.subplots(len(self.scoretypes), 1, num=title)
if len(self.scoretypes) == 1:
ax = [ax] # Hotfix for when only 1 score is used
for i in range(len(self.scoretypes)):
ax[i].plot(self.guesses, self.scores[self.scoretypes[i]])
ax[i].set_title(self.scoretitles[self.scoretypes[i]])
return fig, ax
#############
class ClusterEngine2D(ClusterEngine):
def __init__(
self, guesses, minClusterSize=None, minClusterFraction=None, scoretypes=["sil"]
):
super().__init__(guesses, minClusterSize, minClusterFraction, scoretypes)
@staticmethod
def convertComplexToInterleaved2D(x):
if x.dtype == np.complex64:
return x.view(np.float32).reshape((-1, 2))
elif x.dtype == np.complex128:
return x.view(np.float64).reshape((-1, 2))
else:
return x
def cluster(self, x: np.ndarray, verbose: bool = False):
# Check if input is complex, view as interleaved
x = self.convertComplexToInterleaved2D(x)
# Call the original cluster, but sorting is ignored for 2D
bestguess, bestmodel, idxRemoved, idxUsed = super().cluster(x, False, verbose)
return bestguess, bestmodel, idxRemoved, idxUsed
@staticmethod
def plotClusters(
x, bestmodel, idxRemoved, idxUsed, colours=["r", "b"], ax=None, title="Clusters"
):
# Check if input is complex, view as interleaved
x = ClusterEngine2D.convertComplexToInterleaved2D(x)
if ax is None: # Make a new plot with the title
fig, ax = plt.subplots(1, 1, num=title)
else: # Else just plot on existing axes object
fig = None
# Plot all the points
ax.plot(x[:, 0], x[:, 1], "x")
# Plot the outliers removed
if idxRemoved.size > 0:
ax.plot(
x[idxRemoved, 0],
x[idxRemoved, 1],
"ko",
label="Outliers (%d)" % idxRemoved.size,
)
# Plot the clusters via labels
for i in range(bestmodel.n_clusters):
si = idxUsed[np.argwhere(bestmodel.labels_ == i)]
ax.plot(
x[si, 0],
x[si, 1],
colours[i % len(colours)] + "s",
markerfacecolor="none",
label="Cluster %d (%d)" % (i, si.size),
)
ax.legend()
return fig, ax
#############
class AngularClusterEngine(ClusterEngine2D):
def __init__(
self,
guesses,
minClusterSize=None,
minClusterFraction=None,
scoretypes=["sil"],
minAngleSep=None,
):
super().__init__(guesses, minClusterSize, minClusterFraction, scoretypes)
self.minAngleSep = minAngleSep
def cluster(self, x: np.ndarray, verbose: bool = False):
# Same as 2D
bestguess, bestmodel, idxRemoved, idxUsed = super().cluster(x, verbose)
# Check extra conditionals
if self.minAngleSep is not None:
numCombined = 0
for i in range(bestmodel.n_clusters):
for j in range(i + 1, bestmodel.n_clusters):
# Check if the current cluster is within angular range of the later clusters
angle_ij = (
np.arccos(
np.dot(
bestmodel.cluster_centers_[i],
bestmodel.cluster_centers_[j],
)
)
/ np.linalg.norm(bestmodel.cluster_centers_[i])
/ np.linalg.norm(bestmodel.cluster_centers_[j])
)
if np.abs(angle_ij) < self.minAngleSep:
# We set all of cluster i to the later cluster j
bestmodel.labels_[bestmodel.labels_ == i] = j
# Decrement the number of clusters
numCombined += 1
if verbose:
print("Cluster %d merged into %d" % (i, j))
break
# We correct the number of clusters
bestmodel.n_clusters -= numCombined
# Also shift the labels back to 0
bestmodel.labels_ = bestmodel.labels_ - np.min(bestmodel.labels_)
# And correct the best guess
bestguess = bestmodel.n_clusters
if verbose and numCombined > 0:
print("Merges result finally in %d clusters instead" % bestguess)
return bestguess, bestmodel, idxRemoved, idxUsed
@staticmethod
def plotClusters(
x, bestmodel, idxRemoved, idxUsed, colours=["r", "b"], ax=None, title="Clusters"
):
fig, ax = ClusterEngine2D.plotClusters(
x, bestmodel, idxRemoved, idxUsed, colours, ax, title
)
# Set a simple circle and axis limits
theta = np.arange(0, 2 * np.pi, 0.01)
circle = np.vstack((np.cos(theta), np.sin(theta)))
ax.plot(circle[0], circle[1], "k--")
ax.axis([-1.1, 1.1, -1.1, 1.1])
ax.set_aspect("equal")
# Plot dashed lines to the circle for each cluster
for i, center in enumerate(bestmodel.cluster_centers_):
line = np.array([[0, 0], center])
ax.plot(line[:, 0], line[:, 1], colours[i % len(colours)] + "--")
phi = np.arctan2(center[1], center[0])
ax.annotate("%.3f" % phi, center / 2)
return fig, ax
# %%
if __name__ == "__main__":
print("Running test.")
# Generate one feature
f1 = np.random.randn(100)
# Generate a little less of another feature
f2 = np.random.randn(30) + 10
# Generate some outliers
o = np.array([-50, -90, -100])
# Attach together
m = np.hstack((f1, f2, o))
np.random.shuffle(m) # shuffles in place
# Test the cluster engine
cle = ClusterEngine(np.arange(2, 10), minClusterSize=5, scoretypes=["sil", "db"])
bestguess, bestmodel, idxRemoved, idxUsed = cle.cluster(m, verbose=True)
# View
plt.close("all")
cle.plotClusters(m, bestmodel, idxRemoved, idxUsed)
# Plot the scores
cle.plotScores()
## Angular Clustering
# Generate some angles
a0 = np.random.randn(100) * 0.01
a1 = np.random.randn(10) * 0.01 + np.pi / 2
# Attach
ma = np.hstack((a0, a1))
ma_cplx = np.exp(1j * ma)
# Test the cluster engine
acle = AngularClusterEngine(np.arange(2, 10), minAngleSep=np.pi / 24)
aguess, amodel, aRemoved, aUsed = acle.cluster(ma_cplx, verbose=True)
acle.plotClusters(ma_cplx, amodel, aRemoved, aUsed, title="Angle Clusters")
# Test the cluster engine when it's just one cluster
aguess, amodel, aRemoved, aUsed = acle.cluster(ma_cplx[: a0.size], verbose=True)
acle.plotClusters(
ma_cplx[: a0.size], amodel, aRemoved, aUsed, title="Angle Clusters (Only 1)"
)