-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn-kdtree-pca.py
46 lines (35 loc) · 1.45 KB
/
knn-kdtree-pca.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
#!/usr/bin/python
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from preproc import pca,medianfilter
def knn(train,test,labels,neighbours=10, runAll=None, median=True, runPCA=True, components=80):
print "Putting training data into matrix"
trainM = np.mat(train)
print "Running K nearest Neighbour, default = 10"
knn = KNeighborsClassifier(n_neighbors=neighbours, algorithm="kd_tree")
if(median == True):
print "Running data through Median filter..."
trainfilter = medianfilter(train)
knn.fit(trainfilter,labels)
result = knn.predict(trainfilter)
print "Writing to output file output.knn-kdtree-median.csv\n"
fwrite(result,fname='output.knn-kdtree-median.csv')
return(0)
if (runPCA==True):
trainReduce, testReduce = pca(train,test,components)
knn.fit(trainReduce,labels) #print at beginning of this
result = knn.predict(testReduce)
print "Writing output to file output.knn-kdtree-pca.csv\n"
fwrite(result,fname='output.knn-kdtree-pca.csv')
print "Running without PCA\n"
knn.fit(trainM,labels) #need this here - might as well print
result = knn.predict(test)
print "Writing output to file output.knn-kdtree.csv\n"
fwrite(result,fname="output.knn-kdtree.csv")
#return result
if __name__ == '__main__':
from fileio import fread, fwrite
train,labels = fread(f='data/train.csv', train=True)
test,tmplbl = fread(f='data/test.csv')
result = knn(train, test, labels,runPCA=False)
#print "RESULT IS: ",result