-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogistic_regression.py
39 lines (28 loc) · 953 Bytes
/
logistic_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import numpy as np
class LogisticRegression:
def __init__(self, learning_rate=.05, n_iter=100):
self.learning_rate = learning_rate
self.n_iter = n_iter
def activation(self,z):
return 1. / (1. + np.exp(-z))
def net_input(self, X):
return np.dot(self.weights, X.T)+self.bias
def predict(self, X):
z = self.net_input(X)
a = self.activation(z)
y_pred = (a>=0.5).astype(int)
return y_pred
def derivate_costfunc(self, X, y):
z = self.net_input(X)
a = self.activation(z)
errors = (y - a)
dw = X.T.dot(errors)
db = errors.sum()
return (dw,db)
def fit(self, X, y):
self.weights = np.zeros(X.shape[1])
self.bias = 0
for _ in range(self.n_iter):
dw, db = self.derivate_costfunc(X, y)
self.weights += self.learning_rate*dw
self.bias += self.learning_rate*db