liveBook · Manning

Regularization

As explained in Grokking Machine Learning

Regularization is a model-training technique that penalizes complexity while optimizing predictive performance.

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

How Grokking Machine Learning covers it

How deep it goes
Develops a roof-repair analogy, a penalized error expression, coefficient-shrinkage differences, and a Turi Create experiment.
Examples
Compares three roof repairs and three polynomial fits using no penalty, L1 regularization, and L2 regularization.
What it assumes
Builds from an everyday cost analogy and explains polynomial feature expansion before using regression models in Turi Create.
What you can do after
Apply it via Turi Create’s linear_regression.create with l1_penalty or l2_penalty, then inspect test predictions and coefficients.
How it connects
Connects regularization to overfitting, underfitting, polynomial regression, L1 and L2 norms, testing, and validation.

Also explained in

Compare how 13 books explain regularization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Machine Learning. The section links point to the parts of the book it draws on.