LabelPowerset<T>

Models & Types · AiDotNet.Classification.MultiLabel

Implements the Label Powerset approach for multi-label classification.

Inheritance: MultiLabelClassifierBase<T> → LabelPowerset<T>

For Beginners

Label Powerset takes a clever approach:

Instead of predicting labels independently, it treats each unique combination of labels as a single class. For example, if you have 3 labels (action, comedy, romance):

  • “none” becomes class 0
  • “action only” becomes class 1
  • “comedy only” becomes class 2
  • “romance only” becomes class 3
  • “action+comedy” becomes class 4
  • “action+romance” becomes class 5
  • “comedy+romance” becomes class 6
  • “action+comedy+romance” becomes class 7

Now we train ONE multi-class classifier that directly predicts which combination applies.

Pros:

  • Perfectly captures label correlations (impossible combinations never predicted)
  • Only one classifier to train
  • Naturally handles label interdependencies

Cons:

  • Number of possible classes = 2^n (exponential in number of labels)
  • Many classes may have very few examples (data sparsity)
  • New label combinations unseen in training cannot be predicted

Works best when:

  • Number of labels is small (≤10)
  • Label combinations in test data were seen in training
  • Label correlations are important

How It Works

Label Powerset transforms the multi-label problem into a single multi-class problem by treating each unique combination of labels as a separate class.

Example

// Create Label Powerset treating each label combination as a class
            
// Prepare features and multi-label targets
var features = new Matrix<double>(4, 2);
features[0, 0] = 1.0; features[0, 1] = 2.0;
features[1, 0] = 3.0; features[1, 1] = 4.0;
features[2, 0] = 5.0; features[2, 1] = 6.0;
features[3, 0] = 7.0; features[3, 1] = 8.0;
var labels = new Matrix<double>(4, 3);
labels[0, 0] = 1; labels[0, 1] = 0; labels[0, 2] = 1;
labels[1, 0] = 1; labels[1, 1] = 1; labels[1, 2] = 0;
labels[2, 0] = 0; labels[2, 1] = 1; labels[2, 2] = 1;
labels[3, 0] = 0; labels[3, 1] = 0; labels[3, 2] = 1;
            
// Train single multi-class classifier on label combination classes
var result = new AiModelBuilder<double, Matrix<double>, Matrix<double>>()
    .ConfigureModel(new LabelPowerset<double>())
    .Build(features, labels);
            
// Predict complete label combination for new sample
var newSample = new Matrix<double>(1, 2);
newSample[0, 0] = 2.0; newSample[0, 1] = 3.0;
var prediction = result.Predict(newSample);

Constructors

Constructor Summary
LabelPowerset() Initializes a new instance with default settings using Gaussian Naive Bayes as the base classifier.
LabelPowerset(Func<IClassifier<T>> classifierFactory, ClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the LabelPowerset class with a classifier factory.

Properties

Property Summary
int NumLabelCombinations { get; } Gets the number of unique label combinations found in training data.

Methods

Method Summary
IFullModel<T, Matrix<T>, Matrix<T>> WithParameters(Vector<T> parameters) Creates a new instance of the model with the specified parameters.
Matrix<T> PredictMultiLabelProbabilities(Matrix<T> input) Predicts probabilities for each label for each sample.
Vector<T> ComputeGradients(Matrix<T> input, Matrix<T> target, ILossFunction<T> lossFunction) Computes gradients for gradient-based optimization.
bool[] GetLabelsForClass(int classIndex) Gets the label combination for a given class index.
void ApplyGradients(Vector<T> gradients, T learningRate) Applies gradients to update model parameters.