-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_signal.py
210 lines (179 loc) · 6.69 KB
/
number_signal.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
# -*- coding: utf-8 -*-
#from dtw import dtw
from fastdtw import fastdtw
import numpy as np
import matplotlib.pyplot as plt
from random import randrange
from scipy import stats
from sklearn.neighbors import KNeighborsClassifier
#use temp variable if a function term is calculated >1 times-----------------------
def select_signal(train):
select_index1=randrange(0,len(train))
t1=train[select_index1]
train=np.delete(train,(select_index1),axis=0)
select_index2=randrange(0,len(train))
t2=train[select_index2]
train=np.delete(train,(select_index2),axis=0)
select_index3=randrange(0,len(train))
t3=train[select_index3]
train=np.delete(train,(select_index3),axis=0)
select_signal=np.delete([t1,t2,t3],(0),axis=1)
#print select_index1, select_index2
return train,select_signal
def dist(train_x,select_signal,train_y):#delete train_y---------------------------
def d(x, y):
#return (dtw(x,y,dist=lambda x,y: abs(x-y))[0])*1000
return (fastdtw(x,y)*1000)[0]
d1=[]
d2=[]
d3=[]
for i in range(len(train_x)):
d1.append(d(train_x[i],select_signal[0]))
for j in range(len(train_x)):
d2.append(d(train_x[j],select_signal[1]))
for k in range(len(train_x)):
d3.append(d(train_x[k],select_signal[2]))
dist=np.transpose([d1,d2,d3,train_y])
return dist
def projection(dist,train_y):#ONLY IN 2 CLASS LABELS, BUT not in consider now----------------------
val_1=1
val_2=-1
p1_idx=[index for index, val in enumerate(train_y) if val==val_1]
p2_idx=[index for index, val in enumerate(train_y) if val==val_2]
p1_dist=np.asmatrix(dist)[p1_idx]
p2_dist=np.asmatrix(dist)[p2_idx]
plt.plot(p1_dist[:,0],p1_dist[:,1],'bs',p2_dist[:,0],p2_dist[:,1],'g^')
plt.show()
return p1_dist,p2_dist
#=======================================================================gai dao zhe li =====================================
def box(dist,div_1,div_2):
dist2=np.transpose(dist)
x=max(dist2[0])/div_1 #length of a box
y=max(dist2[1])/div_2
z=max(dist2[2])/div_1
box_number=[]
for idx in range(len(dist)):
box_number.append(judge_box(dist[idx][0],dist[idx][1],dist[idx][2],x,y,z,div_1,div_2))
return np.reshape(box_number,(-1,1)),x,y,z
# x,y is unit length
def judge_box(x1,x2,x3,x,y,z,div_1,div_2):# need a default for data out of borders?
bx1=div_1+5-1
bx2=div_2+5-1
bx3=div_1+5-1
for i in range(div_1+5):
#bx1=range(div_1+5)
if x1>=i*x and x1<(i+1)*x:
bx1=i
else:
pass
for j in range(div_2+5):
#bx2=range(div_2+5)
if x2>=j*y and x2<(j+1)*y:
bx2=j
else:
pass
for k in range(div_2+5):
#bx2=range(div_2+5)
if x3>=k*z and x3<(k+1)*z:
bx3=k
else:
pass
return bx3*(div_1+5)*(div_2+5)+bx2*(div_1+5)+bx1
def bayes(dist,div_1,div_2):#array label, box_number
#p(c|Box)~~~p(Box|c)*p(c)=#c in B / # all
#each Box, take max{p(c|BOX)} wrt c
prob=[]
temp=np.delete(dist,(0),axis=1)#box_number
temp2=np.asarray(temp,dtype=int)
for i in range((div_2+5)*(div_1+5)*(div_1+5)):
temp_idx= [index for index, val in enumerate(temp2.tolist()) if val==[i]]
if(len(temp_idx)>0):
temp_list=np.asarray(np.delete(dist[temp_idx],(1),axis=1),dtype=int).tolist()
tttt=(stats.mode(temp_list)[0][0]).tolist()#return [2]
prob=prob+tttt
else:
#print "ggggggggggggggggggggggggggggggggggggggggggggg"
prob.append(99999)
return prob#[,,,,,]
def test(test,selected_signal,prob,x,y,z,div_1,div_2):
test_y=test[:,0]
test_x=test[:,1:len(test[0])]
#cal dtw dist
dist_test=np.transpose(dist(test_x,selected_signal,test_y))
#assign to boxes
test_box=[]
for i in range(len(test)):
test_box.append(judge_box(dist_test[0][i],dist_test[1][i],dist_test[2][i],x,y,z,div_1,div_2))
#print test_box
#predict
label=[]
acc=0.0
for h in range(len(test_box)):
label.append(prob[test_box[h]])
acc=cal_acc(label,test_y)
print "round acc is :",acc
return label,acc
def cal_acc(predict,true):
acc=0.0
for p in range(len(true)):
if predict[p]==true[p]:
acc=acc+1
acc=acc/len(true)
return acc
def train(train,test_data,div_1,div_2):
train,selected_signal=select_signal(train)
train_y=train[:,0]
train_x=np.delete(train,(0),axis=1)
#test_y=test[:,0]
temp_d=dist(train_x,selected_signal,train_y)#array x1,x2,label
#projection(temp_d,train_y)
box_number,x,y,z=box(temp_d,div_1,div_2)
temp_d=np.append(temp_d,box_number,axis=1)
temp_d=np.delete(temp_d,(0,1,2),axis=1)#array label, box_number
prob=bayes(temp_d,div_1,div_2)
#print prob
predict_label,acc=test(test_data,selected_signal,prob,x,y,z,div_1,div_2)
'''
#knn================================================================================================================================================
test_y=test_data[:,0]
test_x=np.delete(test_data,(0),axis=1)
a_d=dist(train_x,selected_signal,train_y).tolist()
b_d=dist(test_x,selected_signal,test_y).tolist()
a_d=(np.delete(a_d,(-1),axis=1)).tolist()
b_d=(np.delete(b_d,(-1),axis=1)).tolist()
neigh=KNeighborsClassifier(n_neighbors=1)
neigh.fit(a_d,train_y)
predict_label222=neigh.predict(b_d)
'''
#=====================================================================================================================================================
return predict_label
def ensemble(times,train_data,test,div_1,div_2):
temp_label=[]
label=[]
test_y=test[:,0]
#test_y=test[:][0]
for i in range(times):
print "round ",i+1
temp_label.append(train(train_data,test,div_1,div_2))
temp_label=np.transpose(temp_label)
accu_acc=[]
for k in range(times):
for i in range(len(test)):
no_99=temp_label[i][0:k+1]
no_temp=[]
for no in range(len(no_99)):
if no_99[no]==99999:
no_temp.append(no)
else:
pass
no_99=np.delete(no_99,no_temp)
tttt=(stats.mode(no_99)[0])
label.append(tttt)
#print label
acc=cal_acc(label,test_y)
label=[]
accu_acc.append(acc)
print accu_acc
print "final acc is : ",accu_acc[-1]
plt.plot(accu_acc)
plt.show()