Orange Forum • View topic - Get information aobut a classifying node

Get information aobut a classifying node

A place to ask questions about methods in Orange and how they are used and other general support.

Get information aobut a classifying node

Postby sebdes » Tue Feb 28, 2012 23:33

Hello,

I am looking for an easy way to get information about the leaf which classifies an instance when using a decision tree and the class Orange.classification.tree.TreeClassifier.

More precisely, I need the class samples of the learning set that correspond to this leaf (not just the probability distribution).

I have been looking around for a way to do it, but unfortunately have not found it (all I found was orange.GetProbability).

Thanks in advance for any help.

Seb

Re: Get information aobut a classifying node

Postby Ales » Thu Mar 01, 2012 13:39

You can descend on the 'branches' of the 'classifier.tree' to reach the leaf nodes. If the classifier was build by passing 'store_instances=True' to the TreeLearner then the leaf nodes will contain all of the training instances in the 'instances' attribute.

Example code:
Code: Select all
import Orange
data  = Orange.data.Table("iris")
learner = Orange.classification.tree.TreeLearner(store_instances=True)
classifier = learner(data)

def iter_leafs(node):
    if node.branches:
        for b in node.branches:
             for b1 in iter_leafs(b):
                yield b1
    else:
        yield node

for leaf in iter_leafs(classifier.tree):
   print leaf
   print leaf.instances[:]

Re: Get information aobut a classifying node

Postby sebdes » Sun Mar 04, 2012 9:52

Thanks for the answer. Actually just using the descender was engouh for me in this case (I just had to figure out how the descender worked exactly), but I guess I might be in trouble if the descender does not return a leaf node.

Here is a glimpse of the code I used

Code: Select all
import Orange
data  = Orange.data.Table("iris")
learner = Orange.classification.tree.TreeLearner(store_instances=True)
classifier = learner(data)
# for sake of example, take instance 56
example=data[56]

def get_distribution(example):
    answer=classifier.descender(classifier.tree,example)
    print answer[0].distribution


Return to Questions & Support



cron