-
Notifications
You must be signed in to change notification settings - Fork 1
/
knn.py
82 lines (60 loc) · 3.19 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
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
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
import numpy as np
#from numpy import genfromtxt
#from numpy import array
from optparse import OptionParser
from timeit import default_timer as timer
#training_data = 'datasets/wine/wine.data.ope.training'
#testing_data = 'datasets/wine/wine.data.ope.testing'
#training_data = 'datasets/wine/wine.data.training'
#testing_data = 'datasets/wine/wine.data.testing'
#training_data = 'datasets/abalone/abalone.data.ope.training'
#testing_data = 'datasets/abalone/abalone.data.ope.testing'
#training_data = 'datasets/abalone/abalone.data.training'
#testing_data = 'datasets/abalone/abalone.data.testing'
#training_data = 'datasets/iris/iris.data.training'
#testing_data = 'datasets/iris/iris.data.testing'
#training_data = 'datasets/iris/iris.data.ope.training'
#testing_data = 'datasets/iris/iris.data.ope.testing'
#training_data = 'datasets/climate/climate.data.training'
#testing_data = 'datasets/climate/climate.data.testing'
#training_data = 'datasets/climate/climate.data.ope.training'
#testing_data = 'datasets/climate/climate.data.ope.testing'
#training_data = 'datasets/credit/credit.data.training'
#testing_data = 'datasets/credit/credit.data.testing'
#training_data = 'datasets/credit/credit.data.ope.training'
#testing_data = 'datasets/credit/credit.data.ope.testing'
parser = OptionParser()
parser.add_option("-f", dest="base_filename", help="base name of the training set and testing set files")
parser.add_option("-k", type="int", dest="k", help="number of nearest neighbors")
parser.add_option("-m", dest="method", default='uniform', help="method: uniform or distance")
(options, args) = parser.parse_args()
#knn = KNeighborsClassifier(n_neighbors=int(options.k), weights='distance', algorithm='brute')
knn = KNeighborsClassifier(n_neighbors=int(options.k), weights=options.method, algorithm='brute')
# reading the training data
training_data = options.base_filename + ".training"
data_and_classes = np.genfromtxt(training_data, dtype='int', skip_header=1)
#data = np.array(data_and_classes[:, 0:(len(data_and_classes[0]) - 1)])
data = data_and_classes[:, 0:(len(data_and_classes[0]) - 1)]
#classes = np.array(data_and_classes[:, (len(data_and_classes[0]) - 1)])
classes = data_and_classes[:, (len(data_and_classes[0]) - 1)]
knn.fit(data, classes)
# reading the testing data
testing_data = options.base_filename + ".testing"
data_and_classes = np.genfromtxt(testing_data, dtype='int', skip_header=1)
data = data_and_classes[:, 0:(len(data_and_classes[0]) - 1)]
#data = np.array(data_and_classes[:, 0:(len(data_and_classes[0]) - 1)])
classes = data_and_classes[:, (len(data_and_classes[0]) - 1)]
#classes = np.array(data_and_classes[:, (len(data_and_classes[0]) - 1)])
start = timer()
print(knn.score(data, classes))
end = timer()
print("Time to classify %d instances: %lf (average time: %lf)" % (len(data), end - start, (end-start)/len(data)))
prediction = knn.predict(data);
outputfile = open(options.base_filename + ".prediction" + ".k" + str(options.k) , "w+");
for class_predicted in prediction:
outputfile.write(str(class_predicted) + "\n")
outputfile.close()
#print prediction
#print confusion_matrix(classes, prediction);