-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
309 lines (218 loc) · 9.95 KB
/
tests.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
303
304
305
306
307
308
309
import unittest
import numpy as np
import time
# import timeout_decorator
from decision_tree import *
from naive_bayes import *
from crossval import *
from load_all_data import load_all_data
class Homework1TestCase(unittest.TestCase):
@staticmethod
def unique_data():
"""Set up uniquely identifiable examples"""
train_data = np.zeros((4, 10))
# label = 0
train_data[:, 0] = [0, 0, 0, 0]
train_data[:, 1] = [0, 0, 0, 1]
train_data[:, 2] = [0, 0, 1, 0]
train_data[:, 3] = [0, 0, 1, 1]
train_data[:, 4] = [0, 1, 0, 0]
# label = 1
train_data[:, 5] = [0, 1, 0, 1]
train_data[:, 6] = [0, 1, 1, 0]
train_data[:, 7] = [0, 1, 1, 1]
train_data[:, 8] = [1, 0, 0, 0]
train_data[:, 9] = [1, 0, 0, 1]
train_labels = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
return train_data, train_labels
@staticmethod
def real_data():
"""Load 20 newsgroups data"""
num_words, num_training, num_testing, train_data, test_data, train_labels, test_labels = load_all_data()
# do feature selection
#version1
d = 5000
gain = calculate_information_gain(train_data, train_labels)
# sort features by calculated information gain
ranks = gain.argsort()[::-1]
train_data = train_data[ranks[:d], :]
test_data = test_data[ranks[:d], :]
# convert training data to dense ndarray
train_data = train_data.toarray()
test_data = test_data.toarray()
return train_data, train_labels
def test_decision_tree_memorization(self):
"""Test whether a decision tree can memorize unique data"""
train_data, train_labels = self.unique_data()
# train_data, train_labels = self.real_data()
# set tree depth to unlimited
params = {"max_depth": np.inf}
model = decision_tree_train(train_data, train_labels, params)
print(model)
predictions = decision_tree_predict(train_data, model)
print(predictions)
print(train_labels)
assert np.all(predictions == train_labels), "Decision tree was unable to memorize 10 unique examples"
@staticmethod
def test_decision_tree_speed():
"""Check that the decision tree implementation is fast."""
n = 50000
d = 100
train_data = np.random.randint(0, 2, size=(d, n))
train_labels = np.random.randint(0, 5, size=n)
start_time = time.time()
params = {"max_depth": 10}
model = decision_tree_train(train_data, train_labels, params)
predictions = decision_tree_predict(train_data, model)
end_time = time.time()
assert predictions.size == n, "decision_tree_predict didn't return the correct size predictions"
print("Decision tree training and prediction took %f seconds." % (end_time - start_time))
assert (end_time - start_time) < 10.0, "Training on a (somewhat) large dataset took longer than 10 seconds."
def test_decision_tree_depth(self):
"""Test that max_depth is implemented correctly."""
train_data, train_labels = self.unique_data()
expected_correct = [5, 7, 9, 9, 10]
for depth in range(4):
params = {"max_depth": depth}
model = decision_tree_train(train_data, train_labels, params)
predictions = decision_tree_predict(train_data, model)
# Accuracy should be 0.5 at depth 0.
num_correct = np.sum(predictions == train_labels)
self.assertEqual(num_correct, expected_correct[depth], ("Incorrect accuracy for depth %d." % depth))
def test_decision_tree_improvement(self):
"""Test that deeper decision trees have better training performance on real data"""
train_data, train_labels = self.real_data()
params = {"max_depth": 0} #0
shallow_tree = decision_tree_train(train_data, train_labels, params)
shallow_predictions = decision_tree_predict(train_data, shallow_tree)
for depth in range(1, 5): #1,5
params = {"max_depth": depth}
deep_tree = decision_tree_train(train_data, train_labels, params)
deep_predictions = decision_tree_predict(train_data, deep_tree)
num_correct_shallow = np.sum(shallow_predictions == train_labels)
num_correct_deep = np.sum(deep_predictions == train_labels)
print("Shallow tree correctly labels %d training examples" % num_correct_shallow)
print("Deep tree correctly labels %d training examples" % num_correct_deep)
assert num_correct_deep > num_correct_shallow, "Deep tree didn't improve training accuracy over shallow tree."
shallow_predictions = deep_predictions
@staticmethod
def test_decision_tree_test_accuracy():
"""Test that decision tree accuracy is sufficiently high."""
num_words, num_training, num_testing, train_data, test_data, train_labels, test_labels = load_all_data()
# do feature selection
d = 5000
gain = calculate_information_gain(train_data, train_labels)
# sort features by calculated information gain
ranks = gain.argsort()[::-1]
train_data = train_data[ranks[:d], :]
test_data = test_data[ranks[:d], :]
# convert training data to dense ndarray
train_data = train_data.toarray()
test_data = test_data.toarray()
# start decision tree training
params = {"max_depth": 16}
model = decision_tree_train(train_data, train_labels, params)
predictions = decision_tree_predict(test_data, model)
accuracy = np.sum(predictions == test_labels) / float(num_testing)
print("Decision tree accuracy was %f" % accuracy)
assert accuracy > 0.3, "Decision tree accuracy was lower than expected."
def test_naive_bayes_simple(self):
"""Test naive Bayes on a tiny toy problem."""
train_data = np.zeros((4, 4))
# negative examples
train_data[:, 0] = [1, 0, 0, 0]
train_data[:, 1] = [0, 1, 0, 0]
# positive examples
train_data[:, 2] = [0, 0, 1, 0]
train_data[:, 3] = [0, 0, 0, 1]
train_labels = np.array([0, 0, 1, 1])
model = naive_bayes_train(train_data, train_labels, {"alpha": 0.1})
predictions = naive_bayes_predict(train_data, model)
accuracy = np.mean(predictions == train_labels)
print("Accuracy on toy data was %f" % accuracy)
self.assertEqual(accuracy, 1.0)
@staticmethod
def test_naive_bayes_accuracy():
"""Test that decision tree accuracy is sufficiently high."""
num_words, num_training, num_testing, train_data, test_data, train_labels, test_labels = load_all_data()
# do feature selection
d = 5000
gain = calculate_information_gain(train_data, train_labels)
# sort features by calculated information gain
ranks = gain.argsort()[::-1]
train_data = train_data[ranks[:d], :]
test_data = test_data[ranks[:d], :]
# convert training data to dense ndarray
train_data = train_data.toarray()
test_data = test_data.toarray()
# start naive Bayes training
params = {"alpha": 1e-4}
model = naive_bayes_train(train_data, train_labels, params)
predictions = naive_bayes_predict(test_data, model)
accuracy = np.mean(predictions == test_labels)
print("Naive Bayes accuracy was %f" % accuracy)
assert accuracy > 0.65, "Naive Bayes accuracy was lower than expected."
@staticmethod
def test_naive_bayes_speed():
"""Check that the decision tree implementation is fast."""
n = 50000 #50000
d = 100
train_data = np.random.randint(0, 1, size=(d, n))
train_labels = np.random.randint(0, 5, size=n)
start_time = time.time()
params = {"alpha": 0.1}
model = naive_bayes_train(train_data, train_labels, params)
predictions = naive_bayes_predict(train_data, model)
end_time = time.time()
assert predictions.size == n, "naive_bayes_predict didn't return the correct size predictions"
print("Naive Bayes training and prediction took %f seconds." % (end_time - start_time))
assert (end_time - start_time) < 1.0, "Training on a (somewhat) large dataset took longer than a second."
@staticmethod
def dummy_learner(data, labels, params):
"""Empty learner for testing cross-validation."""
return None
@staticmethod
def dummy_predictor(data, model):
"""Empty predictor for testing cross-validation."""
return np.zeros(data.shape[1])
def test_crossval_speed(self):
"""Test speed of data splitting for cross validation."""
n = 50000
d = 100
train_data = np.random.randint(0, 1, size=(d, n))
train_labels = np.random.randint(0, 5, size=n)
start_time = time.time()
score, models = cross_validate(self.dummy_learner, self.dummy_predictor, train_data, train_labels, 10, {})
print("Finished doing data splitting for cross validation in %f seconds." % (time.time() - start_time))
assert (time.time() - start_time) < 1.0, "Cross-validation too slow."
def test_crossval_correctness(self):
"""Test that cross validation returns the correct number of scores."""
train_data, train_labels = self.real_data()
for folds in [5, 10, 20]:
score, models = cross_validate(self.dummy_learner, self.dummy_predictor, train_data, train_labels, folds, {})
self.assertEqual(len(models), folds)
@staticmethod
def test_crossval_naive_bayes():
"""Test that cross-validation on naive Bayes gives scores close to held-out test accuracy"""
num_words, num_training, num_testing, train_data, test_data, train_labels, test_labels = load_all_data()
# do feature selection
d = 5000
gain = calculate_information_gain(train_data, train_labels)
# sort features by calculated information gain
ranks = gain.argsort()[::-1]
train_data = train_data[ranks[:d], :]
test_data = test_data[ranks[:d], :]
# convert training data to dense ndarray
train_data = train_data.toarray()
test_data = test_data.toarray()
# start cross-validation
params = {"alpha": 1e-4}
score, models = cross_validate(naive_bayes_train, naive_bayes_predict, train_data, train_labels, 3, params)
print("Cross-validation score was %f." % score)
model = naive_bayes_train(train_data, train_labels, params)
predictions = naive_bayes_predict(test_data, model)
accuracy = np.mean(predictions == test_labels)
print("Naive Bayes accuracy was %f" % accuracy)
assert np.abs(accuracy - score) < 0.2, "Accuracy and cross-validation score were not close."
if __name__ == '__main__':
unittest.main()