Many features
Last updated
One input is rare. A penguin’s body mass depends on its flipper length, its bill and more; a house price on its size, its age and where it is. This lesson grows the line into a model with a weight for every feature, writes its predictions and its gradient as matrix products, and trains it on the penguins. The code hardly changes, as long as every array has the shape you think it has.
By the end of this lesson you can
- Write a linear model with many features as , and say the shape of every part
- Compute predictions and the gradient with matrix products
- Train a many-feature linear model by gradient descent, and read its weights
- Spot a shape mismatch that broadcasting hides
From one feature to many
With one feature the model was . With features, each gets its own weight:
That's a dot product (lesson 1.3): multiply each feature by its weight and add them up. Each weight says how much the prediction changes when that feature goes up by 1 and the others stay the same.
The task for this lesson: predict a penguin's body mass, in grams, from its bill length, bill depth and flipper length. That's features and 342 penguins:
X has one row per penguin and one column per feature (lesson 2.3); y has one mass per penguin.
Vectorised predictions
You could loop over the penguins and take a dot product for each. A matrix product does all 342 at once (lesson 1.4):
is and has 3 entries, so has 342: row of dotted with is penguin 's prediction. Adding adds it to every one. This is called vectorised code: one array operation instead of a Python loop, and much faster (lesson 0.2).
The second cell standardises each column first (lesson 0.5), so every feature has mean 0 and spread 1. Lesson 3.7 explains why gradient descent needs this. Then it starts every weight at 0 and predicts: pred has shape (342,), one prediction per penguin.
The vectorised gradient
Lesson 3.3's gradient for one weight was : the errors times that weight's input, summed. For weight it's the same with , the -th feature of example . All of them together are one matrix product:
In words: (X turned on its side, lesson 1.4) has one row per feature; multiplying it by the 342 errors dots each feature's column with the errors. The result has one entry per weight, the same shape as , so the update works entry by entry.
Every array in one training step, with its shape drawn to scale:
Every shape lines up: the gradient X.T @ (pred − y) has shape (3,), the same as w, so w − lr × gradient works. Dashed outlines are 1-D arrays.
Training on the penguins
The training loop is lesson 3.3's, with @ where there was *:
After 500 steps, is 4,201.8 g: with standardised features, is the mean mass, the prediction for a perfectly average penguin. The weights say how many grams one standard deviation of each feature adds: flipper length 705.8 g, bill depth 39.5 g, bill length 22.7 g. Flipper length does nearly all the work.
The predictions are off by 391 g on average, in the root-mean-square sense (, in grams like the masses). Penguins weigh 2,700 to 6,300 g, so that's a useful model, though not a precise one: three lengths can't capture everything about a bird's weight.
Wrong way: (n,) against (n, 1)
NumPy has two ways to hold 342 numbers: a 1-D array of shape (342,), and a column, a 2-D array of shape (342, 1). They print almost the same, and they don't behave the same:
pred is (342,) and y_col is (342, 1). Broadcasting, NumPy's rule for combining arrays of different shapes, stretches each to match the other: the result is (342, 342), every prediction minus every penguin's mass. No error. The "loss" is a number, 891,254, instead of the right 193,612, and gradient descent on it would train quietly to the wrong answer.
Columns like this come from reshape(-1, 1), from slicing a table with double brackets (df[["mass"]]), and from some libraries. The defence is cheap: print shapes as you write each line, and check that the loss's input has the shape you expect. Turn on "Make y a column" in the shape tracer above to see where it goes wrong.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 5: Machine Learning Basics: linear regression with a weight vector, and its gradient (5.1.4)
- NumPy: Broadcasting: the rules that turn (342,) minus (342, 1) into (342, 342)
- Horst, Hill and Gorman, palmerpenguins: Palmer Archipelago (Antarctica) penguin data, 2020: the penguins dataset, released under CC0