-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogisticregression.py
240 lines (185 loc) · 8.43 KB
/
logisticregression.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# organize imports
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# ignore all warnings
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
# load scikit-learn's breast cancer dataset
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
print(data.keys())
print("No.of.data points (rows) : {}".format(len(data.data)))
print("No.of.features (columns) : {}".format(len(data.feature_names)))
print("No.of.classes : {}".format(len(data.target_names)))
print("Class names : {}".format(list(data.target_names)))
# view the datatype of each column
df = pd.DataFrame(data.data)
print(df.dtypes)
# split the dataset into training and testing
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.20, random_state=9)
print("X_train : " + str(X_train.shape))
print("y_train : " + str(y_train.shape))
print("X_test : " + str(X_test.shape))
print("y_test : " + str(y_test.shape))
#---------------------------------------------
# logistic regression without regularization
#---------------------------------------------
def sigmoid(score):
return (1 / (1 + np.exp(-score)))
def predict_probability(features, weights):
score = np.dot(features, weights)
return sigmoid(score)
def feature_derivative(errors, feature):
derivative = np.dot(np.transpose(errors), feature)
return derivative
def l2_feature_derivative(errors, feature, weight, l2_penalty, feature_is_constant):
derivative = np.dot(np.transpose(errors), feature)
if not feature_is_constant:
derivative -= 2 * l2_penalty * weight
return derivative
def compute_log_likelihood(features, labels, weights):
indicators = (labels==+1)
scores = np.dot(features, weights)
ll = np.sum((np.transpose(np.array([indicators]))-1)*scores - np.log(1. + np.exp(-scores)))
return ll
def l2_compute_log_likelihood(features, labels, weights, l2_penalty):
indicators = (labels==+1)
scores = np.dot(features, weights)
ll = np.sum((np.transpose(np.array([indicators]))-1)*scores - np.log(1. + np.exp(-scores))) - (l2_penalty * np.sum(weights[1:]**2))
return ll
# logistic regression without L2 regularization
def logistic_regression(features, labels, lr, epochs):
# add bias (intercept) with features matrix
bias = np.ones((features.shape[0], 1))
features = np.hstack((bias, features))
# initialize the weight coefficients
weights = np.zeros((features.shape[1], 1))
logs = []
# loop over epochs times
for epoch in range(epochs):
# predict probability for each row in the dataset
predictions = predict_probability(features, weights)
# calculate the indicator value
indicators = (labels==+1)
# calculate the errors
errors = np.transpose(np.array([indicators])) - predictions
# loop over each weight coefficient
for j in range(len(weights)):
# calculate the derivative of jth weight cofficient
derivative = feature_derivative(errors, features[:,j])
weights[j] += lr * derivative
# compute the log-likelihood
ll = compute_log_likelihood(features, labels, weights)
logs.append(ll)
import matplotlib.pyplot as plt
x = np.linspace(0, len(logs), len(logs))
fig = plt.figure()
plt.plot(x, logs)
fig.suptitle('Training the classifier (without L2)')
plt.xlabel('Epoch')
plt.ylabel('Log-likelihood')
fig.savefig('train_without_l2.jpg')
plt.show()
return weights
# logistic regression with L2 regularization
def l2_logistic_regression(features, labels, lr, epochs, l2_penalty):
# add bias (intercept) with features matrix
bias = np.ones((features.shape[0], 1))
features = np.hstack((bias, features))
# initialize the weight coefficients
weights = np.zeros((features.shape[1], 1))
logs = []
# loop over epochs times
for epoch in range(epochs):
# predict probability for each row in the dataset
predictions = predict_probability(features, weights)
# calculate the indicator value
indicators = (labels==+1)
# calculate the errors
errors = np.transpose(np.array([indicators])) - predictions
# loop over each weight coefficient
for j in range(len(weights)):
isIntercept = (j==0)
# calculate the derivative of jth weight cofficient
derivative = l2_feature_derivative(errors, features[:,j], weights[j], l2_penalty, isIntercept)
weights[j] += lr * derivative
# compute the log-likelihood
ll = l2_compute_log_likelihood(features, labels, weights, l2_penalty)
logs.append(ll)
import matplotlib.pyplot as plt
x = np.linspace(0, len(logs), len(logs))
fig = plt.figure()
plt.plot(x, logs)
fig.suptitle('Training the classifier (with L2)')
plt.xlabel('Epoch')
plt.ylabel('Log-likelihood')
fig.savefig('train_with_l2.jpg')
plt.show()
return weights
# logistic regression without regularization
def lr_without_regularization():
# hyper-parameters
learning_rate = 1e-7
epochs = 500
# perform logistic regression and get the learned weights
learned_weights = logistic_regression(X_train, y_train, learning_rate, epochs)
# make predictions using learned weights on testing data
bias_train = np.ones((X_train.shape[0], 1))
bias_test = np.ones((X_test.shape[0], 1))
features_train = np.hstack((bias_train, X_train))
features_test = np.hstack((bias_test, X_test))
test_predictions = (predict_probability(features_test, learned_weights).flatten()>0.5)
train_predictions = (predict_probability(features_train, learned_weights).flatten()>0.5)
print("Accuracy of our LR classifier on training data: {}".format(accuracy_score(np.expand_dims(y_train, axis=1), train_predictions)))
print("Accuracy of our LR classifier on testing data: {}".format(accuracy_score(np.expand_dims(y_test, axis=1), test_predictions)))
# using scikit-learn's logistic regression classifier
model = LogisticRegression(random_state=9)
model.fit(X_train, y_train)
sk_test_predictions = model.predict(X_test)
sk_train_predictions = model.predict(X_train)
print("Accuracy of scikit-learn's LR classifier on training data: {}".format(accuracy_score(y_train, sk_train_predictions)))
print("Accuracy of scikit-learn's LR classifier on testing data: {}".format(accuracy_score(y_test, sk_test_predictions)))
#visualize_weights(np.squeeze(learned_weights), 'weights_without_l2.jpg')
# logistic regression with regularization
def lr_with_regularization():
# hyper-parameters
learning_rate = 1e-7
epochs = 300000
l2_penalty = 0.001
# perform logistic regression and get the learned weights
learned_weights = l2_logistic_regression(X_train, y_train, learning_rate, epochs, l2_penalty)
# make predictions using learned weights on testing data
bias_train = np.ones((X_train.shape[0], 1))
bias_test = np.ones((X_test.shape[0], 1))
features_train = np.hstack((bias_train, X_train))
features_test = np.hstack((bias_test, X_test))
test_predictions = (predict_probability(features_test, learned_weights).flatten()>0.5)
train_predictions = (predict_probability(features_train, learned_weights).flatten()>0.5)
print("Accuracy of our LR classifier on training data: {}".format(accuracy_score(np.expand_dims(y_train, axis=1), train_predictions)))
print("Accuracy of our LR classifier on testing data: {}".format(accuracy_score(np.expand_dims(y_test, axis=1), test_predictions)))
# using scikit-learn's logistic regression classifier
model = LogisticRegression(random_state=9)
model.fit(X_train, y_train)
sk_test_predictions = model.predict(X_test)
sk_train_predictions = model.predict(X_train)
print("Accuracy of scikit-learn's LR classifier on training data: {}".format(accuracy_score(y_train, sk_train_predictions)))
print("Accuracy of scikit-learn's LR classifier on testing data: {}".format(accuracy_score(y_test, sk_test_predictions)))
visualize_weights(np.squeeze(learned_weights), 'weights_with_l2.jpg')
# visualize weight coefficients
def visualize_weights(weights, title):
import matplotlib.pyplot as plt
x = np.linspace(0, len(weights), len(weights))
fig = plt.figure()
plt.bar(x, weights, align='center', alpha=0.5)
plt.xlabel("Weight Index (Feature Column Number)")
plt.ylabel("Weight Coefficient")
plt.title('Visualizing Weights')
plt.tight_layout()
fig.savefig(title)
plt.show()
lr_with_regularization()