source: orange/Orange/doc/reference/cb-descender.py @ 9671:a7b056375472

Revision 9671:a7b056375472, 2.8 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

Line 
1# Description: Shows how to derive a tree descender from orange.TreeDescender
2# Category:    classification, decision trees, callbacks to Python
3# Classes:     TreeDescender
4# Uses:        lenses
5# Referenced:  callbacks.htm
6
7import orange, orngTree
8
9import random
10random.seed(0)
11
12data = orange.ExampleTable("lenses")
13
14treeLearner = orange.TreeLearner()
15tree = treeLearner(data)
16orngTree.printTxt(tree)
17
18class Descender_RandomBranch(orange.TreeDescender):
19    def __call__(self, node, example):
20        while node.branchSelector:
21            branch = node.branchSelector(example)
22            if branch.isSpecial()  or int(branch)>=len(node.branches):
23                branch = random.randint(0, len(node.branches)-1)
24                print "Descender decides for ", branch
25            nextNode = node.branches[int(branch)]
26            if not nextNode:
27                break
28            node = nextNode
29        return node
30
31ex = orange.Example(data.domain, data[3])
32ex[tree.tree.branchSelector.classVar] = "?"
33print ex
34
35print "\n\nDecisions by random branch choice"
36tree.descender = Descender_RandomBranch()
37for i in range(3):
38    print tree(ex)
39
40
41
42class Descender_RandomVote(orange.TreeDescender):
43  def __call__(self, node, example):
44    while node.branchSelector:
45      branch = node.branchSelector(example)
46      if branch.isSpecial() or int(branch)>=len(node.branches):
47        votes = orange.DiscDistribution([random.randint(0, 100) for i in node.branches])
48        votes.normalize()
49        print "Weights:", votes
50        return node, votes
51      nextNode = node.branches[int(branch)]
52      if not nextNode:
53        break
54      node = nextNode
55    return node
56
57print "\n\nDecisions by random voting"
58tree.descender = Descender_RandomVote()
59print tree(ex, orange.GetProbabilities)
60
61
62class Descender_Report(orange.TreeDescender):
63    def __call__(self, node, example):
64        print "Descent: root ",
65        while node.branchSelector:
66            branch = node.branchSelector(example)
67            if branch.isSpecial() or int(branch)>=len(node.branches):
68                break
69            nextNode = node.branches[int(branch)]
70            if not nextNode:
71                break
72            print ">> %s = %s" % (node.branchSelector.classVar.name, node.branchDescriptions[int(branch)]),
73            node = nextNode
74        return node
75
76print "\n\nReporting descender"
77tree.descender = Descender_Report()
78print "Classifying example", data[0]
79print "----> %s" % tree(data[1])
80
81tree.descender = Descender_RandomBranch()
82ex = orange.Example(data.domain, list(data[2]))
83ex[tree.tree.branchSelector.classVar] = "?"
84for i in range(5):
85    print tree(ex, orange.Classifier.GetProbabilities)
86
87
88tree.descender = Descender_RandomVote()
89ex = orange.Example(data.domain, list(data[2]))
90ex[tree.tree.branchSelector.classVar] = "?"
91print tree(ex, orange.GetProbabilities)
92
93#orngTree.printModel(tree)
94
Note: See TracBrowser for help on using the repository browser.