Created
February 4, 2016 19:11
-
-
Save ld86/a461dc18d061d7e3162b to your computer and use it in GitHub Desktop.
LSE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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