source: orange/docs/reference/rst/code/treelearner.py @ 10672:d0a4a9251413

Revision 10672:d0a4a9251413, 1.5 KB checked in by mstajdohar, 2 years ago (diff)

Fixed obsolete attribute.

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
9lenses = Orange.data.Table("lenses")
10learner = Orange.classification.tree.TreeLearner()
11
12def printTree0(node, level):
13    if not node:
14        print " "*level + "<null node>"
15        return
16
17    if node.branch_selector:
18        node_desc = node.branch_selector.class_var.name
19        node_cont = node.distribution
20        print "\n" + "   "*level + "%s (%s)" % (node_desc, node_cont),
21        for i in range(len(node.branches)):
22            print "\n" + "   "*level + ": %s" % node.branch_descriptions[i],
23            printTree0(node.branches[i], level+1)
24    else:
25        node_cont = node.distribution
26        major_class = node.node_classifier.default_value
27        print "--> %s (%s) " % (major_class, node_cont),
28
29def printTree(x):
30    if isinstance(x, Orange.classification.tree.TreeClassifier):
31        printTree0(x.tree, 0)
32    elif isinstance(x, Orange.classification.tree.Node):
33        printTree0(x, 0)
34    else:
35        raise TypeError, "invalid parameter"
36
37learner.stop = Orange.classification.tree.StopCriteria_common()
38print learner.stop.max_majority, learner.stop.min_instances
39
40print "\n\nTree with minExamples = 5.0"
41learner.stop.min_instances = 5.0
42tree = learner(lenses)
43print tree
44
45print "\n\nTree with maxMajority = 0.5"
46learner.stop.max_majority = 0.5
47tree = learner(lenses)
48print tree
Note: See TracBrowser for help on using the repository browser.