-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailSpamClassifier_LogisiticRegression
257 lines (206 loc) · 7.19 KB
/
EmailSpamClassifier_LogisiticRegression
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import numpy as np
import os
from pylab import *
import matplotlib.pyplot as plt
%matplotlib inline
from helper import *
print('You\'re running python %s' % sys.version.split(' ')[0])
np.random.seed(12)
n_samples = 500
class_one = np.random.multivariate_normal([5, 10], [[1, .25],[.25, 1]], n_samples)
class_one_labels = -np.ones(n_samples)
class_two = np.random.multivariate_normal([0, 5], [[1, .25],[.25, 1]], n_samples)
class_two_labels = np.ones(n_samples)
features = np.vstack((class_one, class_two))
labels = np.hstack((class_one_labels, class_two_labels))
# We can visualize these data distributions
plt.figure(figsize=(9, 6))
plt.scatter(features[:, 0], features[:, 1],
c = labels, alpha = .6);
plt.title("Binary labeled data in 2D", size=15);
plt.xlabel("Feature 1", size=13);
plt.ylabel("Feature 2", size=13);
def sigmoid(z):
# Input:
# z : scalar or array of dimension n
# Output:
# sgmd: scalar or array of dimension n
# YOUR CODE HERE
sgmd=1.0/(1.0+np.exp(-z))
return sgmd
raise NotImplementedError()
def y_pred(X, w, b=0):
# Input:
# X: nxd matrix
# w: d-dimensional vector
# b: scalar (optional, if not passed on is treated as 0)
# Output:
# prob: n-dimensional vector
# YOUR CODE HERE
inner=np.dot(X,np.transpose(w))
Result=sigmoid(inner+b)
return(Result)
raise NotImplementedError()
#return prob
def log_loss(X, y, w, b=0):
# Input:
# X: nxd matrix
# y: n-dimensional vector with labels (+1 or -1)
# w: d-dimensional vector
# Output:
# a scalar
assert np.sum(np.abs(y))==len(y) # check if all labels in y are either +1 or -1
# YOUR CODE HERE
Ypredict=y_pred(X, w, b)
probs=np.multiply(Ypredict,y)
probs=np.where(probs<0,1+probs,probs)
probs=-np.sum(np.log(probs))
return(probs)
raise NotImplementedError()
def gradient(X, y, w, b):
# Input:
# X: nxd matrix
# y: n-dimensional vector with labels (+1 or -1)
# w: d-dimensional vector
# b: a scalar bias term
# Output:
# wgrad: d-dimensional vector with gradient
# bgrad: a scalar with gradient
n, d = X.shape
wgrad = np.zeros(d)
bgrad = 0.0
# YOUR CODE HERE
y=np.multiply(y,-1)
Ypredict=y_pred(X, w, b)
probs=np.multiply(Ypredict,y)
probs=np.where(probs<0,1+probs,probs)
outer=np.multiply(y,probs)
bgrad=np.sum(outer)
wgrad=np.dot(outer.reshape(-1,n),X)
wgrad=wgrad.reshape(-1,)
return (wgrad, bgrad)
raise NotImplementedError()
#return wgrad, bgrad
def logistic_regression(X, y, max_iter, alpha):
n, d = X.shape
w = np.zeros(d)
b = 0.0
losses = np.zeros(max_iter)
for step in range(max_iter):
# YOUR CODE HERE
# YOUR CODE HERE
current_log_loss=log_loss(X,y,w,b) #this returns a number, the degree of loss,
wgrad, bgrad = gradient(X, y, w, b) #this returns the gradient, which is the log-loss with respect to the weight vector
direction_s=-1*np.multiply(alpha,wgrad) #multiply weights time alpha, becomes very small
direction_s2=-1*np.multiply(alpha,bgrad) #multiply weights time alpha, becomes very small
update_loss=np.multiply(np.dot(np.transpose(wgrad),wgrad),alpha) #this is the mutiplying
LossAfter1Update=current_log_loss-update_loss
losses2=log_loss(X,y,w+direction_s,b+direction_s2)
losses[step]=losses2
if LossAfter1Update>=current_log_loss:
break
w=w+direction_s
b=b+direction_s2
#raise NotImplementedError()
#b=bgrad
return (w, b, losses)
weight, b, losses = logistic_regression(features, labels, 1000, 1e-04)
plot(losses)
xlabel('iterations')
ylabel('log_loss')
# your loss should go down :-)
max_iter = 10000
alpha = 1e-4
final_w, final_b, losses = logistic_regression(features, labels, max_iter, alpha)
plt.figure(figsize=(9, 6))
plt.plot(losses)
plt.title("Loss vs. iteration", size=15)
plt.xlabel("Num iteration", size=13)
plt.ylabel("Loss value", size=13)
scores = y_pred(features, final_w, final_b)
pred_labels = (scores > 0.5).astype(int)
pred_labels[pred_labels != 1] = -1
plt.figure(figsize=(9, 6))
# plot the decision boundary
x = np.linspace(np.amin(features[:, 0]), np.amax(features[:, 0]), 10)
y = -(final_w[0] * x + final_b)/ final_w[1]
plt.plot(x, y)
plt.scatter(features[:, 0], features[:, 1],
c = pred_labels, alpha = .6)
plt.title("Predicted labels", size=15)
plt.xlabel("Feature 1", size=13)
plt.ylabel("Feature 2", size=13)
plt.axis([-3,10,0,15])
#Building the classifier
import pandas as pd
import dask
import dask.bag
from dask.diagnostics import ProgressBar
train_url = 's3://codio/CIS530/CIS533/data_train'
test_url = 's3://codio/CIS530/CIS533/data_test'
# tokenize the email and hashes the symbols into a vector
def extract_features_naive(email, B):
# initialize all-zeros feature vector
v = np.zeros(B)
email = ' '.join(email)
# breaks for non-ascii characters
tokens = email.split()
for token in tokens:
v[hash(token) % B] = 1
return v
def load_spam_data(extract_features, B=512, url=train_url):
'''
INPUT:
extractfeatures : function to extract features
B : dimensionality of feature space
path : the path of folder to be processed
OUTPUT:
X, Y
'''
all_emails = pd.read_csv(url+'/index', header=None).values.flatten()
xs = np.zeros((len(all_emails), B))
ys = np.zeros(len(all_emails))
labels = [k.split()[0] for k in all_emails]
paths = [url+'/'+k.split()[1] for k in all_emails]
ProgressBar().register()
dask.config.set(scheduler='threads', num_workers=50)
bag = dask.bag.read_text(paths, storage_options={'anon':True})
contents = dask.bag.compute(*bag.to_delayed())
for i, email in enumerate(contents):
# make labels +1 for "spam" and -1 for "ham"
ys[i] = (labels[i] == 'spam') * 2 - 1
xs[i, :] = extract_features(email, B)
print('Loaded %d input emails.' % len(ys))
return xs, ys
Xspam, Yspam = load_spam_data(extract_features_naive)
Xspam.shape
# Split data into training (xTr and yTr)
# and testing (xTv and yTv)
n, d = Xspam.shape
# Allocate 80% of the data for training and 20% for testing
cutoff = int(np.ceil(0.8 * n))
# indices of training samples
xTr = Xspam[:cutoff,:]
yTr = Yspam[:cutoff]
# indices of testing samples
xTv = Xspam[cutoff:]
yTv = Yspam[cutoff:]
max_iter = 5000
alpha = 1e-5
final_w_spam, final_b_spam, losses = logistic_regression(xTr, yTr, max_iter, alpha)
plt.figure(figsize=(9, 6))
plt.plot(losses)
plt.title("Loss vs. iteration", size=15)
plt.xlabel("Num iteration", size=13)
plt.ylabel("Loss value", size=13)
# evaluate training accuracy
scoresTr = y_pred(xTr, final_w_spam, final_b_spam)
pred_labels = (scoresTr > 0.5).astype(int)
pred_labels[pred_labels != 1] = -1
trainingacc = np.mean(pred_labels == yTr)
# evaluate testing accuracy
scoresTv = y_pred(xTv, final_w_spam, final_b_spam)
pred_labels = (scoresTv > 0.5).astype(int)
pred_labels[pred_labels != 1] = -1
validationacc = np.mean(pred_labels==yTv)
print("Training accuracy %2.2f%%\nValidation accuracy %2.2f%%\n" % (trainingacc*100,validationacc*100))