@kanaries/ml
API Reference/Tree

Decision Tree Classifier

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.

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.

Quick start

DecisionTreeClassifier in Python vs JavaScript / TypeScript

If you searched for "DecisionTreeClassifier in JavaScript" or "DecisionTreeClassifier in TypeScript", this section maps the familiar scikit-learn call to the equivalent @kanaries/ml usage for browser and Node.js runtimes.

Python
scikit-learn
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
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

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]]);

Detailed API reference

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.