Linear Regression Closed Form
Linear Regression Closed Form
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.
Formula
w=(XTX)−1XTyWhere 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.
Examples
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.
Hint 1
Look into np.linalg.inv for computing (XTX)−1.
Hint 2
NumPy's @ operator chains matrix multiplications, so the entire formula can fit in one expression.
Requirements
- Use numpy to convert X and y to arrays
- Compute the transpose XT
- Compute the products XTX and XTy
- Compute the inverse (XTX)−1
- Multiply the inverse by XTy to get w
- Return a list of floats or a numpy array
Constraints
- X has at least as many rows as columns (n≥d)
- XTX is always invertible
- Return length must equal d, the number of columns in X
- numpy is available as np
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Linear Regression Closed Form
Linear Regression Closed Form
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.
Formula
w=(XTX)−1XTyWhere 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.
Examples
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.
Hint 1
Look into np.linalg.inv for computing (XTX)−1.
Hint 2
NumPy's @ operator chains matrix multiplications, so the entire formula can fit in one expression.
Requirements
- Use numpy to convert X and y to arrays
- Compute the transpose XT
- Compute the products XTX and XTy
- Compute the inverse (XTX)−1
- Multiply the inverse by XTy to get w
- Return a list of floats or a numpy array
Constraints
- X has at least as many rows as columns (n≥d)
- XTX is always invertible
- Return length must equal d, the number of columns in X
- numpy is available as np
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array