-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestlearner.py
62 lines (59 loc) · 2.26 KB
/
testlearner.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
import KNNlearner as p
import numpy as np
import time
import math
import matplotlib.pyplot as plt
def call_this():
for data in range(0,2):
learner = {}
if(data == 0):
f = open('/nethome/nagarwal36/QSTK/Examples/KNN/data-classification-prob.csv', 'r')
file = 'data-classification-prob.csv'
plot = 'plot1.pdf'
else:
global i
i=0
f = open('/nethome/nagarwal36/QSTK/Examples/KNN/data-ripple-prob.csv','r')
file = 'data-ripple-prob.csv'
plot = 'plot2.pdf'
karr = []
plott = []
for k in range(1,80):
#print "k is",k
learner = p.KNNlearner(k,'mean')
karr.append(k);
f = open('/nethome/nagarwal36/QSTK/Examples/KNN/'+file, 'r')
x = np.array([map(float,s.strip().split(',')) for s in f.readlines()])
starttime = time.clock()
for eviden in range(0,int(math.ceil(0.6*len(x)))):
#print x[eviden]
learner.addEvidence(x[eviden],x[eviden])
endtime = time.clock()
#print 'add evidence time',endtime-starttime
learner.change()
#f1 = open('/nethome/nagarwal36/QSTK/Examples/KNN/data1.csv','r')
second_half = x #np.array([map(float,s.strip().split(',')) for s in f1.readlines()])
result = []
coff = []
expected = []
actual = []
begintime = time.clock()
for i in range(int(math.ceil(0.6*len(second_half))),len(second_half)):
result = learner.query(second_half[i])
expected.append(result)
actual.append(second_half[i][2])
finaltime = time.clock()
#print 'query time',finaltime - begintime
try:
#print expected
#print actual
value = np.corrcoef([actual,expected])
plott.append(value[0][1])
except:
print "ERROR"
plt.clf()
plt.plot(karr,plott)
plt.ylabel("correlation coefficient")
plt.xlabel("k values")
plt.savefig(plot,type='pdf')
call_this()