Linear algebraLesson 3 of 6
Make every prediction at once
Matrix shapes and multiplication
Our candidate weights are . Calculating a prediction for each record gives , , and . We can organize the three dot products in one expression:
The matrix contains the inputs: one record per row and one feature per column. It is often called a design matrix in regression. Its entries are the same measurements as before, arranged for a repeated calculation.
Check the shapes first
Section titled “Check the shapes first”has shape : three rows, two columns. The weight vector has shape . Their product has shape :
The matching inner sizes mean every row has one entry for each weight. The outer sizes tell us there will be three predictions. A shape check can catch an impossible calculation before you do any arithmetic.
Shape is necessary but does not check meaning. A row ordered as “sets, hours” still has two entries, so the multiplication runs. It gives the wrong interpretation if the weights expect “hours, sets.” Keep the labels attached while learning the notation.
Entry means row , column . Here : the third record has one practice set. This differs from , which is not present because the matrix has only two columns.
Several weight vectors
Section titled “Several weight vectors”Suppose we also want predictions from weights . Put both candidate weight vectors in columns:
The first result column contains predictions from the first model; the second contains predictions from the second. Each output entry comes from a row of dotted with a column of . For example, row 3, column 2 is .
In general, produces an matrix. This is not entry-by-entry multiplication: that is a different operation. Order also matters. Here is defined, but is not: its inner sizes would be 2 and 3.
What transpose does
Section titled “What transpose does”Transposing exchanges rows and columns:
It has shape . The hours column of becomes the hours row of . Transpose rearranges entries; it does not undo a matrix multiplication.
Try it
Section titled “Try it”Using , calculate . Does agreeing with the first model on record 2 mean agreeing everywhere?
Work through the answer
The predictions are . Both candidates predict 5 for record 2, but the first predicts . One matching prediction does not determine the weights.
Georgia Tech’s matrix multiplication chapter develops the same row-by-column rule.