MeanShift<T>
Models & Types · AiDotNet.Clustering.Density
Mean Shift clustering implementation.
Inheritance: ClusteringBase<T> → MeanShift<T>
For Beginners
Mean Shift finds the centers of data “clumps”.
Think of it like:
- Drop a ball at each data point
- Each ball rolls uphill (toward denser areas)
- Balls that end up at the same peak belong to the same cluster
Benefits:
- Doesn’t need to know number of clusters
- Finds natural cluster shapes
- Robust to outliers
Limitations:
- Bandwidth selection is important
- Can be slow for large datasets
How It Works
Mean Shift is a mode-seeking algorithm that iteratively shifts points toward the weighted mean of points within a bandwidth radius. Points that converge to the same mode form a cluster.
Algorithm:
- Start with each point as a potential mode
- For each point, compute weighted mean of neighbors within bandwidth
- Shift point toward this mean
- Repeat until convergence
- Merge nearby modes to get final cluster centers
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.Density;
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 MeanShift<double>())
.ConfigureDataLoader(DataLoaders.FromMatrix(data))
.BuildAsync();
var labels = result.Predict(data);
Console.WriteLine($"MeanShift: clustered {labels.Length} points.");
Constructors
| Constructor | Summary |
|---|---|
MeanShift(MeanShiftOptions<T> options) |
Initializes a new MeanShift instance. |
Properties
| Property | Summary |
|---|---|
T Bandwidth { get; } |
Gets the bandwidth used for clustering. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
|
ModelOptions GetOptions() |
|
Vector<T> FitPredict(Matrix<T> x) |
|
Vector<T> Predict(Matrix<T> x) |
|
void Train(Matrix<T> x, Vector<T> y) |