import numpy as np
def costFunction(X, y, theta):
m = len(y)
hypothesis = X @ theta
err = hypothesis - y
return ((1 / (2 * m)) * (np.transpose(err) @ err).item((0, 0)))
Let's run it against some fake housing data:
X = np.array([
[1, 3000],
[1, 4000],
[1, 5000],
])
y = np.array([
[1550000],
[2050000],
[2550000],
])
bad_theta = np.array([
[0],
[0],
])
good_theta = np.array([
[50000],
[500],
])
print("With bad theta values - cost:", costFunction(X, y, bad_theta))
print("With good theta values - cost:", costFunction(X, y, good_theta))
With bad theta values - cost: 2184583333333.3333 With good theta values - cost: 0.0