@kanaries/ml
API Reference/Ensemble

RandomForestRegressor

Predict continuous targets with tree ensembles using the RandomForestRegressor JavaScript and TypeScript implementation in @kanaries/ml.

View as Markdown

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.

Live browser model

RandomForestRegressor playground

Adjust the data and model, then click the chart to add a training observation.

Fitted with @kanaries/ml
-3-2-10123-1.9-0.90.01.01.9feature xtarget y
prediction training holdout your points
Train RMSE0.122
Holdout RMSE0.247
Holdout R²0.695
Custom points0

Quick start example

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

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.