# source:orange/Orange/testing/regression/tests_20/reference_treelearner.py@9952:986e9cd806f4

Revision 9952:986e9cd806f4, 1.5 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

Tests moved and renamed from orange 20.

Line
1# Description: Shows how to construct trees
2# Category:    learning, decision trees, classification
3# Classes:     TreeLearner, TreeClassifier, TreeStopCriteria, TreeStopCriteria_common
4# Uses:        lenses
5# Referenced:  TreeLearner.htm
6
7import orange
8
9data = orange.ExampleTable("lenses")
10learner = orange.TreeLearner()
11
12def printTree0(node, level):
13    if not node:
14        print " "*level + "<null node>"
15        return
16
17    if node.branchSelector:
18        nodeDesc = node.branchSelector.classVar.name
19        nodeCont = node.distribution
20        print "\n" + "   "*level + "%s (%s)" % (nodeDesc, nodeCont),
21        for i in range(len(node.branches)):
22            print "\n" + "   "*level + ": %s" % node.branchDescriptions[i],
23            printTree0(node.branches[i], level+1)
24    else:
25        nodeCont = node.distribution
26        majorClass = node.nodeClassifier.defaultValue
27        print "--> %s (%s) " % (majorClass, nodeCont),
28
29def printTree(x):
30    if type(x) == orange.TreeClassifier:
31        printTree0(x.tree, 0)
32    elif type(x) == orange.TreeNode:
33        printTree0(x, 0)
34    else:
35        raise TypeError, "invalid parameter"
36
37print learner.split
38learner(data)
39print learner.split
40
41learner.stop = orange.TreeStopCriteria_common()
42print learner.stop.maxMajority, learner.stop.minExamples
43
44print "\n\nTree with minExamples = 5.0"
45learner.stop.minExamples = 5.0
46tree = learner(data)
47printTree(tree)
48
49print "\n\nTree with maxMajority = 0.5"
50learner.stop.maxMajority = 0.5
51tree = learner(data)
52printTree(tree)
Note: See TracBrowser for help on using the repository browser.