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 MarkdownVoting 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.
Gaussian Naive Bayes, LDA, and Extra Trees cast class votes in hard mode. Soft voting averages their probabilities.
Class 0 Class 1Filled = training · Hollow = held out · × = missed
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.
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.
Class 0 Class 1Filled = training · Hollow = held out · × = missed
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 method | Behavior |
|---|---|
estimators | Required by both constructors. Pass an array of named classifier pairs. Each name must be unique. |
voting, weights | The voter defaults to hard voting with equal weights. For soft voting every member must implement predictProba. |
cv, finalEstimator | The 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.
RandomForestRegressor
Predict continuous targets with Random Forest regression in JavaScript and TypeScript using @kanaries/ml. Includes live demo, sklearn comparison, and tuning.
BaggingClassifier
Reduce classification variance with bootstrap aggregating in JavaScript and TypeScript using BaggingClassifier from @kanaries/ml with default or custom models.