forked from DanyunHe/MIT6.036
-
Notifications
You must be signed in to change notification settings - Fork 3
/
code_for_hw3_part1.py
302 lines (264 loc) · 10.5 KB
/
code_for_hw3_part1.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Implement perceptron, average perceptron, and pegasos
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
import pdb
import itertools
import operator
import functools
print("Importing code_for_hw03")
######################################################################
# Plotting
def tidy_plot(xmin, xmax, ymin, ymax, center = False, title = None,
xlabel = None, ylabel = None):
plt.ion()
plt.figure(facecolor="white")
ax = plt.subplot()
if center:
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
else:
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
eps = .05
plt.xlim(xmin-eps, xmax+eps)
plt.ylim(ymin-eps, ymax+eps)
if title: ax.set_title(title)
if xlabel: ax.set_xlabel(xlabel)
if ylabel: ax.set_ylabel(ylabel)
return ax
def plot_separator(ax, th, th_0):
xmin, xmax = ax.get_xlim()
ymin,ymax = ax.get_ylim()
pts = []
eps = 1.0e-6
# xmin boundary crossing is when xmin th[0] + y th[1] + th_0 = 0
# that is, y = (-th_0 - xmin th[0]) / th[1]
if abs(th[1,0]) > eps:
pts += [np.array([x, (-th_0 - x * th[0,0]) / th[1,0]]) \
for x in (xmin, xmax)]
if abs(th[0,0]) > 1.0e-6:
pts += [np.array([(-th_0 - y * th[1,0]) / th[0,0], y]) \
for y in (ymin, ymax)]
in_pts = []
for p in pts:
if (xmin-eps) <= p[0] <= (xmax+eps) and \
(ymin-eps) <= p[1] <= (ymax+eps):
duplicate = False
for p1 in in_pts:
if np.max(np.abs(p - p1)) < 1.0e-6:
duplicate = True
if not duplicate:
in_pts.append(p)
if in_pts and len(in_pts) >= 2:
# Plot separator
vpts = np.vstack(in_pts)
ax.plot(vpts[:,0], vpts[:,1], 'k-', lw=2)
# Plot normal
vmid = 0.5*(in_pts[0] + in_pts[1])
scale = np.sum(th*th)**0.5
diff = in_pts[0] - in_pts[1]
dist = max(xmax-xmin, ymax-ymin)
vnrm = vmid + (dist/10)*(th.T[0]/scale)
vpts = np.vstack([vmid, vnrm])
ax.plot(vpts[:,0], vpts[:,1], 'k-', lw=2)
# Try to keep limits from moving around
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
else:
print('Separator not in plot range')
def plot_data(data, labels, ax = None, clear = False,
xmin = None, xmax = None, ymin = None, ymax = None):
if ax is None:
if xmin == None: xmin = np.min(data[0, :]) - 0.5
if xmax == None: xmax = np.max(data[0, :]) + 0.5
if ymin == None: ymin = np.min(data[1, :]) - 0.5
if ymax == None: ymax = np.max(data[1, :]) + 0.5
ax = tidy_plot(xmin, xmax, ymin, ymax)
x_range = xmax - xmin; y_range = ymax - ymin
if .1 < x_range / y_range < 10:
ax.set_aspect('equal')
xlim, ylim = ax.get_xlim(), ax.get_ylim()
elif clear:
xlim, ylim = ax.get_xlim(), ax.get_ylim()
ax.clear()
else:
xlim, ylim = ax.get_xlim(), ax.get_ylim()
colors = np.choose(labels > 0, cv(['r', 'g']))[0]
ax.scatter(data[0,:], data[1,:], c = colors,
marker = 'o', s=50, edgecolors = 'none')
# Seems to occasionally mess up the limits
ax.set_xlim(xlim); ax.set_ylim(ylim)
ax.grid(True, which='both')
#ax.axhline(y=0, color='k')
#ax.axvline(x=0, color='k')
return ax
# Must either specify limits or existing ax
def plot_nonlin_sep(predictor, ax = None, xmin = None , xmax = None,
ymin = None, ymax = None, res = 30):
if ax is None:
ax = tidy_plot(xmin, xmax, ymin, ymax)
else:
if xmin == None:
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
else:
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
cmap = colors.ListedColormap(['black', 'white'])
bounds=[-2,0,2]
norm = colors.BoundaryNorm(bounds, cmap.N)
ima = np.array([[predictor(x1i, x2i) \
for x1i in np.linspace(xmin, xmax, res)] \
for x2i in np.linspace(ymin, ymax, res)])
im = ax.imshow(np.flipud(ima), interpolation = 'none',
extent = [xmin, xmax, ymin, ymax],
cmap = cmap, norm = norm)
######################################################################
# Utilities
# Takes a list of numbers and returns a column vector: n x 1
def cv(value_list):
return np.transpose(rv(value_list))
# Takes a list of numbers and returns a row vector: 1 x n
def rv(value_list):
return np.array([value_list])
# x is dimension d by 1
# th is dimension d by 1
# th0 is a scalar
# return a 1 by 1 matrix
def y(x, th, th0):
return np.dot(np.transpose(th), x) + th0
# x is dimension d by 1
# th is dimension d by 1
# th0 is dimension 1 by 1
# return 1 by 1 matrix of +1, 0, -1
def positive(x, th, th0):
return np.sign(y(x, th, th0))
# data is dimension d by n
# labels is dimension 1 by n
# ths is dimension d by 1
# th0s is dimension 1 by 1
# return 1 by 1 matrix of integer indicating number of data points correct for
# each separator.
def score(data, labels, th, th0):
return np.sum(positive(data, th, th0) == labels)
######################################################################
# Data Sets
# Return d = 2 by n = 4 data matrix and 1 x n = 4 label matrix
def super_simple_separable_through_origin():
X = np.array([[2, 3, 9, 12],
[5, 1, 6, 5]])
y = np.array([[1, -1, 1, -1]])
return X, y
def super_simple_separable():
X = np.array([[2, 3, 9, 12],
[5, 2, 6, 5]])
y = np.array([[1, -1, 1, -1]])
return X, y
def xor():
X = np.array([[1, 2, 1, 2],
[1, 2, 2, 1]])
y = np.array([[1, 1, -1, -1]])
return X, y
def xor_more():
X = np.array([[1, 2, 1, 2, 2, 4, 1, 3],
[1, 2, 2, 1, 3, 1, 3, 3]])
y = np.array([[1, 1, -1, -1, 1, 1, -1, -1]])
return X, y
######################################################################
# Tests for part 2: features
# Make it take miscellaneous args and pass into learner
def test_linear_classifier_with_features(dataFun, learner, feature_fun,
draw = True, refresh = True, pause = True):
raw_data, labels = dataFun()
data = feature_fun(raw_data) if feature_fun else raw_data
if draw:
ax = plot_data(raw_data, labels)
def hook(params):
(th, th0) = params
plot_nonlin_sep(
lambda x1,x2: int(positive(feature_fun(cv([x1, x2])), th, th0)),
ax = ax)
plot_data(raw_data, labels, ax)
plt.pause(0.05)
print('th', th.T, 'th0', th0)
if pause: input('press enter here to continue:')
else:
hook = None
th, th0 = learner(data, labels, hook = hook)
if hook: hook((th, th0))
print("Final score", int(score(data, labels, th, th0)))
print("Params", np.transpose(th), th0)
def mul(seq):
return functools.reduce(operator.mul, seq, 1)
def make_polynomial_feature_fun(order):
# raw_features is d by n
# return is k by n where k = sum_{i = 0}^order multichoose(d, i)
def f(raw_features):
d, n = raw_features.shape
result = [] # list of column vectors
for j in range(n):
features = []
for o in range(order+1):
indexTuples = \
itertools.combinations_with_replacement(range(d), o)
for it in indexTuples:
features.append(mul(raw_features[i, j] for i in it))
result.append(cv(features))
return np.hstack(result)
return f
def test_with_features(dataFun, order = 2, draw=True, pause=True):
test_linear_classifier_with_features(
dataFun, # data
perceptron, # learner
make_polynomial_feature_fun(order), # feature maker
draw=draw,
pause=pause)
# Perceptron algorithm with offset.
# data is dimension d by n
# labels is dimension 1 by n
# T is a positive integer number of steps to run
def perceptron(data, labels, params = {}, hook = None):
T = params.get('T', 3000)
(d, n) = data.shape
m = 0
theta = np.zeros((d, 1)); theta_0 = np.zeros((1, 1))
for t in range(T):
for i in range(n):
x = data[:,i:i+1]
y = labels[:,i:i+1]
if y * positive(x, theta, theta_0) <= 0.0:
m += 1
theta = theta + y * x
theta_0 = theta_0 + y
if hook: hook((theta, theta_0))
return theta, theta_0
######################################################################
# Tests for part 2D: Encoding discrete values
def one_hot_internal(x, k):
# Make an empty column vector
v = np.zeros((k, 1))
# Set an entry to 1
v[x-1, 0] = 1
return v
def test_one_hot(sub):
if one_hot_internal(3, 5).tolist() == sub(3, 5).tolist() and one_hot_internal(4, 7).tolist() == sub(4, 7).tolist():
print("Passed! \n")
else: print("Test Failed")
#-----------------------------------------------------------------------------
print("Imported tidy_plot, plot_separator, plot_data, plot_nonlin_sep, cv, rv, y, positive, score")
print("Datasets: super_simple_separable_through_origin(), super_simple_separable(), xor(), xor_more()")
print("Tests for part 2: test_linear_classifier_with_features, mul, make_polynomial_feature_fun, ")
print(" test_with_features")
print("Also loaded: perceptron, one_hot_internal, test_one_hot")
######################################################################
# Example for part 3B) test_with_features()
test_with_features(xor_more, 3, draw=False, pause=False)