Skip to content

DecisionTreeClassifier

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

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

Decision Tree Classifier for non-parametric supervised classification.

Splits internal nodes to maximize impurity reduction based on Gini impurity or Shannon entropy:

Hgini(Q)=1−∑k=1Kpk2,Hentropy(Q)=−∑k=1Kpklog⁡2(pk)H_{\text{gini}}(Q) = 1 - \sum_{k=1}^{K} p_k^2, \quad H_{\text{entropy}}(Q) = -\sum_{k=1}^{K} p_k \log_2(p_k)
Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

def __init__(
out self,
criterion: String = "gini",
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 the quality of a split (‘gini’, ‘entropy’, ‘log_loss’). Default ‘gini’.
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
classes_ Sorted list of unique class labels observed during fit.
n_classes_ Number of unique classes observed.
n_features_in_ Number of features seen during fit.
is_fitted Boolean flag indicating if estimator has been fitted.

Method Description
DecisionTreeClassifier.fit() Fits the decision tree classifier on (X, y).
DecisionTreeClassifier.predict() Generates discrete class predictions for input matrix X.
DecisionTreeClassifier.predict_proba() Generates class probability estimates for input matrix X.
DecisionTreeClassifier.get_depth() Returns the maximum depth of the fitted tree.
DecisionTreeClassifier.get_n_leaves() Returns the total number of leaf nodes in the fitted tree.
DecisionTreeClassifier.serialize() Serializes DecisionTreeClassifier parameters and fitted state into BufferWriter.
DecisionTreeClassifier.deserialize() Deserializes DecisionTreeClassifier 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 classifier 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[Int]
def predict[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> List[Int]

Overload Note: This method accepts either standard (X, y) inputs or a unified dataset: Dataset container.

Generates discrete class 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[Int]


def predict_proba[feat_dtype: DType](
self,
X: Matrix[feat_dtype],
) -> Matrix[feat_dtype]
def predict_proba[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> Matrix[feat_dtype]

Overload Note: This method accepts either standard (X, y) inputs or a unified dataset: Dataset container.

Generates class probability estimates 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: Matrix[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 DecisionTreeClassifier parameters and fitted state into BufferWriter.


def deserialize(mut reader: BufferReader) -> Self

Deserializes DecisionTreeClassifier from BufferReader.

from strata.tree import DecisionTreeClassifier
from strata.core import Matrix
var tree = DecisionTreeClassifier[DType.float64](max_depth=5, criterion="gini")
tree.fit(X_train, y_train)
var preds = tree.predict(X_test)