Linear Regression Using Normal Equation

Easy
Machine Learning

Write a Python function that performs linear regression using the normal equation. The function should take a matrix X (features) and a vector y (target) as input, and return the coefficients of the linear regression model. Round your answer to four decimal places, -0.0 is a valid result for rounding a very small number.

Examples

Example 1:
Input: X = [[1, 1], [1, 2], [1, 3]], y = [1, 2, 3]
Output: [0.0, 1.0]
Explanation: The linear model is y = 0.0 + 1.0*x, perfectly fitting the input data.

Starter Code

import numpy as np
def linear_regression_normal_equation(X: list[list[float]], y: list[float]) -> list[float]:
	# Your code here, make sure to round
	return theta
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews