GMeans<T>

Models & Types · AiDotNet.Clustering.AutoK

G-Means clustering with automatic K determination using Gaussian tests.

Inheritance: ClusteringBase<T> → GMeans<T>

For Beginners

G-Means assumes good clusters are Gaussian.

A Gaussian distribution is the famous “bell curve”:

  • Most points near the center
  • Fewer points as you go farther out
  • Symmetric left and right

If a cluster doesn’t look like a bell curve:

  • It might contain multiple groups
  • Split it into two and check again

This works well when true clusters are roughly Gaussian.

How It Works

G-Means tests whether data in each cluster comes from a Gaussian distribution. If not, the cluster is split. It uses the Anderson-Darling test projected onto the principal direction connecting cluster centers.

Algorithm:

  1. Start with K=1 (all data in one cluster)
  2. For each cluster, project data onto the main axis
  3. Test if projection is Gaussian (Anderson-Darling)
  4. If not Gaussian, split the cluster
  5. Repeat until all clusters are Gaussian or max K reached

Example

using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.AutoK;
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 GMeans<double>())
    .ConfigureDataLoader(DataLoaders.FromMatrix(data))
    .BuildAsync();

var labels = result.Predict(data);
Console.WriteLine($"GMeans: clustered {labels.Length} points.");

Constructors

Constructor Summary
GMeans(GMeansOptions<T> options) Initializes a new GMeans instance.

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)