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 DecisionTreeClassifierDecision Tree Classifier for non-parametric supervised classification.
Splits internal nodes to maximize impurity reduction based on Gini impurity or Shannon entropy:
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 = "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. |
Attributes
Section titled “Attributes”| 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. |
Methods Overview
Section titled “Methods Overview”| 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. |
Method Details
Section titled “Method Details”DecisionTreeClassifier.fit()
Section titled “DecisionTreeClassifier.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 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)) |
DecisionTreeClassifier.predict()
Section titled “DecisionTreeClassifier.predict()”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 unifieddataset: Datasetcontainer.
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]
DecisionTreeClassifier.predict_proba()
Section titled “DecisionTreeClassifier.predict_proba()”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 unifieddataset: Datasetcontainer.
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]
DecisionTreeClassifier.get_depth()
Section titled “DecisionTreeClassifier.get_depth()”def get_depth(self) -> IntReturns the maximum depth of the fitted tree.
Returns: Int
DecisionTreeClassifier.get_n_leaves()
Section titled “DecisionTreeClassifier.get_n_leaves()”def get_n_leaves(self) -> IntReturns the total number of leaf nodes in the fitted tree.
Returns: Int
DecisionTreeClassifier.serialize()
Section titled “DecisionTreeClassifier.serialize()”def serialize(self, mut writer: BufferWriter)Serializes DecisionTreeClassifier parameters and fitted state into BufferWriter.
DecisionTreeClassifier.deserialize()
Section titled “DecisionTreeClassifier.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes DecisionTreeClassifier from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.tree import DecisionTreeClassifierfrom 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)