-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest20151115.py
132 lines (115 loc) · 4.07 KB
/
test20151115.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
# Copyright (C) 2015 Rene Pihlak
import numpy as np
import collections
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.animation as animation
def updatefig(*args):
global index, new_data, in_data, classifierSVM, classifierKNN, axsvm, axknn
arr = []
if(index < len(in_data) - 5):
new_data.append(in_data[index])
predictedSVM = classifierSVM.predict(new_data)
predictedKNN = classifierKNN.predict(new_data)
for i in range(0, len(new_data)):
arr.append(new_data[i])
linesvm.set_ydata(arr)
lineknn.set_ydata(arr)
linesvm.set_xdata(range(index, index + 5))
lineknn.set_xdata(range(index, index + 5))
axsvm.set_ylim(-0.2, 1.3)
axknn.set_ylim(-0.2, 1.3)
axsvm.set_xlim(index - 0.5, index + len(arr) - 0.5)
axknn.set_xlim(index - 0.5, index + len(arr) - 0.5)
#print(learn_labels_names[predictedSVM[0]])
axsvm.set_title("SVM: %s" % learn_labels_names[predictedSVM[0]])
axknn.set_title("KNN: %s" % learn_labels_names[predictedKNN[0]])
axsvm.figure.canvas.draw()
axknn.figure.canvas.draw()
index += 1
return linesvm, lineknn,
figure = plt.figure(figsize=(27, 9))
#fig, ax = plt.subplots()
#line, = ax.step([], [], lw=2)
#ax.grid()
#xdata, ydata = [], []
axsvm = plt.subplot(2, 4, 6)
linesvm, = axsvm.step([], [], lw=2)
axsvm.grid()
xdatasvm, ydatasvm = [], []
axknn = plt.subplot(2, 4, 7)
lineknn, = axknn.step([], [], lw=2)
axknn.grid()
xdataknn, ydataknn = [], []
#MUST HAVE EQUAL NUMBER OF ALL TYPES,
#i.e., if n classified groups, then learning set size = n x m
learn_data = [
[1,1,1,1,1],
[1,1,1,0,0],
[1,0,0,0,0],
[0,0,0,0,0],
[1,1,1,1,1],
[0,1,1,1,0],
[0,1,0,0,0],
[0,0,0,0,0],
[1,1,1,1,1],
[0,0,1,1,1],
[0,0,1,0,0],
[0,0,0,0,0],
[1,1,1,1,1],
[1,0,0,1,1],
[0,0,0,1,0],
[0,0,0,0,0],
[1,1,1,1,1],
[1,1,0,0,1],
[0,0,0,0,1],
[0,0,0,0,0]]
learn_labels = [0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3]
learn_labels_names = ["dead", "dying", "ok", "perfect", "dead", "dying", "ok", "perfect", "dead", "dying", "ok", "perfect", "dead", "dying", "ok", "perfect", "dead", "dying", "ok", "perfect"]
print("LEARNING DATA:")
for index in range(0, len(learn_data)):
print("%s: %d :: %s" % (learn_data[index], learn_labels[index], learn_labels_names[index]))
if(index < 4):
ax1 = plt.subplot(2, 4, index + 1)
line1, = ax1.step([], [], lw=2)
ax1.grid()
line1.set_ydata(learn_data[index])
line1.set_xdata(range(0, 5))
ax1.set_ylim(-0.2, 1.3)
ax1.set_xlim(-0.5, 4.5)
ax1.set_title("Training: %s" % learn_labels_names[index])
ax1.figure.canvas.draw()
#plt.step(range(0,5),learn_data[10], where='pre')
#plt.xlim(-0.5, len(learn_data[10]))
#plt.ylim(-0.2, 1.3)
#plt.title("Learned: %s" % (learn_labels_names[10]))
#plt.draw()
in_data = [1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,0,1,1,1]
new_data = collections.deque(maxlen=5)
new_data.append(0)
new_data.append(0)
new_data.append(0)
new_data.append(0)
new_data.append(0)
index = 0
#ax.set_ylim(-1.1, 1.1)
#ax.set_xlim(0, 10)
#del xdata[:]
#del ydata[:]
#line.set_data(xdata, ydata)
# Create a classifier: a support vector classifier
classifierSVM = svm.SVC(gamma=0.001)
classifierKNN = KNeighborsClassifier(4)
# We learn the digits on the first half of the digits
classifierSVM.fit(learn_data, learn_labels)
classifierKNN.fit(learn_data, learn_labels)
#predicted = classifier.predict(data)
movis = animation.writers['avconv']
movi = movis(fps=1, bitrate=1800)
ani = animation.FuncAnimation(figure, updatefig, interval=500, blit=True)
ani.save('mlmultipletest.mp4', writer=movi, fps=1, bitrate=1800)
#print("ANALYSED DATA:")
#for index in range(0, len(data)):
# print("%s: %s" % (data[index], learn_labels_names[predicted[index]]))
plt.show()