-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathlog_reg_regular.py
More file actions
175 lines (128 loc) · 3.51 KB
/
log_reg_regular.py
File metadata and controls
175 lines (128 loc) · 3.51 KB
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
from numpy import loadtxt, where, zeros, e, array, log, ones, append, linspace
from pylab import scatter, show, legend, xlabel, ylabel, contour, title
from scipy.optimize import fmin_bfgs
def sigmoid(X):
'''Compute the sigmoid function '''
#d = zeros(shape=(X.shape))
den = 1.0 + e ** (-1.0 * X)
d = 1.0 / den
return d
def cost_function_reg(theta, X, y, l):
'''Compute the cost and partial derivatives as grads
'''
h = sigmoid(X.dot(theta))
thetaR = theta[1:, 0]
J = (1.0 / m) * ((-y.T.dot(log(h))) - ((1 - y.T).dot(log(1.0 - h)))) \
+ (l / (2.0 * m)) * (thetaR.T.dot(thetaR))
delta = h - y
sumdelta = delta.T.dot(X[:, 1])
grad1 = (1.0 / m) * sumdelta
XR = X[:, 1:X.shape[1]]
sumdelta = delta.T.dot(XR)
grad = (1.0 / m) * (sumdelta + l * thetaR)
out = zeros(shape=(grad.shape[0], grad.shape[1] + 1))
out[:, 0] = grad1
out[:, 1:] = grad
return J.flatten(), out.T.flatten()
def map_feature(x1, x2):
'''
Maps the two input features to quadratic features.
Returns a new feature array with more features, comprising of
X1, X2, X1 ** 2, X2 ** 2, X1*X2, X1*X2 ** 2, etc...
Inputs X1, X2 must be the same size
'''
x1.shape = (x1.size, 1)
x2.shape = (x2.size, 1)
degree = 6
out = ones(shape=(x1[:, 0].size, 1))
m, n = out.shape
for i in range(1, degree + 1):
for j in range(i + 1):
r = (x1 ** (i - j)) * (x2 ** j)
out = append(out, r, axis=1)
return out
#load the dataset
data = loadtxt('ex2data2.txt', delimiter=',')
X = data[:, 0:2]
y = data[:, 2]
pos = where(y == 1)
neg = where(y == 0)
scatter(X[pos, 0], X[pos, 1], marker='o', c='b')
scatter(X[neg, 0], X[neg, 1], marker='x', c='r')
xlabel('Microchip Test 1')
ylabel('Microchip Test 2')
legend(['y = 1', 'y = 0'])
#show()
m, n = X.shape
y.shape = (m, 1)
it = map_feature(X[:, 0], X[:, 1])
#Initialize theta parameters
initial_theta = zeros(shape=(it.shape[1], 1))
#Set regularization parameter lambda to 1
l = 1
# Compute and display initial cost and gradient for regularized logistic
# regression
cost, grad = cost_function_reg(initial_theta, it, y, l)
def decorated_cost(theta):
return cost_function_reg(theta, it, y, l)
print fmin_bfgs(decorated_cost, initial_theta, maxfun=400)
theta = [
1.273005,
0.624876,
1.177376,
-2.020142,
-0.912616,
-1.429907,
0.125668,
-0.368551,
-0.360033,
-0.171068,
-1.460894,
-0.052499,
-0.618889,
-0.273745,
-1.192301,
-0.240993,
-0.207934,
-0.047224,
-0.278327,
-0.296602,
-0.453957,
-1.045511,
0.026463,
-0.294330,
0.014381,
-0.328703,
-0.143796,
-0.924883,
]
#Plot Boundary
u = linspace(-1, 1.5, 50)
v = linspace(-1, 1.5, 50)
z = zeros(shape=(len(u), len(v)))
for i in range(len(u)):
for j in range(len(v)):
z[i, j] = (map_feature(array(u[i]), array(v[j])).dot(array(theta)))
z = z.T
contour(u, v, z)
title('lambda = %f' % l)
xlabel('Microchip Test 1')
ylabel('Microchip Test 2')
legend(['y = 1', 'y = 0', 'Decision boundary'])
show()
def predict(theta, X):
'''Predict whether the label
is 0 or 1 using learned logistic
regression parameters '''
m, n = X.shape
p = zeros(shape=(m, 1))
h = sigmoid(X.dot(theta.T))
for it in range(0, h.shape[0]):
if h[it] > 0.5:
p[it, 0] = 1
else:
p[it, 0] = 0
return p
#% Compute accuracy on our training set
p = predict(array(theta), it)
print 'Train Accuracy: %f' % ((y[where(p == y)].size / float(y.size)) * 100.0)