-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathassignment-1.py
More file actions
40 lines (30 loc) · 1.17 KB
/
assignment-1.py
File metadata and controls
40 lines (30 loc) · 1.17 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
import warnings
import matplotlib.pyplot as plt
from ml1assignments import (
iris_data, knn, zero_one_loss, train_test_val_split,
plot_decision_boundary)
if __name__ == '__main__':
# Load data.
X, z = iris_data('data/iris.data')
X = X[:, (0, 2)]
# Split data into training, validation and testing splits.
RX, rz, VX, vz, TX, tz = train_test_val_split(X, z, 0.5, 0.25, 0.25)
# Try out different values for k and find best k for the validation set.
best_predict = None
best_error = float('inf')
for i in range(1, 10):
predict = knn(RX, rz, i)
predictions = predict(VX)
error = zero_one_loss(vz, predictions)
print 'Validation error using %i neighbours: %.4f' % (i, error)
if error < best_error:
best_predict = predict
best_error = error
# Plot the decision boundary of the best predictor.
x_extent = X[:, 0].min() - .2, X[:, 0].max() + .2
y_extent = X[:, 1].min() - .2, X[:, 1].max() + .2
fig = plt.figure()
ax = fig.add_subplot(111)
plot_decision_boundary(ax, best_predict, x_extent, y_extent)
ax.scatter(TX[:, 0], TX[:, 1], c=tz, s=50)
plt.show()