Unit 2, lesson 11

Vectors, Matrices, and Linear Predictions

How a feature vector and a coefficient vector give one prediction through a dot product, and how a design matrix with an intercept column gives a whole vector of linear predictions with matching dimensions.

  • Video 11
  • Linear Algebra for Predictions

A delivery service wants a time estimate from two measurements, the route distance in kilometers and the number of packages. The constructed rule used on this page is 5 minutes plus 2 minutes per kilometer plus 3 minutes per package, with coefficients chosen for practice rather than estimated from data. How should such a rule be written so that it applies to one delivery or to thousands at once, without mixing up which number belongs to which measurement? This lesson writes one delivery as a feature vector, a table of deliveries as a design matrix, and the predictions as a dot product and a matrix-vector product, with every dimension checked.

Key terms

Feature vector

Definition A vector in Rp\mathbb{R}^p is an ordered list of pp real numbers. On this page every vector is a column, and its transpose, written with ⊤^{\top}, is the same numbers written as a row. In a data set with pp features listed in a fixed order, the feature vector of one observation is

x=[x1⋮xp]∈Rp,\mathbf{x} = \begin{bmatrix} x_1 \\ \vdots \\ x_p \end{bmatrix} \in \mathbb{R}^p ,

where xjx_j is the value of feature jj in its stated unit. A linear model pairs it with a coefficient vector w=(w1,…,wp)⊤\mathbf{w} = (w_1, \dots, w_p)^{\top} in the same order, so wjw_j belongs to feature jj and is measured in units of the prediction per unit of feature jj. The same coefficients give meaningful predictions only for feature vectors that list the same features in the same order and units.

In plain words A feature vector is one row of the data table read as a list of numbers. The position of an entry says which measurement it is, so the order is part of the data. The entries may have different units, as long as each coefficient converts its own feature into the unit of the prediction.

Example A delivery of 4 kilometers with 2 packages, distance first, has the feature vector x=(4,2)⊤\mathbf{x} = (4, 2)^{\top}. The rule’s coefficient vector is w=(2,3)⊤\mathbf{w} = (2, 3)^{\top}, in minutes per kilometer and minutes per package. The vector (2,4)⊤(2, 4)^{\top} would describe a different delivery, 2 kilometers with 4 packages.

Dot product

Definition For two vectors u\mathbf{u} and v\mathbf{v} in Rp\mathbb{R}^p, the dot product is

u⊤v=∑j=1pujvj=u1v1+⋯+upvp.\begin{aligned} \mathbf{u}^{\top}\mathbf{v} &= \sum_{j=1}^{p} u_j v_j \\[0.4em] &= u_1 v_1 + \dots + u_p v_p . \end{aligned}

It is defined only when both vectors have the same number of entries. It is symmetric, u⊤v=v⊤u\mathbf{u}^{\top}\mathbf{v} = \mathbf{v}^{\top}\mathbf{u}, and linear in each vector. As a matrix product, the 1×p1 \times p row u⊤\mathbf{u}^{\top} times the p×1p \times 1 column v\mathbf{v} is a 1×11 \times 1 matrix, which is treated as a single number.

In plain words Multiply matching entries and add the products. In a prediction each product is the contribution of one feature, and the sum is meaningful only when every contribution has the same unit, here minutes.

Example For the delivery above,

x⊤w=4⋅2+2⋅3=8+6=14.\begin{aligned} \mathbf{x}^{\top}\mathbf{w} &= 4 \cdot 2 + 2 \cdot 3 \\[0.4em] &= 8 + 6 = 14 . \end{aligned}

Distance contributes 8 minutes and the packages contribute 6 minutes, so the features account for 14 minutes. Adding the intercept of 5 minutes gives a prediction of 19 minutes.

Matrix and design matrix

Definition A real n×pn \times p matrix is a rectangular array of real numbers with nn rows and pp columns. Its entry in row ii and column jj is written XijX_{ij}, and its transpose X⊤X^{\top} is the p×np \times n matrix with entries (X⊤)ji=Xij(X^{\top})_{ji} = X_{ij}. For a data set of nn observations with pp features, the design matrix XX is the n×pn \times p matrix whose row ii is xi⊤\mathbf{x}_i^{\top}, the feature vector of observation ii written as a row. Column jj then holds the values of feature jj for all nn observations. The targets form a vector y\mathbf{y} in Rn\mathbb{R}^n listed in the same row order. On this page XX holds only measured features, and the intercept column of a later card is kept separate.

In plain words The design matrix is the numeric part of the data table, with one row per example and one column per feature. Its row order must match the order of the targets, and its column order must match the order of the coefficients.

Example Three deliveries cover 4 kilometers with 2 packages, 7 kilometers with 1 package, and 10 kilometers with 3 packages. Their design matrix is

X=[4271103],X = \begin{bmatrix} 4 & 2 \\ 7 & 1 \\ 10 & 3 \end{bmatrix},

with n=3n = 3 and p=2p = 2. The entry X21=7X_{21} = 7 is the distance of the second delivery, and the second column (2,1,3)⊤(2, 1, 3)^{\top} lists the package counts of all three. The transpose X⊤X^{\top} is 2×32 \times 3, with one row per feature.

