---
title: "RandomForestRegressor in JavaScript with @kanaries/ml"
description: "Predict continuous targets with tree ensembles using the RandomForestRegressor JavaScript and TypeScript implementation in @kanaries/ml."
canonical_url: "https://ml.kanaries.net/docs/apis/ensemble/randomForestRegressor"
markdown_url: "https://ml.kanaries.net/docs/apis/ensemble/randomForestRegressor.md"
---
# RandomForestRegressor in JavaScript

## Algorithm overview

RandomForestRegressor averages predictions from many decision trees. It is useful for nonlinear numeric prediction when a single regression tree is too noisy.

## JavaScript implementation

`@kanaries/ml` exposes `Ensemble.RandomForestRegressor` for JavaScript and TypeScript projects. It supports bootstrap sampling, feature subsampling, and seeded randomness.

## Interactive random forest regression playground

Change the number of trees, dataset, and noise below. The prediction line is produced by a live `Ensemble.RandomForestRegressor`; click the chart to add another training point.

> The HTML version includes an interactive Random Forest 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/randomForestRegressor).

## Quick start example

```ts
import { Ensemble } from '@kanaries/ml';

const reg = new Ensemble.RandomForestRegressor({
  nEstimators: 25,
  maxDepth: 4,
  randomState: 7,
});

reg.fit([[0], [1], [2], [3]], [0, 1, 4, 9]);
const pred = reg.predict([[4]]);
console.log(pred);
```

## Detailed API reference

```ts
new Ensemble.RandomForestRegressor(props?: {
  nEstimators?: number;
  bootstrap?: boolean;
  maxDepth?: number;
  minSamplesSplit?: number;
  maxFeatures?: number | 'sqrt' | 'log2';
  randomState?: number;
})
```

Methods:

- `fit(trainX: number[][], trainY: number[]): void`
- `predict(testX: number[][]): number[]`

Defaults are `nEstimators: 100`, `bootstrap: true`, and `maxFeatures: 1.0`.
