Skip to content

Instantly share code, notes, and snippets.

@ld86
Created February 4, 2016 19:13
Show Gist options
  • Save ld86/16db3d5c0d8dfec37569 to your computer and use it in GitHub Desktop.
Save ld86/16db3d5c0d8dfec37569 to your computer and use it in GitHub Desktop.
LS
class Regression:
def __init(self):
pass
def __loss(self, X, y, coefs):
l = (y - np.dot(X, coefs))
return np.dot(l.T, l)
def fit(self, X, y):
n_elems, n_coefs = X.shape
X_with_ones = np.hstack([np.ones((n_elems, 1)), X])
self.coefs = np.ones((n_coefs + 1, 1))
for i in xrange(10):
print(self.__loss(X_with_ones, y, self.coefs))
grad = self.__grad(X_with_ones, y, self.coefs)
self.coefs += 0.01 * grad
def __grad(self, X, y, coefs):
return 2 * np.dot(X.T, (y - np.dot(X, coefs)))
def predict(self, X):
n_elems, n_coefs = X.shape
X_with_ones = np.hstack([np.ones((n_elems, 1)), X])
return np.dot(X_with_ones, self.coefs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment