Skip to main content

DecisionTreeClassifier

A decision tree classifier using CART algorithm with Gini impurity.

Constructor

import { DecisionTreeClassifier } from "bun-scikit";

const clf = new DecisionTreeClassifier({
  maxDepth: 12,
  minSamplesSplit: 2,
  minSamplesLeaf: 1,
  maxFeatures: "sqrt",
  randomState: 42
});

Parameters

maxDepth
number
default:"12"
Maximum depth of the tree. Deeper trees can model more complex patterns but may overfit.
minSamplesSplit
number
default:"2"
Minimum number of samples required to split an internal node.
minSamplesLeaf
number
default:"1"
Minimum number of samples required to be at a leaf node.
maxFeatures
'sqrt' | 'log2' | number | null
default:"null"
Number of features to consider when looking for the best split:
  • "sqrt": sqrt(n_features)
  • "log2": log2(n_features)
  • number: specific number of features
  • null: use all features
randomState
number
Random seed for reproducible feature selection when maxFeatures < n_features.

Methods

fit()

Train the decision tree classifier.
clf.fit(X: Matrix, y: Vector): DecisionTreeClassifier
X
Matrix
required
Training data of shape [n_samples, n_features].
y
Vector
required
Target values (class labels).

predict()

Predict class labels for samples.
clf.predict(X: Matrix): Vector
X
Matrix
required
Samples to predict, shape [n_samples, n_features].
Returns: Predicted class labels.

score()

Return the accuracy on the given test data.
clf.score(X: Matrix, y: Vector): number
Returns: Accuracy score between 0 and 1.

dispose()

Free native resources if using Zig backend.
clf.dispose(): void

Properties

classes_
Vector
Unique class labels found during training.
featureImportances_
Vector | null
Feature importances (Gini importance). Higher values indicate more important features.
fitBackend_
'zig' | 'js'
Backend used for training: "zig" (native) or "js" (JavaScript).
fitBackendLibrary_
string | null
Path to native library if Zig backend was used.

Zig Backend

DecisionTreeClassifier can use a Zig-powered native backend for faster training. The backend is automatically selected when:
  • Number of classes is between 2 and 256
  • BUN_SCIKIT_TREE_BACKEND is not set to "js" or "off"
Control backend selection:
export BUN_SCIKIT_TREE_BACKEND="zig"   # Force native (default)
export BUN_SCIKIT_TREE_BACKEND="js"    # Force JavaScript
export BUN_SCIKIT_TREE_BACKEND="auto"  # Auto-select

Example

import { DecisionTreeClassifier } from "bun-scikit";

// Create classifier with max depth 5
const clf = new DecisionTreeClassifier({ 
  maxDepth: 5,
  randomState: 42 
});

// Training data
const X = [
  [2.5, 1.2],
  [3.1, 1.5],
  [1.2, 0.8],
  [0.9, 0.5]
];
const y = [0, 0, 1, 1];

// Train
clf.fit(X, y);

// Predict
const predictions = clf.predict([[2.0, 1.0], [1.0, 0.6]]);
console.log(predictions); // [0, 1]

// Feature importances
console.log(clf.featureImportances_);
// [0.66, 0.34] - first feature is more important

// Clean up
clf.dispose();

DecisionTreeRegressor

A decision tree regressor using CART algorithm with variance reduction.

Constructor

import { DecisionTreeRegressor } from "bun-scikit";

const reg = new DecisionTreeRegressor({
  maxDepth: 12,
  minSamplesSplit: 2,
  minSamplesLeaf: 1,
  maxFeatures: null,
  randomState: 42
});

Parameters

maxDepth
number
default:"12"
Maximum depth of the tree.
minSamplesSplit
number
default:"2"
Minimum number of samples required to split an internal node.
minSamplesLeaf
number
default:"1"
Minimum number of samples required to be at a leaf node.
maxFeatures
'sqrt' | 'log2' | number | null
default:"null"
Number of features to consider when looking for the best split.
randomState
number
Random seed for reproducible feature selection.

Methods

fit()

Train the decision tree regressor.
reg.fit(X: Matrix, y: Vector): DecisionTreeRegressor
X
Matrix
required
Training data of shape [n_samples, n_features].
y
Vector
required
Target values (continuous).

predict()

Predict target values for samples.
reg.predict(X: Matrix): Vector
X
Matrix
required
Samples to predict, shape [n_samples, n_features].
Returns: Predicted continuous values.

score()

Return the R² score on the given test data.
reg.score(X: Matrix, y: Vector): number
Returns: R² score (coefficient of determination). Best possible score is 1.0.

Properties

featureImportances_
Vector | null
Feature importances based on variance reduction.

Example

import { DecisionTreeRegressor } from "bun-scikit";

// Create regressor
const reg = new DecisionTreeRegressor({ 
  maxDepth: 3,
  minSamplesLeaf: 2
});

// Training data
const X = [
  [1.0],
  [2.0],
  [3.0],
  [4.0],
  [5.0]
];
const y = [1.5, 2.1, 2.9, 4.2, 5.1];

// Train
reg.fit(X, y);

// Predict
const predictions = reg.predict([[2.5], [3.5]]);
console.log(predictions); // [2.5, 3.55]

// R² score
const r2 = reg.score(X, y);
console.log(r2); // 0.98

// Feature importances
console.log(reg.featureImportances_);

Build docs developers (and LLMs) love