Ridge
Fit a linear model while charging it for large weights. The model must balance matching the examples against relying on extreme coefficients.
Start with an example
A high-degree delivery curve may wiggle between examples. A Ridge penalty can reduce those swings by shrinking its coefficients.
How it learns
An input feature is a measurement about an example. The target is its known answer: a number for regression, or a category for classification. Training means learning from examples whose answers are already known.
- A linear model multiplies inputs by weights and adds them. Ridge chooses those weights by balancing prediction errors against an extra cost for large weights: add each weight multiplied by itself. This is the L2 penalty.
- The alpha setting controls the penalty strength. Raising it encourages smaller weights, so predictions vary less with the inputs. Put measurements on comparable scales before fitting so their units do not unfairly determine the penalty.
Try it yourself
The example above gives the idea. The experiment below uses a separate, made-up dataset with unitless measurements so you can focus on how the model works. Read the graph guide first, then change one thing at a time.
1. Understand what you are looking at
See how a penalty discourages a line from relying on a large slope. This graph fits one straight line, even when the example above discusses a more complex curve.
- Across: the horizontal axis
- The input measurement x, from 0 to 10. In a delivery task, an input could be distance; here it has no unit.
- Up: the vertical axis
- The target: the numerical answer being predicted. In a delivery task, it could be time; here the target has no unit.
Each dark dot pairs one input with its observed target. The green line gives the model's predicted target at each input. A vertical gap from a dot to the line is a prediction error.
How to read one result: Start at x on the bottom axis and follow the vertical dashed line to the outlined diamond on the curve. Read its height on the left: that is the predicted target for this input.
2. Predict, change, observe
- Set Penalty strength (alpha) to 0 and compare the prediction at x = 0 with the one at x = 10.
- Predict what will happen to that difference when alpha rises to 20. Increase it and watch the line become flatter; the fitting procedure recomputes the line for each setting.
- Observations
- Model prediction
- Inspected result (diamond)
Mean squared training error: 0.888
Mean squared training error averages the squared vertical gaps from the observed dots to the line. Smaller means a closer fit to these training examples, not necessarily better predictions on new examples.
3. Explain it in your own words
If the line becomes flatter as the penalty grows, has the input become less important to this fitted model, or has it been proved irrelevant in the real world?
Compare your reasoning
The fitted model relies less on the input. This does not prove real-world irrelevance. The penalty trades a closer fit for smaller weights. If unsure, compare alpha = 0 and alpha = 20 and describe the change in predictions without making a claim about cause and effect.
Ready to continue when you can explain the result and why it changes. If the explanation is still unclear, reset and repeat the two steps above with one control at a time.
What this experiment simplifies
This one-feature fit minimizes mean squared error plus a penalty on the slope. Increasing alpha shrinks the slope; L1 can make it exactly zero. The intercept is not penalized.
These are small teaching implementations, not scikit-learn runs. Training error on these examples does not estimate performance on new data.
View the plotted examples
| Input feature (x) | Observed target |
|---|---|
| 0.30 | 2.78 |
| 0.92 | 2.97 |
| 1.54 | 4.17 |
| 2.16 | 4.08 |
| 2.78 | 4.33 |
| 3.40 | 3.24 |
| 4.02 | 3.41 |
| 4.64 | 2.87 |
| 5.26 | 3.37 |
| 5.88 | 3.16 |
| 6.50 | 4.58 |
| 7.12 | 5.27 |
| 7.74 | 6.6 |
| 8.36 | 6.59 |
| 8.98 | 7.51 |
| 9.60 | 7.14 |
The math, one step at a time
Read the symbols after the idea makes sense. You do not need to memorize the formula.
α is nonnegative penalty strength and w are feature coefficients. Here the intercept is fitted separately without the penalty; objective scaling differs across estimator conventions.
What to watch for
- Too much penalty can erase real structure and cause underfitting.
- Shrinkage usually keeps coefficients nonzero; a small weight is not proof that an input has no real-world effect.
Explore a related model
Further reading
The official documentation goes further when you are ready.