-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_regression.py
78 lines (57 loc) · 2.36 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
from development import Development
from sklearn.utils import shuffle
DEBUG: bool = False
class LogisticRegression:
def __init__(self, h=0.0001, l=0.001, epochs=800):
self.h = h
self.l = l
self.epochs = epochs
self.weights = None # weights vector
def _sigmoid(self, x):
if x < 0:
return np.exp(x) / (1 + np.exp(x))
return 1.0 / (1.0 + np.exp(-x))
def fit(self, X, y):
n_data, n_features = X.shape
self.weights = np.random.randn(n_features) # start with random weights
for epoch in range(self.epochs):
if DEBUG:
total_loss = 0
print(epoch)
# Shuffle training examples
X, y = shuffle(X, y, random_state=epoch)
for i in range(n_data):
# Compute prediction and gradient loss
# for current training example
prediction = self._sigmoid(np.dot(self.weights, X[i]))
error = y[i] - prediction
gradient_loss = error * X[i]
# Calculate gradient for L2 regularization
gradient_regularization = 2 * self.l * self.weights
# Calculate weight update based on gradient loss
# and gradient regularization
weight_update = self.h * (gradient_loss - gradient_regularization)
self.weights += weight_update
if DEBUG:
# Add loss for monitoring
total_loss += error**2
if np.isnan(np.sum(self.weights)):
print("NaN values detected during training.")
print(f"epoch {epoch} i {i}")
return
if DEBUG:
# Print average loss for monitoring
average_loss = total_loss / n_data
print(f"Epoch {epoch}, Average Loss: {average_loss}")
def predict(self, x):
predicted_classes = []
for i in range(len(x)):
prediction = self._sigmoid(np.dot(self.weights, x[i]))
predicted_classes.append(1 if prediction > 0.5 else 0)
return np.array(predicted_classes)
def main():
development = Development()
development.evaluate_classifier(LogisticRegression(h=0.0001, l=0.001, epochs=800))
if __name__ == "__main__":
main()