OneVsRestClassifier<T>

Models & Types · AiDotNet.Classification.Meta

One-vs-Rest (also called One-vs-All) classifier for multi-class and multi-label classification.

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

Implements: IGeneratedParameterRegistrar<T>

For Beginners

One-vs-Rest is a simple strategy for multi-class and multi-label classification:

For 3 classes (A, B, C):

  • Classifier 1: Is it A vs not-A?
  • Classifier 2: Is it B vs not-B?
  • Classifier 3: Is it C vs not-C?

For prediction, the class whose classifier gives the highest score wins.

Advantages:

  • Simple and effective
  • Trains K classifiers for K classes
  • Easily parallelizable

Disadvantages:

  • Class imbalance (one class vs all others)
  • Classifiers don’t see inter-class relationships

How It Works

Trains one binary classifier per class, treating it as the positive class and all other classes as the negative class. This approach works for both multi-class (exactly one label per sample) and multi-label (zero or more labels per sample) problems.

Multi-label usage: This classifier also supports multi-label classification via TrainMultiLabel, along with the NumLabels and LabelNames properties. When using this class in a multi-label setting, prefer TrainMultiLabel over Train.

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 Vector<double>(new double[] { 0.0, 1.0, 0.0, 1.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 onevsrestclassifier classification
var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new OneVsRestClassifier<double>(() => new GaussianNaiveBayes<double>()));
            
var result = builder.Build(features, labels);
var prediction = result.Predict(newSample);

Constructors

Constructor Summary
OneVsRestClassifier() Initializes a new instance with default settings using Gaussian Naive Bayes as the base estimator.
OneVsRestClassifier(Func<IClassifier<T>> estimatorFactory, MetaClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the OneVsRestClassifier 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)
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) Trains the One-vs-Rest classifier on the provided data.
void TrainMultiLabel(Matrix<T> x, Matrix<T> yMultiLabel) Returns the model type identifier for this classifier.