Multi-Output Linear Prediction
Extending Linear Prediction to Multiple Inputs and Outputs
This module extends the linear prediction framework from the case of a single input variable and a single output variable to multiple inputs and multiple outputs. It introduces the matrix formulation, the concept of training a predictor, and the closed-form solution for the optimal weights.
Introduction: From Simple to Multiple Variables
In the previous section, predicting a single output y from a single input x used a formula:
Now we consider two general cases:
One input, multiple outputs — straightforward: create separate formulas for each output.
Multiple inputs, one or multiple outputs — requires a matrix approach.
One Input, Multiple Outputs
If there is only one input x but we want to predict several outputs y1,y2,y3, we simply build separate prediction functions:
Each output uses its own formula (e.g., linear regression with its own slope and intercept). This case does not introduce new complexity; it is just multiple independent models.
Multiple Inputs, One or Multiple Outputs (The Core Problem)
When the input consists of several features (multiple columns) — for example, age, years with employer, income — and we want to predict one or more outcomes (e.g., credit score), we need a matrix representation.
Each row in the input table corresponds to one observation (e.g., one person).
Each column is a feature (dimension).
The output can be a single column (vector) or multiple columns (matrix).
Let the input matrix be X with n rows (observations) and d columns (features). The desired output matrix Y has n rows and m columns (multiple outputs). The goal is to learn a function that maps rows of X to rows of Y, so that for new, unseen rows we can predict their outputs.
The Machine Learning Framework
This structure is the basic machine learning framework used throughout the course:
Input: X (matrix of features)
Training: Use known pairs of (X,Y) to learn a predictor g(X)
Prediction: Apply g(Xnew) to unseen data to obtain Y^new
The data table has:
In‑sample rows: where both inputs and outputs are known (training data).
Out‑of‑sample rows: where only inputs are known; we want to predict the outputs.
Categorical data (e.g., “married”, job title) must be converted to numeric values (e.g., one‑hot encoding) or discarded.
The underlying assumption is that there exists an unknown function f such that Y=f(X), and we aim to approximate f with a model g.
Matrix Formulation of Linear Regression
We extend the simple linear model y^=w1x+w0 to multiple inputs:
Where:
X is an n×(d+1) matrix. The extra column is a column of ones to handle the bias term (y‑intercept).
W is a (d+1)×m matrix of coefficients (weights).
Y^ is the n×m matrix of predicted outputs.
The column of ones ensures that the first weight w0 acts as the bias. For example, for a single row with features [1,25,3,50] and a weight vector [w0,w1,w2,w3], the prediction is:
This is a dot product; w0 becomes the baseline (intercept).
Calculating the Best Weights: The Normal Equation
To find W that minimizes prediction error on the training data, we define the Mean Squared Error (MSE). For a single output (m=1), the error for one row is:
The overall cost function (scalar) is:
In matrix form, for multiple outputs:
where ∥⋅∥F is the Frobenius norm.
To find the W that minimizes E, we take the gradient with respect to W and set it to zero. This yields the normal equation:
Normal Equation (closed‑form solution):
W=(XTX)−1XTY
This is a powerful, tractable result. Despite potentially large n, the matrix (XTX) is only d×d, and its inverse is feasible if d is not too large. The resulting W is d×m.
Dimensionality Check
X: n×d (with the ones column included, d includes the bias)
XTX: d×d (compact)
(XTX)−1: d×d
XTY: d×m
Final W: d×m
Thus, the solution collapses the entire training set into a small weight matrix.
Example with Code (NumPy)
Using the sample data from the lecture (4 people, features: age, years with employer, income in thousands, plus the ones column):
Ones | Age | Years | Income | Credit Score (y) |
|---|---|---|---|---|
1 | 25 | 3 | 50 | 580 |
1 | 23 | 2 | 60 | 580 |
1 | 30 | 5 | 70 | 620 |
1 | 28 | 4 | 65 | 610 |
In Python using NumPy:
import numpy as np
X = np.array([[1, 25, 3, 50],
[1, 23, 2, 60],
[1, 30, 5, 70],
[1, 28, 4, 65]])
Y = np.array([[580],
[580],
[620],
[610]])
W = np.linalg.inv(X.T @ X) @ X.T @ Y
print(W)The computed weight vector (approximate values from the lecture):
Interpretation:
w0=426: baseline credit score (bias)
w1=−0.60: for each additional year of age, credit score decreases by 0.6 points
w2=149: for each additional year with current employer, credit score increases by 149 points
w3=3.7: for each additional $1k income, credit score increases by 3.7 points
This is purely illustrative; real credit scoring would use many more features and sophisticated models.
Using a Library Function (e.g., scikit‑learn)
The same result can be obtained with a high‑level library:
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X[:, 1:], Y) # exclude ones column; library adds bias automatically
W = [model.intercept_, *model.coef_[0]]The code is identical for single or multiple inputs/outputs — the library handles the matrix math internally. This module reveals what is happening “under the hood” when you call such a function.
Summary of the Machine Learning Framework
Input: X (single value, vector, or matrix)
Predictor function: g(X) (e.g., linear: XW)
Training: Use known data to learn the best parameters for g (e.g., W via normal equation)
Prediction: Apply learned g to new X to get Y^
For linear regression, the optimal parameters can be calculated directly (closed‑form). For more complex models, we often use iterative methods (e.g., gradient descent) that start with a guess, evaluate the error, and adjust repeatedly until g(X) approximates the true function f(X) to an acceptable accuracy.
This framework applies to many prediction and classification tasks covered later in the course.

Ask anything
We've read this whole study set, so questions about it get the best answers.