BinaryRelevance<T>

Models & Types · AiDotNet.Classification.MultiLabel

Implements the Binary Relevance approach for multi-label classification.

Inheritance: MultiLabelClassifierBase<T> → BinaryRelevance<T>

For Beginners

Binary Relevance takes the “divide and conquer” approach:

Instead of trying to predict all labels at once, it trains a separate binary classifier for each possible label. For example, if you’re classifying movies into 5 genres:

  1. Train a classifier that asks: “Is this movie action?” (yes/no)
  2. Train a classifier that asks: “Is this movie comedy?” (yes/no)
  3. Train a classifier that asks: “Is this movie drama?” (yes/no)
  4. And so on for each genre…

To predict labels for a new movie, we run all 5 classifiers and combine their answers.

Pros:

  • Simple to understand and implement
  • Can use any binary classifier
  • Parallelizable (each label classifier can train independently)

Cons:

  • Ignores correlations between labels (e.g., “horror” and “thriller” often appear together)
  • May produce inconsistent predictions (e.g., predicting “sequel” without “action”)

For problems where labels are correlated, consider using Classifier Chains or Label Powerset instead.

How It Works

Binary Relevance is the simplest multi-label classification method. It transforms the multi-label problem into multiple independent binary classification problems, one for each label.

Example

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, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 }, { 0.0, 0.0 } });
var newSample = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
// Use AiModelBuilder facade for binaryrelevance classification
var builder = new AiModelBuilder<double, Matrix<double>, Matrix<double>>()
    .ConfigureModel(new BinaryRelevance<double>(() => new GaussianNaiveBayes<double>()));
            
var result = builder.Build(features, labels);
var prediction = result.Predict(newSample);

Constructors

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

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.