---
title: "Decision Tree Classifier in JavaScript with @kanaries/ml"
description: "Learn what Decision Tree Classifier does, when to use it, and how to run DecisionTreeClassifier in JavaScript or TypeScript with @kanaries/ml for browser and Node.js applications."
canonical_url: "https://ml.kanaries.net/docs/apis/tree/decisionTreeClassifier"
markdown_url: "https://ml.kanaries.net/docs/apis/tree/decisionTreeClassifier.md"
---
# Decision Tree Classifier in JavaScript

## Algorithm overview

DecisionTreeClassifier learns human-readable if/else rules for classification tasks on tabular data.

This algorithm is especially useful when:

- Interpretability and decision-path transparency are important.
- Feature interactions are non-linear and heterogeneous.
- You need a baseline that is easy to inspect and debug.

## JavaScript implementation

@kanaries/ml gives JavaScript teams an interpretable decision tree classifier that fits naturally into product logic, browser demos, and Node.js APIs. This is useful when teams care about inspecting split paths, explaining predictions, or debugging model decisions in the same codebase that serves the application.

Because tree behavior is easy to reason about, this implementation is especially practical in products where transparency matters more than squeezing out the last bit of benchmark accuracy.

## Interactive decision tree playground

Change the tree depth, split constraints, dataset, and impurity criterion below. The decision surface and tree diagram are fitted live with `Tree.DecisionTreeClassifier` in your browser. Click the chart to add a class A or class B observation and see which rule changes.

> The HTML version includes an interactive Decision Tree Classifier playground. You can tune the tree depth, split constraints, dataset noise, and criterion; add observations; and inspect the fitted predictions and tree structure. The guide, runnable example, and API reference continue below. [Open the HTML page](https://ml.kanaries.net/docs/apis/tree/decisionTreeClassifier).

## Quick start

### DecisionTreeClassifier: Python and JavaScript / TypeScript

The Python example uses scikit-learn; the TypeScript example uses @kanaries/ml in browser or Node.js runtimes.

#### Python (scikit-learn)

```python
from sklearn.tree import DecisionTreeClassifier

X = [[0, 0], [1, 1], [1, 0], [0, 1]]
y = [0, 1, 1, 0]

clf = DecisionTreeClassifier(max_depth=3, criterion='gini', random_state=0)
clf.fit(X, y)
pred = clf.predict([[0.9, 0.8], [0.1, 0.2]])
```

#### JavaScript / TypeScript (@kanaries/ml)

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

const X = [[0, 0], [1, 1], [1, 0], [0, 1]];
const y = [0, 1, 1, 0];

const clf = new Tree.DecisionTreeClassifier({ max_depth: 3, criterion: 'gini' });
clf.fit(X, y);
const pred = clf.predict([[0.9, 0.8], [0.1, 0.2]]);
```

### Quick JavaScript example

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

const X = [[0, 0], [1, 1], [1, 0], [0, 1]];
const y = [0, 1, 1, 0];

const clf = new Tree.DecisionTreeClassifier({ max_depth: 3, criterion: 'gini' });
clf.fit(X, y);
const pred = clf.predict([[0.9, 0.8], [0.1, 0.2]]);
console.log(pred);
```

## Detailed API reference

```ts
interface DecisionTreeProps {
    max_depth?: number;
    min_samples_split?: number;
    criterion?: 'entropy' | 'gini';
}

constructor(props: DecisionTreeProps = {})
```

Defaults are `max_depth: Infinity` (grow until pure or `min_samples_split` blocks a split), `min_samples_split: 2`, and `criterion: 'entropy'`.

### Algorithm

The classifier follows CART/sklearn split semantics:

- **Criterion:** `gini` or `entropy` impurity, chosen via `criterion`.
- **Thresholds:** candidate thresholds are the midpoints of adjacent unique feature values, and the convention is `x <= threshold` goes left (sklearn's convention).
- **max\_depth:** enforced while building so leaves sit at depth `== max_depth` (sklearn semantics), instead of truncating the tree at predict time.
- **Non-separable samples:** when no feature separates the samples in a node (for example duplicate rows with conflicting labels), the node becomes a leaf that predicts the majority class rather than creating empty children.

### Methods

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

### Implementation workflow

1. Prepare cleaned tabular features and split train/validation data.
2. Fit the classifier and inspect depth, splits, and leaf purity.
3. Tune depth/min-sample settings to reduce overfitting.

### JavaScript deployment notes

- Use a decision tree classifier when interpretability and rule-like behavior matter.
- Control depth and split thresholds to avoid overfitting on smaller tabular datasets.
- Trees are a strong product baseline because their behavior is easy to inspect and explain.
