BaggingClassifier<T>
Models & Types · AiDotNet.Classification.Meta
Bagging (Bootstrap Aggregating) classifier.
Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → MetaClassifierBase<T> → BaggingClassifier<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
Bagging is a technique to reduce overfitting:
- Create N bootstrap samples (random samples with replacement)
- Train one classifier on each sample
- For prediction, each classifier votes
- Final prediction is the majority vote
Benefits:
- Reduces variance (less overfitting)
- Works well with high-variance classifiers like decision trees
- Easily parallelizable
When to use:
- When your base classifier tends to overfit
- When you want more robust predictions
- As a simpler alternative to boosting
How It Works
Bagging trains multiple classifiers on bootstrap samples of the training data and combines their predictions through voting.
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 bagging classification
var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new BaggingClassifier<double>(() => new GaussianNaiveBayes<double>()));
var result = builder.Build(features, labels);
var prediction = result.Predict(newSample);
Constructors
| Constructor | Summary |
|---|---|
BaggingClassifier() |
Initializes a new instance with default settings using Gaussian Naive Bayes as the base estimator. |
BaggingClassifier(Func<IClassifier<T>> estimatorFactory, BaggingClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) |
Initializes a new instance of the BaggingClassifier 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. |