Skip to content

Instantly share code, notes, and snippets.

@dtellogaete
Last active February 13, 2020 15:19
Show Gist options
  • Save dtellogaete/998d9fcf2efee06ed40110b5bf5ac9e9 to your computer and use it in GitHub Desktop.
Save dtellogaete/998d9fcf2efee06ed40110b5bf5ac9e9 to your computer and use it in GitHub Desktop.
Linear Regression Gradient Descent Python
class LinearRegressionGD(object):
def __init__(self, l_rate = 0.1, n_iter =10000):
self.l_rate = l_rate
self.n_iter = n_iter
def fit(self, X, y, theta):
self.theta = theta
X_value = X[:,1].reshape(-1, 1)
const = self.l_rate*(1/X.shape[0])
for i in range(0, self.n_iter):
h = X.dot(self.theta)
self.theta[0] = self.theta[0]-const*sum(h-y)
self.theta[1] = self.theta[1]-const*sum((h-y).transpose().dot(X_value))
return self.theta
def predict(self, X):
X_test = X[:, 1]
predict_value = X_test*self.theta[1]+self.theta[0]
return predict_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment