Unit 1 of 8 · about 5 hours · derive, implement

CART: split objectives, impurity, stopping and pruning

Trees decide by recursive splits, so every later ensemble inherits their split logic, their variance and their failure modes. If you cannot score a split by hand, forest and boosting diagnostics stay magic.

You'll be able to

  • Compute weighted Gini, entropy and variance reduction for a candidate split by hand and name the winning split.
  • Implement a CART-style best-split scan with a stated tie rule, stopping conditions and leaf predictions, and differential-test it against sklearn on small data.
  • Predict how max_depth, min_samples_split and ccp_alpha change bias, variance and tree size.

Covers

CART classification and regression split objectivesGini, entropy, information gain and variance reductionStopping, pruning and deterministic split ties
1

Read

In this order, only the parts listed

0 of 4 done

  1. HoML Chapter 5: growing and regularizing treesHands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical models

    Book Chapter 5, from Training and Visualizing a Decision Tree through Decision Trees Have a High Variance (printed pp. 179-193). Notebook 05 cells: Training and Visualizing a Decision Tree, Making Predictions, Estimating Class Probabilities, Regularization Hyperparameters, Regression, Sensitivity to axis orientation, Decision Trees Have High Variance

  2. sklearn decision trees: split search, criteria and complexityscikit-learn - neighbors, SVMs, trees and ensembles

    1.10.7 Mathematical formulation with 1.10.7.1 Classification criteria and 1.10.7.2 Regression criteria; 1.10.4 Complexity for fit and predict cost

  3. sklearn decision trees: CART scope, tips and pruningscikit-learn - neighbors, SVMs, trees and ensembles

    1.10.6 CART versus ID3 and C4.5 (binary splits, no categorical support); 1.10.5 Tips on practical use; 1.10.9 Minimal Cost-Complexity Pruning with the weakest-link rule and ccp_alpha

  4. ISLP Chapter 8: regression and classification treesISLP - tree-based methods and support vector machines

    Sections 8.1.1 and 8.1.2 (pp. 331 to 341): recursive binary splitting, Gini versus entropy similarity, and classification error reserved for pruning

2

Focus on

  • Split quality is the weighted child impurity . The parent impurity is the same for every candidate at a node, so minimizing is the same as maximizing the impurity decrease.
  • sklearn tries midpoints between sorted distinct values with greedy exhaustive search, while Deep-ML 138 scores the raw values: the same four points yield threshold 3.0 under sklearn and 2.5 in problem 138.
  • Gini and entropy are numerically close and either works for growing; ISLP reserves the classification error rate for pruning toward accuracy.
  • ccp_alpha penalizes leaf count and prunes the weakest link first, which produces the nested candidate path that cross-validation then selects from.
Common mistakes · 3
  • Equal impurity does not mean a unique split. With two identical feature columns, sklearn picks feature 1 under seeds 0 and 1 but feature 0 under seed 42, so your scanner must define first-wins and test it.
  • A child can be less pure than its parent: the A, B child has Gini 0.5 against the parent 0.32. Only the weighted sum has to decrease.
  • HoML Chapter 5 never sets criterion, so every tree there is Gini. Request entropy explicitly, and remember the 138-versus-sklearn threshold convention before comparing answers.
3

Try it yourself

Closed-book, before checking anything

By hand on ordered labels A, B, A, A, A: parent Gini 0.32, split after the 2nd instance gives , split after the 1st gives 0.3. Then implement a midpoint scan with first-in-scan tie wins and match sklearn threshold 1.5 on X = [0, 1, 2, 3] with y = [0, 0, 1, 1] at max_depth = 1.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A node holds 3 positives and 1 negative. What are its Gini impurity and its entropy in bits?Answer

    . bits.

  2. 2.Why does ISLP call the error rate weak for growing but prefer it for pruning?Answer

    Growing needs sensitivity to purity shifts that leave the majority label unchanged. Pruning selects the final tree for accuracy, which is exactly what the error rate measures.

  3. 3.The same data and code give different trees under a new random_state. Is that a bug?Answer

    No. Tied splits can resolve differently across seeds, as the identical-feature test shows. Fix the seed for reproducibility and define your own deterministic tie rule.

  4. 4.One leaf holds one vote for each of three classes. What does sklearn predict and with what probabilities?Answer

    It predicts classes_[0], the lowest index, with probabilities . Argmax takes the first maximum.

5

Practice

  • Deep-ML 138: Find the Best Gini-Based Split for a Binary Decision Tree

    Implement find_best_split(X, y); the visible tests check (0, 2.5) on the 4-point split and (0, 1) on the all-pure tie

    The visible tests pin thresholds to sorted unique values (2.5, not the sklearn midpoint 3.0) with first minimum in feature-then-value order winning. Implement that convention, not sklearn midpoints.

  • Deep-ML 20: Decision Tree Learning

    Implement learn_decision_tree(examples, attributes, target_attr); the visible tests check the two-attribute PlayTennis tree and a single-attribute tree

    Splits use entropy with information gain, values in sorted order, first attribute on ties and alphabetical leaf on ties.

You're done when

Your hand table gives 0.32, 0.2 and 0.3, your scanner matches sklearn on two small sets, and both Deep-ML problems pass.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 5 more in the section
  • Hands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical modelsAurelien Geron (O'Reilly, 2025) · primary

    Read Chapters 3, 5 and 6 and the online Appendix C notebook as the first pass through performance measures, trees, ensembles and SVMs, running notebooks 03, 05 and 06 alongside. Vary depth, estimators, learning rate, k, C and RBF width under fixed splits.

  • scikit-learn - neighbors, SVMs, trees and ensemblesscikit-learn · reference

    Check official estimator constraints, complexity, preprocessing needs, missing-value behavior and probability caveats. Do not use library calls as the mechanism implementation.

  • ISLP - tree-based methods and support vector machinesJames, Witten, Hastie, Tibshirani and Taylor · required

    Use Chapters 8 and 9 with their Python labs for the statistical account of trees, bagging, random forests, boosting and SVMs that the notebooks leave implicit, and for model-choice and feature-importance arguments.

Also in this section

  • Deep-ML - tree, ensemble and kernel problemsDeep-ML · support

    Use 138 Best Gini-Based Split, 20 Decision Tree Learning, 307 Bagging Classifier, 21 Pegasos Kernel SVM and 280 RBF Kernel as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin, including tie and update-order conventions.

  • Probabilistic Machine Learning, Chapter 16: exemplar-based methodsKevin Murphy · support

    Read Sections 16.1 to 16.1.3 for the KNN decision rule, the curse of dimensionality with the growing-hypercube example, and the speed section on k-d trees, hashing and approximate search.

  • XGBoost parameters and sklearn estimator interfaceXGBoost developers · reference

    Read the tree-booster parameter list for eta, depth, sampling and regularization names and defaults, and the sklearn page for early stopping with eval_set.

  • LightGBM parametersLightGBM developers · reference

    Read learning_rate, num_leaves, max_depth, min_data_in_leaf, feature_fraction, bagging_fraction with bagging_freq, lambda_l2 and early stopping defaults.

  • CatBoost common parameters and categorical featuresCatBoost developers · reference

    Read iterations, learning_rate, l2_leaf_reg, depth, grow_policy, subsample, rsm, boosting_type, use_best_model and one_hot_max_size defaults, plus native categorical handling.