import numpy as np
from numpy.linalg import pinv
def normalEquation(X, y):
Xt = np.transpose(X)
return pinv(Xt @ X) @ Xt @ y
Let's run it against some fake housing data:
actual_theta = np.array([
[100],
[40],
])
X = np.array([
[1, 0.8],
[1, 2.3],
[1, 1.6],
])
y = X @ actual_theta
theta = normalEquation(X, y)
print("Actual theta_0:", actual_theta.item(0, 0), " Normal equation theta_0:", theta.item(0, 0))
print("Actual theta_1:", actual_theta.item(1, 0), " Normal equation theta_1:", theta.item(1, 0))
Actual theta_0: 100 Normal equation theta_0: 99.99999999999994 Actual theta_1: 40 Normal equation theta_1: 40.000000000000085