BisectingKMeans<T>

Models & Types · AiDotNet.Clustering.Hierarchical

Bisecting K-Means clustering algorithm implementation.

Inheritance: ClusteringBase<T> → BisectingKMeans<T>

For Beginners

Bisecting K-Means is like chopping a log:

  • Start with one big piece (all your data)
  • Find the biggest piece and split it in half
  • Keep splitting the biggest pieces
  • Stop when you have k pieces

Why use this instead of regular K-Means?

  • More consistent results (less affected by random starting points)
  • Often finds better clusters for complex data
  • Naturally creates a hierarchy (like a family tree of clusters)
  • Each split is a small K-Means problem, so it’s efficient

How It Works

Bisecting K-Means is a divisive hierarchical clustering algorithm that combines the efficiency of K-Means with a top-down hierarchical approach. It starts with all data in one cluster and recursively bisects until k clusters are achieved.

Algorithm steps:

  1. Start with all points in a single cluster
  2. Select a cluster to split (usually the largest or highest inertia)
  3. Bisect the selected cluster using K-Means with k=2
  4. Replace the original cluster with the two new clusters
  5. Repeat steps 2-4 until k clusters are formed

Example

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

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

Constructors

Constructor Summary
BisectingKMeans(BisectingKMeansOptions<T> options) Initializes a new BisectingKMeans instance with the specified options.

Properties

Property Summary
IReadOnlyList<BisectionNode> Hierarchy { get; } Gets the bisection hierarchy if BuildHierarchy was enabled.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
ModelOptions GetOptions()
Vector<T> Predict(Matrix<T> x)
void Train(Matrix<T> x, Vector<T> y)