Skip to content
nonconvexcard

Implement kernel ridge regression

CodingIntermediate⏱ 1 hourClassical MLkernel ridgeimplementationbandwidtheffective dof

Problem

Implement kernel ridge regression with a Gaussian kernel.

  1. Fit a one-dimensional function and visualise the effect of σ\sigma and λ\lambda.
  2. Plot the representer expansion term by term and check the terms sum to the fit.
  3. Compute the effective degrees of freedom tr⁡(K(K+nλI)−1)\operatorname{tr}(\mathbf K(\mathbf K+n\lambda I)^{-1}) and relate it to the visual smoothness.
  4. Select (σ,λ)(\sigma,\lambda) by cross-validation and compare with the visually best fit.

Implement it, then check the implementation against something you can verify independently.

Write it

Solve it here5 tests · Python runs in your browser
⌘/Ctrl + ↵ to run · limit 40s · draft saved in this browser
5 tests will run against your code. The first run downloads the Python runtime — several megabytes, once per browser.

Solution

Opening this before you have a real attempt on paper is the fastest way to feel like you understand something you cannot reproduce.

Related problems

All problems
SolvedProblemFieldTypeDifficultyTime
Implement backpropagation from scratch runDeep LearningCodingIntermediate3 hours
Implement multi-head attention from scratch runDeep LearningCodingIntermediate3 hours
Implement power iteration and verify the convergence rate runFoundationsCodingIntermediate1 hour
Implement random projection and measure the distortion runHigh-Dim ProbabilityCodingIntermediate1 hour
Implement ridge and find the optimal lambda runClassical MLCodingIntermediate1 hour