-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogistic_regression.py
202 lines (156 loc) · 6.39 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import datetime
import os
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
class LogisticRegressionScratch:
def __init__(self, lr=0.01, num_iter=10000, fit_intercept=True, verbose=False):
self.lr = lr
self.num_iter = num_iter
self.fit_intercept = fit_intercept
self.verbose = verbose
# weights initialization
self.theta = np.zeros(X.shape[1])
@staticmethod
def __add_intercept(x):
intercept = np.ones((x.shape[0], 1))
return np.concatenate((intercept, x), axis=1)
@staticmethod
def __sigmoid(z):
return 1 / (1 + np.exp(-z))
@staticmethod
def __loss(h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
def fit(self, X, y):
if self.fit_intercept:
X = self.__add_intercept(X)
for i in range(self.num_iter):
z = np.dot(X, self.theta)
h = self.__sigmoid(z)
gradient = np.dot(X.T, (h - y)) / y.size
self.theta -= self.lr * gradient
z = np.dot(X, self.theta)
h = self.__sigmoid(z)
loss = self.__loss(h, y)
if self.verbose == True and i % 10000 == 0:
print(f'loss: {loss} \t')
def predict_prob(self, X):
if self.fit_intercept:
X = self.__add_intercept(X)
return self.__sigmoid(np.dot(X, self.theta))
def predict(self, X):
return self.predict_prob(X).round()
iris = datasets.load_iris()
def plotDataset():
# The indices of the features that we are plotting
x_index = 0
y_index = 1
# this formatter will label the colorbar with the correct target names
formatter = plt.FuncFormatter(lambda i, *args: iris.target_names[int(i)])
plt.figure(figsize=(10, 8))
plt.scatter(iris.data[:, x_index], iris.data[:, y_index], c=iris.target)
plt.colorbar(ticks=[0, 1, 2], format=formatter)
plt.xlabel(iris.feature_names[x_index])
plt.ylabel(iris.feature_names[y_index])
plt.tight_layout()
plt.show()
# test data with :2 (only sepal length/width) & :4 (all 4 attributes - sepal length/width, petal length/width)
X = iris.data[:, :2]
y = (iris.target != 0) * 1
# figure with 2 types of iris: setosa & virginica
def plotRestrictedDataset():
plt.figure(figsize=(10, 6))
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0 - setosa')
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1 - virginica')
plt.legend()
plt.show()
def sklearnLogisticRegression(iterations):
model = LogisticRegression(C=iterations, solver='lbfgs')
# solver used to avoid FutureWarning on default solver
model.fit(X, y)
preds = model.predict(X)
(preds == y).mean()
print("sklearn accuracy: ", model.score(X, y))
def scratchLogisticRegression(iterations):
model = LogisticRegressionScratch(lr=0.1, num_iter=iterations)
model.fit(X, y)
preds = model.predict(X)
print("scratch accuracy: ", (preds == y).mean())
def plotScratchLogisticRegression(iterations):
model = LogisticRegressionScratch(lr=0.1, num_iter=iterations)
model.fit(X, y)
preds = model.predict(X)
plt.figure(figsize=(10, 6))
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0 - setosa')
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1 - virginica')
plt.legend()
x1_min, x1_max = X[:, 0].min(), X[:, 0].max(),
x2_min, x2_max = X[:, 1].min(), X[:, 1].max(),
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
grid = np.c_[xx1.ravel(), xx2.ravel()]
probs = model.predict_prob(grid).reshape(xx1.shape)
plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors='black')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.show()
# tests: 1.000, 50.000, 200.000, 300.000
numberOfIterations = 50000
print("number of iterations: ", numberOfIterations)
def scratch(numberOfIterations):
scratchMethodStart = datetime.datetime.now()
scratchLogisticRegression(numberOfIterations)
scratchMethodEnd = datetime.datetime.now()
print("scratch duration: ", scratchMethodEnd - scratchMethodStart)
def scikit(numberOfIterations):
skMethodStart = datetime.datetime.now()
sklearnLogisticRegression(numberOfIterations)
skMethodEnd = datetime.datetime.now()
print("sklearn duration: ", skMethodEnd - skMethodStart)
def display_title_bar():
# Clears the terminal screen, and displays a title bar.
os.system('clear')
print("\t**********************************************")
print("\t*********** Logistic Regression ************")
print("\t**********************************************")
choice = ''
while choice != 'quit':
display_title_bar()
# Let users know what they can do.
print("\n[1] See logistic regression from scratch.")
print("[2] See logistic regression from scikit.")
print("[3] Compare logistic regression implementations.")
print("[4] See logistic regression from scratch plot.")
print("[5] See Iris dataset plot.")
print("[6] See binary Iris dataset plot.")
print("[quit] Quit.")
choice = input("What would you like to do? ")
# Respond to the user's choice.
if choice == '1':
numberOfIterations = int(float(input("How many iterations should we try? \n")))
scratch(numberOfIterations)
input("Press Enter to continue..")
elif choice == '2':
numberOfIterations = int(float(input("How many iterations should we try? \n")))
scikit(numberOfIterations)
input("Press Enter to continue..")
elif choice == '3':
numberOfIterations = int(float(input("How many iterations should we try? \n")))
scratch(numberOfIterations)
scikit(numberOfIterations)
input("Press Enter to continue..")
elif choice == '4':
numberOfIterations = int(float(input("How many iterations should we try? \n")))
plotScratchLogisticRegression(numberOfIterations)
input("Press Enter to continue..")
elif choice == '5':
plotDataset()
input("Press Enter to continue..")
elif choice == '6':
plotRestrictedDataset()
input("Press Enter to continue..")
elif choice == 'quit':
print("\nThanks for coming. Bye.")
else:
print("\nI didn't understand that choice.\n")
input("Press Enter to continue..")