---
title: "Voting and Stacking Classifiers in JavaScript"
description: "Compare voting and stacking classifiers in JavaScript. Fit both ensembles in your browser and reuse their @kanaries/ml APIs in Node.js."
canonical_url: "https://ml.kanaries.net/docs/apis/ensemble/votingAndStackingClassifiers"
markdown_url: "https://ml.kanaries.net/docs/apis/ensemble/votingAndStackingClassifiers.md"
---
# Voting and Stacking Classifiers in JavaScript

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.

> In the interactive decision map, change the training points or classifier parameters to see the fitted regions and holdout accuracy. [Open the HTML page](https://ml.kanaries.net/docs/apis/ensemble/votingAndStackingClassifiers).

## 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.

> In the interactive decision map, change the training points or classifier parameters to see the fitted regions and holdout accuracy. [Open the HTML page](https://ml.kanaries.net/docs/apis/ensemble/votingAndStackingClassifiers).

## 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.

```js
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](/playground/classifier-boundaries).
