@kanaries/ml
API Reference/Ensemble

Voting and Stacking Classifiers

Compare voting and stacking classifiers in JavaScript. Fit both ensembles in your browser and reuse their @kanaries/ml APIs in Node.js.

View as Markdown

Voting and stacking combine the predictions of several classifiers. For example, a Gaussian classifier can do well in the center of a cluster while a tree can capture a sharp change near its edge. The two ensembles handle disagreement differently. Voting gives each member a fixed say in the decision. Stacking trains another classifier to combine the member outputs.

Both run in the browser or Node.js through the Ensemble namespace in @kanaries/ml. Choose the same dataset in the two examples to see where the choice of combination rule changes a prediction.

Voting classifier

With hard voting, each member contributes its predicted label. With soft voting, each member contributes a probability for each class. A close call from one member can then carry less influence than a confident prediction from another. Switch the voting rule and inspect the regions where the predictions differ.

3

Gaussian Naive Bayes, LDA, and Extra Trees cast class votes in hard mode. Soft voting averages their probabilities.

Training accuracy 88% (64 points)Holdout accuracy 88% (16 points)
Voting Classifier decision regions for moons-2-20022Feature 1Feature 2

Class 0 Class 1Filled = training · Hollow = held out · × = missed

Add a training point

Choose a class and click the map, or enter coordinates. The holdout points stay fixed.

0 added
Enter coordinates

Stacking classifier

During training, each base model makes predictions for rows held out from its own fit. A final classifier learns from these out-of-fold predictions. At prediction time the ensemble uses base models fitted on the full training set. Change the number of folds and inspect the effect on the boundary and held-out accuracy.

3
3

Logistic regression learns from the out-of-fold probabilities of Gaussian Naive Bayes, LDA, and Extra Trees. Change the fold count to refit the stack.

Training accuracy 86% (64 points)Holdout accuracy 88% (16 points)
Stacking Classifier decision regions for moons-2-20022Feature 1Feature 2

Class 0 Class 1Filled = training · Hollow = held out · × = missed

Add a training point

Choose a class and click the map, or enter coordinates. The holdout points stay fixed.

0 added
Enter coordinates

Quick start

Fit a soft vote and a stack to the same observations. Each model predicts the class of a new observation with feature values 3 and 2.

import { Bayes, Ensemble } from '@kanaries/ml';

const X = [[0, 0], [0, 1], [1, 0], [1, 1], [3, 3], [3, 4], [4, 3], [4, 4]];
const y = [0, 0, 0, 0, 1, 1, 1, 1];
const makeMembers = () => [
  ['gaussian', new Bayes.GaussianNB()],
  ['trees', new Ensemble.ExtraTreesClassifier({ nEstimators: 5, max_depth: 2, randomState: 7 })],
];

const vote = new Ensemble.VotingClassifier({ estimators: makeMembers(), voting: 'soft' });
const stack = new Ensemble.StackingClassifier({ estimators: makeMembers(), cv: 2 });
vote.fit(X, y);
stack.fit(X, y);
console.log(vote.predict([[3, 2]]), stack.predict([[3, 2]]));

API reference

Option or methodBehavior
estimatorsRequired by both constructors. Pass an array of named classifier pairs. Each name must be unique.
voting, weightsThe voter defaults to hard voting with equal weights. For soft voting every member must implement predictProba.
cv, finalEstimatorThe stack uses five stratified folds and logistic regression by default. Pass a different final classifier to change how member outputs are combined.
fit(X, y), predict(X)Fit the ensemble on a numeric feature matrix and labels, then return one predicted class label per new row.

For more models on the same datasets, open the classifier decision boundary playground.