-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHAR_modeling
257 lines (202 loc) · 7.21 KB
/
HAR_modeling
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in
#-----------------
# Michael Hackenberg
# This code is copied from the Kaggle online editor so I don't know if it will run the way it is currently formatted
# Each section denoted by a dashed line was in its own cell in the kaggle notebook
#-----------------
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))
# Any results you write to the current directory are saved as output.
import matplotlib
import matplotlib.pyplot as plt
test = pd.read_csv("../input/test.csv")
train = pd.read_csv("../input/train.csv")
from sklearn.utils import shuffle
train = shuffle(train)
train.head()
#-----------------------
import sklearn
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
features = train.iloc[:,0:562]
label = train['Activity']
clf = ExtraTreesClassifier()
clf = clf.fit(features, label)
model = SelectFromModel(clf,prefit=True)
new_Features = model.transform(features)
print(new_Features.shape)
new_Features
#---------------------------
from sklearn.neighbors import KDTree
from sklearn.neighbors import NearestNeighbors
nn = NearestNeighbors(5, algorithm='kd_tree')
k = nn.fit(new_Features)
#--------------------------------
# Create a graph to ompare several different models. Used as a starting point for the project
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
array = test.values
X = array[:,0:561]
Y = array[:,562]
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
results = []
names = []
scoring = 'accuracy'
for name, model in models:
kfold = model_selection.KFold(n_splits=10, shuffle=True, random_state=7)
cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()
#------------------------
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
array = train.values
info = array[:,0:561]
activity = array[:,562]
arr2 = test.values
inf2 = arr2[:,0:561]
act2 = arr2[:,562]
knn = KNeighborsClassifier()
knn.fit(info, activity)
KNeighborsClassifier(algorithm='kd_tree', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=1, n_neighbors=5, p=2,
weights='uniform')
predictions = knn.predict(inf2)
activity_names = ['STANDING','SITTING','LAYING','WALKING','WALKING_UPSTAIRS','WALKING_DOWNSTAIRS']
print(classification_report(act2, predictions)) #,target_names=activity_names
#---------------------------------
# Combine the two provided .csv files so that the data can be speparated however desired
array = train.values
arr2 = test.values
arr3 = np.concatenate((array,arr2),axis=0)
print(arr3.shape)
#-------------------------
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import classification_report
#combine data
arr1 = train.values
arr2 = test.values
arr3 = np.concatenate((arr1,arr2),axis=0)
#separate train and test labels
trainLabel = arr3[:7000,562]
testLabel = arr3[7000:,562]
#Create matrix of data
#allData = arr3.iloc[:,:561].as_matrix()
dataMatrix = np.asmatrix(arr3[:,:561],dtype='float')
#Find Most relevant features
U, s, V = np.linalg.svd(dataMatrix,full_matrices=False)
#Use most relevant features
numFeatures = 250
newtrain = U[:7000,:numFeatures]
newtest = U[7000:,:numFeatures]
#run the model
lda = LinearDiscriminantAnalysis()
lda.fit(newtrain, trainLabel)
predictions = lda.predict(newtest)
activity_names = ['STANDING','SITTING','LAYING','WALKING','WALKING_UPSTAIRS','WALKING_DOWNSTAIRS']
print(classification_report(testLabel, predictions))
#-----------------------------------------
# Retrive the precision value from the formatted output of classification report
def collect_avg_precision(report):
report_data = []
lines = report.split('\n')
values = lines[9].split(' ')
strAvg = values[2].strip()
fltAvg = float(strAvg)
return fltAvg
#-------------------------------------
# Run a model with variable amount of features to populate a collection of average precision values
avgPrec = []
#lda = LinearDiscriminantAnalysis()
#knn = KNeighborsClassifier() # max precision .88 with 23-25 features
lr = LogisticRegression()
for numFeatures in range(10,560,10):#10,560,10
#separate data
newtrain = U[:7000,:numFeatures]
newtest = U[7000:,:numFeatures]
#run the model
lr.fit(newtrain, trainLabel)
#test the model
predictions = lr.predict(newtest)
activity_names = ['STANDING','SITTING','LAYING','WALKING','WALKING_UPSTAIRS','WALKING_DOWNSTAIRS']
report = classification_report(testLabel, predictions)
value = collect_avg_precision(report)
avgPrec.append([numFeatures, value])
#---------------------------------------------
trainL = arr3[:7000,562]
testL = arr3[7000:,562]
trainD = arr3[:7000,0:561]
testD = arr3[7000:,0:561]
#lda = LinearDiscriminantAnalysis()
#knn = KNeighborsClassifier() # max precision .88 with 23-25 features
lr = LogisticRegression()
#run the model
lr.fit(trainD, trainL)
#test the model
predictions = lr.predict(testD)
activity_names = ['STANDING','SITTING','LAYING','WALKING','WALKING_UPSTAIRS','WALKING_DOWNSTAIRS']
report = classification_report(testL, predictions)
value = collect_avg_precision(report)
value
#avgPrec.append([numFeatures, value])
#------------------------------------------
# Create the graph of average precision based on number of features selected
depVar = []
indVar = []
for i in range(len(avgPrec)):
depVar.append(avgPrec[i][0])
indVar.append(avgPrec[i][1])
fig = plt.figure()
fig.suptitle('Features and Precision')
fig.add_subplot(111)
plt.scatter(depVar, indVar)
plt.show()
#---------------------------------------
V
vArr = np.squeeze(np.asarray(V))
vArr.sort()
vArr
#--------------------------------------
# FIND IMPORTANT FEATURES
#U
#V
col1Mat = V[:,0]
col1Arr = np.squeeze(np.asarray(col1Mat))
#col1Arr #raw: first:-1.64580409e-02 last:0.00000000e+00
#sorted(col1Arr) # sorted: from -0.3419128796815134 to 0.27418404488480869
col1Arr.sort()
col1Arr[0]
#col1Arr[-1]
# Graph it
depVar = []
indVar = range(len(col1Arr))
for i in range(len(col1Arr)):
depVar.append(col1Arr[i])
fig = plt.figure()
fig.suptitle('SVD Important Features')
fig.add_subplot(111)
plt.scatter(indVar, depVar)
plt.show()