forked from Ninja91/Human-Activity-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.py
58 lines (34 loc) · 1.11 KB
/
knn.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
## Author: Hariharan Seshadri ##
## This program uses the KNN classifer to classify human activities ##
import numpy as np
from sklearn import neighbors, datasets
import common
###########################################################
print "Parsing"
X_train = common.parseFile( 'X_train.txt')
Y_train = common.parseFile( 'Y_train.txt')
Y_train = Y_train.flatten()
X_test = common.parseFile( 'X_test.txt')
Y_test = common.parseFile( 'Y_test.txt')
Y_test= Y_test.flatten()
print "Done"
print "Fitting Data"
ne = []
mean = []
for i in range(5,60,5):
n_neighbors = i ## Hyper - Parameter ##
clf = neighbors.KNeighborsClassifier(n_neighbors, weights='distance')
clf.fit(X_train, Y_train)
print"Done"
print "Predicting"
predicted = []
for x_test in X_test:
predicted.append( clf.predict(x_test)[0] )
print "Done"
print "Checking accuracy"
precision,recall,f_score = common.checkAccuracy( Y_test , predicted , [1,2,3,4,5,6]) # Must provide list of relavent labels #
ne.append(i)
mean.append( np.mean(f_score))
print mean
print "Done"
###########################################################