-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathk_means_gui.py
More file actions
98 lines (89 loc) · 2.66 KB
/
k_means_gui.py
File metadata and controls
98 lines (89 loc) · 2.66 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
from gui import *
from distances import *
from mean_classifier import *
points = []
labels = []
means = []
def redisplay():
get_axes().clear()
for i in range(len(points)):
if labels[i]==0:
get_axes().plot([points[i][0]], [points[i][1]], "r+")
elif labels[i]==1:
get_axes().plot([points[i][0]], [points[i][1]], "b+")
if len(means)==2:
get_axes().plot([means[0][0]], [means[0][1]], "ro")
get_axes().plot([means[1][0]], [means[1][1]], "bo")
redraw()
def clear_command():
global points, labels, means
points = []
labels = []
means = []
message("")
get_axes().clear()
redraw()
def random_labels_command():
global labels, means
labels = random_labels(points, 2)
means = []
message("")
redisplay()
def train_command():
def internal():
global means
means = train(points, labels)
message("{:.3f}".format(cost(points, labels, means)))
redisplay()
if not all_labels(labels, 2):
message("Missing class")
elif not all_labeled(labels):
message("Random labels first")
else:
message("Training")
get_window().after(10, internal)
def reclassify_all_command():
def internal():
global labels
labels = reclassify_all(points, means)
message("{:.3f}".format(cost(points, labels, means)))
redisplay()
if len(means)==0:
message("Train first")
else:
message("Reclassifying all")
get_window().after(10, internal)
def loop_command():
def internal(last_cost):
global labels, means
means = train(points, labels)
labels = reclassify_all(points, means)
this_cost = cost(points, labels, means)
message("{:.3f}".format(this_cost))
redisplay()
if this_cost<last_cost:
get_window().after(500, lambda: internal(this_cost))
else:
message("Done")
if not all_labeled(labels):
message("Random labels first")
elif not all_labels(labels, 2):
message("Missing class")
else:
infinity = float("inf")
internal(infinity)
def click(x, y):
message("")
points.append([x, y])
labels.append(-1)
get_axes().plot([x], [y], "g+")
redraw()
add_button(0, 0, "Clear", clear_command, nothing)
add_button(0, 1, "Random labels", random_labels_command, nothing)
add_button(0, 2, "Train", train_command, nothing)
add_button(0, 3, "Reclassify all", reclassify_all_command, nothing)
add_button(0, 4, "Loop", loop_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)