Suppose three roof repairs stop a leak: a bandage, shingles, and a block of titanium. Measuring only water left leaking favors titanium, but its cost makes shingles the more sensible choice. A model can face the same problem: a complicated solution may fit its training data well without being the best choice for new data. Regularization encourages a balance between fit and complexity while the model is trained. section 4.5 section 4.0
The idea is to add a complexity penalty to the error being minimized: error = regression error + λ × regularization term. The parameter λ controls how much the penalty counts. Unlike selecting among several models by a complexity graph, this penalty is part of training a model. It does not itself require a testing set, although testing remains useful for evaluating predictions. section 4.5 section 4.6
Two common ways to measure complexity are:
- L1 regularization, or lasso regression, uses the L1 norm. It can shrink many feature coefficients to zero, making it useful when there are numerous features. section 4.5 section 4.7
- L2 regularization, or ridge regression, uses the L2 norm. It tends to make coefficients small rather than zero; it is recommended here when there are fewer features. section 4.5 section 4.7
Try it with a curved dataset
For data shaped like a downward-opening parabola, add columns for powers of x and fit polynomial regression. The example expands the data to 200 columns, splits it into training and testing sets, then trains three Turi Create models: no penalty, L1 penalty 0.1, and L2 penalty 0.1. Compare their curves, test predictions, and coefficients. The unregularized model has large coefficients; the penalized models have smaller ones. What changes when you adjust the penalties? Keep the testing data out of training and model decisions. section 4.6 section 4.7
Figure 4.9 Three polynomial regression models for our dataset. The model on the left has no regularization, the model in the middle has L1 regularization with a parameter of 0.1, and the model on the right has L2 regularization with a parameter of 0.1.
Compare the fitted curves for no regularization, L1 regularization, and L2 regularization on the same dataset. section 4.6












