SpectralClustering<T>
Models & Types · AiDotNet.Clustering.Spectral
Spectral Clustering implementation using graph Laplacian eigendecomposition.
Inheritance: ClusteringBase<T> → SpectralClustering<T>
For Beginners
Spectral clustering is like finding communities in a network.
The key insight: Similar points should be in the same cluster. Instead of looking at distances directly, we:
- Build a “friendship network” where similar points are connected
- Find the natural groups in this network
- Use these groups as clusters
This works better than K-Means when:
- Clusters aren’t round/spherical
- Clusters have complex shapes (moons, spirals)
- You care about connectivity, not just distance
How It Works
Spectral clustering works by:
- Building an affinity (similarity) matrix
- Computing the graph Laplacian
- Finding eigenvectors corresponding to smallest eigenvalues
- Clustering points in the reduced eigenspace
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.Spectral;
using AiDotNet.Tensors.LinearAlgebra;
var data = new Matrix<double>(6, 2);
double[][] rows = { new[] { 1.0, 1.0 }, new[] { 1.2, 0.9 }, new[] { 1.1, 1.1 },
new[] { 8.0, 8.0 }, new[] { 8.2, 7.9 }, new[] { 7.9, 8.1 } };
for (int i = 0; i < 6; i++) { data[i, 0] = rows[i][0]; data[i, 1] = rows[i][1]; }
var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new SpectralClustering<double>())
.ConfigureDataLoader(DataLoaders.FromMatrix(data))
.BuildAsync();
var labels = result.Predict(data);
Console.WriteLine($"SpectralClustering: clustered {labels.Length} points.");
Constructors
| Constructor | Summary |
|---|---|
SpectralClustering(SpectralOptions<T> options) |
Initializes a new SpectralClustering instance. |
Properties
| Property | Summary |
|---|---|
Matrix<T> AffinityMatrix { get; } |
Gets the affinity matrix. |
Matrix<T> Embedding { get; } |
Gets the spectral embedding. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
|
Matrix<T> GetSpectralEmbedding() |
Gets the spectral embedding of the data. |
ModelOptions GetOptions() |
|
Vector<T> FitPredict(Matrix<T> x) |
|
Vector<T> Predict(Matrix<T> x) |
|
void Train(Matrix<T> x, Vector<T> y) |