AdaptiveRandomForestClassifier<T>

Models & Types · AiDotNet.Classification.Online

Implements Adaptive Random Forest (ARF) for online classification with drift detection.

Inheritance: ClassifierBase<T> → AdaptiveRandomForestClassifier<T>

Implements: IOnlineClassifier<T>

For Beginners

Adaptive Random Forest is an ensemble of Hoeffding trees designed to handle concept drift in data streams. Each tree has an associated drift detector, and when drift is detected, the affected tree is replaced with a new one trained on recent data.

How It Works

How it works:

  • Maintain an ensemble of Hoeffding trees
  • Each tree has a “warning” detector and a “drift” detector
  • When warning is triggered, start training a background tree
  • When drift is confirmed, replace the old tree with the background tree
  • Use majority voting for predictions

Key innovations:

  • Per-tree drift detection for local adaptation
  • Resampling with Poisson(λ=6) for diversity
  • Background tree preparation for smooth transitions
  • Weighted voting based on tree accuracy

Advantages:

  • Handles both sudden and gradual drift
  • Maintains diversity through random subspaces
  • Provides robust predictions through ensemble voting
  • Adapts locally without resetting entire ensemble

Reference: Gomes et al., “Adaptive Random Forests for Evolving Data Stream Classification” (2017)

Example

// Create adaptive random forest for streaming data with concept drift
var options = new AdaptiveRandomForestOptions<double>();
            
// Prepare initial training data
var features = new Matrix<double>(new double[,] { { 1.0, 1.1 }, { 1.2, 0.9 }, { 0.8, 1.0 }, { 5.0, 5.1 }, { 5.2, 4.9 }, { 4.8, 5.0 } });
var labels = new Vector<double>(new double[] { 0, 0, 0, 1, 1, 1 });
            
// Train with per-tree drift detection for local adaptation
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new AdaptiveRandomForestClassifier<double>(options))
    .Build(features, labels);
            
// Predict with weighted voting across adapted trees
var newSample = new Matrix<double>(new double[,] { { 1.1, 1.0 } });
var prediction = result.Predict(newSample);
// Result is available in the returned value

Constructors

Constructor Summary
AdaptiveRandomForestClassifier(AdaptiveRandomForestOptions<T> options) Creates a new Adaptive Random Forest classifier.

Properties

Property Summary
bool IsWarm { get; } Gets whether the model has seen at least one sample.
double AverageTreeAccuracy { get; } Gets the average accuracy across all trees.
int TreeCount { get; } Gets the number of trees in the ensemble.
int TreesInWarning { get; } Gets the number of trees currently in warning state.
long SamplesSeen { get; set; } Gets the total number of samples the model has seen.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
ModelOptions GetOptions()
Vector<T> Predict(Matrix<T> input)
void PartialFit(Matrix<T> features, Vector<T> labels) Updates the model with a batch of training samples.
void PartialFit(Vector<T> features, T label) Updates the model with a batch of training samples.
void Train(Matrix<T> x, Vector<T> y)