def buildSubTree(self, trainingExamples, currNode):
     """
     method to build our tree - this build method uses OO concepts which we use during our prediction later on
     """
     if self.attributesAndValues == []:
         return currNode
     divisionAttribute = self.getDivisionAttribute(trainingExamples)
     if currNode == None: # rootptr
         currNode = TreeNode(divisionAttribute.attrName)
     else:
         currNode.name = divisionAttribute.attrName
     # divide up our data based on the attribute we got back
     subLists = {}
     for attrValue in divisionAttribute.attrValues:
         subLists[attrValue] = []
     for example in trainingExamples:
         # if the example attribute matches our division attributes, add training example to correct sublist
         for attribute in example.attributes:
             if attribute.attrName == divisionAttribute.attrName:
                 subLists[attribute.attrValues[0]].append(example)
     # check if any of the sublists would require us to return a leaf node
     for subListKey in subLists:
         childNode = TreeNode()
         subList = subLists[subListKey]
         if subList == []: # no training examples, default to most common target value
             childNode.isLeaf = True
             childNode.targetValue = "p"
             currNode.childrenNodes[subListKey] = childNode
         elif self.isLeafNode(subList):
             childNode.isLeaf = True
             childNode.targetValue = subList[0].targetValue
             currNode.childrenNodes[subListKey] = childNode
         else:
             currNode.childrenNodes[subListKey] = childNode
             # recursively build using each sublist
             self.buildSubTree(subList, childNode)
     #return the root node with everything built on
     return currNode
Beispiel #2
0
 def buildSubTree(self, trainingExamples, currNode):
     """
     method to build our tree - this build method uses OO concepts which we use during our prediction later on
     """
     if self.attributesAndValues == []:
         return currNode
     divisionAttribute = self.getDivisionAttribute(trainingExamples)
     if currNode == None:  # rootptr
         currNode = TreeNode(divisionAttribute.attrName)
     else:
         currNode.name = divisionAttribute.attrName
     # divide up our data based on the attribute we got back
     subLists = {}
     for attrValue in divisionAttribute.attrValues:
         subLists[attrValue] = []
     for example in trainingExamples:
         # if the example attribute matches our division attributes, add training example to correct sublist
         for attribute in example.attributes:
             if attribute.attrName == divisionAttribute.attrName:
                 subLists[attribute.attrValues[0]].append(example)
     # check if any of the sublists would require us to return a leaf node
     for subListKey in subLists:
         childNode = TreeNode()
         subList = subLists[subListKey]
         if subList == []:  # no training examples, default to most common target value
             childNode.isLeaf = True
             childNode.targetValue = "p"
             currNode.childrenNodes[subListKey] = childNode
         elif self.isLeafNode(subList):
             childNode.isLeaf = True
             childNode.targetValue = subList[0].targetValue
             currNode.childrenNodes[subListKey] = childNode
         else:
             currNode.childrenNodes[subListKey] = childNode
             # recursively build using each sublist
             self.buildSubTree(subList, childNode)
     #return the root node with everything built on
     return currNode