-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw_train_model.py
More file actions
213 lines (173 loc) · 5.06 KB
/
w_train_model.py
File metadata and controls
213 lines (173 loc) · 5.06 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
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
#
# DISCLAIMER
#
# This script is copyright protected 2015 by
# Edison Thomaz, Irfan Essa, Gregory D. Abowd
#
# All software is provided free of charge and "as is", without
# warranty of any kind, express or implied. Under no circumstances
# and under no legal theory, whether in tort, contract, or otherwise,
# shall Edison Thomaz, Irfan Essa or Gregory D. Abowd be liable to
# you or to any other person for any indirect, special, incidental,
# or consequential damages of any character including, without
# limitation, damages for loss of goodwill, work stoppage, computer
# failure or malfunction, or for any and all other damages or losses.
#
# If you do not agree with these terms, then you are advised to
# not use this software.
#
from __future__ import division
import time
import datetime
import csv
from sklearn import svm, neighbors, metrics, cross_validation, preprocessing
from sklearn.externals import joblib
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import auc, silhouette_score
from sklearn.cluster import KMeans, DBSCAN
from scipy import *
from scipy.stats import *
from scipy.signal import *
from numpy import *
# Set the frame and step size
frame_size_seconds = 6
step_size_seconds = int(frame_size_seconds/2)
sampling_rate = 25
frame_size = frame_size_seconds * sampling_rate
step_size = step_size_seconds * sampling_rate
# -----------------------------------------------------------------------------------
#
#
#
#
#
# Train Model
#
#
#
#
#
#
# -----------------------------------------------------------------------------------
ts = time.time()
current_time = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print ""
print "---------------------------------------------------------"
print ""
print ""
print ""
print ""
print "Train Model for All"
print ""
print current_time
print ""
print ""
print ""
print ""
print "---------------------------------------------------------"
print ""
first_time_in_exclude_loop = 1
for p_counter in xrange(1, 22, 1):
if p_counter==14:
continue
try:
print "Loading: " + "../participants/" + str(p_counter) + "/datafiles/waccel_tc_ss_label.csv"
L_T = genfromtxt("../participants/" + str(p_counter) + "/datafiles/waccel_tc_ss_label.csv", delimiter=',')
except:
error_participant_string = str(p_counter)
print "** Error loading data for participant: " + str(p_counter)
continue
# Remove the relative timestamp
L_T = L_T[:,1:]
if first_time_in_exclude_loop==1:
first_time_in_exclude_loop = 0
Z_T = L_T
else:
Z_T = vstack((Z_T,L_T))
print ""
print "Shape of training data: " + str(Z_T.shape)
print ""
print str(Z_T)
print ""
# Number of inputs
number_of_inputs = Z_T.shape[1]-1
# -----------------------------------------------------------------------------------
#
# Training
#
# -----------------------------------------------------------------------------------
print ""
print "---------------------------------------------------------"
print " Loading Features + Build Model"
print "---------------------------------------------------------"
print ""
pos_examples_counter = 0
neg_examples_counter = 0
# Calculate features for frame
for counter in xrange(0,len(Z_T),step_size):
# Add up labels
A_T = Z_T[counter:counter+frame_size, number_of_inputs]
S_T = sum(A_T)
if S_T>step_size:
pos_examples_counter = pos_examples_counter + 1
S_T = 1
else:
neg_examples_counter = neg_examples_counter + 1
S_T = 0
R_T = Z_T[counter:counter+frame_size, :number_of_inputs]
M_T = mean(R_T,axis=0)
V_T = var(R_T,axis=0)
SK_T = stats.skew(R_T,axis=0)
K_T = stats.kurtosis(R_T,axis=0)
RMS_T = sqrt(mean(R_T**2,axis=0))
H_T = hstack((M_T,V_T))
H_T = hstack((H_T,SK_T))
H_T = hstack((H_T,K_T))
H_T = hstack((H_T,RMS_T))
# ----------------------------- Label -------------------------------------
# Add label
H_T = hstack((H_T,S_T))
if counter==0:
F_T = H_T
else:
F_T = vstack((F_T,H_T))
# if S_T==1:
# for p_counter in xrange(0,5,1):
# F_T = vstack((F_T,H_T))
print ""
print "Positive Examples: " + str(pos_examples_counter)
print "Negative Examples: " + str(neg_examples_counter)
print ""
print ""
print "Print of F_T: " + str(F_T)
print ""
# Get features and labels
X_T = F_T[:,:number_of_inputs*5]
Y_T = F_T[:,number_of_inputs*5]
print ""
print "X_T: " + str(X_T)
print ""
print "Shape of X_T: " +str(X_T.shape)
print ""
print "Y_T: " + str(Y_T)
print ""
print "Shape of Y_T: " + str(Y_T.shape)
# Train classifier
#clf = ExtraTreesClassifier(n_estimators=100)
clf = RandomForestClassifier(n_estimators=185)
#clf = AdaBoostClassifier(n_estimators=185)
#clf = KNeighborsClassifier(n_neighbors=2)
#clf = svm.LinearSVC()
#clf = GaussianNB()
#clf = DecisionTreeClassifier()
#clf = LogisticRegression()
print ""
print "Training model..."
clf.fit(X_T,Y_T)
print ""
print "Saving model..."
joblib.dump(clf, '../model/wrist.pkl')