Matrix-vector product

Definition Let XX be an n×pn \times p matrix with rows x1⊤,…,xn⊤\mathbf{x}_1^{\top}, \dots, \mathbf{x}_n^{\top}, and let w\mathbf{w} be a vector in Rp\mathbb{R}^p. The product XwX\mathbf{w} is the vector in Rn\mathbb{R}^n whose entry ii is the dot product of row ii with w\mathbf{w},

(Xw)i=xi⊤w=∑j=1pXijwj.(X\mathbf{w})_i = \mathbf{x}_i^{\top}\mathbf{w} = \sum_{j=1}^{p} X_{ij} w_j .

The product is defined only when the number of columns of XX equals the number of entries of w\mathbf{w}, and the shapes combine as (n×p)(p×1)=n×1(n \times p)(p \times 1) = n \times 1. Equivalently, if cj\mathbf{c}_j denotes column jj of XX, then Xw=w1c1+⋯+wpcpX\mathbf{w} = w_1 \mathbf{c}_1 + \dots + w_p \mathbf{c}_p, a combination of the feature columns weighted by the coefficients.

In plain words Every row of the table is dotted with the same coefficient vector, giving one number per row. The matrix form does the same row by row arithmetic in one expression and keeps the results in the order of the rows. A correct shape is necessary but not sufficient, because a product with the right shape can still pair coefficients with the wrong features.

Example For the three deliveries and w=(2,3)⊤\mathbf{w} = (2, 3)^{\top},

Xw=[4271103][23]=[141729].\begin{aligned} X\mathbf{w} &= \begin{bmatrix} 4 & 2 \\ 7 & 1 \\ 10 & 3 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \end{bmatrix} \\[0.4em] &= \begin{bmatrix} 14 \\ 17 \\ 29 \end{bmatrix}. \end{aligned}

The column view gives the same numbers, 2 (4,7,10)⊤+3 (2,1,3)⊤=(8,14,20)⊤+(6,3,9)⊤2\,(4, 7, 10)^{\top} + 3\,(2, 1, 3)^{\top} = (8, 14, 20)^{\top} + (6, 3, 9)^{\top}. The reversed product wX\mathbf{w}X is not defined, because a 2×12 \times 1 vector cannot multiply a 3×23 \times 2 matrix. The product X⊤1X^{\top}\mathbf{1}, where 1\mathbf{1} is the vector of three ones, is defined and has shape 2×12 \times 1. It gives the column totals, 21 kilometers and 6 packages, one number per feature rather than one prediction per delivery.

Intercept column

Definition A linear model with intercept bb adds the same number bb to every prediction,

y^=Xw+b 1,\hat{\mathbf{y}} = X\mathbf{w} + b\,\mathbf{1},

where 1\mathbf{1} is the vector of nn ones. The same predictions come from the augmented design matrix and the augmented coefficient vector

X~=[1X],θ=(b,w1,…,wp)⊤,\begin{gathered} \tilde{X} = \begin{bmatrix} \mathbf{1} & X \end{bmatrix}, \\[0.4em] \boldsymbol{\theta} = (b, w_1, \dots, w_p)^{\top}, \end{gathered}

since X~θ=b 1+Xw\tilde{X}\boldsymbol{\theta} = b\,\mathbf{1} + X\mathbf{w}. The matrix X~\tilde{X} is n×(p+1)n \times (p + 1) and θ\boldsymbol{\theta} has p+1p + 1 entries. The first column of X~\tilde{X}, all ones, is the intercept column. It is bookkeeping for the constant term, not a measured feature, and it stays labeled apart from the features because fitting procedures often treat it differently. For example, the intercept is usually left out of a regularization penalty, and the ones column is not standardized like a feature.

In plain words The intercept is the part of every prediction that does not depend on the features. A column of ones lets a single matrix product include it, and each prediction receives the intercept exactly once, not once per feature.

Example For the three deliveries,

X~θ=[1421711103][523]=[192234],\begin{gathered} \tilde{X}\boldsymbol{\theta} = \begin{bmatrix} 1 & 4 & 2 \\ 1 & 7 & 1 \\ 1 & 10 & 3 \end{bmatrix} \begin{bmatrix} 5 \\ 2 \\ 3 \end{bmatrix} \\[0.4em] = \begin{bmatrix} 19 \\ 22 \\ 34 \end{bmatrix}, \end{gathered}

which equals Xw+5 1=(14+5,17+5,29+5)⊤X\mathbf{w} + 5\,\mathbf{1} = (14 + 5, 17 + 5, 29 + 5)^{\top}. Adding 5 to each of the two feature contributions instead would give (24,27,39)⊤(24, 27, 39)^{\top}, which counts the intercept twice. In NumPy the intercept is often kept as a separate number.

import numpy as np

X = np.array([[4, 2], [7, 1], [10, 3]])  # 3 deliveries, 2 features
w = np.array([2, 3])                     # minutes per km, minutes per package
b = 5                                    # intercept in minutes

