forked from Dharmitha/Fall-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experiment_2.py
224 lines (186 loc) · 6.9 KB
/
Experiment_2.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 17 00:21:10 2018
@author: Admin
"""
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense, Dropout
from numpy import genfromtxt
import numpy
import codecs
import csv
from sklearn.metrics import confusion_matrix,roc_auc_score,roc_curve,auc,accuracy_score
from sklearn.metrics import precision_score,recall_score,classification_report
import matplotlib.pyplot as plt
from keras.utils import np_utils
import numpy as np
# create a sequence classification instance
def get_sequence(n_timesteps,time):
x = [data[index] for index in range((time*10)+1 ,(time*10)+10+1)]
x = numpy.delete(x, (data.shape[1]-1), axis=1)
x= numpy.array(x)
y = [Y[index] for index in range((time*10) ,(time*10)+10)]
y=numpy.array(y)
# reshape input and output data to be suitable for LSTMs
X = x.reshape(1, n_timesteps, (data.shape[1]-1))
y = y.reshape(1, n_timesteps, y.shape[1])
return X, y
data = genfromtxt('Experiment2.csv', delimiter=',')
m =[data[i][-1] for i in range(1,7671)]
Y = np_utils.to_categorical(m)
# define problem properties
n_timesteps = 10
# define LSTM
model = Sequential()
model.add(LSTM(30,input_shape = (None, (data.shape[1]-1)),return_sequences=True))
model.add(Dropout(0.30))
model.add(Dense(3, activation='softmax'))
#model.add(Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
print(model.summary())
# train LSTM
for epoch in range(0,149):
# generate new random sequence
X,y = get_sequence(n_timesteps,epoch)
# fit model for one epoch on this sequence
model.fit(X, y,batch_size=1, verbose=0)
for epoch in range(192,343):
# generate new random sequence
X,y = get_sequence(n_timesteps,epoch)
# fit model for one epoch on this sequence
model.fit(X, y,batch_size=1, verbose=0)
for epoch in range(385,534):
# generate new random sequence
X,y = get_sequence(n_timesteps,epoch)
# fit model for one epoch on this sequence
model.fit(X, y,batch_size=1, verbose=0)
for epoch in range(576,727):
# generate new random sequence
X,y = get_sequence(n_timesteps,epoch)
# fit model for one epoch on this sequence
model.fit(X, y,batch_size=1, verbose=0)
countp=0
countn=0
ypredicted = []
yactual = []
for d in range(150,191):
X,y = get_sequence(n_timesteps,d)
#yactual[] = [y[i] for i in range(len(y))]
yhat = model.predict(X,verbose=0)[0]
for index1 in range(10):
i = np.where(yhat[index1] == yhat[index1].max())
hin = i[0]
for index2 in range(3):
if(index2==hin):
yhat[index1][index2]=1
else:
yhat[index1][index2]=0
j= yhat
k= y[0]
for index1 in range(10):
ypredicted.append(j[index1])
yactual.append(k[index1])
for d in range(343,384):
X,y = get_sequence(n_timesteps,d)
#yactual[] = [y[i] for i in range(len(y))]
yhat = model.predict(X,verbose=0)[0]
for index1 in range(10):
i = np.where(yhat[index1] == yhat[index1].max())
hin = i[0]
for index2 in range(3):
if(index2==hin):
yhat[index1][index2]=1
else:
yhat[index1][index2]=0
j= yhat
k= y[0]
for index1 in range(10):
ypredicted.append(j[index1])
yactual.append(k[index1])
for d in range(534,575):
X,y = get_sequence(n_timesteps,d)
#yactual[] = [y[i] for i in range(len(y))]
yhat = model.predict(X,verbose=0)[0]
for index1 in range(10):
i = np.where(yhat[index1] == yhat[index1].max())
hin = i[0]
for index2 in range(3):
if(index2==hin):
yhat[index1][index2]=1
else:
yhat[index1][index2]=0
j= yhat
k= y[0]
for index1 in range(10):
ypredicted.append(j[index1])
yactual.append(k[index1])
for d in range(727,766):
X,y = get_sequence(n_timesteps,d)
#yactual[] = [y[i] for i in range(len(y))]
yhat = model.predict(X,verbose=0)[0]
for index1 in range(10):
i = np.where(yhat[index1] == yhat[index1].max())
hin = i[0]
for index2 in range(3):
if(index2==hin):
yhat[index1][index2]=1
else:
yhat[index1][index2]=0
j= yhat
k= y[0]
for index1 in range(10):
ypredicted.append(j[index1])
yactual.append(k[index1])
ya = []
yp = []
for index1 in range(len(ypredicted)):
if (ypredicted[index1][0]==1 and ypredicted[index1][1]==0 and ypredicted[index1][2]==0):
yp.append(0)
if (ypredicted[index1][0]==0 and ypredicted[index1][1]==1 and ypredicted[index1][2]==0 ):
yp.append(1)
if (ypredicted[index1][0]==0 and ypredicted[index1][1]==0 and ypredicted[index1][2]==1 ):
yp.append(2)
for index1 in range(len(yactual)):
if (yactual[index1][0]==1 and yactual[index1][1]==0 and yactual[index1][2]==0):
ya.append(0)
if (yactual[index1][0]==0 and yactual[index1][1]==1 and yactual[index1][2]==0 ):
ya.append(1)
if (yactual[index1][0]==0 and yactual[index1][1]==0 and yactual[index1][2]==1 ):
ya.append(2)
for i in range(len(ya)):
if(ya[i] == yp[i]):
countp = countp+1
else:
if(i!=len(ya)-1):
if((ya[i+1]!=0 and yp[i]!=0) or (ya[i-1]!=0 and yp[i]!=0)):
countp=countp+1
else:
countn = countn+1
else:
countn = countn+1
yaa=[]
ypp=[]
for i in range(len(ya)):
if(ya[i]==yp[i]):
yaa.append(ya[i])
ypp.append(yp[i])
else:
if(ya[i]!=yp[i]):
if((ya[i]==2 and yp[i+1]==2) or (ya[i-1]==2 and yp[i]==2) or (ya[i]==2 and yp[i-1]==2) or (ya[i+1]==2 and yp[i]==2)):
yaa.append(2)
ypp.append(2)
else:
yaa.append(ya[i])
ypp.append(yp[i])
conf_arr = confusion_matrix(yaa, ypp)
conf_ar_1 = confusion_matrix(ya, yp)
print(classification_report(yaa, ypp))
print(classification_report(ya, yp))
print(accuracy_score(yaa,ypp))
print(accuracy_score(ya,yp))
with codecs.open("output.txt", "a", "utf-8") as my_file:
wr = csv.writer(my_file,delimiter="\n")
for i in range(len(ya)):
t = ('Expected:', ya[i], 'Predicted', yp[i])
wr.writerow(t)