Package rdkit :: Package ML :: Package DecTree :: Module DecTree
[hide private]
[frames] | no frames]

Source Code for Module rdkit.ML.DecTree.DecTree

  1  # 
  2  #  Copyright (C) 2000-2004  greg Landrum and Rational Discovery LLC 
  3  #  All Rights Reserved 
  4  # 
  5  """ Defines the class _DecTreeNode_, used to represent decision trees 
  6   
  7    _DecTreeNode_ is derived from _Tree.TreeNode_ 
  8   
  9  """ 
 10  from rdkit.ML.DecTree import Tree 
 11   
12 -class DecTreeNode(Tree.TreeNode):
13 """ This is used to represent decision trees 14 15 _DecTreeNode_s are simultaneously the roots and branches of decision trees. 16 Everything is nice and recursive. 17 18 _DecTreeNode_s can save the following pieces of internal state, accessible via 19 standard setter/getter functions: 20 21 1) _Examples_: a list of examples which have been classified 22 23 2) _BadExamples_: a list of examples which have been misclassified 24 25 3) _TrainingExamples_: the list of examples used to train the tree 26 27 4) _TestExamples_: the list of examples used to test the tree 28 29 """
30 - def __init__(self,*args,**kwargs):
31 #apply(Tree.TreeNode.__init__,(self,)+args,kwargs) 32 Tree.TreeNode.__init__(self, *args, **kwargs) 33 self.examples = [] 34 self.badExamples = [] 35 self.trainingExamples = [] 36 self.testExamples = []
37 - def ClassifyExample(self,example,appendExamples=0):
38 """ Recursively classify an example by running it through the tree 39 40 **Arguments** 41 42 - example: the example to be classified 43 44 - appendExamples: if this is nonzero then this node (and all children) 45 will store the example 46 47 **Returns** 48 49 the classification of _example_ 50 51 **NOTE:** 52 In the interest of speed, I don't use accessor functions 53 here. So if you subclass DecTreeNode for your own trees, you'll 54 have to either include ClassifyExample or avoid changing the names 55 of the instance variables this needs. 56 57 """ 58 if appendExamples: 59 self.examples.append(example) 60 if self.terminalNode: 61 return self.label 62 else: 63 val = example[self.label] 64 return self.children[val].ClassifyExample(example,appendExamples)
65
66 - def AddChild(self,name,label=None,data=None,isTerminal=0):
67 """ Constructs and adds a child with the specified data to our list 68 69 **Arguments** 70 71 - name: the name of the new node 72 73 - label: the label of the new node (should be an integer) 74 75 - data: the data to be stored in the new node 76 77 - isTerminal: a toggle to indicate whether or not the new node is 78 a terminal (leaf) node. 79 80 **Returns* 81 82 the _DecTreeNode_ which is constructed 83 84 """ 85 child = DecTreeNode(self,name,label,data,level=self.level+1,isTerminal=isTerminal) 86 self.children.append(child) 87 return child
88
89 - def GetExamples(self):
90 return self.examples
91 - def SetExamples(self,examples):
92 self.examples = examples
93
94 - def GetBadExamples(self):
95 return self.badExamples
96 - def SetBadExamples(self,examples):
97 self.badExamples = examples
98
99 - def GetTrainingExamples(self):
100 return self.trainingExamples
101 - def SetTrainingExamples(self,examples):
102 self.trainingExamples = examples
103
104 - def GetTestExamples(self):
105 return self.testExamples
106 - def SetTestExamples(self,examples):
107 self.testExamples = examples
108
109 - def ClearExamples(self):
110 self.examples = [] 111 self.badExamples = [] 112 self.trainingExamples = [] 113 self.testExamples = [] 114 for child in self.GetChildren(): 115 child.ClearExamples()
116