-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogisticRegression.py
More file actions
37 lines (25 loc) · 893 Bytes
/
LogisticRegression.py
File metadata and controls
37 lines (25 loc) · 893 Bytes
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
import numpy as np
def sigmoid(x):
return 1/(1 + np.exp(-x))
class LogisticRegression:
def __init__(self,lr=0.001, epochs=1000):
self.lr = lr
self.epochs = epochs
self.w = None
self.b = None
def fit(self,X,y):
n_samples, n_features = X.shape
self.w = np.zeros(n_features)
self.b = 0
for _ in range(self.epochs):
linear_pred = np.dot(X, self.w) + self.b
predictions = sigmoid(linear_pred)
dw = (1 / n_samples) * np.dot(X.T, (predictions - y))
db = (1 / n_samples) * np.sum(predictions - y)
self.w -= self.lr * dw
self.b -= self.lr * db
def predict(self,X):
linear_pred = np.dot(X, self.w) + self.b
y_pred = sigmoid(linear_pred)
class_pred = [0 if y <= 0.5 else 1 for y in y_pred]
return class_pred