Trees, Ensembles, Neighbors & Kernels
These families encode different geometries and costs. The goal is to choose among them from evidence and constraints instead of defaulting to whichever library wins one score.
Section 3 of 5 · 8 units · about 28 hours
By the end you can Implement a tested tree core plus one neighbor or kernel mechanism, then defend a family choice using geometry, uncertainty, interpretability, fit cost and prediction cost.
Units
- CART: split objectives, impurity, stopping and pruning5h
- Bagging, random forests and out-of-bag estimates3h
- AdaBoost and gradient boosting5h
- XGBoost, LightGBM and CatBoost configuration2h
- k-nearest neighbors, search cost and the curse of dimensionality3h
- Support vector machines: margins, hinge loss and slack4h
- Kernels, bandwidth and kernel matrix cost3h
- Feature importance caveats and class weighting effects3h
Build it
Mini project · about 12 to 16 hours
Tree core, one neighbor or kernel mechanism, and a family-choice defense
Implement a deterministic CART core with one ensemble and one neighbor or kernel mechanism in NumPy in your own repository, test them against hand cases and scikit-learn, configure the boosting libraries on one fixed split, and defend a family choice from geometry, uncertainty, interpretability, fit cost and prediction cost.
- 1Build the CART core: Gini and entropy impurity, an exhaustive split scan with a stated threshold and tie rule, growth limits (
max_depth,min_samples_leaf) and prediction. Match hand cases, thenDecisionTreeClassifierpredictions on tie-free data with matching limits. - 2Prune by cost complexity: take the path and choose by cross-validation on the training split only.
- 3Add bagging or a random forest over your core. Show validation error and its spread over seeds against the number of trees, next to a single tree.
- 4Add one neighbor or kernel mechanism: kNN with a stated tie rule and a scaled-versus-unscaled test, a Pegasos kernel SVM, or an RBF kernel matrix with a positive semidefinite check. Compare predictions with
KNeighborsClassifierorSVCon a fixed split. - 5On one fixed tabular split, configure HistGradientBoosting and XGBoost, LightGBM and CatBoost from their parameter pages with early stopping on a validation split. State each library's categorical handling and record the best iteration, validation score, fit time and predict time.
- 6Add a random high-cardinality column and compare impurity (MDI) importance with permutation importance on held-out data.
- 7Defend a family choice: compare a tree, an ensemble, a boosting model, kNN and an SVM on the same split with paired uncertainty on the validation metric, plus calibration, interpretability, fit cost and prediction cost. Name the condition that would reverse the choice.
What you hand in
- The tree package: impurity, split search, growth limits, pruning and the ensemble, with tests.
- The neighbor or kernel mechanism with its differential tests.
- The family-choice dossier: one comparison table with paired uncertainty and costs, the boosting-library runs, the importance comparison, the decision and its tripwire.
How to check it
- Deep-ML 138, 20, 307, 21 and 280
Public tests. Pass the ones matching your mechanisms after your local tests do; the cards state each convention the tests pin, such as raw-value thresholds in 138 and in-place per-sample updates in 21.
- Self-grade with the rubric below
How it's graded
Tree core correctness
Pass: Impurity and split search match hand cases, and predictions match scikit-learn on tie-free data.
Great: Property tests cover ties under a documented rule, and depth and leaf limits reproduce scikit-learn's node counts.
Ensemble
Pass: The ensemble lowers validation error or its spread over seeds compared with one tree.
Great: An out-of-bag estimate tracks a held-out estimate, and the report explains any gap.
Neighbor or kernel mechanism
Pass: Predictions match scikit-learn on a fixed split.
Great: Tests cover feature scaling and a tie or degenerate-kernel case.
Boosting configuration
Pass: HistGradientBoosting and at least one of XGBoost, LightGBM or CatBoost run with early stopping and stated categorical handling.
Great: All three external libraries run, and the report explains the learning-rate and iteration tradeoff from the recorded best iterations.
Family-choice defense
Pass: One table reports the metric with paired uncertainty, fit time and predict time for each family.
Great: The choice survives a small change in the best score, and the report names the condition that would reverse it.
Resources
- Hands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical modelsAurelien Geron (O'Reilly, 2025)
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.
- ISLP - tree-based methods and support vector machinesJames, Witten, Hastie, Tibshirani and Taylor
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.
- Deep-ML - tree, ensemble and kernel problemsDeep-ML
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
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.
- scikit-learn - neighbors, SVMs, trees and ensemblesscikit-learn
Check official estimator constraints, complexity, preprocessing needs, missing-value behavior and probability caveats. Do not use library calls as the mechanism implementation.
- XGBoost parameters and sklearn estimator interfaceXGBoost developers
Read the tree-booster parameter list for
eta, depth, sampling and regularization names and defaults, and the sklearn page for early stopping witheval_set. - LightGBM parametersLightGBM developers
Read
learning_rate,num_leaves,max_depth,min_data_in_leaf,feature_fraction,bagging_fractionwithbagging_freq,lambda_l2and early stopping defaults. - CatBoost common parameters and categorical featuresCatBoost developers
Read
iterations,learning_rate,l2_leaf_reg,depth,grow_policy,subsample,rsm,boosting_type,use_best_modelandone_hot_max_sizedefaults, plus native categorical handling.