1
2
3
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
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 """
31
32 Tree.TreeNode.__init__(self, *args, **kwargs)
33 self.examples = []
34 self.badExamples = []
35 self.trainingExamples = []
36 self.testExamples = []
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
92 self.examples = examples
93
95 return self.badExamples
97 self.badExamples = examples
98
100 return self.trainingExamples
102 self.trainingExamples = examples
103
105 return self.testExamples
107 self.testExamples = examples
108
110 self.examples = []
111 self.badExamples = []
112 self.trainingExamples = []
113 self.testExamples = []
114 for child in self.GetChildren():
115 child.ClearExamples()
116