Linear regression finds the weight vector that minimizes the sum of squared errors between predictions and targets. The closed-form solution (the normal equation) computes the optimal weights directly using matrix algebra, without iterative optimization.
Given a feature matrix X and a target vector y, compute the weight vector w using the normal equation.
Where X is the n×d feature matrix (n samples, d features), y is the n-dimensional target vector, and w is the d-dimensional weight vector.
Input: X=[[1],[2],[3]], y=[2,4,6]
Output: [2.0]
The line y=2x perfectly fits the data.
Input: X=[[1,1],[1,2],[1,3]], y=[3,5,7]
Output: [1.0,2.0]
The first column acts as a bias. The model y=1+2x fits perfectly.
Look into np.linalg.inv for computing (XTX)−1.
NumPy's @ operator chains matrix multiplications, so the entire formula can fit in one expression.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Linear regression finds the weight vector that minimizes the sum of squared errors between predictions and targets. The closed-form solution (the normal equation) computes the optimal weights directly using matrix algebra, without iterative optimization.
Given a feature matrix X and a target vector y, compute the weight vector w using the normal equation.
Where X is the n×d feature matrix (n samples, d features), y is the n-dimensional target vector, and w is the d-dimensional weight vector.
Input: X=[[1],[2],[3]], y=[2,4,6]
Output: [2.0]
The line y=2x perfectly fits the data.
Input: X=[[1,1],[1,2],[1,3]], y=[3,5,7]
Output: [1.0,2.0]
The first column acts as a bias. The model y=1+2x fits perfectly.
Look into np.linalg.inv for computing (XTX)−1.
NumPy's @ operator chains matrix multiplications, so the entire formula can fit in one expression.
Sign in to take notes on this problem
Accepts: array
Accepts: array