Understand the idea
A decision tree works exactly like a flowchart: is flipper length under 207mm? If yes, go left; if no, go right. Keep asking questions like this, each one splitting the data into two more similar groups, until you reach a leaf: a final answer based on whichever training rows ended up there. The whole model is something you can print and read, question by question, which is its biggest selling point over more complex methods.
Here is the trap every tree falls into if you let it: grown without limits, a tree keeps asking questions until each leaf holds almost none of the original data, effectively MEMORIZING the training examples rather than learning a general pattern. A tree like that will look almost perfectly accurate on the data it was trained on and can still be much worse than a simpler tree on new data it has never seen. That is exactly why this card reports cross-validated accuracy (tested on held-out folds) as its headline number, with the in-sample accuracy shown only as a cautionary side note: on one real dataset, a fully grown tree looked nearly perfect in-sample but was actually WORSE on new data than a much smaller, pruned tree.
'Feature importance' (which predictors the tree leans on most) sounds like it should answer 'what causes the outcome', but it does not. A tree can lean heavily on a predictor purely because that predictor happened to be useful for memorizing quirks of the training data, which is a completely different thing from that predictor genuinely driving the outcome in the real world.
A decision tree of this kind is a CART model, short for classification and regression tree, built by recursive partitioning: splitting the data again and again on the question that best separates the outcome. Cost-complexity pruning then cuts back branches that add little, to stop the tree from memorizing noise.
Practitioner explanation
KernelStats fits scikit-learn's CART: regression splits minimize residual sum of squares; classification splits minimize the Gini index. Every split is chosen greedily (best split right now, no lookahead), so the resulting tree is good but not guaranteed globally optimal. Cost-complexity pruning (the pruning parameter alpha) is the principled control, penalizing the number of leaves directly; max_depth and min_samples_leaf are blunter pre-pruning controls, and all of them should be chosen by the cross-validated score, never the in-sample one; the card demonstrates this concretely with its own worked comparison (a fully grown tree's in-sample RMSE of ~69g on body mass versus its cross-validated RMSE of ~494g, against a pruned depth-3 tree's cross-validated ~388g: the smaller tree is genuinely better, and only cross-validation reveals that).
Two DIFFERENT feature-importance measures are reported, and they can disagree: impurity-based importance (free, computed while fitting, but biased toward high-cardinality predictors: a column with many possible split points gets more chances to look useful even as pure noise) and permutation importance (shuffle one predictor and measure the resulting drop in score). Critically, KernelStats computes permutation importance OUT OF SAMPLE: inside each cross-validation fold, the tree is refit on the training part and predictors are permuted only on the held-out part, then folds are averaged; permuting on the SAME rows the tree memorized would measure how hard the tree leaned on a column to memorize training data, not whether that column predicts anything new. The training-set version remains available separately (permutation importance on the training data) specifically so the size of that gap is visible, not hidden.
Partial dependence plots for the top predictors show the tree's fitted STAIRCASE shape (never a smooth curve, because a tree only asks threshold questions); this describes what the fitted model does, not a causal effect, and can mislead when predictors are correlated (varying one while holding a correlated partner fixed visits combinations of values the real data never contained). When a tree is pruned back to its root and uses no predictor at all, every importance is exactly 0 and the card names no 'top predictor' rather than reporting an arbitrary tie-break as if it meant something.
When to use it
Consider it when
You want to predict a category or a number from a mix of numeric and categorical predictors and specifically need a model a human can read and explain: trees handle non-linear relationships and interactions automatically and need no predictor scaling.
Use caution when
Maximizing predictive accuracy matters more than interpretability (a random forest or gradient boosting ensemble will generally out-predict a single tree), the predictors are highly correlated (importance can arbitrarily split credit between correlated twins), or the training sample is small relative to the number of predictors (a single tree is high-variance and can look very different on a slightly different sample).
Formulas and symbols
Regression split criterion
The split minimizing the combined residual sum of squares across the two resulting regions R1, R2.
Gini impurity (classification split criterion)
p-hat_mk the share of class k in node m; 0 when a node holds a single class, largest when classes are evenly mixed.
Cost-complexity pruning objective
|T| the number of leaves; alpha the price per leaf. alpha=0 keeps the full tree; larger alpha trades training fit for simplicity.
Assumptions and checks
- A fully-grown tree (no depth limit, no pruning) memorizes the training data; its in-sample accuracy or RMSE overstates how well it will predict new rows, sometimes dramatically, and the gap between in-sample and cross-validated performance is the direct evidence of how much.
In KernelStats: Checked directly: the card flags an unconstrained, fully-grown tree as an 'Overfitting control' warning and separately reports the actual train-vs-cross-validated performance gap as its own check; every headline metric shown is itself k-fold cross-validated, with in-sample numbers shown only alongside it to expose the optimism gap.
- With too few rows per encoded feature (including one-hot-encoded categorical levels), the tree's split choices and feature importances become unstable: a slightly different sample would grow a visibly different tree.
In KernelStats: Checked directly: the card reports the rows-per-encoded-feature ratio and flags it as a warning below 10:1.
- With a severely imbalanced classification target, a tree (like any classifier) can reach high accuracy by mostly predicting the majority class, so accuracy alone overstates how useful the model is for the minority class.
In KernelStats: Checked directly for classification trees: the card reports the smallest class's share of rows and flags it as a warning below 10%, though it does not automatically switch to a rebalancing strategy or a different metric (e.g. balanced accuracy, F1).
Alternatives
- Random Forest. Predictive accuracy matters more than a single readable flowchart: averaging many decorrelated trees trades interpretability for stability and accuracy.
- Gradient Boosting (Trees). You want the strongest predictive accuracy from a tree-based ensemble and are willing to give up the single-flowchart interpretability entirely.
- Logistic Regression. A linear, coefficient-based model with well-understood inferential properties (p-values, confidence intervals on effects) is preferred over a nonparametric flowchart.
Worked example
Scenario
Predicting penguin body mass from bill and flipper measurements, comparing a fully grown tree against a cost-complexity-pruned depth-3 tree.
Result
The fully grown tree: in-sample RMSE ~69g, cross-validated RMSE ~494g. The pruned tree: cross-validated RMSE ~388g (its in-sample RMSE is higher than the full tree's, but its cross-validated RMSE is lower).
Interpretation
The pruned, visibly simpler tree is the genuinely better model for predicting NEW penguins, even though it fits the training data less perfectly, a direct demonstration that in-sample fit and real predictive performance can point in opposite directions, and only cross-validation reveals which tree to trust.
Dataset: penguins.csv
Common misconceptions
Misconception
A tree with near-100% in-sample accuracy is an excellent model.
Correction
A tree grown without limits can memorize its training data almost perfectly, which is exactly why in-sample accuracy near 100% is common and MEANINGLESS as a measure of real predictive quality. Cross-validated accuracy (performance on rows the tree never saw during fitting) is the number that actually reflects how the tree will perform on new data, and it is frequently much worse than the in-sample figure for an unpruned tree.
Read more: Misconception: a model's training-set accuracy is its real-world performance
Misconception
A predictor with high feature importance is a cause of the outcome.
Correction
Feature importance measures how much the tree RELIES ON a predictor to make its predictions: it says nothing about causation. Correlated predictors can arbitrarily swap which one the tree happens to use, and no split accounts for confounding. Treat importance as 'what the model leans on', never as evidence for 'what drives the outcome in the real world'.
References
- An Introduction to Statistical Learning with Applications in R (2nd ed.), Chapter 8, James, G., Witten, D., Hastie, T. and Tibshirani, R. (2021).
- Bias in random forest variable importance measures: Illustrations, sources and a solution, Strobl, C., Boulesteix, A.-L., Zeileis, A. and Hothorn, T. (2007), BMC Bioinformatics, 8, 25.
