Skip to main content

DecisionTreeClassifier

#include <Skigen/Tree>

template <typename Scalar = double>
class Skigen::DecisionTreeClassifier(max_depth=-1, min_samples_split=2)

A decision tree classifier.

A non-parametric supervised learning method used for classification. The model predicts the value of a target variable by learning simple decision rules inferred from the data features. Uses Gini impurity as the splitting criterion.

Mirrors sklearn.tree.DecisionTreeClassifier.


Parameters:

  • max_depth : int, default=-1 Maximum depth (int, default -1 for unlimited).

  • min_samples_split : int, default=2 Minimum samples to split (int, default 2).


Attributes:

  • is_fitted : bool Whether the estimator has been fitted.

Methods

fit(X, y)

Build a decision tree classifier from the training set.

Parameters:

  • X : MatrixType Training data of shape (n_samples, n_features).

  • y : IndexVector Target values of shape (n_samples,) with integer class labels.

Returns:

  • result : DecisionTreeClassifier Reference to the fitted estimator (*this).

Throws:

  • std::invalid_argument — if X and y have inconsistent lengths.

predict(X)

Predict class labels for samples in X.

Parameters:

  • X : MatrixType Sample matrix of shape (n_samples, n_features).

Returns:

  • result : IndexVector Integer vector of predicted class labels (n_samples,).

Throws:

  • std::runtime_error — if the model has not been fitted.

score(X, y)

Return the mean accuracy on the given test data and labels.

Parameters:

  • X : MatrixType Test samples of shape (n_samples, n_features).

  • y : IndexVector True class labels of shape (n_samples,).

Returns:

  • result : Scalar Mean accuracy.

Example

// Best model
Skigen::DecisionTreeClassifier<double> best(5);
best.fit(split.X_train, split.y_train);
auto best_pred = best.predict(split.X_test);

std::cout << "\n=== Confusion Matrix (depth=5) ===\n";
auto cm = Skigen::Metrics::confusion_matrix(split.y_test, best_pred);
std::cout << cm << "\n";