# Author: J Zabkar # Version: 1.0 # Description: Demostration of use of orngTree module: prin out # a tree in text and dot format # Category: modelling # Uses: iris # Referenced: orngTree.htm import orange, orngTree data = orange.ExampleTable("iris.tab") tree = orange.TreeLearner(data) orngTree.printTree(tree) orngTree.printDot(tree, fileName="tree1.dot")
# Author: J Zabkar # Version: 1.0 # Description: Pre-prunning of classification tree using orngTree module # Category: modelling # Uses: iris.tab # Referenced: orngTree.htm import orange, orngTree data = orange.ExampleTable("iris.tab") print "BIG TREE:" tree1 = orngTree.TreeLearner(data) orngTree.printTree(tree1, leafStr="%m", nodeStr=".") print "\nPRE-PRUNED TREE:" tree2 = orngTree.TreeLearner(data, maxMajority=0.7) orngTree.printTree(tree2, leafStr="%m", nodeStr=".")
import orange, orngTree reload(orngTree) data = orange.ExampleTable("iris") tree = orngTree.TreeLearner(data, maxDepth=3) formats = ["", "%V (%M out of %N)", "%V (%^MbA%, %^MbP%)", '%C="Iris-versicolor" (%^c="Iris-versicolor"% of node, %^CbA="Iris-versicolor"% of versicolors)', "%D", "%.2d"] for format in formats: print '\n\n*** FORMAT: "%s"\n' % format orngTree.printTree(tree, leafStr=format) formats2 = [("%V", "."), ('%^.1CbA="Iris-virginica"% (%^.1CbP="Iris-virginica"%)', '.'), ("%V %D %.2DbP %.2dbP", "%D %.2DbP %.2dbP")] for fl, fn in formats2: orngTree.printTree(tree, leafStr=fl, nodeStr=fn) data = orange.ExampleTable("housing") tree = orngTree.TreeLearner(data, maxDepth=3) formats = ["", "%V"] for format in formats: print '\n\n*** FORMAT: "%s"\n' % format orngTree.printTree(tree, leafStr=format) formats2 = [("[SE: %E]\t %V %I(90)", "[SE: %E]"), ("%C<22 (%cbP<22)", "."), ("%C![20,22] (%^cbP![20,22]%)", ".")] for fl, fn in formats2: orngTree.printTree(tree, leafStr=fl, nodeStr=fn)
tree = orngTree.TreeLearner(data, maxDepth=3) def getMargin(dist): if dist.abs < 1e-30: return 0 l = list(dist) l.sort() return (l[-1] - l[-2]) / dist.abs def replaceB(strg, mo, node, parent, tree): margin = getMargin(node.distribution) by = mo.group("by") if margin and by: whom = orngTree.byWhom(by, parent, tree) if whom and whom.distribution: divMargin = getMargin(whom.distribution) if divMargin > 1e-30: margin /= divMargin else: orngTree.insertDot(strg, mo) else: return orngTree.insertDot(strg, mo) return orngTree.insertNum(strg, mo, margin) myFormat = [(re.compile("%"+orngTree.fs+"B"+orngTree.by), replaceB)] orngTree.printTree(tree, leafStr="%V %^B% (%^3.2BbP%)", userFormats = myFormat)
def getMargin(dist): if dist.abs < 1e-30: return 0 l = list(dist) l.sort() return (l[-1] - l[-2]) / dist.abs def replaceB(strg, mo, node, parent, tree): margin = getMargin(node.distribution) by = mo.group("by") if margin and by: whom = orngTree.byWhom(by, parent, tree) if whom and whom.distribution: divMargin = getMargin(whom.distribution) if divMargin > 1e-30: margin /= divMargin else: orngTree.insertDot(strg, mo) else: return orngTree.insertDot(strg, mo) return orngTree.insertNum(strg, mo, margin) myFormat = [(re.compile("%" + orngTree.fs + "B" + orngTree.by), replaceB)] orngTree.printTree(tree, leafStr="%V %^B% (%^3.2BbP%)", userFormats=myFormat)