---
title: "BaggingRegressor in JavaScript with @kanaries/ml"
description: "Reduce regression variance with bootstrap aggregation using the BaggingRegressor JavaScript and TypeScript implementation in @kanaries/ml."
canonical_url: "https://ml.kanaries.net/docs/apis/ensemble/baggingRegressor"
markdown_url: "https://ml.kanaries.net/docs/apis/ensemble/baggingRegressor.md"
---
# BaggingRegressor in JavaScript

## Algorithm overview

BaggingRegressor fits cloned regressors on random sample subsets and averages their predictions. It is useful for reducing the variance of unstable learners such as regression trees.

## JavaScript implementation

`Ensemble.BaggingRegressor` defaults to decision trees and can clone any registered regressor that implements `fit` and `predict`. Seedable sampling makes browser and Node.js runs reproducible.

## Interactive bagging regression playground

Vary the ensemble size and sample noise to see bootstrap averaging at work. The curve is fitted live with `Ensemble.BaggingRegressor` and responds to observations you add.

> The HTML version includes an interactive Bagging Regression playground powered by @kanaries/ml. You can change the dataset, noise, and model controls; add observations; and inspect live predictions plus train and holdout metrics. The runnable guide and API reference continue below. [Open the HTML page](https://ml.kanaries.net/docs/apis/ensemble/baggingRegressor).

## Quick start example

```ts
import { Ensemble } from '@kanaries/ml';
const X = [[0], [1], [2], [3], [4]];
const y = [0, 1, 4, 9, 16];
const model = new Ensemble.BaggingRegressor({ nEstimators: 25, maxSamples: .8, randomState: 42 });
model.fit(X, y);
console.log(model.predict([[2.5]]));
```

## Detailed API reference

Constructor options are `estimator?`, `nEstimators?` (default 10), `maxSamples?` (integer or fraction), `bootstrap?` (default true), and `randomState?`. Methods are `fit(X, y)` and `predict(X)`.
