DecisionTreeRegressor
#include <Skigen/Tree>
template <typename Scalar = double>
class Skigen::DecisionTreeRegressor(max_depth=-1, min_samples_split=2)
A decision tree regressor.
Uses MSE (Mean Squared Error) reduction as the splitting criterion. The prediction for a leaf node is the mean of the target values.
Mirrors sklearn.tree.DecisionTreeRegressor.
Parameters:
-
max_depth : int, default=-1 Maximum depth (
int, default-1for unlimited). -
min_samples_split : int, default=2 Minimum samples to split (
int, default2).
Attributes:
- is_fitted : bool Whether the estimator has been fitted.
Methods
fit(X, y)
Build a decision tree regressor from the training set.
Parameters:
-
X : MatrixType Training data of shape (n_samples, n_features).
-
y : VectorType Target values of shape (n_samples,).
Returns:
- result : DecisionTreeRegressor
Reference to the fitted estimator (
*this).
Throws:
std::invalid_argument— if X and y have inconsistent lengths.
predict(X)
Predict target values for X.
Parameters:
- X : MatrixType Sample matrix of shape (n_samples, n_features).
Returns:
- result : VectorType Predicted values of shape (n_samples,).
Throws:
std::runtime_error— if the model has not been fitted.
score(X, y)
Return the coefficient of determination.
Parameters:
-
X : MatrixType Test samples of shape (n_samples, n_features).
-
y : VectorType True values of shape (n_samples,).
Returns:
- result : Scalar score.
Example
// Decision tree for regression
Eigen::VectorXd y_reg(split.X_train.rows());
for (Eigen::Index i = 0; i < y_reg.size(); ++i)
y_reg(i) = split.X_train(i, 0) * split.X_train(i, 0) + noise(rng);
Skigen::DecisionTreeRegressor<double> tree_reg(5);
tree_reg.fit(split.X_train, y_reg);
Eigen::VectorXd y_reg_test(split.X_test.rows());
for (Eigen::Index i = 0; i < y_reg_test.size(); ++i)
y_reg_test(i) = split.X_test(i, 0) * split.X_test(i, 0);
std::cout << "\n=== DecisionTreeRegressor (depth=5) ===\n";
std::cout << "R²: " << tree_reg.score(split.X_test, y_reg_test) << "\n";