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