Skip to content

DecisionTreeRegressor

Module: strata.tree • Kind: struct • Traits: Copyable, Movable, Regressor, Serializable
Source: strata/tree/regressor.mojo

struct DecisionTreeRegressor[compute_dtype: DType = DType.float64](Copyable, Movable, Regressor, Serializable)
from strata.tree import DecisionTreeRegressor

Decision Tree Regressor for non-parametric continuous target regression.

Builds a regression tree by minimizing sample variance (mean squared error) or mean absolute deviation across recursive binary splits:

HMSE(Q)=1∣Q∣∑i∈Q(yi−yˉQ)2H_{\text{MSE}}(Q) = \frac{1}{|Q|} \sum_{i \in Q} (y_i - \bar{y}_Q)^2
Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

def __init__(
out self,
criterion: String = "squared_error",
splitter: String = "best",
max_depth: Int = -1,
min_samples_split: Int = 2,
min_samples_leaf: Int = 1,
min_impurity_decrease: Float64 = 0.0,
max_features: String = "all",
max_features_count: Int = -1,
max_features_ratio: Float64 = 0.0,
random_state: Int = 42,
)
Argument Type Description
criterion String The function to measure split quality (‘squared_error’, ‘friedman_mse’, ‘absolute_error’). Default ‘squared_error’.
splitter String Strategy used to choose the split at each node (‘best’, ‘random’). Default ‘best’.
max_depth Int Maximum tree depth. -1 indicates unlimited depth. 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 at 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 when looking for best split (‘all’, ‘sqrt’, ‘log2’, ‘custom’). Default ‘all’.
max_features_count Int Explicit number of features to evaluate when max_features=‘custom’. Default -1.
max_features_ratio Float64 Proportion of features to evaluate when max_features=‘custom’. Default 0.0.
random_state Int PRNG seed for deterministic feature and split selection. Default 42.

Attribute Description
n_features_in_ Number of features seen during fit.
is_fitted Boolean flag indicating if estimator has been fitted.

Method Description
DecisionTreeRegressor.fit() Fits the decision tree regressor on (X, y).
DecisionTreeRegressor.predict() Generates regression predictions for input matrix X.
DecisionTreeRegressor.get_depth() Returns the maximum depth of the fitted tree.
DecisionTreeRegressor.get_n_leaves() Returns the total number of leaf nodes in the fitted tree.
DecisionTreeRegressor.serialize() Serializes DecisionTreeRegressor parameters and fitted state into BufferWriter.
DecisionTreeRegressor.deserialize() Deserializes DecisionTreeRegressor from BufferReader.

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 unified dataset: Dataset container.

Fits the decision tree regressor 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))

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 unified dataset: Dataset container.

Generates regression predictions for input matrix X.

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]]


def get_depth(self) -> Int

Returns the maximum depth of the fitted tree.

Returns: Int


def get_n_leaves(self) -> Int

Returns the total number of leaf nodes in the fitted tree.

Returns: Int


def serialize(self, mut writer: BufferWriter)

Serializes DecisionTreeRegressor parameters and fitted state into BufferWriter.


def deserialize(mut reader: BufferReader) -> Self

Deserializes DecisionTreeRegressor from BufferReader.

from strata.tree import DecisionTreeRegressor
from strata.core import Matrix
var reg = DecisionTreeRegressor[DType.float64](max_depth=4)
reg.fit(X_train, y_train)
var preds = reg.predict(X_test)