1
2
3
4
5
6 """ Defines the class _QuantTreeNode_, used to represent decision trees with automatic
7 quantization bounds
8
9 _QuantTreeNode_ is derived from _DecTree.DecTreeNode_
10
11 """
12 from rdkit.ML.DecTree import DecTree,Tree
13
15 """
16
17 """
23 """ Recursively classify an example by running it through the tree
24
25 **Arguments**
26
27 - example: the example to be classified
28
29 - appendExamples: if this is nonzero then this node (and all children)
30 will store the example
31
32 **Returns**
33
34 the classification of _example_
35
36 **NOTE:**
37 In the interest of speed, I don't use accessor functions
38 here. So if you subclass DecTreeNode for your own trees, you'll
39 have to either include ClassifyExample or avoid changing the names
40 of the instance variables this needs.
41
42 """
43 if appendExamples:
44 self.examples.append(example)
45 if self.terminalNode:
46 return self.label
47 else:
48 val = example[self.label]
49 if not hasattr(self,'nBounds'): self.nBounds = len(self.qBounds)
50 if self.nBounds:
51 for i,bound in enumerate(self.qBounds):
52 if val < bound:
53 val = i
54 break
55 else:
56 val = i+1
57 else:
58 val = int(val)
59 return self.children[val].ClassifyExample(example,appendExamples=appendExamples)
60
62 self.qBounds = qBounds[:]
63 self.nBounds = len(self.qBounds)
66
71
73 """ returns a string representation of the tree
74
75 **Note**
76
77 this works recursively
78
79 """
80 here = '%s%s %s\n'%(' '*self.level,self.name,str(self.qBounds))
81 for child in self.children:
82 here = here + str(child)
83 return here
84