Welcome. This course builds a working mathematical backbone for conventional machine learning: enough to inspect a proposed model, reason about its behavior, and communicate precisely with engineers. This first module focuses on the compact linear-algebra notation behind many models and the optimization methods used to fit them.
In this lesson, you will move from the familiar scalar equation of a linear model to two equivalent representations:
- a dot product for one prediction;
- a matrix-vector product for predictions across an entire dataset.
The notation is compact, but it is not merely shorthand. It makes clear what a model consumes, what its parameters are, and which dimensions must agree before a pipeline can produce valid predictions.
One observation: a prediction is a weighted sum
Suppose a model estimates the weekly number of support tickets for a customer account from two features:
- : active users, measured in thousands;
- : whether a campaign is running, encoded as or .
A linear prediction rule might be
Here:
- is the model’s prediction;
- is the intercept, the baseline prediction;
- and are coefficients or weights;
- each feature contributes its observed value multiplied by its coefficient.
For example, let
For an account with thousand active users and an active campaign:
The interpretation is conditional on the other features being held fixed. In this model, one additional thousand active users changes the predicted ticket count by ; enabling the campaign changes it by .
A linear model is therefore linear in its parameters: it combines feature values through weighted addition. The features themselves can still be engineered transformations. For instance, including a feature such as allows curved behavior with respect to the original variable while retaining a model that is linear in its coefficients.
Folding the intercept into the feature vector
The intercept is handled neatly by defining a constant feature . For one observation, write the augmented feature vector and coefficient vector as
The prediction becomes a dot product:
where is the number of non-intercept features.
For the support-ticket example,
and hence
The dot product is the fundamental unit of a linear-model prediction: match each feature with its corresponding weight, multiply, and add.
This has an important operational consequence. The order of columns is part of the model contract. If the feature vector is supplied as while the coefficient vector expects , the dimensions still match and the computation succeeds, but the prediction is semantically wrong. Schema enforcement and feature definitions matter as much as successful matrix operations.
Matrix Multiplication — Topic 19 of Machine Learning Foundations
Watch “Matrix Multiplication — Topic 19 of Machine Learning Foundations” by Jon Krohn for a visual refresher on shape compatibility, the row-by-vector calculation, and its direct use in regression.
Start with the shape rule to fix the basic constraint: the first operand’s column count must equal the second operand’s row count. Then watch matrix vector multiplication, following how each row produces one output through a dot product. Finally, skip to the regression connection and focus on why rows represent observations and why a column of ones represents the intercept.
The highlighted row and column in the image below show precisely the calculation for one entry of a matrix product: a row from the left matrix is paired with a column from the right matrix.

From one prediction to a whole batch
A model is ordinarily evaluated or deployed over many observations. Rather than write one dot product per account, we stack observations into a design matrix.
Suppose there are observations and features, excluding the intercept. Each row is one observation; each non-intercept column is one feature:
Its shape is
The parameter vector has one coefficient for each column:
The predicted values for every observation are then
The shapes explain why this is valid:
The output contains exactly one prediction for each observation.
| Object | Meaning | Shape |
|---|---|---|
| Design matrix: rows are observations, columns are features | ||
| Intercept and feature coefficients | ||
| Batch of model predictions |
The inner dimensions, both , must match. The outer dimensions determine the output shape. This is the most reliable mental check for matrix multiplication.
5.4 - A Matrix Formulation of the Multiple Regression Model | STAT 462
Read Penn State STAT 462’s “A Matrix Formulation of the Multiple Regression Model” to consolidate the connection between individual regression equations, the design matrix, and row-by-column matrix multiplication.
In the section “A matrix formulation of the multiple regression model,” read from the matrix-model setup. Focus on the fact that the single equation represents n scalar equations simultaneously, and note the stated dimensions of \mathbf{X}, \boldsymbol{\beta}, the target vector, and the error vector. Then read the section “Matrix multiplication,” beginning with the paragraph that starts “Okay, now that we know when we can multiply two matrices together” through the multiplication rule, and continue through the numerical matrix-product example before “Matrix addition.” Track one output entry at a time: its row chooses an observation-side set of values, and its column chooses the matching values from the second operand.
Working through a batch prediction
Return to the ticket model, and consider two accounts:
| Account | Active users, thousands | Campaign active |
|---|---|---|
| A | 4 | 1 |
| B | 7 | 0 |
Including the intercept column, the design matrix is
and the parameter vector is
Therefore,
Nothing mysterious happens in the batch operation. The first row produces Account A’s prediction, and the second row produces Account B’s prediction. Matrix multiplication simply performs the same dot-product rule once per row.
In NumPy, this is expressed with the matrix-multiplication operator:
import numpy as np
X = np.array([
[1.0, 4.0, 1.0],
[1.0, 7.0, 0.0]
])
beta = np.array([20.0, 3.0, 8.0])
y_hat = X @ beta
# array([40., 41.])
For arrays, @ communicates matrix multiplication directly. In contrast, X * beta performs elementwise multiplication through broadcasting; it produces a matrix of separate products rather than the vector of summed predictions. The final summation across each row is essential.
NumPy commonly represents beta above as a one-dimensional array of shape , and correspondingly returns y_hat with shape . Mathematically, it is useful to retain the column-vector convention because it makes every multiplication and dimension explicit.
Predictions, observed targets, and errors
It is worth separating three related expressions that are often compressed into one notation.
A fitted model makes deterministic predictions, given a feature matrix and fitted coefficient vector:
The observed targets are the actual ticket counts, prices, durations, or other measured outcomes:
The difference between them is the residual vector:
In a statistical model, one commonly writes
This says that observed outcomes comprise a systematic linear component plus an error term. At this stage, treat it as a modeling statement, not as an assertion that the model predicts errors. For prediction, the central object is . Later lessons will address how coefficients are fitted by minimizing losses based on residuals.
The same core computation also appears in classification. A linear classifier first produces a score
Logistic regression subsequently transforms that score into a probability. The interpretation of the output changes, but the feature–weight dot product remains the computational backbone.
A compact review checklist
When reading a linear-model proposal, notebook, or model-serving specification, verify these points:
- Prediction unit: What does one row represent: a customer, transaction, device-day, or another unit?
- Feature definition: Which columns appear in , in what order, with what units and encodings?
- Intercept convention: Is a column of ones included explicitly, or is an intercept stored separately by the modeling library?
- Parameter alignment: Does every feature column have exactly one corresponding coefficient?
- Shape compatibility: If is , is the parameter vector ?
- Output meaning: Is a regression prediction, a classifier score, or an intermediate quantity?
These checks catch both obvious matrix-shape errors and more dangerous semantic errors, such as swapped feature columns or inconsistent feature transformations between training and production.
A linear-model prediction is a weighted sum. For one observation, it is a dot product:
For a dataset, stacking observations as rows gives the equivalent batch computation:
The column of ones incorporates the intercept, and the shape rule ensures the operation is valid. Most importantly, the mathematical alignment between feature columns and coefficients is also a data-contract requirement in a real ML system.
Next, we will use this notation to compute gradients of scalar loss functions with respect to vector-valued model parameters—the step that turns a prediction rule into a trainable model.
Can't find a good explanation? Sign up and we'll make it for you
Sign up