milia.cluster.models.spectral#
Spectral clustering on a k-nearest-neighbour graph, with an eigengap estimate of k.
Functions
|
Number of clusters suggested by the largest gap in the Laplacian spectrum. |
Classes
|
Normalised spectral clustering (Ng, Jordan and Weiss, 2001). |
|
Validated hyperparameters of the spectral clusterer. |
- milia.cluster.models.spectral.eigengap(affinity, candidates=15)#
Number of clusters suggested by the largest gap in the Laplacian spectrum.
Uses the unnormalised graph Laplacian
D - Wof an affinity matrixW; the k with the largest jump between consecutive smallest eigenvalues is the eigengap heuristic’s read of the number of clusters (von Luxburg, 2007).- Parameters:
- Return type:
- Returns:
The suggested number of clusters, at least 1.
Examples
>>> import numpy as np >>> blocks = np.kron(np.eye(3), np.ones((4, 4))) >>> eigengap(blocks) 3
- class milia.cluster.models.spectral.Spectral(n_clusters=3, n_neighbors=10, candidates=15, random_state=0)#
Bases:
BaseClustererNormalised spectral clustering (Ng, Jordan and Weiss, 2001).
The neighbour count is capped below the sample count so small populations do not fail. After fitting,
eigengap_holds the eigengap heuristic’s independent estimate of the number of clusters.- Parameters:
Examples
>>> import numpy as np >>> rng = np.random.default_rng(0) >>> X = np.repeat(np.array([[0.0, 0.0], [6.0, 6.0], [0.0, 6.0]]), 20, axis=0) + rng.normal( ... scale=0.4, size=(60, 2) ... ) >>> Spectral(n_clusters=3).fit(X).n_clusters_ 3
- class milia.cluster.models.spectral.SpectralConfig(*, name='spectral', kind='clusterer', space=SearchSpace(n_clusters=(2, 12), n_neighbors=(5, 30)), n_clusters=3, n_neighbors=10, candidates=15, random_state=0)#
Bases:
BaseModelConfigValidated hyperparameters of the spectral clusterer.
Fields mirror
Spectral’s constructor;spaceis the default Optuna search space. Build the clusterer withmake("clusterer", "spectral", ...)orSpectral(**config.params()).Examples
>>> SpectralConfig().name 'spectral'