ClassifierChainClassifier<T>

Models & Types · AiDotNet.Classification.MultiLabel

Implements the Classifier Chain approach for multi-label classification.

Inheritance: MultiLabelClassifierBase<T> → ClassifierChainClassifier<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Classifier Chain improves on Binary Relevance by capturing label correlations:

Consider predicting movie genres:

  1. First classifier asks: “Is this a sequel?” (uses only movie features)
  2. Second classifier asks: “Is this action?” (uses movie features + “is sequel” prediction)
  3. Third classifier asks: “Is this comedy?” (uses movie features + “is sequel” + “is action”)
  4. And so on…

By including previous predictions as features, each classifier can learn from label dependencies. For example, if a movie is classified as “sequel”, the next classifier knows this and can adjust its predictions accordingly (sequels are more likely to be action movies).

Pros:

  • Captures label dependencies
  • Still relatively simple to implement
  • Often outperforms Binary Relevance

Cons:

  • Chain order matters (different orders can give different results)
  • Errors can propagate through the chain
  • Order selection can be tricky

The chain order can be specified manually or determined randomly. For best results, consider training multiple chains with different orders and combining their predictions (Ensemble of Classifier Chains).

How It Works

Classifier Chain is an extension of Binary Relevance that models label dependencies by training classifiers in a chain, where each classifier uses the predictions of previous classifiers as additional features.

Example

// Create Classifier Chain that captures label dependencies
            
// Prepare features and multi-label targets
var features = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
var labels = new Matrix<double>(new double[,] { { 1, 0, 1 }, { 1, 1, 0 }, { 0, 1, 1 }, { 0, 0, 1 } });
            
// Train chained classifiers where each uses previous predictions as features
var result = new AiModelBuilder<double, Matrix<double>, Matrix<double>>()
    .ConfigureModel(new ClassifierChainClassifier<double>())
    .Build(features, labels);
            
// Predict label set with label correlation awareness
var newSample = new Matrix<double>(new double[,] { { 2.0, 3.0 } });
var prediction = result.Predict(newSample);

Constructors

Constructor Summary
ClassifierChainClassifier() Initializes a new instance with default settings using Gaussian Naive Bayes as the base classifier.
ClassifierChainClassifier(Func<IClassifier<T>> classifierFactory, int[] chainOrder, bool useRandomOrder, int? seed, ClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the ClassifierChainClassifier class with a classifier factory.

Properties

Property Summary
int[] ChainOrder { get; } Gets the chain order used for training and prediction.

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.
void ApplyGradients(Vector<T> gradients, T learningRate) Applies gradients to update model parameters.