-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.py
More file actions
executable file
·142 lines (122 loc) · 4.5 KB
/
unit_tests.py
File metadata and controls
executable file
·142 lines (122 loc) · 4.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import ID3, parse, random
def testID3AndEvaluate():
data = [dict(a=1, b=0, Class=1), dict(a=1, b=1, Class=1)]
tree = ID3.ID3(data, 0)
if tree != None:
ans = ID3.evaluate(tree, dict(a=1, b=0))
if ans != 1:
print("ID3 test failed.")
else:
print("ID3 test succeeded.")
else:
print("ID3 test failed -- no tree returned")
def testPruning():
# data = [dict(a=1, b=1, c=1, Class=0), dict(a=1, b=0, c=0, Class=0), dict(a=0, b=1, c=0, Class=1), dict(a=0, b=0, c=0, Class=1), dict(a=0, b=0, c=1, Class=0)]
# validationData = [dict(a=0, b=0, c=1, Class=1)]
data = [dict(a=0, b=1, c=1, d=0, Class=1), dict(a=0, b=0, c=1, d=0, Class=0), dict(a=0, b=1, c=0, d=0, Class=1),
dict(a=1, b=0, c=1, d=0, Class=0), dict(a=1, b=1, c=0, d=0, Class=0), dict(a=1, b=1, c=0, d=1, Class=0),
dict(a=1, b=1, c=1, d=0, Class=0)]
validationData = [dict(a=0, b=0, c=1, d=0, Class=1), dict(a=1, b=1, c=1, d=1, Class = 0)]
tree = ID3.ID3(data, 0)
#print(ID3.evaluate(tree, dict(a=0, b=0, c=1, d=0)))
ID3.prune(tree, validationData)
if tree != None:
ans = ID3.evaluate(tree, dict(a=0, b=0, c=1, d=0))
#print(ans)
if ans != 1:
print("pruning test failed.")
else:
print("pruning test succeeded.")
else:
print("pruning test failed -- no tree returned.")
def testID3AndTest():
trainData = [dict(a=1, b=0, c=0, Class=1), dict(a=1, b=1, c=0, Class=1),
dict(a=0, b=0, c=0, Class=0), dict(a=0, b=1, c=0, Class=1)]
testData = [dict(a=1, b=0, c=1, Class=1), dict(a=1, b=1, c=1, Class=1),
dict(a=0, b=0, c=1, Class=0), dict(a=0, b=1, c=1, Class=0)]
#print(trainData)
tree = ID3.ID3(trainData, 0)
#print(trainData)
fails = 0
if tree != None:
acc = ID3.test(tree, trainData)
if acc == 1.0:
print("testing on train data succeeded.")
else:
print("testing on train data failed.")
fails = fails + 1
acc = ID3.test(tree, testData)
if acc == 0.75:
print("testing on test data succeeded.")
else:
print("testing on test data failed.")
fails = fails + 1
if fails > 0:
print("Failures: ", fails)
else:
print("testID3AndTest succeeded.")
else:
print("testID3andTest failed -- no tree returned.")
# inFile - string location of the house data file
def testPruningOnHouseData(inFile):
withPruning = []
withoutPruning = []
data = parse.parse(inFile)
for i in range(100):
random.shuffle(data)
train = data[:len(data)//2]
valid = data[len(data)//2:3*len(data)//4]
test = data[3*len(data)//4:]
tree = ID3.ID3(train, 'democrat')
acc = ID3.test(tree, train)
print("training accuracy: ",acc)
acc = ID3.test(tree, valid)
print("validation accuracy: ",acc)
acc = ID3.test(tree, test)
print("test accuracy: ",acc)
ID3.prune(tree, valid)
acc = ID3.test(tree, train)
print("pruned tree train accuracy: ",acc)
acc = ID3.test(tree, valid)
print("pruned tree validation accuracy: ",acc)
acc = ID3.test(tree, test)
print("pruned tree test accuracy: ",acc)
withPruning.append(acc)
tree = ID3.ID3(train+valid, 'democrat')
acc = ID3.test(tree, test)
print("no pruning test accuracy: ",acc)
withoutPruning.append(acc)
print(withPruning)
print(withoutPruning)
print("average with pruning",sum(withPruning)/len(withPruning)," without: ",sum(withoutPruning)/len(withoutPruning))
def customPruningOnHouseData(inFile,n):
"""
Additional function for creating graph. Kept to reference
"""
withPruning = []
withoutPruning = []
data = parse.parse(inFile)
data = data[0:n]
for i in range(100):
random.shuffle(data)
train = data[:len(data)//2]
valid = data[len(data)//2:3*len(data)//4]
test = data[3*len(data)//4:]
tree = ID3.ID3(train, 'democrat')
acc = ID3.test(tree, train)
acc = ID3.test(tree, valid)
acc = ID3.test(tree, test)
ID3.prune(tree, valid)
acc = ID3.test(tree, train)
acc = ID3.test(tree, valid)
acc = ID3.test(tree, test)
withPruning.append(acc)
tree = ID3.ID3(train+valid, 'democrat')
acc = ID3.test(tree, test)
withoutPruning.append(acc)
print("average with pruning",sum(withPruning)/len(withPruning)," without: ",sum(withoutPruning)/len(withoutPruning))
return n, sum(withPruning)/len(withPruning), sum(withoutPruning)/len(withoutPruning)
testID3AndEvaluate()
testID3AndTest()
testPruning()
testPruningOnHouseData("house_votes_84.data")