LabelSpreading<T>

Models & Types · AiDotNet.Classification.SemiSupervised

Implements the Label Spreading algorithm for semi-supervised classification.

Inheritance: ClassifierBase<T> → SemiSupervisedClassifierBase<T> → LabelSpreading<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Label Spreading improves on Label Propagation in two ways:

  1. Symmetric Normalization: Instead of just normalizing rows, it normalizes both

rows and columns. This makes the algorithm more stable when you have clusters of very different sizes. Think of it like making sure influence flows equally in both directions between connected points.

  1. Alpha Parameter: This controls how much the original labels are preserved.

With alpha = 0.2, each iteration keeps 20% of the original label and takes 80% from neighbors. This prevents the algorithm from “forgetting” the original labels while still allowing information to spread.

The result is often more robust predictions, especially on noisy data or when clusters have unequal sizes.

How It Works

Label Spreading is a variant of Label Propagation that uses symmetric normalization and a clamping factor (alpha) to balance between original labels and propagated information.

Example

// Create label spreading with symmetric normalization
            
// Prepare data: -1 indicates unlabeled samples
var features = new Matrix<double>(6, 2);
features[0, 0] = 1.0; features[0, 1] = 1.1;
features[1, 0] = 1.2; features[1, 1] = 0.9;
features[2, 0] = 0.8; features[2, 1] = 1.0;
features[3, 0] = 5.0; features[3, 1] = 5.1;
features[4, 0] = 5.2; features[4, 1] = 4.9;
features[5, 0] = 4.8; features[5, 1] = 5.0;
var labels = new Vector<double>(new double[] { 0, -1, -1, 1, -1, -1 });
            
// Spread labels with alpha clamping to preserve original labels
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new LabelSpreading<double>())
    .Build(features, labels);
            
// Predict labels for all samples
var prediction = result.Predict(features);
// Result is available in the returned value

Constructors

Constructor Summary
LabelSpreading() Initializes a new instance of the LabelSpreading class with default settings.
LabelSpreading(IKernelFunction<T> kernel, int maxIterations, T tolerance, T alpha, int? seed) Initializes a new instance of the LabelSpreading class with specified parameters.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) Creates a new instance of the model with the specified parameters.
Matrix<T> PredictProbabilities(Matrix<T> input) Predicts class probabilities for each sample.
Vector<T> Predict(Matrix<T> input) Predicts class labels for new samples.