OneVsOneClassifier<T>
Models & Types · AiDotNet.Classification.Meta
One-vs-One classifier for multi-class classification.
Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → MetaClassifierBase<T> → OneVsOneClassifier<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
One-vs-One trains a classifier for every pair of classes:
For 3 classes (A, B, C):
- Classifier 1: A vs B
- Classifier 2: A vs C
- Classifier 3: B vs C
For K classes, this requires K*(K-1)/2 classifiers.
For prediction, each classifier votes for one class, and the class with the most votes wins.
Advantages:
- Each classifier is trained on balanced binary problems
- Works well with SVM and other pairwise classifiers
- Good for small to medium number of classes
Disadvantages:
- Requires many classifiers for large K (K*(K-1)/2)
- Slower training for many classes
How It Works
Trains one binary classifier for each pair of classes. Uses voting to determine the final prediction.
Example
// Create One-vs-One classifier with pairwise binary classifiers
var options = new MetaClassifierOptions<double>();
// Prepare multi-class training data (3 classes, 9 samples, 2 features)
var features = new Matrix<double>(9, 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] = 3.0; features[3, 1] = 3.1;
features[4, 0] = 3.2; features[4, 1] = 2.9;
features[5, 0] = 2.8; features[5, 1] = 3.0;
features[6, 0] = 5.0; features[6, 1] = 5.1;
features[7, 0] = 5.2; features[7, 1] = 4.9;
features[8, 0] = 4.8; features[8, 1] = 5.0;
var labels = new Vector<double>(new double[] { 0, 0, 0, 1, 1, 1, 2, 2, 2 });
// Train K*(K-1)/2 binary classifiers for each class pair
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new OneVsOneClassifier<double>())
.Build(features, labels);
// Predict using pairwise voting
var newSample = new Matrix<double>(1, 2);
newSample[0, 0] = 3.1; newSample[0, 1] = 3.0;
var prediction = result.Predict(newSample);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
OneVsOneClassifier() |
Initializes a new instance with default settings using Gaussian Naive Bayes as the base estimator. |
OneVsOneClassifier(Func<IClassifier<T>> estimatorFactory, MetaClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) |
Initializes a new instance of the OneVsOneClassifier class. |
Methods
| Method | Summary |
|---|---|
Matrix<T> PredictLogProbabilities(Matrix<T> input) |
|
Matrix<T> PredictProbabilities(Matrix<T> input) |
|
Vector<T> Predict(Matrix<T> input) |
|
void Train(Matrix<T> x, Vector<T> y) |
Returns the model type identifier for this classifier. |