Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

DecisionTreeClassifier

Classifier sklearn-compatible 🌲 Tree-Based

Decision tree classifier — CART with Gini/Entropy criterion, binned splits. / Arbre de décision classifieur — CART avec critère Gini/Entropie, splits binnés.

⚡ Rust-native ✓ sklearn parity
Quick start — Python
import seraplot as sp, numpy as np
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
tree = sp.DecisionTreeClassifier(max_depth=4)
tree.fit(X, y)
print(f"Accuracy: {tree.score(X, y):.3f}")
💡
EN — Drop-in replacement: sp.DecisionTreeClassifier has the same API as sklearn.
FR — Remplacement direct : même API que sklearn, changez l'import.

API Reference

JSON function name

ml_decision_tree_classifier — aliases: decision_tree_classifier, dt_cls

Python class
sp.DecisionTreeClassifier(max_depth=∞, min_samples_split=2, min_samples_leaf=1, max_features=null, criterion=gini)
Constructor Parameters
ParameterTypeDefaultDescription
max_depthintMaximum tree depth.
min_samples_splitint2Minimum samples to split a node.
min_samples_leafint1Minimum samples in a leaf.
max_featuresint|strnullMax features per split (int or `sqrt`/`log2`).
criterionstrginiSplit criterion: `gini` or `entropy`.
Returns

JSON with predictions, feature_importances, classes.

Algorithm

$$\text{Gini}(t) = 1 - \sum_{k} p_k^2$$

Example
import seraplot as sp, numpy as np
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
tree = sp.DecisionTreeClassifier(max_depth=4)
tree.fit(X, y)
print(f"Accuracy: {tree.score(X, y):.3f}")

Référence API

Nom de fonction JSON

ml_decision_tree_classifier — alias : decision_tree_classifier, dt_cls

Classe Python
sp.DecisionTreeClassifier(max_depth=∞, min_samples_split=2, min_samples_leaf=1, max_features=null, criterion=gini)
Paramètres du constructeur
ParamètreTypeDéfautDescription
max_depthintProfondeur maximale de l'arbre.
min_samples_splitint2Minimum d'échantillons pour diviser un nœud.
min_samples_leafint1Minimum d'échantillons dans une feuille.
max_featuresint|strnullMax features par split (int ou `sqrt`/`log2`).
criterionstrginiCritère de split : `gini` ou `entropy`.
Retourne

JSON avec predictions, feature_importances, classes.

Algorithme

$$\text{Gini}(t) = 1 - \sum_{k} p_k^2$$

Exemple
import seraplot as sp, numpy as np
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
tree = sp.DecisionTreeClassifier(max_depth=4)
tree.fit(X, y)
print(f"Précision : {tree.score(X, y):.3f}")

DecisionTreeRegressor

Regressor sklearn-compatible 🌲 Tree-Based

Decision tree regressor — CART with MSE variance reduction, binned splits. / Arbre de décision régresseur — CART avec réduction de variance MSE, splits binnés.

⚡ Rust-native ✓ sklearn parity
Quick start — Python
import seraplot as sp, numpy as np
X = np.random.randn(400, 4)
y = X[:, 0] ** 2 + X[:, 1] - X[:, 2] + np.random.randn(400) * 0.5
tree = sp.DecisionTreeRegressor(max_depth=5)
tree.fit(X, y)
print(tree.score(X, y))
💡
EN — Drop-in replacement: sp.DecisionTreeRegressor has the same API as sklearn.
FR — Remplacement direct : même API que sklearn, changez l'import.

API Reference

JSON function name

ml_decision_tree_regressor — aliases: decision_tree_regressor, dt_reg

Python class
sp.DecisionTreeRegressor(max_depth=∞, min_samples_split=2, min_samples_leaf=1, max_features=null)
Constructor Parameters
ParameterTypeDefaultDescription
max_depthintMaximum tree depth.
min_samples_splitint2Minimum samples to split a node.
min_samples_leafint1Minimum samples in a leaf.
max_featuresint|strnullMax features per split.
Returns

JSON with predictions, feature_importances.

Algorithm

$$\text{MSE}(t) = \frac{1}{n_t}\sum_{i \in t}(y_i - \bar{y}_t)^2$$

Example
import seraplot as sp, numpy as np
X = np.random.randn(400, 4)
y = X[:, 0] ** 2 + X[:, 1] - X[:, 2] + np.random.randn(400) * 0.5
tree = sp.DecisionTreeRegressor(max_depth=5)
tree.fit(X, y)
print(tree.score(X, y))

Référence API

Nom de fonction JSON

ml_decision_tree_regressor — alias : decision_tree_regressor, dt_reg

Classe Python
sp.DecisionTreeRegressor(max_depth=∞, min_samples_split=2, min_samples_leaf=1, max_features=null)
Paramètres du constructeur
ParamètreTypeDéfautDescription
max_depthintProfondeur maximale de l'arbre.
min_samples_splitint2Minimum d'échantillons pour diviser un nœud.
min_samples_leafint1Minimum d'échantillons dans une feuille.
max_featuresint|strnullMax features par split.
Retourne

JSON avec predictions, feature_importances.

Algorithme

$$\text{MSE}(t) = \frac{1}{n_t}\sum_{i \in t}(y_i - \bar{y}_t)^2$$

Exemple
import seraplot as sp, numpy as np
X = np.random.randn(400, 4)
y = X[:, 0] ** 2 + X[:, 1] - X[:, 2] + np.random.randn(400) * 0.5
tree = sp.DecisionTreeRegressor(max_depth=5)
tree.fit(X, y)
print(tree.score(X, y))