---
title: "RandomForestClassifier in JavaScript with @kanaries/ml"
description: "Train bootstrap decision-tree ensembles with the RandomForestClassifier JavaScript and TypeScript implementation in @kanaries/ml."
canonical_url: "https://ml.kanaries.net/docs/apis/ensemble/randomForestClassifier"
markdown_url: "https://ml.kanaries.net/docs/apis/ensemble/randomForestClassifier.md"
---
# RandomForestClassifier in JavaScript

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

```ts
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]]);
console.log(pred);
```

## Detailed API reference

```ts
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'`.
