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 DecisionTreeRegressorDecision 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:
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, 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. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
n_features_in_ |
Number of features seen during fit. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| 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. |
Method Details
Section titled “Method Details”DecisionTreeRegressor.fit()
Section titled “DecisionTreeRegressor.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 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)) |
DecisionTreeRegressor.predict()
Section titled “DecisionTreeRegressor.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.
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]]
DecisionTreeRegressor.get_depth()
Section titled “DecisionTreeRegressor.get_depth()”def get_depth(self) -> IntReturns the maximum depth of the fitted tree.
Returns: Int
DecisionTreeRegressor.get_n_leaves()
Section titled “DecisionTreeRegressor.get_n_leaves()”def get_n_leaves(self) -> IntReturns the total number of leaf nodes in the fitted tree.
Returns: Int
DecisionTreeRegressor.serialize()
Section titled “DecisionTreeRegressor.serialize()”def serialize(self, mut writer: BufferWriter)Serializes DecisionTreeRegressor parameters and fitted state into BufferWriter.
DecisionTreeRegressor.deserialize()
Section titled “DecisionTreeRegressor.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes DecisionTreeRegressor from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.tree import DecisionTreeRegressorfrom strata.core import Matrix
var reg = DecisionTreeRegressor[DType.float64](max_depth=4)reg.fit(X_train, y_train)var preds = reg.predict(X_test)