Skip to Content
OchreML
Trees, Ensembles & Boosting

RandomForestRegressor

Parallel bagged regression tree ensemble with N6 GPU histogram acceleration on CUDA/wgpu and Rayon parallelism on CPU/TPU.

RandomForestRegressor trains an ensemble of regression trees on bootstrap samples of the training data and averages their predictions to reduce variance while preserving non-linear expressiveness. (^▽^)

Backend & Hardware Acceleration

  • GPU N6 Accelerator-First Path: On CUDA and wgpu devices, RandomForestRegressor uses the N6 quantized histogram regression tree builder with sibling histogram subtraction to accelerate split finding across features.
  • CPU Rayon Fallback: On CPU and TPU devices (or small datasets below GPU thresholds), trees are constructed in parallel across CPU cores via Rust Rayon.
  • Zero-Copy Buffers: fit, predict, and score consume contiguous float64 buffers directly without Python-side conversion overhead.

Import & Signature

from ochreml.ensemble import RandomForestRegressor
# or: from ochreml import RandomForestRegressor

RandomForestRegressor(
    n_estimators: int = 100,
    criterion: str = "squared_error",
    max_depth: Optional[int] = None,
    min_samples_split: int = 2,
    min_samples_leaf: int = 1,
    max_features: Union[str, int, float, None] = 1.0,
    bootstrap: bool = True,
    random_state: Optional[int] = None,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
n_estimatorsint100Number of regression trees in the forest.
criterionstr"squared_error"Split quality criterion ("squared_error").
max_depthint | NoneNoneMaximum depth of each tree.
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 | None1.0Number of features considered at each split: "sqrt", "log2", "all", positive int, fraction float in (0.0, 1.0], or None.
bootstrapboolTrueWhether bootstrap samples are used 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
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) -> RandomForestRegressorFit the random forest regressor on training data with optional sample weights.
predictpredict(X) -> np.ndarrayPredict continuous target values for samples in X.
scorescore(X, y) -> floatReturn the coefficient of determination R^2 on (X, y).
savesave(path: str) -> NoneSave the fitted forest to a binary .dit file.
loadRandomForestRegressor.load(path: str, device: str | None = None)Load a fitted regressor from a .dit file.
toto(device: str) -> RandomForestRegressorMove the estimator compute target to device.
set_deviceset_device(device: str | None = None) -> RandomForestRegressorSet or reset the compute device for the estimator.

Example

import numpy as np
from ochreml.ensemble import RandomForestRegressor

rng = np.random.default_rng(7)
X = rng.uniform(-3.0, 3.0, size=(120, 3))
y = np.sin(X[:, 0]) + 0.5 * X[:, 1] ** 2 - 0.3 * X[:, 2]

reg = RandomForestRegressor(
    n_estimators=50,
    max_depth=8,
    random_state=7,
    device="auto",
)
reg.fit(X, y)

preds = reg.predict(X[:5])
print("R^2 score:", reg.score(X, y))
print("Feature importances:", reg.feature_importances_)
print("Sample predictions:", preds)

On this page