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
N6Accelerator-First Path: On CUDA andwgpudevices,RandomForestRegressoruses theN6quantized 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, andscoreconsume contiguousfloat64buffers 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
| Parameter | Type | Default | Description |
|---|---|---|---|
n_estimators | int | 100 | Number of regression trees in the forest. |
criterion | str | "squared_error" | Split quality criterion ("squared_error"). |
max_depth | int | None | None | Maximum depth of each tree. |
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 | 1.0 | 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 used 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 |
|---|---|---|
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) -> RandomForestRegressor | Fit the random forest regressor on training data with optional sample weights. |
predict | predict(X) -> np.ndarray | Predict continuous target values for samples in X. |
score | score(X, y) -> float | Return the coefficient of determination R^2 on (X, y). |
save | save(path: str) -> None | Save the fitted forest to a binary .dit file. |
load | RandomForestRegressor.load(path: str, device: str | None = None) | Load a fitted regressor from a .dit file. |
to | to(device: str) -> RandomForestRegressor | Move the estimator compute target to device. |
set_device | set_device(device: str | None = None) -> RandomForestRegressor | Set 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)