Skip to content

Instantly share code, notes, and snippets.

@curiousily
Created March 31, 2019 13:09
Show Gist options
  • Save curiousily/9bf2d20f1bc12f509161908beec8cffb to your computer and use it in GitHub Desktop.
Save curiousily/9bf2d20f1bc12f509161908beec8cffb to your computer and use it in GitHub Desktop.
class LogisticRegressor:
def _add_intercept(self, X):
intercept = np.ones((X.shape[0], 1))
return np.concatenate((intercept, X), axis=1)
def predict_probs(self, X):
X = self._add_intercept(X)
return sigmoid(np.dot(X, self.W))
def predict(self, X):
return self.predict_probs(X).round()
def fit(self, X, y, n_iter=100000, lr=0.01):
X = self._add_intercept(X)
self.W = np.zeros(X.shape[1])
for i in range(n_iter):
z = np.dot(X, self.W)
h = sigmoid(z)
gradient = np.dot(X.T, (h - y)) / y.size
self.W -= lr * gradient
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment