BalancedBaggingClassifier<T>

Models & Types · AiDotNet.Classification.ImbalancedEnsemble

Balanced Bagging Classifier for imbalanced datasets.

Inheritance: ClassifierBase<T> → BalancedBaggingClassifier<T>

For Beginners

Bagging (Bootstrap Aggregating) creates diverse classifiers by training each on a random bootstrap sample. Balanced Bagging adds undersampling to ensure each bootstrap is class-balanced, improving minority class detection.

How It Works

How it works: For each base classifier:

  • Sample minority class with replacement (bootstrap)
  • Undersample majority class to match minority count
  • Train a base classifier (typically decision tree) on balanced bootstrap
  • Combine predictions using majority voting

Key advantages:

  • Reduces variance: Multiple diverse classifiers give stable predictions
  • Handles imbalance: Each classifier sees balanced data
  • Parallelizable: Each base classifier can be trained independently
  • Flexible: Works with any base classifier, not just trees

Difference from BalancedRandomForest: BalancedBagging can use any base classifier and each classifier sees all features, while BalancedRandomForest uses trees with random feature subsets at each split.

When to use:

  • When you want ensemble benefits with imbalanced data
  • When you want to use a specific base classifier (not just trees)
  • When variance reduction is more important than bias reduction

References:

  • Hido, S., & Kashima, H. (2009). “Roughly Balanced Bagging for Imbalanced Data”
  • Wang, S., & Yao, X. (2009). “Diversity Analysis on Imbalanced Data Sets”

Example

var newSample = 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 features = 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 balanced bagging classification
var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new BalancedBaggingClassifier<double>(nEstimators: 10));
            
var result = builder.Build(features, labels);
var prediction = result.Predict(newSample);

Constructors

Constructor Summary
BalancedBaggingClassifier(int nEstimators, int? maxDepth, int minSamplesSplit, int minSamplesLeaf, double samplingRatio, bool bootstrapMinority, int? seed) Initializes a new instance of BalancedBaggingClassifier.

Methods

Method Summary
Dictionary<string, T> GetFeatureImportance() Gets feature importance based on split usage.
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) Creates a new instance with the specified parameters.
Vector<T> Predict(Matrix<T> input) Predicts class labels for the given input data.
void Train(Matrix<T> x, Vector<T> y) Trains the balanced bagging classifier.