StackingClassifier<T>

Models & Types · AiDotNet.Classification.Meta

Stacking classifier that uses predictions from base classifiers as features for a meta-classifier.

Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → MetaClassifierBase<T> → StackingClassifier<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Stacking is a sophisticated ensemble method:

  1. Train multiple base classifiers
  2. Get predictions from each on training data (using cross-validation)
  3. Use these predictions as features for a “meta” classifier
  4. Train the meta-classifier on these stacked predictions

For prediction:

  1. Get predictions from all base classifiers
  2. Stack them as features
  3. Feed to meta-classifier for final prediction

Benefits:

  • Can combine very different types of classifiers
  • Often achieves better accuracy than individual classifiers
  • The meta-classifier learns which base classifiers to trust

Considerations:

  • More complex to implement
  • Risk of overfitting if not using cross-validation
  • Computationally expensive

How It Works

Stacking trains multiple base classifiers and then uses their predictions as features to train a final meta-classifier.

Example

// Create stacking classifier with base classifiers and meta-learner
var options = new StackingClassifierOptions<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 base classifiers and meta-classifier on stacked predictions
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new StackingClassifier<double>())
    .Build(features, labels);
            
// Predict using meta-classifier on base classifier outputs
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
StackingClassifier() Initializes a new instance with default settings using Gaussian Naive Bayes estimators.
StackingClassifier(IEnumerable<IClassifier<T>> estimators, Func<IClassifier<T>> finalEstimator, StackingClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the StackingClassifier 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.