Multiple & Polynomial Regression
One measurement may not be enough to explain the outcome. We can give a regression model several inputs, including transformed inputs, and work out how much each contributes to the prediction.
16 sections · 15 questions · 4 coding challenges
The arrow keys move between sections too.
What If One Influence Changes the Effect of Another?
BOTIE
Box and Wilson's 1951 work on finding favourable experimental conditions developed what became response surface methodology. They used relatively simple fitted surfaces to guide experiments, rather than demanding a complete physical theory of the process before making any useful adjustment.
A surface can include a separate term for each input, squared terms for changing effects, and products for interactions. A temperature-pressure product lets the contribution of temperature depend on the pressure. This is why several inputs and polynomial features belong together. The fitted surface remains an approximation, especially away from the region where the experiments supplied evidence.
Sources for this history
When One Input Cannot Explain Enough
Height provides one clue about weight. Another measurement may distinguish people whose heights are the same. We may also need a relationship whose effect changes as an input grows.
Separate adding a new measurement from calculating a new feature from measurements already available.
One input offers one kind of evidence
A height-only rule must give the same prediction to two people of the same height. It cannot use a difference that was never supplied.
Person A
- Same height
Person B
- Same height
Height-only rule
- Same prediction
Every added column needs a reason
We can now arrange features as matrix columns and write the combined prediction. Matrix notation organizes the same job of combining evidence.
The later sections examine redundant inputs, flexibility, and evaluation. More columns do not automatically provide more useful information.
Explore the full example
Compare how the prediction changes when another input or a higher polynomial degree is available. A closer fit to the displayed points is only the first check.
Drag to turn the room. The indigo line is the cut at the fixed age, the amber line the cut at the fixed height.
Weight against height
Slope of the cut: … kg per cm. The larger dots are people within 6 years of the fixed value.
Weight against age
Slope of the cut: … kg per year. The larger dots are people within 6 cm of the fixed value.
Part 1. From One Input to Several
1. Why one input may not be enough
Simple regression predicted weight from height alone, and height alone runs out quickly. People of the same height weigh different amounts, and a line that only reads height has to give all of them the same answer.
Worked example
Two of the twenty people in the box above are 168 cm tall. One is 31 and weighs 64.9 kg, the other is 60 and weighs 74.3 kg. A line fitted on height alone predicts 67.24 kg for both, because 168 is all it can see.
Let the model read age as well and the two predictions part company, 64.95 kg for the younger person and 69.41 kg for the older one. Neither is exact. Both are closer than the single number the line was forced to give.
That is the whole motivation. Some of the difference between people of one height goes with another measured variable, and multiple regression lets several inputs contribute to one prediction.
2. Building a prediction from several contributions
Take one person before taking a whole dataset. With two inputs the prediction is
ŷ = β₁x₁ + β₂x₂ + αand it is easiest to read as three separate pieces that get added up.
| Contribution | Calculation |
|---|---|
| height contribution | β₁ × height |
| age contribution | β₂ × age |
| intercept | α |
| prediction | the sum of all three |
Below, the three pieces are laid end to end along a number line. The coefficients start at the values the fit chose for the twenty people, and every slider is yours after that. Push the age coefficient negative and watch its block turn round and walk the prediction backwards.
-49.12 + 110.67 + 5.39 = 66.94
Each input contributes its value multiplied by its coefficient, and the contributions are added. That sentence is the entire model, and everything else on this page is either a way of seeing it or a way of fitting it.
3. The dot-product form
Once the pieces are familiar, group them. Put the inputs in one vector and the coefficients in another,
x = (x₁, x₂) β = (β₁, β₂)and the weighted sum collapses to a dot product.
| Expression | What changed |
|---|---|
| β₁x₁ + β₂x₂ + α | the sum from section 2 |
| (x₁, x₂) · (β₁, β₂) + α | the same products, written as a dot product |
| x · β + α | the vectors named |
Nothing was added and nothing was hidden. The dot product is compact notation for the weighted sum already calculated by hand, and the linear algebra primer called it the shape of every linear prediction for exactly this reason. With twenty inputs the expression stays three symbols long.
4. Reading one coefficient
Take the height coefficient first. Hold age at one value, raise height by one centimetre, and read the prediction again.
Worked example
| Record A | Record B | Record C | |
|---|---|---|---|
| height | 170 | 171 | 170 |
| age | 35 | 35 | 36 |
| prediction | 66.86 kg | 67.51 kg | 67.02 kg |
A to B changes height alone and the prediction moves by 0.651 kg, the height coefficient. A to C changes age alone and it moves by 0.154 kg, the age coefficient.
So a coefficient describes how the prediction changes with one input while the other inputs in the model stay where they are. Any two records that differ in one input only will differ in prediction by exactly that input’s coefficient.
Keep in mind
“Holding the others fixed” describes the coefficient mathematically. It does not promise that the dataset contains that comparison. If height and age are strongly related in the data, or if some combinations barely occur, then the coefficient is resting on comparisons the data represents thinly, and it can swing a long way when a few rows change. In the twenty people here, taller people are on the whole younger, with a correlation of −0.46, so “same age, one centimetre taller” is a comparison the data supports less well than the arithmetic suggests.
Part 2. Seeing Multiple Regression Geometrically
5. From a line to a plane
With one input the prediction rule was a line. Give it a second input and the rule needs another direction to change in.
| Inputs | Prediction shape |
|---|---|
| one | a line |
| two | a plane |
| three or more | a flat surface in more dimensions than can be drawn |
Picture the height-and-weight scatter from the regression page, height across and weight up, with its fitted line. Now let age run into the depth of the page. Every person moves back or forward according to their age, and the line, which had no opinion about depth, extends into a sheet that can tilt along the new direction too. That sheet is the plane in the box at the top of the page, and pressing “face the height axis” up there looks along the age direction so the room flattens back into the familiar scatter.
Adding a second input gives the prediction rule one more direction it can change in. That is all a plane is.
6. Slicing the regression plane
A plane is hard to read as a whole and easy to read one direction at a time. The two flat panels under the room are cuts through it.
Fix an age and the plane seen against height is a straight line, drawn in indigo on the wireframe, and its slope is the height coefficient, 0.651 kg per centimetre. Fix a height and the plane seen against age is another straight line, in amber, with slope 0.154 kg per year. Slide either fixed value and the cut moves across the plane while its slope does not change, because a plane tilts by the same amount everywhere.
That is section 4’s “holding the other input fixed” made literal. A coefficient is the slope of the plane along one axis, and the cut is what you see when you stand at a fixed value of the other axis and look.
7. Residuals and least squares in several dimensions
The people do not sit on the plane, and that part is familiar. Each person has a measured weight, the plane produces a predicted weight at their height and age, and the vertical gap between them is the residual, exactly as on the regression page.
eᵢ = yᵢ − ŷᵢSwitch on the residual drops in the box above to see them, green where the person sits above the plane and rose where they sit below it. The residual sum of squares still squares and adds those drops, 214.2 for the twenty people, and the fit is still the plane that makes that total as small as it can be. R² reads 0.865, the share of the weight variation that height and age explain together.
Multiple regression changes the shape of the prediction surface and nothing about how it is judged. Residuals, RSS and the least-squares rule carry over untouched.
8. Fitting coefficients together
It is tempting to imagine the fit working one input at a time, fitting height, locking it, then fitting age on what is left. It does not, and the twenty people show why it cannot.
Worked example
| Model | kg per cm of height | kg per year of age |
|---|---|---|
| height alone | 0.574 | |
| age alone | −0.119 | |
| height and age together | 0.651 | +0.154 |
The age coefficient changes sign. On its own age appears to go with lower weight, because taller people in this sample happen to be younger. Beside height, which absorbs that, age goes with higher weight.
Changing one coefficient changes what the others need to do, so the fit chooses all of them at once. RSS is a bowl over every coefficient together, the same bowl the regression page drew over a slope and an intercept, with one more direction per input, and the least-squares answer is its one lowest point. Each coefficient is chosen in the context of every other input in the model, which is why adding or removing an input can move every coefficient, not only the one that came or went.
Part 3. Features Can Be Constructed
9. Constructing features
Now the thrown ball. Its height rises and then falls, and a straight line cannot rise and then fall, however it is placed. A model with transformed input columns can fit a curve, though, and it does not need a second measurement to do it. A new input can be calculated from an old one.
Start with time t, and manufacture t² from it.
| time t | squared time t² |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
A second column, built from the first before any fitting starts.
A model can receive transformed versions of an existing input as extra inputs, and the fit neither knows nor cares that one column was made from another. Polynomial regression is nothing more than this. It builds the power columns and hands the widened table to the machinery of Parts 1 and 2.
10. Adding t²
The two-input model on those two columns is
ĥ = β₁·t + β₂·t² + αand it can be read two ways. As multiple regression it has two inputs, t and t², two coefficients and an intercept, and the prediction is still a weighted sum. As a function of time it is curved, since a nonzero coefficient on t² makes a parabola.
The two drawings show the same fit. On the left, time is plotted against height, producing a parabola. On the right, time and squared time are separate coordinates, and the fitted prediction is a plane.
squared-time coordinate = t² = t × tActual inputs cannot move freely around that plane: squared time is determined by time. The highlighted path follows that constraint. Reading the plane along this path gives the curve on the left.
In the original input, a curve
In the constructed features, a plane
Drag to turn. The thick line is the left-hand curve, lying on the plane.
| Fitted as | intercept | on t | on t² |
|---|---|---|---|
| a degree-2 curve in t | … | … | … |
| a plane over two inputs | … | … | … |
The table under the pictures fits the data twice, once as a degree-2 curve in t and once as a plane over two inputs that happen to be called t and t², and the two sets of numbers agree to every digit. They are one fit. The curve in the original input is a plane in the constructed features, which is the page’s central claim.
11. How a linear model produces a curve
This deserves its own moment, because “linear” is doing something specific. A model counts as linear regression when its coefficients enter linearly,
ŷ = β₁φ₁(x) + β₂φ₂(x) + αThe feature functions may themselves be nonlinear. Here, one keeps the original time and the other squares it.
φ₁(t) = t
φ₂(t) = t²The prediction is nonlinear in time but linear in the coefficients being fitted. That second property is what allows the same linear least-squares fitting method to work.
Below, the five basic shapes 1, t, t², t³ and t⁴ are drawn unscaled along the top. Each slider scales one of them, the scaled shapes are dashed in their own colours, and the solid line is their sum. The sliders start on the coefficients recovered from the ideal throw.
Dashed lines are the scaled shapes, and a shape that leaves the frame runs along its edge. The solid line is their sum, and the grey dots are the ideal throw.
Every curve this widget can draw is assembled by adding scaled copies of fixed shapes, and the only things a fit chooses are the amounts. Polynomial regression produces a curved prediction while remaining a linear model in the parameters it learns, and that is why the straight line’s solver handles it without modification.
Check yourself
Questions on Parts 1 to 3
Question 1 of 5
Two of the twenty people are 168 cm tall, one aged 31 and weighing 64.9 kg, the other aged 60 and weighing 74.3 kg. What does the line fitted on height alone predict for them?
Part 4. Work the Ball Example
12. The ball's physical relationship
Physics writes the height of a thrown ball as
h(t) = h₀ + v₀·t − ½·g·t²with h₀ the starting height, v₀ the vertical launch speed and g the acceleration due to gravity. Line that up against the regression and every term has a partner.
| Regression term | Physical meaning |
|---|---|
| α | starting height h₀ |
| β₁ | launch speed v₀ |
| β₂ | −½ g, negative one half of gravity’s acceleration |
The simulated throw starts at ground level with an upward speed of twenty metres per second.
height h(t) = 20t − 4.9t²
gravity magnitude g = −2 × coefficient on t²
= −2 × (−4.9) = 9.8 m/s²The coefficient of squared time is minus half the gravitational acceleration. Its value is not the acceleration itself.
13. Recovering the ideal curve
Take the noiseless case first. Five exact readings of the throw at t = 0, 1, 2, 3 and 4 are the “ideal case” button below. Press it, set the degree to 2, and read the chips.
…
What comes back
| generating equation | recovered by the fit | |
|---|---|---|
| intercept | 0 | 0.00 |
| on t | 20 | 20.00 |
| on t² | −4.9 | −4.90 |
| R² | 1.000 |
The steps were exactly the ones Part 3 described. Generate exact heights, construct the t² column, fit two inputs, read off the coefficients. Nothing in the fit knows any physics. It minimised squared misses over a table with a t column and a t² column, and the physics came out because the physics is what generated the data.
Keep in mind
R² is exactly 1 here and that is the correct answer, not a warning. The data has no noise and the model has the right form, so a perfect score is what should happen. Section 21 is about when the same number means something else.
14. Comparing degrees zero, one, and two
Do not jump from a straight line to a degree-9 curve. The useful comparison is the three smallest models on the same five ideal points.
| Degree | Can do | Fitted rule | RSS | R² |
|---|---|---|---|---|
| 0 | one constant, no relationship with time | ĥ = 10.6 | 337.74 | 0.000 |
| 1 | rise or fall, never both | ĥ = 0.4·t + 9.8 | 336.14 | 0.005 |
| 2 | one turning point | ĥ = 20·t − 4.9·t² | 0.00 | 1.000 |
Degree 0 is the mean of the heights, the baseline R² measures against. Drag the degree slider above between 1 and 2 to see the middle two.
The straight line barely improves on the mean, because the best line through a symmetric arc is almost flat. The parabola explains everything. The difference between a useless model and a perfect one was one manufactured column, and the lesson is that a model can fail because the inputs it was given cannot express the pattern in the data.
15. Residual patterns and underfitting
The degree-1 model is not just worse by the numbers. Its misses have a shape. Plot each residual against time and the line’s residuals are negative at both ends and positive in the middle, which is the arc it could not draw, left over in the errors.
Degree 1, a straight line
Degree 2, a parabola
On the ideal throw the parabola’s residuals are zero. On the noisy throw they are a scatter with no obvious curve, which is what residuals look like when the model has taken the structure and left the noise. A visible pattern in the residuals says the model has left systematic structure unexplained, and that is worth checking on any fit, not only polynomial ones.
This has a name. A model too rigid to represent the pattern in the data is underfitting. It shows large systematic residuals, a poor training score and poor predictions, and on the ball the straight line is the textbook case.
Part 5. Increasing Polynomial Degree
16. What polynomial degree permits
Each degree adds one power of t as a new column, and each power buys the curve the possibility of one more turn.
| Degree | Highest power | Turning points at most |
|---|---|---|
| 1 | t | 0 |
| 2 | t² | 1 |
| 3 | t³ | 2 |
| d | tᵈ | d − 1 |
The word is permits. A higher degree allows a more complex shape and does not require it. A degree-4 curve may turn three times, and the fitted coefficients decide how many of those turns actually appear. On the ideal throw a degree-4 fit puts 0.00 on t³ and 0.00 on t⁴ and draws the same parabola as degree 2, because the extra turns would not help. Section 11’s sliders are the place to feel this. Every shape is available and the amounts decide which powers contribute to the curve up.
17. Adding measurement noise
Real readings of a thrown ball carry error, a wobble of the hand or the ruler that says nothing about the next throw. The noise slider in section 13’s box adds exactly that. It takes fifteen exact readings of the same 20t − 4.9t² throw and shifts each by its own draw, scaled to the amount you choose, and the “show the true throw” box draws the arc the readings came from.
What you see with noise on is three things at once. There is the true height, dashed. There is the noise, the vertical gap from the dashed curve to each dot. And there is the observed height, the dot itself, which is all the model is ever shown. The fit sees the measured values and not the hidden relationship that produced them, and every question about overfitting comes from that fact.
18. Why training fit improves with degree
Now sweep the degree upward on the fifteen noisy readings, refitting at each step and scoring on the same fifteen.
The bars can only climb or hold as the degree rises, which is exactly why the training score cannot be the judge.
The bars can only rise or hold still, and there is a formal reason. The degree-3 model contains every degree-2 model, since it can set the coefficient on t³ to zero and reproduce the degree-2 fit exactly, and the same holds one degree up from any degree. So the best degree-3 fit is at least as good on the training rows as the best degree-2 fit, and it may use the new column to do better. Training R² for nested models can only rise or stay unchanged as terms are added.
Degree 2 reads 0.996 here and degree 9 reads 0.997, with 10 coefficients doing the work of 3. Every step past 2 bought almost nothing on the training rows, and the next Part is about what it cost.
Part 6. Underfitting and Overfitting
19. Holding back data for evaluation
The argument against the high-degree curve is usually that it looks implausible, and a stronger argument is available. Split the readings. Fit on some of them and keep the rest hidden, and let the hidden ones judge.
Below, four of the fifteen noisy readings are hidden from the fit and from you. Pick a degree, decide where you think the four missing dots fall, and then reveal them.
Four of the fifteen measurements are hidden. Pick a degree, decide where you think they fall, then reveal them.
A model should be judged, at least in part, by observations it did not use to fit itself. The training score measures how well the curve fits where it was told to fit. The held-out score measures whether what it learned travels.
20. Overfitting
Trace both scores across every degree and the picture arrives in one chart.
The indigo curve can only climb as the degree rises, while the amber one turns around, and the turn is where honest capacity ends.
Training R² climbs from 0.222 at degree 1 to 0.999 at degree 9 and never falls. The held-out score rises to 0.989 at degree 2, holds near there through degree 5, drops to 0.693 at degree 6, and then collapses, −104 at degree 8 and −5479 at degree 9. A curve that scores 0.999 on the eleven readings it was fitted to is wrong by street widths on the four it was not.
That is overfitting. Flexibility improves the reproduction of the training sample while harming predictions on new observations. The signs are very low training error, large and unstable coefficients, sensitivity to individual measurements and residuals on the training data that are tiny while errors elsewhere grow. At degree 9 the coefficient on t⁴ is −229.5 and on t⁵ is +169.5, two huge terms cancelling each other almost everywhere and not quite cancelling between the points.
21. When a perfect training score is meaningful
Section 13 scored 1.000 and was right to. Section 20 is heading toward 1.000 and is wrong. The number is the same and the situations are not.
| Case | Training R² = 1 | What it means |
|---|---|---|
| noiseless quadratic data, degree 2 | expected | the correct form recovered the exact relationship |
| noisy data, degree near the number of points | suspicious | the curve is likely reproducing the noise |
A score becomes concerning because of how it was achieved and whether the model generalises, and not merely because it is high. Fourteen noisy points and a degree-13 curve will score exactly 1 by threading every one of them, and that says nothing about the fifteenth.
22. Balancing model flexibility
Set the three fits from section 19 beside each other in your head, degree 1, degree 2 and degree 9, all on the same eleven training points and judged by the same four hidden ones. The first is too stiff to follow the arc and misses both groups. The second follows the arc and scores well on both. The third follows the eleven exactly and misses the four badly.
Too little flexibility leaves structure unfitted. Too much reacts to the particular sample it was handed, noise included. A useful model sits between those, and the way to find it is performance on data outside the fit, which is the question the evaluation page takes up properly. None of the three is the simple model or the advanced one. They differ in flexibility, and that is the only axis this comparison is about.
Check yourself
Questions on Parts 4 to 6
Question 1 of 5
The ideal throw fits a coefficient of −4.9 on t². What gravitational acceleration does that imply?
Part 7. Matrix Form
23. Building the design matrix
Everything so far can be written in one line if the data is arranged as a matrix. Start from the table anyone would write down for the ideal throw, and rearrange it in stages.
| observation | t | t² | height h |
|---|---|---|---|
| 1 | 0 | 0 | 0 |
| 2 | 1 | 1 | 15.1 |
| 3 | 2 | 4 | 20.4 |
| 4 | 3 | 9 | 15.9 |
| 5 | 4 | 16 | 1.6 |
The t² column was manufactured from the t column before any fitting began.
The result is a matrix X with one row per observation and one column per input, a coefficient vector β, and their product, which is the vector of predictions.
ŷ = XβThe leading column of ones is the intercept’s seat. If α is to live inside β, X has to carry a column that multiplies it, and multiplying a column of ones by α adds the same α to every prediction. Leave that column out and the normal equations below solve for a model with no intercept at all.
24. Predicting the dataset with matrix multiplication
Choose one row of X in the third stage above, say row 2, and it is the vector (1, 1, 1). Dot it with (α, β₁, β₂) and you get
ĥ₂ = α + β₁·1 + β₂·1 = 0 + 20 − 4.9 = 15.1This is section 2’s weighted sum for one observation. Row 4 contains the intercept feature, time and squared time.
row 4 prediction = (1, 3, 9) · (0, 20, −4.9)
= 0 + 60 − 44.1
= 15.9Matrix multiplication performs this dot product for every row at once. The result is the whole column of predictions.
25. The normal equations
The derivation is the regression page’s, in vector form. The residuals are a vector, RSS is that vector dotted with itself, and the bowl’s floor is where every partial derivative is zero at once.
›Why this works
| Expression | What changed |
|---|---|
| e = y − Xβ | the residuals, all of them, as one vector |
| RSS = (y − Xβ)ᵀ(y − Xβ) | a vector dotted with itself is the sum of its squares |
| ∇β RSS = −2Xᵀ(y − Xβ) | differentiate with respect to every coefficient at once |
| Xᵀ(y − Xβ) = 0 | the floor of the bowl, gradient zero |
| XᵀX β = Xᵀy | rearranged, the normal equations |
With one input and the intercept column, the two rows of XᵀX β = Xᵀy are exactly the two equations the regression page solved by substitution. The powers of t change what the columns of X hold and nothing about the derivation.
Keeping track of shapes is most of the work of reading this, so here they are for n observations and p columns of X, where p counts the column of ones.
| Object | Shape | For the ideal throw |
|---|---|---|
| X | n × p | 5 × 3 |
| β | p × 1 | 3 × 1 |
| y | n × 1 | 5 × 1 |
| Xβ | n × 1 | 5 × 1 |
| XᵀX | p × p | 3 × 3 |
| Xᵀy | p × 1 | 3 × 1 |
Each multiplication is legal because the inner dimensions agree, and the normal equations are p equations in p unknowns.
The normal equations describe the condition the least-squares solution satisfies. They do not say to compute (XᵀX)⁻¹Xᵀy by forming the inverse, and in practice nobody should. A working implementation solves the system directly, and numerical libraries usually go further and avoid forming XᵀX at all, using a QR decomposition or a singular value decomposition of X, which are better behaved when columns are nearly dependent. A unique solution also needs enough independent information in the columns, which is the next section.
26. Dependent feature columns
Suppose one column holds t and another holds 2t. The second says nothing the first did not, and the fit has no way to decide how much of the effect belongs to each.
20.00 × t + 0.00 × 2t = 20 × t
| t | 2t | first share | second share | their sum |
|---|---|---|---|---|
| 0 | 0 | 0.00 | 0.00 | 0.00 |
| 1 | 2 | 20.00 | 0.00 | 20.00 |
| 2 | 4 | 40.00 | 0.00 | 40.00 |
| 3 | 6 | 60.00 | 0.00 | 60.00 |
| 4 | 8 | 80.00 | 0.00 | 80.00 |
Move the slider anywhere. The last column never changes, so no residual changes either, and the fit has no reason to prefer one position over another.
Every position of the slider is a different pair of coefficients and the identical set of predictions, so the residuals are identical too and RSS gives the fit no reason to prefer any of them. The bowl has a flat valley floor instead of a single lowest point, XᵀX has no inverse, and the normal equations have infinitely many solutions.
The name for this is collinearity, and for an exact copy like 2t it is perfect multicollinearity. A coefficient cannot be uniquely identified when its column can be reconstructed from the others. The fit behind this page refuses such a table outright rather than picking one of the infinitely many answers, and the nearly-dependent case, where two columns are almost but not quite copies, is one of the things ridge regression exists to handle.
Part 8. Boundaries and Forward Connections
27. Extrapolation and numerical scale
High-degree polynomials behave dramatically outside the interval they were fitted on. Section 13’s box has a range control for this. Load the full throw, set the degree to 9, and switch from the observed range to a little beyond and then far beyond. The training points stay where they are, and the curve leaves the frame.
Two fits, past the last measurement
| Time | Degree 2 predicts | Degree 9 predicts |
|---|---|---|
| 6 s | −56.7 m | 20,906 m |
| 8 s | −153.5 m | 1,130,430 m |
| −2 s | −58.8 m | −18,516 m |
Both fits score above 0.995 on the readings between 0 and 4 seconds. Neither has any idea what happens after.
Even the parabola’s answers are wrong, since a real ball stops at the ground rather than continuing below it, and the degree-9 curve is wrong by a kilometre within two seconds of the last reading. A curve that fits well within the measured interval may be unreliable the moment it leaves it, and nothing in the training score can warn you.
A quieter problem comes from the columns themselves. The powers of t live on very different scales.
| t | t² | t³ | t⁴ | |
|---|---|---|---|---|
| at t = 4 | 4 | 16 | 64 | 256 |
| at t = 4 after centring t on 2 | 2 | 4 | 8 | 16 |
Centring shifts the time axis so its middle is zero. The same throw, the same curve, and the largest column entry falls from 256 to 16.
Wide differences in scale make XᵀX badly conditioned, so its solution swings on the last digits of the data. Centring and scaling the input before raising it to powers is the usual remedy, and the feature scaling page takes that up in full.
28. Where evaluation and regularization enter
This page ends on two questions it has raised and not answered, and they are different questions.
How do we decide which degree predicts new data best? Section 19 held back four readings once. Doing that systematically, with every reading taking a turn as the judge, is cross-validation, and choosing a degree by it is what the held-out evaluation and grid search pages do.
How do we restrain a flexible model without throwing away the inputs it needs? Keep the degree-9 columns and penalise large coefficients, so the t⁴ and t⁵ terms cannot reach −229 and +169 in the first place. That is ridge and lasso, and it also answers section 26’s nearly-dependent columns.
In a model
Regularisation is one response to overfitting and not the only one. Choosing a smaller degree, gathering more readings, or selecting columns are all responses too, and which is right depends on the question. What every one of them needs is a way of scoring a model on data it did not see, which is why evaluation comes before regularisation in the order of this site.
Check yourself
Questions on Parts 7 and 8
Question 1 of 5
What is the leading column of ones in X for?
Try it with Botie
Build with Botie
Write and run Python