VotingClassifier<T>
Models & Types · AiDotNet.Classification.Meta
Voting classifier that combines multiple classifiers through voting.
Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → MetaClassifierBase<T> → VotingClassifier<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
Voting combines predictions from multiple models:
Hard Voting:
- Each classifier votes for a class
- The class with most votes wins
- Example: [A, A, B] -> A wins (2 vs 1)
Soft Voting:
- Average the probability predictions
- Pick the class with highest average probability
- Generally works better when classifiers output calibrated probabilities
When to use:
- To combine different types of classifiers
- When you want to reduce the risk of a single bad model
- To leverage the strengths of different algorithms
How It Works
Voting classifier combines predictions from multiple different classifiers using either hard voting (majority vote) or soft voting (average probabilities).
Example
// Create voting classifier combining multiple models
var options = new VotingClassifierOptions<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 multiple diverse classifiers
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new VotingClassifier<double>())
.Build(features, labels);
// Predict using majority vote or averaged probabilities
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 |
|---|---|
VotingClassifier() |
Initializes a new instance with default settings using Gaussian Naive Bayes estimators. |
VotingClassifier(IEnumerable<IClassifier<T>> estimators, VotingClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) |
Initializes a new instance of the VotingClassifier class. |
Methods
| Method | Summary |
|---|---|
Matrix<T> PredictLogProbabilities(Matrix<T> input) |
|
Matrix<T> PredictProbabilities(Matrix<T> input) |
|
ModelMetadata<T> GetModelMetadata() |
|
Vector<T> Predict(Matrix<T> input) |
|
void Train(Matrix<T> x, Vector<T> y) |
Returns the model type identifier for this classifier. |