print(X.shape, w.shape)   # (3, 2) (2,)
print(X @ w + b)          # [19 22 34]

NumPy stores w as a one-dimensional array of length 2 rather than a 2×12 \times 1 column, and adding the number b broadcasts it to every entry, which is the same as adding 5 15\,\mathbf{1}.

Linear prediction

Definition For a feature vector x\mathbf{x} in Rp\mathbb{R}^p, coefficients w\mathbf{w} in Rp\mathbb{R}^p and an intercept bb, the linear prediction is

y^=x⊤w+b=∑j=1pwjxj+b.\begin{aligned} \hat{y} &= \mathbf{x}^{\top}\mathbf{w} + b \\[0.4em] &= \sum_{j=1}^{p} w_j x_j + b . \end{aligned}

For nn observations the predictions are y^=Xw+b 1\hat{\mathbf{y}} = X\mathbf{w} + b\,\mathbf{1}, with XX of shape n×pn \times p, w\mathbf{w} with pp entries and y^\hat{\mathbf{y}} with nn entries. As a function of x\mathbf{x} the map is linear in the strict sense only when b=0b = 0. With b≠0b \ne 0 it is affine, a linear map plus a constant, and “linear model” is the usual name that includes the intercept. For a fixed x\mathbf{x} the prediction is linear in the parameters bb and w\mathbf{w}. Raising feature jj by one unit while every other feature stays fixed changes y^\hat{y} by exactly wjw_j.

In plain words Each feature contributes its value times its coefficient, and the contributions plus the intercept add up to the prediction. A coefficient describes how the rule responds to its inputs. It is not evidence that changing the feature in the world would change the outcome by that amount, because a coefficient estimated from data can reflect correlated features, omitted variables or a narrow range of observations.

Example At fixed distance, one more package raises the prediction by 3 minutes, so 4 kilometers with 3 packages gives 5+8+9=225 + 8 + 9 = 22 minutes instead of 19. Doubling both features of the delivery (4,2)⊤(4, 2)^{\top} to (8,4)⊤(8, 4)^{\top} gives 33 minutes, not twice 19, because the intercept is added once. The intercept of 5 minutes is the prediction at zero distance and zero packages, a case that may never occur. Units are part of the rule as well. With distance in meters the distance coefficient must become 0.0020.002 minute per meter, so that 4000⋅0.002=84000 \cdot 0.002 = 8 minutes as before. Keeping the coefficient 2 would make the distance contribution 8000 minutes.

Check yourself

  1. Another constructed rule uses an intercept of 6 minutes, 1.5 minutes per kilometer and 2 minutes per package. Predict the time for a delivery of 8 kilometers with 4 packages, and list the contribution of each part.
Show the answer

Distance contributes 1.5⋅8=121.5 \cdot 8 = 12 minutes, the packages contribute 2⋅4=82 \cdot 4 = 8 minutes and the intercept contributes 6 minutes. The prediction is 12+8+6=2612 + 8 + 6 = 26 minutes. Every term is in minutes, so the sum is meaningful.

  1. The same rule is applied to a batch of five deliveries with the same two features. Give the shapes of XX, w\mathbf{w}, XwX\mathbf{w} and the intercept term, and then the shapes in the augmented form. A classmate adds 5⋅6=305 \cdot 6 = 30 minutes to each prediction because there are five rows. What went wrong?
Show the answer

The design matrix is 5×25 \times 2, the coefficient vector is 2×12 \times 1 and the product XwX\mathbf{w} is 5×15 \times 1. The intercept term 6 16\,\mathbf{1} is also 5×15 \times 1, a vector of five entries that each equal 6. In the augmented form the matrix X~\tilde{X} is 5×35 \times 3, with the ones column first, and θ=(6,1.5,2)⊤\boldsymbol{\theta} = (6, 1.5, 2)^{\top} is 3×13 \times 1, so X~θ\tilde{X}\boldsymbol{\theta} is again 5×15 \times 1 and holds the same five predictions.

The classmate confused the number of rows with the number of times the intercept is added. Each row receives the intercept once, so each prediction gets 6 minutes, not 30.

  1. A software update reorders the columns of the delivery table so that package count comes first, but the model still uses w=(2,3)⊤\mathbf{w} = (2, 3)^{\top} and b=5b = 5. What predictions does it give for the three deliveries, and why does a shape check not catch the error?
Show the answer

The new rows are (2,4)(2, 4), (1,7)(1, 7) and (3,10)(3, 10). The predictions become 2⋅2+3⋅4+5=212 \cdot 2 + 3 \cdot 4 + 5 = 21, then 2⋅1+3⋅7+5=282 \cdot 1 + 3 \cdot 7 + 5 = 28 and 2⋅3+3⋅10+5=412 \cdot 3 + 3 \cdot 10 + 5 = 41 minutes, instead of 19, 22 and 34.

The matrix is still 3×23 \times 2 and the coefficient vector still has 2 entries, so the product is defined and has the right shape. What broke is the meaning, since the coefficient in minutes per kilometer now multiplies a package count. Storing the feature names, order and units with the model, and checking them before predicting, catches this error.

How this lesson connects