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, andscore_bufferwithout copying into Python lists. - CPU Rayon & GPU
N6Path: Feature split evaluation runs in parallel across CPU cores with Rust Rayon. On CUDA andwgpudevices (for datasets with at least 64 rows), the tree builder can quantize continuous features intou8bins (max_bins=256) and accelerate split finding via theN6histogram 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
| Parameter | Type | Default | Description |
|---|---|---|---|
criterion | str | "gini" | Function to measure split quality: "gini" (Gini impurity) or "entropy" (information gain). |
max_depth | int | None | None | Maximum depth of the tree. If None, nodes expand until leaves are pure or contain fewer than min_samples_split samples. |
min_samples_split | int | 2 | Minimum number of samples required to split an internal node. |
min_samples_leaf | int | 1 | Minimum number of samples required at a leaf node. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
classes_ | list[float] | Unique class labels discovered during fit. |
n_features_in_ | int | None | Number of features seen during fit. |
device_ | str | None | Resolved hardware device string used by the underlying Rust estimator. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y) -> DecisionTreeClassifier | Build a decision tree classifier from training set (X, y). |
predict | predict(X) -> np.ndarray | Predict class labels for samples in X (falls back to Python list if NumPy is unavailable). |
score | score(X, y) -> float | Return the mean classification accuracy on test data (X, y). |
save | save(path: str) -> None | Serialize the fitted model to a binary .dit file. |
load | DecisionTreeClassifier.load(path: str, device: str | None = None) | Load a fitted classifier from a .dit file. |
to | to(device: str) -> DecisionTreeClassifier | Move 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)