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 from rdkit.six import cmp
14
16 """
17
18 """
24 """ Recursively classify an example by running it through the tree
25
26 **Arguments**
27
28 - example: the example to be classified
29
30 - appendExamples: if this is nonzero then this node (and all children)
31 will store the example
32
33 **Returns**
34
35 the classification of _example_
36
37 **NOTE:**
38 In the interest of speed, I don't use accessor functions
39 here. So if you subclass DecTreeNode for your own trees, you'll
40 have to either include ClassifyExample or avoid changing the names
41 of the instance variables this needs.
42
43 """
44 if appendExamples:
45 self.examples.append(example)
46 if self.terminalNode:
47 return self.label
48 else:
49 val = example[self.label]
50 if not hasattr(self,'nBounds'): self.nBounds = len(self.qBounds)
51 if self.nBounds:
52 for i,bound in enumerate(self.qBounds):
53 if val < bound:
54 val = i
55 break
56 else:
57 val = i+1
58 else:
59 val = int(val)
60 return self.children[val].ClassifyExample(example,appendExamples=appendExamples)
61
63 self.qBounds = qBounds[:]
64 self.nBounds = len(self.qBounds)
67
69 return (self<other)*-1 or (other<self)*1
70
72 if str(type(self)) < str(type(other)): return True
73 if self.qBounds<other.qBounds: return True
74 if Tree.TreeNode.__lt__(self,other): return True
75 return False
77 return not self<other and not other<self
78
80 """ returns a string representation of the tree
81
82 **Note**
83
84 this works recursively
85
86 """
87 here = '%s%s %s\n'%(' '*self.level,self.name,str(self.qBounds))
88 for child in self.children:
89 here = here + str(child)
90 return here
91