Files
rdkit/Python/ML/DecTree/QuantTree.py
Greg Landrum 75a79b6327 initial import
2006-05-06 22:20:08 +00:00

71 lines
2.0 KiB
Python
Executable File

# $Id: QuantTree.py 3596 2004-07-23 01:45:57Z glandrum $
#
# 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)