Skip to content

Instantly share code, notes, and snippets.

@starhopp3r
Created September 26, 2017 02:12
Show Gist options
  • Save starhopp3r/ccbb4f2126a9be9e82f05fc444c60d02 to your computer and use it in GitHub Desktop.
Save starhopp3r/ccbb4f2126a9be9e82f05fc444c60d02 to your computer and use it in GitHub Desktop.
def step_gradient(c_current, m_current, points, learning_rate):
# Gradient descent
c_gradient = 0
m_gradient = 0
N = float(len(points))
# Iterate
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
c_gradient += -(2 / N) * (y - ((m_current * x) + c_current))
m_gradient += -(2 / N) * x * (y - ((m_current * x) + c_current))
# Update m and c
new_c = c_current - (learning_rate * c_gradient)
new_m = m_current - (learning_rate * m_gradient)
return [new_c, new_m]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment