RandomForestRegressor
Module: strata.ensemble • Kind: struct • Traits: Copyable, Movable, Regressor, Serializable
Source: strata/ensemble/forest.mojo
struct RandomForestRegressor[compute_dtype: DType = DType.float64](Copyable, Movable, Regressor, Serializable)from strata.ensemble import RandomForestRegressorRandom Forest Regressor ensemble estimator.
An ensemble of decision trees trained via bootstrap aggregation (bagging). Predictions are computed as the arithmetic mean of individual tree predictions.
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Computational precision data type. Default DType.float64. |
Constructors
Section titled “Constructors”def __init__( out self, n_estimators: Int = 100, criterion: String = "squared_error", max_depth: Int = -1, min_samples_split: Int = 2, min_samples_leaf: Int = 1, min_impurity_decrease: Float64 = 0.0, max_features: String = "sqrt", max_features_count: Int = -1, max_features_ratio: Float64 = 0.0, bootstrap: Bool = True, max_samples_ratio: Float64 = 1.0, max_samples_count: Int = -1, oob_score: Bool = False, random_state: Int = 42,)| Argument | Type | Description |
|---|---|---|
n_estimators |
Int |
Number of trees in the forest. Default 100. |
criterion |
String |
Impurity split criterion (‘squared_error’, ‘friedman_mse’, ‘absolute_error’). Default ‘squared_error’. |
max_depth |
Int |
Maximum tree depth. -1 means unlimited. Default -1. |
min_samples_split |
Int |
Minimum samples required to split an internal node. Default 2. |
min_samples_leaf |
Int |
Minimum samples required to be a leaf node. Default 1. |
min_impurity_decrease |
Float64 |
Split threshold if impurity decrease >= this value. Default 0.0. |
max_features |
String |
Number of features to consider per split (‘all’, ‘sqrt’, ‘log2’). Default ‘sqrt’. |
max_features_count |
Int |
Exact number of features per split. Default -1 (disabled). |
max_features_ratio |
Float64 |
Proportion of features per split. Default 0.0 (disabled). |
bootstrap |
Bool |
Whether to use bootstrap sampling. Default True. |
max_samples_ratio |
Float64 |
Proportion of samples drawn per tree when bootstrap=True. Default 1.0. |
max_samples_count |
Int |
Exact number of samples drawn per tree. Default -1 (disabled). |
oob_score |
Bool |
Whether to compute out-of-bag score after fitting. Default False. |
random_state |
Int |
PRNG seed for deterministic tree builds. Default 42. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
n_features_in_ |
Number of features seen during fit. |
feature_importances_ |
Normalized impurity feature importance vector. |
oob_score_ |
Out-of-bag score (available when oob_score=True). |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
RandomForestRegressor.fit() |
Fits the random forest on (X, y). |
RandomForestRegressor.predict() |
Predicts regression targets as the arithmetic mean across all tree predictions. |
RandomForestRegressor.get_n_estimators() |
Returns the number of fitted trees. |
RandomForestRegressor.get_feature_importances() |
Returns normalized MDI feature importances (sums to 1.0). |
RandomForestRegressor.get_oob_score() |
Returns out-of-bag R² score. Requires oob_score=True and bootstrap=True. |
RandomForestRegressor.serialize() |
Serializes RandomForestRegressor parameters and fitted state into BufferWriter. |
RandomForestRegressor.deserialize() |
Deserializes RandomForestRegressor from BufferReader. |
Method Details
Section titled “Method Details”RandomForestRegressor.fit()
Section titled “RandomForestRegressor.fit()”def fit[feat_dtype: DType, target_dtype: DType]( mut self, X: Matrix[feat_dtype], y: List[Scalar[target_dtype]],)
def fit[feat_dtype: DType, target_dtype: DType]( mut self, dataset: Dataset[feat_dtype, target_dtype],)Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Fits the random forest on (X, y).
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix. |
y |
List[Scalar[target_dtype]] |
Target vector / class labels. |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
RandomForestRegressor.predict()
Section titled “RandomForestRegressor.predict()”def predict[feat_dtype: DType]( self, X: Matrix[feat_dtype],) -> List[Scalar[feat_dtype]]
def predict[feat_dtype: DType, target_dtype: DType]( self, dataset: Dataset[feat_dtype, target_dtype],) -> List[Scalar[feat_dtype]]Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Predicts regression targets as the arithmetic mean across all tree predictions.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix. |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
Returns: List[Scalar[feat_dtype]]
RandomForestRegressor.get_n_estimators()
Section titled “RandomForestRegressor.get_n_estimators()”def get_n_estimators(self) -> IntReturns the number of fitted trees.
Returns: Int
RandomForestRegressor.get_feature_importances()
Section titled “RandomForestRegressor.get_feature_importances()”def get_feature_importances(self) -> List[Float64]Returns normalized MDI feature importances (sums to 1.0).
Returns: List[Float64]
RandomForestRegressor.get_oob_score()
Section titled “RandomForestRegressor.get_oob_score()”def get_oob_score(self) -> Float64Returns out-of-bag R² score. Requires oob_score=True and bootstrap=True.
Returns: Float64
RandomForestRegressor.serialize()
Section titled “RandomForestRegressor.serialize()”def serialize(self, mut writer: BufferWriter)Serializes RandomForestRegressor parameters and fitted state into BufferWriter.
RandomForestRegressor.deserialize()
Section titled “RandomForestRegressor.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes RandomForestRegressor from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.ensemble import RandomForestRegressorfrom strata.core import Matrix
var rf = RandomForestRegressor[DType.float64](n_estimators=50, max_depth=6)rf.fit(X_train, y_train)var preds = rf.predict(X_test)