-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
178 lines (155 loc) · 5.37 KB
/
Copy pathtest.py
File metadata and controls
178 lines (155 loc) · 5.37 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import project4
import infogain
import util
from Node import *
import argparse
import gini
tryEx1 = [[1, 1, 0, 1, 1, 1, 1, 0],[1, 1, 1, 0, 1, 1, 1, 1],[1, 1, 0, 0, 0, 0, 0, 1]]
tryLa1 = [1,1,0]
""" Begin command line argument parsing """
parser = argparse.ArgumentParser()
parser.add_argument('testing', choices=['test1','test2','test3','test4'], help='data set to run testing on')
parser.add_argument('training', choices=['train1','train2','train3','train4'], help='data set to run training on')
parser.add_argument('method', choices=['infogain','gini'], help='select which learning method to use')
args = parser.parse_args()
testNum = args.testing[4]
trainNum = args.training[4]
method = args.method
""" End command line argument parsing """
""" Begin utility functions for trees """
def createTreeGini(node):
children = node.createChildrenGini()
for child in children:
print "Child - ",child," \n",child.toString()
#if examples empty, return parent plurality value
if(child.examplesEmpty()):
print "Examples empty"
#if labels empty return child plurality value
elif(child.attributesEmpty()):
print "Attributes empty"
#if we can determine yes or no
elif(child.isYes() or child.isNo()):
print "Yes or no decision"
#return
else:
createTreeGini(child)
def createTreeInfo(node):
children = node.createChildrenInfo()
for child in children:
if(child.examplesEmpty()):
print "Examples empty"
#if labels empty return child plurality value
elif(child.attributesEmpty()):
print "Attributes empty"
#if we can determine yes or no
elif(child.isYes() or child.isNo()):
print "Yes or no decision"
else:
createTreeInfo(child)
def traverseTree(root):
print "***** NODE ******\n"
print root
print "Parent: ",root.getParent()
print "Attrnum: ",root.attrnum
print root.toString()
children = root.getChildren()
for child in children:
traverseTree(child)
""" Creates a list of tuples, each corresponding to
a an example, label pair """
def createCounter(examples, labels):
newList = []
i = 0
for example in examples:
newList.append((example,labels[i]))
i+=1
return newList
def toString(dict):
for item in dict:
print item
def classifyList(examples, root):
retlist = []
for example in examples:
retval = classifyExample(example,root)
#retlist.append((example,retval))
retlist.append(retval)
return retlist
def classifyExample(example, root):
#print root.toString()
#print root.attrnum
if(root.isYes()):
return 1
elif(root.isNo()):
return 0
#otherwise remove the attribute split by
else:
#print "Attribute number ",root.attrnum
#print example
attrval = example[root.attrnum]
example.pop(root.attrnum)
if attrval == 1:
return classifyExample(example,root.getChildren()[0])
else:
return classifyExample(example,root.getChildren()[1])
def compareTree(root1, root2):
#if we did not split by the same attrnum
if root1.getAttrNum() != root2.getAttrNum():
return False
elif root1.getCounter() != root2.getCounter():
return False
else:
return compareChildren(root1, root2)
def compareChildren(root1, root2):
print "Comparing nodes ",root1," and ",root2
if root1.getAttrNum() != root2.getAttrNum():
print "Nodes were split by separate attribute numbers"
return False
if root2.getCounter() != root2.getCounter():
print "Nodes do not have the same counter"
return False
#if netheir root has children, and the counters and attrnum are the same
#we can return true
if not root1.getChildren() and not root2.getChildren():
print "Nodes are leaf nodes with no children"
print "Node 1 counter \n",root1.toString()
print "Node 2 counter \n",root2.toString()
return True
print "Node 1 counter \n",root1.toString()
print "Node 2 counter \n",root2.toString()
print "Creating children from (rel) attrnum - ",root1.getAttrNum()
return compareChildren(root1.getChildren()[0],root2.getChildren()[0]) and compareChildren(root1.getChildren()[1],root2.getChildren()[1])
def calculatePercentage(calcLabels,givenLabels):
numCorrect = 0.0
total = 0.0
for i in range(0,len(calcLabels)):
if calcLabels[i] == givenLabels[i]:
numCorrect += 1
total += 1
return numCorrect/total
""" END utility functions for trees """
data1Dict = createCounter(project4.data1TrainingExamples,project4.data1TrainingLabels)
data2Dict = createCounter(project4.data2TrainingExamples,project4.data2TrainingLabels)
infoRoot = Node(data2Dict)
giniRoot = Node(data2Dict)
info1Root = Node(data1Dict)
gini1Root = Node(data1Dict)
createTreeInfo(infoRoot)
createTreeGini(giniRoot)
createTreeInfo(info1Root)
createTreeGini(gini1Root)
print "Infogain v GiniIndex -- Data Set 2 -- ",compareTree(infoRoot,giniRoot)
info2Class = project4.evaluateBinaryLearner(project4.data2TestExamples,project4.data2TestLabels,giniRoot)
print info2Class
#print "Percentage Correct - ",calculatePercentage(info2Class,project4.data2TestLabels)
"""
print "Infogain v GiniIndex -- Data Set 2 -- ",compareTree(infoRoot,giniRoot)
print "Classifying Infogain implementation -- Data Set 1 --"
info1Classifications = classifyList(project4.data1TestExamples,info1Root)
print info1Classifications
print "Classifying Infogain implementation -- Data Set 2 --"
info2Classifications = classifyList(project4.data2TestExamples,infoRoot)
print info2Classifications
print "Classifying Gini Index implementation -- Data Set 1 --"
gini1Classifications = classifyList(project4.data1TestExamples,gini1Root)
print gini1Classifications
"""