Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created May 4, 2018 09:07
Show Gist options
  • Save lmiller1990/64e400aa93f9fe185e571f42c9dfd683 to your computer and use it in GitHub Desktop.
Save lmiller1990/64e400aa93f9fe185e571f42c9dfd683 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
# original data set
X = [1, 2, 3]
y = [1, 2.5, 3.5]
# slope of best_fit_1 is 0.5
# slope of best_fit_2 is 1.0
# slope of best_fit_3 is 1.5
hyps = [0.5, 1.0, 1.5]
# mutiply the original X values by the theta
# to produce hypothesis values for each X
def multiply_matrix(mat, theta):
mutated = []
for i in range(len(mat)):
mutated.append(mat[i] * theta)
return mutated
# calculate cost by looping each sample
# subtract hyp(x) from y
# square the result
# sum them all together
def calc_cost(m, X, y):
total = 0
for i in range(m):
squared_error = (y[i] - X[i]) ** 2
total += squared_error
return total * (1 / (2*m))
# calculate cost for each hypothesis
for i in range(len(hyps)):
hyp_values = multiply_matrix(X, hyps[i])
print("Cost for ", hyps[i], " is ", calc_cost(len(X), y, hyp_values))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment