@kanaries/ml
API Reference/Ensemble

RandomForestClassifier

Train bootstrap decision-tree ensembles with the RandomForestClassifier JavaScript and TypeScript implementation in @kanaries/ml.

Algorithm overview

RandomForestClassifier combines many decision trees and predicts by majority vote. It is useful for nonlinear tabular classification when a single tree is too sensitive or underpowered.

JavaScript implementation

@kanaries/ml provides Ensemble.RandomForestClassifier for browser and Node.js applications. The implementation builds DecisionTreeClassifier estimators with bootstrap sampling and feature subsampling.

Quick start example

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

const clf = new Ensemble.RandomForestClassifier({
  nEstimators: 25,
  maxFeatures: 'sqrt',
  randomState: 42,
});

clf.fit([[0, 0], [0, 1], [3, 3], [4, 3]], [0, 0, 1, 1]);
const pred = clf.predict([[1, 1], [4, 4]]);

Detailed API reference

new Ensemble.RandomForestClassifier(props?: {
  nEstimators?: number;
  bootstrap?: boolean;
  maxFeatures?: number | 'sqrt' | 'log2';
  randomState?: number;
  // plus DecisionTreeClassifier options
})

Methods:

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

The classifier defaults to nEstimators: 100, bootstrap: true, and maxFeatures: 'sqrt'.