BalancedRandomForestClassifier<T>

Models & Types · AiDotNet.Classification.ImbalancedEnsemble

Balanced Random Forest Classifier for imbalanced datasets.

Inheritance: ClassifierBase<T> → BalancedRandomForestClassifier<T>

For Beginners

When you have imbalanced data (e.g., 1000 normal transactions vs 10 fraudulent ones), regular classifiers often ignore the minority class. Balanced Random Forest fixes this by training each tree on a balanced subset of data.

How It Works

How it works: For each tree in the forest:

  • Randomly sample from the minority class with replacement
  • Randomly undersample the majority class to match minority count
  • Train a decision tree on this balanced bootstrap

Key advantages:

  • Better detection of minority class compared to standard Random Forest
  • Maintains the ensemble benefits (reduced variance, robustness)
  • No need to manually balance your dataset

When to use:

  • Fraud detection where fraud cases are rare
  • Medical diagnosis where positive cases are uncommon
  • Any binary classification with significant class imbalance

References:

  • Chen, C., Liaw, A., & Breiman, L. (2004). “Using Random Forest to Learn Imbalanced Data”

Example

// Create balanced random forest for imbalanced fraud detection
            
// Prepare imbalanced training data: 8 samples, 6 majority (class 0), 2 minority (class 1)
var features = new Matrix<double>(8, 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] = 1.3; features[3, 1] = 0.7;
features[4, 0] = 1.1; features[4, 1] = 0.8;
features[5, 0] = 0.9; features[5, 1] = 1.2;
features[6, 0] = 5.0; features[6, 1] = 5.1;
features[7, 0] = 5.2; features[7, 1] = 4.9;
var labels = new Vector<double>(new double[] { 0, 0, 0, 0, 0, 0, 1, 1 });
            
// Train trees on balanced bootstrap samples with random feature subsets
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new BalancedRandomForestClassifier<double>())
    .Build(features, labels);
            
// Predict using ensemble vote
var newSample = new Matrix<double>(1, 2);
newSample[0, 0] = 5.1; newSample[0, 1] = 5.0;
var prediction = result.Predict(newSample);
// Result is available in the returned value

Constructors

Constructor Summary
BalancedRandomForestClassifier(int nEstimators, int? maxDepth, int? maxFeatures, int minSamplesSplit, int minSamplesLeaf, string samplingStrategy, bool bootstrap, int? seed) Initializes a new instance of BalancedRandomForestClassifier.

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 random forest classifier.