#!/usr/bin/env python # coding: utf-8 # # Cost function # In[1]: import numpy as np # In[2]: 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: # In[3]: 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))