AffinityPropagation<T>

Models & Types · AiDotNet.Clustering.Partitioning

Affinity Propagation clustering implementation.

Inheritance: ClusteringBase<T> → AffinityPropagation<T>

For Beginners

Affinity Propagation works like a democratic election.

Each point sends two types of messages:

  1. “I think you should be my representative” (responsibility)
  2. “I’m available to be your representative” (availability)

The algorithm iterates until consensus:

  • Some points emerge as exemplars (elected representatives)
  • Other points choose their nearest exemplar
  • Each exemplar defines a cluster

Benefits:

  • Automatically finds the number of clusters
  • Exemplars are actual data points
  • Works well with any similarity measure

How It Works

Affinity Propagation identifies exemplars (cluster centers) by passing messages between data points. It doesn’t require specifying the number of clusters.

The algorithm maintains two types of messages:

  • Responsibility r(i,k): How well-suited point k is to serve as exemplar for i
  • Availability a(i,k): How appropriate it would be for i to choose k as its exemplar

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 AffinityPropagation<double>())
    .ConfigureDataLoader(DataLoaders.FromMatrix(data))
    .BuildAsync();

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

Constructors

Constructor Summary
AffinityPropagation(AffinityPropagationOptions<T> options) Initializes a new AffinityPropagation instance.

Properties

Property Summary
Matrix<T> SimilarityMatrix { get; } Gets the similarity matrix.
int[] ExemplarIndices { get; } Gets the indices of exemplar points.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
Matrix<T> GetExemplars(Matrix<T> originalData) Gets the exemplar points as a matrix.
ModelOptions GetOptions()
Vector<T> FitPredict(Matrix<T> x)
Vector<T> Predict(Matrix<T> x)
void Train(Matrix<T> x, Vector<T> y)