Skip to Content
OchreML
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_state is set.
  • GPU N6 Histogram Support: Integrates with OchreML's N6 histogram infrastructure on CUDA and wgpu devices while falling back cleanly to Rayon CPU parallelism on CPU and TPU targets.
  • Zero-Copy Buffers: fit, predict, predict_proba, and score pass contiguous float64 buffers 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

ParameterTypeDefaultDescription
n_estimatorsint100Number of trees in the forest.
criterionstr"gini"Split quality criterion: "gini" or "entropy".
max_depthint | NoneNoneMaximum depth of each tree. None expands until stopping criteria are met.
min_samples_splitint2Minimum number of samples required to split an internal node.
min_samples_leafint1Minimum number of samples required at a leaf node.
max_featuresstr | 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.
bootstrapboolTrueWhether bootstrap samples are drawn when building trees.
random_stateint | NoneNoneSeed controlling bootstrapping and feature subsampling randomness.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
classes_np.ndarray | NoneSorted 1D float64 array of unique class labels discovered during fit.
n_features_in_int | NoneNumber of features seen during fit.
feature_importances_np.ndarray | NoneNormalized impurity-based feature importances of shape (n_features,).
device_str | NoneResolved hardware device string used by the underlying Rust estimator.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> RandomForestClassifierFit the random forest classifier on training data with optional sample weights.
predictpredict(X) -> np.ndarrayPredict class labels for samples in X.
predict_probapredict_proba(X) -> np.ndarrayPredict class probabilities of shape (n_samples, n_classes).
scorescore(X, y) -> floatReturn mean classification accuracy on (X, y).
savesave(path: str) -> NoneSave the fitted forest to a binary .dit file.
loadRandomForestClassifier.load(path: str, device: str | None = None)Load a fitted classifier from a .dit file.
toto(device: str) -> RandomForestClassifierMove the estimator compute target to device.
set_deviceset_device(device: str | None = None) -> RandomForestClassifierSet 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)

On this page