ConsensusClustering<T>

Models & Types · AiDotNet.Clustering.Ensemble

Consensus (Ensemble) Clustering implementation.

Inheritance: ClusteringBase<T> → ConsensusClustering<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Consensus clustering combines multiple opinions.

Think of it like a jury:

  • Multiple clusterings give their “verdict”
  • We count how often they agree
  • The final answer is based on majority agreement

The co-association matrix captures:

  • “Points A and B were in the same cluster in 8 out of 10 runs”
  • This creates a similarity measure between all pairs
  • We then cluster based on this similarity

Benefits:

  • More stable than single clustering
  • Less sensitive to initialization
  • Can combine different algorithms

How It Works

Consensus clustering combines multiple base clusterings to produce a more robust final result. It builds a co-association matrix showing how often pairs of points are clustered together, then clusters this matrix.

Algorithm:

  1. Generate multiple base clusterings (e.g., K-Means with different k or seeds)
  2. Build co-association matrix: C[i,j] = frequency of i and j in same cluster
  3. Apply final clustering to the co-association matrix

Example

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

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

Constructors

Constructor Summary
ConsensusClustering(ConsensusClusteringOptions<T> options) Initializes a new ConsensusClustering instance.

Properties

Property Summary
Matrix<T> CoAssociationMatrix { get; } Gets the co-association matrix.

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)