Skip to content

Instantly share code, notes, and snippets.

@llSourcell
Last active July 4, 2018 01:18
Show Gist options
  • Select an option

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

Select an option

Save llSourcell/0fdfb5893bc31c169f12f7fc33ee077e to your computer and use it in GitHub Desktop.
//Our linear regression model is y = mx + b
//Our parameters are thus m and b. What are the optimal values...
//Lets use gradient descent to find out!
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