Skip to content

Instantly share code, notes, and snippets.

@llSourcell
Created March 13, 2018 19:31
Show Gist options
  • Select an option

  • Save llSourcell/f198dfd4ef5bd373500c9f0950eb54b7 to your computer and use it in GitHub Desktop.

Select an option

Save llSourcell/f198dfd4ef5bd373500c9f0950eb54b7 to your computer and use it in GitHub Desktop.
def linear_regression(X, y, m_current=0, b_current=0, epochs=1000, learning_rate=0.0001):
N = float(len(y))
for i in range(epochs):
y_current = (m_current * X) + b_current
cost = sum([data**2 for data in (y-y_current)]) / N
m_gradient = -(2/N) * sum(X * (y - y_current))
b_gradient = -(2/N) * sum(y - y_current)
m_current = m_current - (learning_rate * m_gradient)
b_current = b_current - (learning_rate * b_gradient)
return m_current, b_current, cost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment