CLARANS<T>
Models & Types · AiDotNet.Clustering.Partitioning
CLARANS (Clustering Large Applications based on Randomized Search) implementation.
Inheritance: ClusteringBase<T> → CLARANS<T>
For Beginners
CLARANS is like K-Means but uses actual data points.
Key differences from K-Means:
- Cluster centers (medoids) are real data points
- More robust: outliers don’t pull centers as much
- Any distance metric works (not just Euclidean)
The “randomized” part:
- Instead of checking ALL possible swaps (slow!)
- Randomly sample potential swaps
- Still finds good solutions, just faster
Think of finding the best meeting spot for friends:
- Must be at someone’s house (medoid = actual point)
- Try swapping whose house, keep improvements
How It Works
CLARANS is a partitioning algorithm that uses medoids (actual data points) as cluster centers. It improves on PAM by using randomized search to explore the solution space more efficiently.
The algorithm:
- Start with random medoids
- For each iteration, consider swapping a medoid with a non-medoid
- Accept swaps that reduce total cost
- Repeat from different starting points, keep best result
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.Partitioning;
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 CLARANS<double>())
.ConfigureDataLoader(DataLoaders.FromMatrix(data))
.BuildAsync();
var labels = result.Predict(data);
Console.WriteLine($"CLARANS: clustered {labels.Length} points.");
Constructors
| Constructor | Summary |
|---|---|
CLARANS(CLARANSOptions<T> options) |
Initializes a new CLARANS instance. |
Properties
| Property | Summary |
|---|---|
T BestCost { get; } |
Gets the best cost found. |
int[] MedoidIndices { get; } |
Gets the indices of medoid points. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
|
Matrix<T> GetMedoids(Matrix<T> originalData) |
Gets the medoid points as a matrix. |
ModelOptions GetOptions() |
|
Vector<T> FitPredict(Matrix<T> x) |
|
Vector<T> Predict(Matrix<T> x) |
|
void Train(Matrix<T> x) |
Trains the CLARANS clustering model on the given data. |
void Train(Matrix<T> x, Vector<T> y) |