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

## Algorithm overview

DecisionTreeRegressor models non-linear numeric targets with interpretable split rules.

This algorithm is especially useful when:

- Linear regression misses stepwise or interaction-driven behavior.
- You need explainable regression predictions for stakeholders.
- Fast inference with simple control over model complexity is required.

## JavaScript implementation

@kanaries/ml provides a decision tree regressor for JavaScript applications that need non-linear tabular regression with understandable split rules. This is useful for product-side prediction tasks where engineers want to inspect how the model partitions the feature space rather than relying on a purely opaque regressor.

In Node.js services, this keeps feature generation, prediction, and debugging in the same runtime and makes it easier to reason about errors.

## Interactive decision tree playground

Use the controls to change model complexity and data noise. The blue staircase is fitted live with `Tree.DecisionTreeRegressor`; each flat segment corresponds to a leaf prediction. Click anywhere in the chart to add a numeric observation and watch the tree refit.

> The HTML version includes an interactive Decision Tree Regressor 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/decisionTreeRegressor).

## Quick start

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

X = [[0], [1], [2], [3]]
y = [1.0, 2.2, 2.8, 4.0]

reg = DecisionTreeRegressor(max_depth=3, random_state=0)
reg.fit(X, y)
pred = reg.predict([[1.5], [2.5]])
```

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

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

const X = [[0], [1], [2], [3]];
const y = [1.0, 2.2, 2.8, 4.0];

const reg = new Tree.DecisionTreeRegressor({ max_depth: 3 });
reg.fit(X, y);
const pred = reg.predict([[1.5], [2.5]]);
```

### Quick JavaScript example

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

const X = [[0], [1], [2], [3]];
const y = [1.0, 2.2, 2.8, 4.0];

const reg = new Tree.DecisionTreeRegressor({ max_depth: 3 });
reg.fit(X, y);
const pred = reg.predict([[1.5], [2.5]]);
console.log(pred);
```

## Detailed API reference

```ts
interface RegressionTreeProps {
    max_depth?: number;
    min_samples_split?: number;
}

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

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

### Algorithm

The regressor follows CART/sklearn split semantics:

- **Split criterion:** each candidate split minimizes the weighted sum of squared errors `n_l * var_l + n_r * var_r` (evaluated with a sorted prefix-sum scan), not the unweighted variance sum.
- **Thresholds:** candidate thresholds are the midpoints of adjacent unique feature values (computed overflow-safe as `a/2 + b/2`), 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.

### Methods

- `fit(trainX: number[][], trainY: number[]): void`
- `predict(testX: number[][]): number[]`
- `apply(testX: number[][]): number[]` — returns the depth-first leaf id each sample falls into
- `setLeafValues(values: Map<number, number>): void` — overwrites leaf predictions by leaf id (used by boosting learners such as `GradientBoostingClassifier` for the per-leaf Newton step)

### Implementation workflow

1. Train with baseline depth constraints and evaluate residuals.
2. Inspect important splits for domain plausibility.
3. Tune split constraints to balance bias and variance.

### JavaScript deployment notes

- Use a decision tree regressor when linear regression misses non-linear thresholds or interactions.
- Keep an eye on depth and minimum split settings because trees can overfit quickly.
- This model is a good fit for interpretable tabular regression baselines in product code.
