forked from qobi/ece57000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearest_neighbor_classifier_gui.py
61 lines (55 loc) · 1.62 KB
/
nearest_neighbor_classifier_gui.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
from gui import *
from distances import *
from nearest_neighbor_classifier import *
points = []
labels = []
distance = L2_vector(L2_scalar)
def clear_command():
global points, labels
points = []
labels = []
message("")
get_axes().clear()
redraw()
def all_command():
resolution = 50
scale = 1.0/resolution
for y in range(0, resolution+1):
for x in range(0, resolution+1):
label = classify([scale*x, scale*y], distance, points, labels)
if label==0:
get_axes().plot([scale*x], [scale*y], "r.")
else:
get_axes().plot([scale*x], [scale*y], "b.")
redraw()
def click(x, y):
message("")
if mode()==0:
points.append([x, y])
labels.append(mode())
get_axes().plot([x], [y], "r+")
redraw()
elif mode()==1:
points.append([x, y])
labels.append(mode())
get_axes().plot([x], [y], "b+")
redraw()
else:
if len(points)==0:
message("No data")
else:
label = classify([x, y], distance, points, labels)
if label==0:
message("Red")
elif label==1:
message("Blue")
add_button(0, 0, "Clear", clear_command, nothing)
mode = add_radio_button_group([[0, 1, "Red", 0],
[0, 2, "Blue", 1],
[0, 3, "Classify", 2]],
lambda: False)
add_button(0, 4, "All", all_command, nothing)
add_button(0, 5, "Exit", done, nothing)
message = add_message(1, 0, 6)
add_click(click)
start_fixed_size_matplotlib(7, 7, 2, 6)