Skip to Content
OchreML
Trees, Ensembles & Boosting

DecisionTreeClassifier

CART classification tree with Gini impurity and entropy criteria backed by Rust Rayon and N6 GPU histogram acceleration.

DecisionTreeClassifier builds a binary Classification and Regression Tree (CART) by recursively partitioning the feature space to minimize node impurity (Gini impurity or Shannon entropy). (^▽^)

Backend & Hardware Acceleration

  • Zero-Copy Ingestion: Accepts NumPy arrays and Python Buffer Protocol objects directly via fit_buffer, predict_buffer, and score_buffer without copying into Python lists.
  • CPU Rayon & GPU N6 Path: Feature split evaluation runs in parallel across CPU cores with Rust Rayon. On CUDA and wgpu devices (for datasets with at least 64 rows), the tree builder can quantize continuous features into u8 bins (max_bins=256) and accelerate split finding via the N6 histogram primitive. On TPU devices, branch-heavy tree construction automatically routes to CPU Rayon.

Import & Signature

from ochreml.tree import DecisionTreeClassifier
# or: from ochreml import DecisionTreeClassifier

DecisionTreeClassifier(
    criterion: str = "gini",
    max_depth: Optional[int] = None,
    min_samples_split: int = 2,
    min_samples_leaf: int = 1,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
criterionstr"gini"Function to measure split quality: "gini" (Gini impurity) or "entropy" (information gain).
max_depthint | NoneNoneMaximum depth of the tree. If None, nodes expand until leaves are pure or contain fewer than min_samples_split samples.
min_samples_splitint2Minimum number of samples required to split an internal node.
min_samples_leafint1Minimum number of samples required at a leaf node.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
classes_list[float]Unique class labels discovered during fit.
n_features_in_int | NoneNumber of features seen during fit.
device_str | NoneResolved hardware device string used by the underlying Rust estimator.

Methods

MethodSignatureDescription
fitfit(X, y) -> DecisionTreeClassifierBuild a decision tree classifier from training set (X, y).
predictpredict(X) -> np.ndarrayPredict class labels for samples in X (falls back to Python list if NumPy is unavailable).
scorescore(X, y) -> floatReturn the mean classification accuracy on test data (X, y).
savesave(path: str) -> NoneSerialize the fitted model to a binary .dit file.
loadDecisionTreeClassifier.load(path: str, device: str | None = None)Load a fitted classifier from a .dit file.
toto(device: str) -> DecisionTreeClassifierMove the estimator to another target compute device.

Example

import numpy as np
from ochreml.tree import DecisionTreeClassifier

X = np.array(
    [
        [0.1, 0.2],
        [0.2, 0.1],
        [0.8, 0.9],
        [0.9, 0.8],
        [0.15, 0.25],
        [0.85, 0.75],
    ],
    dtype=np.float64,
)
y = np.array([0, 0, 1, 1, 0, 1], dtype=np.float64)

clf = DecisionTreeClassifier(criterion="gini", max_depth=3, device="auto")
clf.fit(X, y)

preds = clf.predict(X)
acc = clf.score(X, y)
print("Classes:", clf.classes_)
print("Predictions:", preds)
print("Accuracy:", acc)

On this page