ClassifierChain<T>

Models & Types · AiDotNet.Classification.Meta

Classifier Chain for multi-label classification.

Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → MetaClassifierBase<T> → ClassifierChain<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Classifier Chain captures label dependencies:

For labels A, B, C:

  • Classifier 1: Predict A using features X
  • Classifier 2: Predict B using features X + prediction of A
  • Classifier 3: Predict C using features X + predictions of A and B

Benefits:

  • Captures dependencies between labels
  • Better than independent binary classifiers

Trade-offs:

  • Order of chain matters (can use random order or learned order)
  • Error propagation (early mistakes affect later predictions)

How It Works

Classifier Chain transforms a multi-label problem into a chain of binary classification problems, where each classifier uses the predictions of previous classifiers as additional features.

Example

// Create classifier chain for multi-label classification with dependencies
var options = new ClassifierChainOptions<double>();
            
// Prepare training data: 6 samples with 2 features
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, 0, 0, 1, 1, 1 });
            
// Train chained classifiers where each uses previous outputs as features
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new ClassifierChain<double>())
    .Build(features, labels);
            
// Predict using chained label dependencies
var newSample = new Matrix<double>(1, 2);
newSample[0, 0] = 1.1; newSample[0, 1] = 1.0;
var prediction = result.Predict(newSample);
// Result is available in the returned value

Constructors

Constructor Summary
ClassifierChain() Initializes a new instance with default settings using Gaussian Naive Bayes as the base estimator.
ClassifierChain(Func<IClassifier<T>> estimatorFactory, ClassifierChainOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the ClassifierChain class.

Properties

Property Summary
int NumLabels { get; } Gets the number of labels (same as number of classes for multi-label).
string[] LabelNames { get; set; } Gets or sets the label names if available.

Methods

Method Summary
Matrix<T> PredictLogProbabilities(Matrix<T> input)
Matrix<T> PredictMultiLabel(Matrix<T> input) Predicts multi-label output for the given input.
Matrix<T> PredictMultiLabelProbabilities(Matrix<T> input)
Matrix<T> PredictProbabilities(Matrix<T> input)
Vector<T> Predict(Matrix<T> input)
void Train(Matrix<T> x, Vector<T> y) Standard training method - converts single labels to multi-label format.
void TrainMultiLabel(Matrix<T> x, Matrix<T> yMultiLabel) Returns the model type identifier for this classifier.