-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampletuning.py
More file actions
82 lines (69 loc) · 2.01 KB
/
exampletuning.py
File metadata and controls
82 lines (69 loc) · 2.01 KB
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
'''
This example file shows how to tune the hyper parameter k.
'''
#get trainer and model
import kNN as knn
import modeler as model
#using pyplotlib to plot error with k
import matplotlib.pyplot as plt
#get trainer
trainer = knn.knn()
#get training data, with some noise
X,Y = model.generateData(100, noiseFactor=.2)
#get validation data, we will assert that this data has no noise, even though this is not accurate in real data
validX, validY = model.generateData(25, noiseFactor=0)
#get test data, also with no noise
testX, testY = model.generateData(25, noiseFactor=0)
#load training data
trainer.loadData(X,Y)
#holds error on validation set for each k
validErrors = {}
#setup k's to test
ks = range(1, 100, 2)
for k in ks:
#holds the error count for this k
error = 0
#set the k parameter in the trainer
trainer.setK(k)
#for each validation example
for i in range(len(validX)):
#try to predict its label using training data
confidence, guess = trainer.predict(validX[i], negativeValue=0)
#check if wrong
if guess != validY[i]:
error += 1
#save error
validErrors[k] = (error / len(validX))
print("Validation Error", k, (error / len(validX)))
#find best k
error = 1
bestK = 1
#for each k tried
for key in validErrors.keys():
#if this k is better, save it
if validErrors[key] < error:
error = validErrors[key]
bestK = key
#set k to trainer
trainer.setK(bestK)
#holds errors
error = 0
#for each item in test data
for i in range(len(testX)):
#try to predict its label using training data
confidence, guess = trainer.predict(testX[i], negativeValue=0)
#check if wrong
if guess != testY[i]:
error += 1
#print error and accuract
print("K:", bestK)
print("Error:", error / len(testX))
print("Accuracy:", 1 - error / len(testX))
#holds errors related to each k
errors = []
#for each k, add its respective error to errors list
for k in ks:
errors.append(validErrors[k])
#plot, k and errors
plt.plot(ks, errors)
plt.show()