Unit 7 of 8 · about 3 hours · derive, measure
Kernels, bandwidth and kernel matrix cost
Kernels buy nonlinearity without paying for explicit features, then charge it back as a dense matrix. The same similarity-versus-cost trade returns in every neighbor and attention method.
You'll be able to
- Derive the kernel-trick substitution with the polynomial and RBF forms, and predict how degree and move bias and locality.
- Explain the RBF similarity geometry: far points contribute near zero and sets the meaning of far.
- Measure kernel matrix time and memory scaling and state the largest you would run dense.
Covers
Read
In this order, only the parts listed
0 of 5 done
- ISLP Chapter 9: kernels and the radial basis functionISLP - tree-based methods and support vector machines
Sections 9.3.1 and 9.3.2 (pp. 377 to 382): feature enlargement, the kernel trick with equations 9.18 to 9.23, the polynomial kernel 9.22, the radial kernel 9.24 and locality
- HoML Appendix C: polynomial, similarity and RBF kernelsHands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical models
Polynomial Kernel, Similarity Features with the
gaussian_rbfgamma0.3 cells, Gaussian RBF Kernel with thegammaandCgrid, Under the hood - sklearn SVM kernel functions and RBF parametersscikit-learn - neighbors, SVMs, trees and ensembles
1.4.6 Kernel functions with 1.4.6.1 Parameters of the RBF Kernel:
Cfor smoothness againstgammafor influence, with exponential grid search over both - sklearn SVM complexityscikit-learn - neighbors, SVMs, trees and ensembles
1.4.4 Complexity: libsvm quadratic to cubic in with cache dependence, against near-linear
LinearSVC - ISLP lab: nonlinear support vector machinesISLP - tree-based methods and support vector machines
9.6.2 Support Vector Machine: the radial kernel at 1 and 1e5 overfitting, then the and grid choosing 1 and 0.5; 9.6.3 ROC Curves: the tuned model against 50 on training and test data
Focus on
- The trick replaces every inner product with , so equals bias plus a sum of over support vectors only. The RBF form makes far points contribute near zero, which is what locality means.
- sets the ruler: large values spike the similarity and overfit, small values smooth everything toward underfit. The HoML grid crosses 0.1 with 5 against 0.001 with 1000 to show both failures.
gammascaleequals 1 over (n_featurestimesX.var()), verified exactly against sklearn internals. It is data-dependent, so rescaling silently retunes the kernel.- The Gram matrix holds floats: 10000 rows in
float64need 763 MiB. Measured on the reference i9 CPU under sklearn 1.9.1, takes 0.025 seconds and 7.6 MiB while takes 0.171 seconds and 122.1 MiB.
Common mistakes · 3
- sklearn is , not . Porting 0.5 from problem 21 means setting 2.0.
- Kernel methods inherit the scaling requirement twice: distances and both assume comparable units.
- Tiny makes every point similar to every other through a near-constant Gram matrix and underfits. Read the grid, never the default alone.
Try it yourself
Closed-book, before checking anything
pairwise.rbf_kernel, then time the Gram matrix at and and confirm the 16x memory step.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Write the RBF matrix for against at 0.5.Answer
Squared distances are 0, 8, 2 and 2, so the matrix is .
2.How much memory does a float64 Gram matrix need at , and about how many rows fill 16 GiB?Answer
bytes = 763 MiB. 16 GiB holds about 46000 rows, since bytes is near 15.8 GiB.
3.Same data, 5 versus 0.1. Which risks overfitting and why?Answer
5: similarity decays within tiny radii, so each point predicts itself and noise becomes boundary. 0.1 smooths distant points together and risks underfitting.
4.Why not expand RBF features explicitly instead of using the kernel?Answer
The RBF expansion is infinite-dimensional. The kernel evaluates that inner product in closed form from pairwise distances.
Practice
- Deep-ML 280: Implement RBF (Gaussian) Kernel Function
Implement
rbf_kernel(X1, X2, gamma); the visible tests check the 0.5 matrix and the 1.0 case at 4 decimalsUse the squared-norm identity with no Python loops. Identical inputs must give exactly 1.0 on the diagonal.
You're done when
You reproduce the test matrix by hand, your timings show the quadratic memory step, and problem 280 passes.
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 witheval_set. - LightGBM parametersLightGBM developers · reference
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 · reference
Read
iterations,learning_rate,l2_leaf_reg,depth,grow_policy,subsample,rsm,boosting_type,use_best_modelandone_hot_max_sizedefaults, plus native categorical handling.