Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created May 4, 2018 09:39
Show Gist options
  • Save lmiller1990/c103cabceccd5fb1c1a7631436b964f5 to your computer and use it in GitHub Desktop.
Save lmiller1990/c103cabceccd5fb1c1a7631436b964f5 to your computer and use it in GitHub Desktop.
import numpy as np
X = np.array([[1], [2], [3]])
y = np.array([[1], [2.5], [3.5]])
get_theta = lambda theta: np.array([[0, theta]])
thetas = list(map(get_theta, [0.5, 1.0, 1.5]))
X = np.hstack([np.ones([3, 1]), X])
def cost(X, y, theta):
inner = np.power(((X @ theta.T) - y), 2)
return np.sum(inner) / (2 * len(X))
for i in range(len(thetas)):
print(cost(X, y, thetas[i]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment