Trees, Ensembles & Boosting
RandomForestClassifier
Parallel bagged decision tree classifier with bootstrap sampling, feature subsampling, and Rayon/N6 acceleration.
RandomForestClassifier fits an ensemble of randomized decision trees on bootstrap sub-samples of the dataset and averages their predicted class probabilities to improve generalization and control overfitting. (^▽^)
Backend & Hardware Acceleration
- Parallel Forest Construction: Individual trees are built concurrently across CPU cores using Rust Rayon with deterministic per-tree RNG seeding when
random_stateis set. - GPU
N6Histogram Support: Integrates with OchreML'sN6histogram infrastructure on CUDA andwgpudevices while falling back cleanly to Rayon CPU parallelism on CPU and TPU targets. - Zero-Copy Buffers:
fit,predict,predict_proba, andscorepass contiguousfloat64buffers directly into Rust.
Import & Signature
from ochreml.ensemble import RandomForestClassifier
# or: from ochreml import RandomForestClassifier
RandomForestClassifier(
n_estimators: int = 100,
criterion: str = "gini",
max_depth: Optional[int] = None,
min_samples_split: int = 2,
min_samples_leaf: int = 1,
max_features: Union[str, int, float, None] = "sqrt",
bootstrap: bool = True,
random_state: Optional[int] = None,
device: Optional[str] = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n_estimators | int | 100 | Number of trees in the forest. |
criterion | str | "gini" | Split quality criterion: "gini" or "entropy". |
max_depth | int | None | None | Maximum depth of each tree. None expands until stopping criteria are met. |
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. |
max_features | str | int | float | None | "sqrt" | Number of features considered at each split: "sqrt", "log2", "all", positive int, fraction float in (0.0, 1.0], or None. |
bootstrap | bool | True | Whether bootstrap samples are drawn when building trees. |
random_state | int | None | None | Seed controlling bootstrapping and feature subsampling randomness. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
classes_ | np.ndarray | None | Sorted 1D float64 array of unique class labels discovered during fit. |
n_features_in_ | int | None | Number of features seen during fit. |
feature_importances_ | np.ndarray | None | Normalized impurity-based feature importances of shape (n_features,). |
device_ | str | None | Resolved hardware device string used by the underlying Rust estimator. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> RandomForestClassifier | Fit the random forest classifier on training data with optional sample weights. |
predict | predict(X) -> np.ndarray | Predict class labels for samples in X. |
predict_proba | predict_proba(X) -> np.ndarray | Predict class probabilities of shape (n_samples, n_classes). |
score | score(X, y) -> float | Return mean classification accuracy on (X, y). |
save | save(path: str) -> None | Save the fitted forest to a binary .dit file. |
load | RandomForestClassifier.load(path: str, device: str | None = None) | Load a fitted classifier from a .dit file. |
to | to(device: str) -> RandomForestClassifier | Move the estimator compute target to device. |
set_device | set_device(device: str | None = None) -> RandomForestClassifier | Set or reset the compute device for the estimator. |
Example
import numpy as np
from ochreml.ensemble import RandomForestClassifier
rng = np.random.default_rng(42)
X_pos = rng.normal(loc=1.5, scale=0.6, size=(60, 4))
X_neg = rng.normal(loc=-1.5, scale=0.6, size=(60, 4))
X = np.vstack([X_neg, X_pos])
y = np.array([0.0] * 60 + [1.0] * 60)
clf = RandomForestClassifier(
n_estimators=50,
max_depth=6,
max_features="sqrt",
random_state=42,
device="auto",
)
clf.fit(X, y)
proba = clf.predict_proba(X[:4])
print("Accuracy:", clf.score(X, y))
print("Feature importances:", clf.feature_importances_)
print("Sample probabilities:\n", proba)