Linear Regression Using the Normal Equation
Problem statement
Write a Python function linear_regression_normal_equation(X, y) that performs linear regression using the normal equation. It takes a matrix X (features, one row per example) and a vector y (targets) and returns the coefficient vector as a list. X is used exactly as given: if an intercept is wanted, X already contains a column of ones, so do not add one yourself. Assume $X^T X$ is invertible.
Round each coefficient to four decimal places; -0.0 is a valid result for rounding a very small number.
Example
Input:
X = [[1, 1], [1, 2], [1, 3]], y = [1, 2, 3]
Output:
[0.0, 1.0]
Reasoning: The linear model is y = 0.0 + 1.0*x, perfectly fitting the input data.
Show hint
If you set the gradient of the squared error with respect to the coefficients to zero, what linear system do you get?
Relevant roles: ML Engineer, Data Scientist, Applied Scientist and Research Scientist.
Related free problems
- Multi-Head AttentionMedium
- Masked Self-AttentionMedium