mirror of
https://github.com/rdkit/rdkit.git
synced 2026-06-05 22:04:27 +08:00
71 lines
2.0 KiB
Python
Executable File
71 lines
2.0 KiB
Python
Executable File
# $Id$
|
|
#
|
|
# Copyright (C) 2001, 2003 greg Landrum and Rational Discovery LLC
|
|
# All Rights Reserved
|
|
#
|
|
""" Defines the class _QuantTreeNode_, used to represent decision trees with automatic
|
|
quantization bounds
|
|
|
|
_QuantTreeNode_ is derived from _DecTree.DecTreeNode_
|
|
|
|
"""
|
|
from ML.DecTree import DecTree,Tree
|
|
|
|
class QuantTreeNode(DecTree.DecTreeNode):
|
|
"""
|
|
|
|
"""
|
|
def __init__(self,*args,**kwargs):
|
|
apply(DecTree.DecTreeNode.__init__,(self,)+args,kwargs)
|
|
self.qBounds = []
|
|
self.nBounds = 0
|
|
def ClassifyExample(self,example,appendExamples=0):
|
|
""" Recursively classify an example by running it through the tree
|
|
|
|
**Arguments**
|
|
|
|
- example: the example to be classified
|
|
|
|
- appendExamples: if this is nonzero then this node (and all children)
|
|
will store the example
|
|
|
|
**Returns**
|
|
|
|
the classification of _example_
|
|
|
|
**NOTE:**
|
|
In the interest of speed, I don't use accessor functions
|
|
here. So if you subclass DecTreeNode for your own trees, you'll
|
|
have to either include ClassifyExample or avoid changing the names
|
|
of the instance variables this needs.
|
|
|
|
"""
|
|
if appendExamples:
|
|
self.examples.append(example)
|
|
if self.terminalNode:
|
|
return self.label
|
|
else:
|
|
val = example[self.label]
|
|
if not hasattr(self,'nBounds'): self.nBounds = len(self.qBounds)
|
|
if self.nBounds:
|
|
for i,bound in enumerate(self.qBounds):
|
|
if val < bound:
|
|
val = i
|
|
break
|
|
else:
|
|
val = i+1
|
|
else:
|
|
val = int(val)
|
|
return self.children[val].ClassifyExample(example,appendExamples=appendExamples)
|
|
|
|
def SetQuantBounds(self,qBounds):
|
|
self.qBounds = qBounds[:]
|
|
self.nBounds = len(self.qBounds)
|
|
def GetQuantBounds(self):
|
|
return self.qBounds
|
|
|
|
def __cmp__(self,other):
|
|
return cmp(type(self),type(other)) or \
|
|
cmp(self.qBounds,other.qBounds) or \
|
|
Tree.TreeNode.__cmp__(self,other)
|