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 is an ordered list of real numbers. On this page every vector is a column, and its transpose, written with , is the same numbers written as a row. In a data set with features listed in a fixed order, the feature vector of one observation is
where is the value of feature in its stated unit. A linear model pairs it with a coefficient vector in the same order, so belongs to feature and is measured in units of the prediction per unit of feature . 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 . The rule’s coefficient vector is , in minutes per kilometer and minutes per package. The vector would describe a different delivery, 2 kilometers with 4 packages.
Dot product
Definition For two vectors and in , the dot product is
It is defined only when both vectors have the same number of entries. It is symmetric, , and linear in each vector. As a matrix product, the row times the column is a 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,
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 matrix is a rectangular array of real numbers with rows and columns. Its entry in row and column is written , and its transpose is the matrix with entries . For a data set of observations with features, the design matrix is the matrix whose row is , the feature vector of observation written as a row. Column then holds the values of feature for all observations. The targets form a vector in listed in the same row order. On this page 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
with and . The entry is the distance of the second delivery, and the second column lists the package counts of all three. The transpose is , with one row per feature.
Matrix-vector product
Definition Let be an matrix with rows , and let be a vector in . The product is the vector in whose entry is the dot product of row with ,
The product is defined only when the number of columns of equals the number of entries of , and the shapes combine as . Equivalently, if denotes column of , then , 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 ,
The column view gives the same numbers, . The reversed product is not defined, because a vector cannot multiply a matrix. The product , where is the vector of three ones, is defined and has shape . 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 adds the same number to every prediction,
where is the vector of ones. The same predictions come from the augmented design matrix and the augmented coefficient vector
since . The matrix is and has entries. The first column of , 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,
which equals . Adding 5 to each of the two feature contributions instead would give , 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 column, and adding the number b broadcasts it to every entry, which is the same as adding .
Linear prediction
Definition For a feature vector in , coefficients in and an intercept , the linear prediction is
For observations the predictions are , with of shape , with entries and with entries. As a function of the map is linear in the strict sense only when . With it is affine, a linear map plus a constant, and “linear model” is the usual name that includes the intercept. For a fixed the prediction is linear in the parameters and . Raising feature by one unit while every other feature stays fixed changes by exactly .
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 minutes instead of 19. Doubling both features of the delivery to 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 minute per meter, so that minutes as before. Keeping the coefficient 2 would make the distance contribution 8000 minutes.
Check yourself
- 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 minutes, the packages contribute minutes and the intercept contributes 6 minutes. The prediction is minutes. Every term is in minutes, so the sum is meaningful.
- The same rule is applied to a batch of five deliveries with the same two features. Give the shapes of , , and the intercept term, and then the shapes in the augmented form. A classmate adds minutes to each prediction because there are five rows. What went wrong?
Show the answer
The design matrix is , the coefficient vector is and the product is . The intercept term is also , a vector of five entries that each equal 6. In the augmented form the matrix is , with the ones column first, and is , so is again 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.
- A software update reorders the columns of the delivery table so that package count comes first, but the model still uses and . 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 , and . The predictions become , then and minutes, instead of 19, 22 and 34.
The matrix is still 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.