@@ -39,6 +39,7 @@ <h1 class="title">Module <code>spectralcluster.spectral_clusterer</code></h1>
39
39
RefinementName = refinement.RefinementName
40
40
LaplacianType = laplacian.LaplacianType
41
41
ConstraintName = constraint.ConstraintName
42
+ EigenGapType = utils.EigenGapType
42
43
43
44
44
45
class SpectralClusterer:
@@ -54,7 +55,8 @@ <h1 class="title">Module <code>spectralcluster.spectral_clusterer</code></h1>
54
55
row_wise_renorm=False,
55
56
custom_dist="cosine",
56
57
max_iter=300,
57
- constraint_options=None):
58
+ constraint_options=None,
59
+ eigengap_type=EigenGapType.Ratio):
58
60
"""Constructor of the clusterer.
59
61
60
62
Args:
@@ -77,6 +79,7 @@ <h1 class="title">Module <code>spectralcluster.spectral_clusterer</code></h1>
77
79
max_iter: the maximum number of iterations for the custom k-means
78
80
constraint_options: a ConstraintOptions object that contains constraint
79
81
arguments
82
+ eigengap_type: the type of the eigengap computation
80
83
"""
81
84
self.min_clusters = min_clusters
82
85
self.max_clusters = max_clusters
@@ -91,6 +94,7 @@ <h1 class="title">Module <code>spectralcluster.spectral_clusterer</code></h1>
91
94
self.custom_dist = custom_dist
92
95
self.max_iter = max_iter
93
96
self.constraint_options = constraint_options
97
+ self.eigengap_type = eigengap_type
94
98
95
99
def _compute_eigenvectors_ncluster(self, affinity, constraint_matrix=None):
96
100
"""Perform eigen decomposition and estiamte the number of clusters.
@@ -133,7 +137,11 @@ <h1 class="title">Module <code>spectralcluster.spectral_clusterer</code></h1>
133
137
(eigenvalues, eigenvectors) = utils.compute_sorted_eigenvectors(affinity)
134
138
# Get number of clusters.
135
139
n_clusters, max_delta_norm = utils.compute_number_of_clusters(
136
- eigenvalues, self.max_clusters, self.stop_eigenvalue, descend=True)
140
+ eigenvalues,
141
+ self.max_clusters,
142
+ self.stop_eigenvalue,
143
+ self.eigengap_type,
144
+ descend=True)
137
145
else:
138
146
# Compute Laplacian matrix
139
147
laplacian_norm = laplacian.compute_laplacian(
@@ -144,7 +152,7 @@ <h1 class="title">Module <code>spectralcluster.spectral_clusterer</code></h1>
144
152
laplacian_norm, descend=False)
145
153
# Get number of clusters. Eigen values are sorted in an ascending order
146
154
n_clusters, max_delta_norm = utils.compute_number_of_clusters(
147
- eigenvalues, self.max_clusters, descend=False)
155
+ eigenvalues, self.max_clusters, self.eigengap_type, descend=False)
148
156
return eigenvectors, n_clusters, max_delta_norm
149
157
150
158
def predict(self, embeddings, constraint_matrix=None):
@@ -187,7 +195,7 @@ <h1 class="title">Module <code>spectralcluster.spectral_clusterer</code></h1>
187
195
(eigenvectors, n_clusters,
188
196
max_delta_norm) = self._compute_eigenvectors_ncluster(
189
197
affinity, constraint_matrix)
190
- ratio = (1 - p_percentile) / max_delta_norm
198
+ ratio = np.sqrt (1 - p_percentile) / max_delta_norm
191
199
return ratio, eigenvectors, n_clusters
192
200
193
201
eigenvectors, n_clusters, _ = self.autotune.tune(p_percentile_to_ratio)
@@ -228,7 +236,7 @@ <h2 class="section-title" id="header-classes">Classes</h2>
228
236
< dl >
229
237
< dt id ="spectralcluster.spectral_clusterer.SpectralClusterer "> < code class ="flex name class ">
230
238
< span > class < span class ="ident "> SpectralClusterer</ span > </ span >
231
- < span > (</ span > < span > min_clusters=None, max_clusters=None, refinement_options=None, autotune=None, laplacian_type=None, stop_eigenvalue=0.01, row_wise_renorm=False, custom_dist='cosine', max_iter=300, constraint_options=None)</ span >
239
+ < span > (</ span > < span > min_clusters=None, max_clusters=None, refinement_options=None, autotune=None, laplacian_type=None, stop_eigenvalue=0.01, row_wise_renorm=False, custom_dist='cosine', max_iter=300, constraint_options=None, eigengap_type=EigenGapType.Ratio )</ span >
232
240
</ code > </ dt >
233
241
< dd >
234
242
< div class ="desc "> < p > Spectral clustering class.</ p >
@@ -264,6 +272,8 @@ <h2 id="args">Args</h2>
264
272
< dt > < strong > < code > constraint_options</ code > </ strong > </ dt >
265
273
< dd > a ConstraintOptions object that contains constraint
266
274
arguments</ dd >
275
+ < dt > < strong > < code > eigengap_type</ code > </ strong > </ dt >
276
+ < dd > the type of the eigengap computation</ dd >
267
277
</ dl > </ div >
268
278
< details class ="source ">
269
279
< summary >
@@ -282,7 +292,8 @@ <h2 id="args">Args</h2>
282
292
row_wise_renorm=False,
283
293
custom_dist="cosine",
284
294
max_iter=300,
285
- constraint_options=None):
295
+ constraint_options=None,
296
+ eigengap_type=EigenGapType.Ratio):
286
297
"""Constructor of the clusterer.
287
298
288
299
Args:
@@ -305,6 +316,7 @@ <h2 id="args">Args</h2>
305
316
max_iter: the maximum number of iterations for the custom k-means
306
317
constraint_options: a ConstraintOptions object that contains constraint
307
318
arguments
319
+ eigengap_type: the type of the eigengap computation
308
320
"""
309
321
self.min_clusters = min_clusters
310
322
self.max_clusters = max_clusters
@@ -319,6 +331,7 @@ <h2 id="args">Args</h2>
319
331
self.custom_dist = custom_dist
320
332
self.max_iter = max_iter
321
333
self.constraint_options = constraint_options
334
+ self.eigengap_type = eigengap_type
322
335
323
336
def _compute_eigenvectors_ncluster(self, affinity, constraint_matrix=None):
324
337
"""Perform eigen decomposition and estiamte the number of clusters.
@@ -361,7 +374,11 @@ <h2 id="args">Args</h2>
361
374
(eigenvalues, eigenvectors) = utils.compute_sorted_eigenvectors(affinity)
362
375
# Get number of clusters.
363
376
n_clusters, max_delta_norm = utils.compute_number_of_clusters(
364
- eigenvalues, self.max_clusters, self.stop_eigenvalue, descend=True)
377
+ eigenvalues,
378
+ self.max_clusters,
379
+ self.stop_eigenvalue,
380
+ self.eigengap_type,
381
+ descend=True)
365
382
else:
366
383
# Compute Laplacian matrix
367
384
laplacian_norm = laplacian.compute_laplacian(
@@ -372,7 +389,7 @@ <h2 id="args">Args</h2>
372
389
laplacian_norm, descend=False)
373
390
# Get number of clusters. Eigen values are sorted in an ascending order
374
391
n_clusters, max_delta_norm = utils.compute_number_of_clusters(
375
- eigenvalues, self.max_clusters, descend=False)
392
+ eigenvalues, self.max_clusters, self.eigengap_type, descend=False)
376
393
return eigenvectors, n_clusters, max_delta_norm
377
394
378
395
def predict(self, embeddings, constraint_matrix=None):
@@ -415,7 +432,7 @@ <h2 id="args">Args</h2>
415
432
(eigenvectors, n_clusters,
416
433
max_delta_norm) = self._compute_eigenvectors_ncluster(
417
434
affinity, constraint_matrix)
418
- ratio = (1 - p_percentile) / max_delta_norm
435
+ ratio = np.sqrt (1 - p_percentile) / max_delta_norm
419
436
return ratio, eigenvectors, n_clusters
420
437
421
438
eigenvectors, n_clusters, _ = self.autotune.tune(p_percentile_to_ratio)
@@ -516,7 +533,7 @@ <h2 id="raises">Raises</h2>
516
533
(eigenvectors, n_clusters,
517
534
max_delta_norm) = self._compute_eigenvectors_ncluster(
518
535
affinity, constraint_matrix)
519
- ratio = (1 - p_percentile) / max_delta_norm
536
+ ratio = np.sqrt (1 - p_percentile) / max_delta_norm
520
537
return ratio, eigenvectors, n_clusters
521
538
522
539
eigenvectors, n_clusters, _ = self.autotune.tune(p_percentile_to_ratio)
0 commit comments