Skip to content

Instantly share code, notes, and snippets.

View final_file
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('data.csv')
# print(data)
def cost_function(m, b, points):
the_total_error = 0
sum_error = 0
View gist:aa7b173eb0a7a29b9777cd757f596104
for i in range(times_to_iterate):
m, b = gradient_descent(m, b, data, Learning_rate)
plt.scatter(data['weight'], data['height'])
plt.plot(list(range(0, 5)), [m * x + b for x in range(0 , 5)], color="red")
plt.show()
View gradient_descent
def gradient_descent(m_now, b_now, points, L):
the_slope_of_thecost_depending_on_m = 0
the_slope_of_thecost_depending_on_b = 0
n = len(points)
for i in range(n):
x = points.iloc[i].weight
y = points.iloc[i].height
the_slope_of_thecost_depending_on_m += - (2/n) * x * (y - (m_now * x + b_now))
View Initialise
m = 0
b = 0
Learning_rate = 0.00000005
cost = 0
times_to_iterate = 1000
View cost_function
def cost_function(m, b, points):
the_total_error = 0
sum_error = 0
for i in range(len(points)):
x = points.iloc[i].weight
y = points.iloc[i].height
the_total_error += (y - (m * x + b)) ** 2
sum_error += the_total_error
print(sum_error)
return sum_error
View gist:1187df57b8d7c0aa8ef3f53b44b25444
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#Print our data
data = pd.read_csv('data.csv')
print(data)
plt.scatter(data['weight'], data['height'])
plt.xlabel("Weight")