Changeset 7474:6bb9b5b70491 in orange


Ignore:
Timestamp:
02/04/11 16:54:52 (3 years ago)
Author:
markotoplak
Branch:
default
Convert:
8e5dcae1e01c6d03ee8808249c3271fdb0f89df1
Message:

Examples for the Orange.classification.tree updated so that they use the
module.

Location:
orange
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/classification/tree.py

    r7439 r7474  
    715715============================================ 
    716716 
    717 obj:`TreeStopCriteria` determines when to stop the induction of subtrees, as described in detail in description of the learning process. XXXXXXXXXX 
     717obj:`TreeStopCriteria` determines when to stop the induction of subtrees, as  
     718described in detail in description of the learning process. XXXXXXXXXX 
    718719 
    719720.. class:: TreeStopCriteria_common 
     
    11451146    --> none (<15.000, 5.000, 4.000>) 
    11461147 
    1147 References 
    1148 ========== 
    1149  
    1150 Bratko, I. (2002). `Prolog Programming for Artificial Intelligence`, Addison  
    1151 Wesley, 2002. 
     1148 
     1149Undocumented 
     1150============ 
    11521151 
    11531152.. class:: TreeNodeList 
    1154  
    1155     Undocumented. 
    1156  
    1157 .. class:: C45TreeNode 
    11581153 
    11591154    Undocumented. 
     
    14201415    :members: 
    14211416 
     1417For a bit more complex example, here's how to write your own stop 
     1418function. The example itself is more funny than useful. It constructs 
     1419and prints two trees. For the first one we define the <code>defStop</code> 
     1420function, which is used by default, and combine it with a random function 
     1421so that the stop criteria will also be met in additional 20% of the cases 
     1422when <code>defStop</code> is false. The second tree is build such that it 
     1423considers only the random function as the stopping criteria. Note that in 
     1424the second case lambda function still has three parameters, since this is 
     1425a necessary number of parameters for the stop function XXXXX link ( 
     1426part of `tree3.py`_ (uses  `iris.tab`_): 
     1427 
     1428.. _tree3.py: code/tree3.py 
     1429 
     1430.. literalinclude:: code/tree3.py 
     1431   :lines: 8-23 
     1432 
     1433The output is not shown here since the resulting trees are rather 
     1434big. 
    14221435 
    14231436Tree size 
     
    19231936 
    19241937.. literalinclude:: code/orngTree2.py 
    1925    :lines: 7-30 
     1938   :lines: 7-31 
    19261939 
    19271940We first defined getMargin which gets the distribution and computes the 
     
    19391952 
    19401953.. literalinclude:: code/orngTree2.py 
    1941     :lines: 32 
     1954    :lines: 33 
    19421955 
    19431956And we get:: 
     
    19561969=========================== 
    19571970 
    1958 Prints the tree to a file in a format used by  
    1959 `GraphViz <http://www.research.att.com/sw/tools/graphviz>`_. 
    1960 Uses the same parameters as :func:`printTxt` defined above 
    1961 plus two parameters which define the shape used for internal 
    1962 nodes and laves of the tree: 
    1963  
    1964 :param leafShape: Shape of the outline around leves of the tree.  
    1965     If "plaintext", no outline is used (default: "plaintext"). 
    1966 :type leafShape: string 
    1967 :param internalNodeShape: Shape of the outline around internal nodes  
    1968     of the tree. If "plaintext", no outline is used (default: "box") 
    1969 :type leafShape: string 
    1970  
    1971 <p>Check <a 
    1972 href="http://www.graphviz.org/doc/info/shapes.html">Polygon-based 
    1973 Nodes</a> for various outlines supported by GraphViz.</p> 
    1974  
    1975 <P>Suppose you saved the tree in a file <code>tree5.dot</code>. You can then print it out as a gif if you execute the following command line 
    1976 <XMP class=code>dot -Tgif tree5.dot -otree5.gif 
    1977 </XMP> 
    1978 </P> 
    1979 GraphViz's dot has quite a few other output formats, check its documentation to learn which.</P> 
    1980  
    1981  
    1982  
     1971.. autofunction:: printDot 
     1972 
     1973.. autofunction:: dotTree 
     1974 
     1975Suppose you saved the tree in a file "tree5.dot". You can then 
     1976print it out as a gif if you execute the following command line 
     1977 
     1978:: 
     1979     
     1980    dot -Tgif tree5.dot -otree5.gif 
     1981 
     1982GraphViz's dot has quite a few other output formats, check  
     1983its documentation to learn which. 
     1984 
     1985References 
     1986========== 
     1987 
     1988Bratko, I. (2002). `Prolog Programming for Artificial Intelligence`, Addison  
     1989Wesley, 2002. 
     1990 
     1991E Koutsofios, SC North. Drawing Graphs with dot. AT&T Bell Laboratories, 
     1992Murray Hill NJ, U.S.A., October 1993. 
     1993 
     1994`Graphviz - open source graph drawing software <http://www.research.att.com/sw/tools/graphviz/>`_ 
     1995A home page of AT&T's dot and similar software packages. 
    19831996 
    19841997""" 
     
    19892002         C45Learner, \ 
    19902003         C45Classifier, \ 
    1991          C45TreeNode, \ 
    1992          C45TreeNodeList, \ 
    1993          TreeDescender, \ 
    1994               TreeDescender_UnknownMergeAsBranchSizes, \ 
    1995               TreeDescender_UnknownMergeAsSelector, \ 
    1996               TreeDescender_UnknownToBranch, \ 
    1997               TreeDescender_UnknownToCommonBranch, \ 
    1998               TreeDescender_UnknownToCommonSelector, \ 
    1999          TreeExampleSplitter, \ 
    2000               TreeExampleSplitter_IgnoreUnknowns, \ 
    2001               TreeExampleSplitter_UnknownsAsBranchSizes, \ 
    2002               TreeExampleSplitter_UnknownsAsSelector, \ 
    2003               TreeExampleSplitter_UnknownsToAll, \ 
    2004               TreeExampleSplitter_UnknownsToBranch, \ 
    2005               TreeExampleSplitter_UnknownsToCommon, \ 
    2006               TreeExampleSplitter_UnknownsToRandom, \ 
    2007          TreeNode, \ 
    2008          TreeNodeList, \ 
    2009          TreePruner, \ 
    2010               TreePruner_SameMajority, \ 
    2011               TreePruner_m, \ 
    2012          TreeSplitConstructor, \ 
    2013               TreeSplitConstructor_Combined, \ 
    2014               TreeSplitConstructor_Measure, \ 
    2015                    TreeSplitConstructor_Attribute, \ 
    2016                    TreeSplitConstructor_ExhaustiveBinary, \ 
    2017                    TreeSplitConstructor_OneAgainstOthers, \ 
    2018                    TreeSplitConstructor_Threshold, \ 
    2019          TreeStopCriteria, \ 
    2020               TreeStopCriteria_Python, \ 
    2021               TreeStopCriteria_common 
    2022  
     2004         C45TreeNode as C45Node, \ 
     2005         C45TreeNodeList as C45NodeList, \ 
     2006         TreeDescender as Descender, \ 
     2007              TreeDescender_UnknownMergeAsBranchSizes as Descender_UnknownMergeAsBranchSizes, \ 
     2008              TreeDescender_UnknownMergeAsSelector as Descender_UnknownMergeAsSelector, \ 
     2009              TreeDescender_UnknownToBranch as Descender_UnknownToBranch, \ 
     2010              TreeDescender_UnknownToCommonBranch as Descender_UnknownToCommonBranch, \ 
     2011              TreeDescender_UnknownToCommonSelector as Descender_UnknownToCommonSelector, \ 
     2012         TreeExampleSplitter as Splitter, \ 
     2013              TreeExampleSplitter_IgnoreUnknowns as Splitter_IgnoreUnknowns, \ 
     2014              TreeExampleSplitter_UnknownsAsBranchSizes as Splitter_UnknownsAsBranchSizes, \ 
     2015              TreeExampleSplitter_UnknownsAsSelector as Splitter_UnknownsAsSelector, \ 
     2016              TreeExampleSplitter_UnknownsToAll as Splitter_UnknownsToAll, \ 
     2017              TreeExampleSplitter_UnknownsToBranch as Splitter_UnknownsToBranch, \ 
     2018              TreeExampleSplitter_UnknownsToCommon as Splitter_UnknownsToCommon, \ 
     2019              TreeExampleSplitter_UnknownsToRandom as Splitter_UnknownsToRandom, \ 
     2020         TreeNode as Node, \ 
     2021         TreeNodeList as NodeList, \ 
     2022         TreePruner as Pruner, \ 
     2023              TreePruner_SameMajority as Pruner_SameMajority, \ 
     2024              TreePruner_m as Pruner_m, \ 
     2025         TreeSplitConstructor as SplitConstructor, \ 
     2026              TreeSplitConstructor_Combined as SplitConstructor_Combined, \ 
     2027              TreeSplitConstructor_Measure as SplitConstructor_Score, \ 
     2028                   TreeSplitConstructor_Attribute as SplitConstructor_Feature, \ 
     2029                   TreeSplitConstructor_ExhaustiveBinary as SplitConstructor_ExhaustiveBinary, \ 
     2030                   TreeSplitConstructor_OneAgainstOthers as SplitConstructor_OneAgainstOthers, \ 
     2031                   TreeSplitConstructor_Threshold as SplitConstructor_Threshold, \ 
     2032         TreeStopCriteria as StopCriteria, \ 
     2033              TreeStopCriteria_Python as StopCriteria_Python, \ 
     2034              TreeStopCriteria_common as StopCriteria_common 
     2035 
     2036import orange 
     2037import operator 
     2038import base64 
     2039import re 
    20232040 
    20242041def _c45_showBranch(node, classvar, lev, i): 
     
    21062123 
    21072124 
    2108  
    2109  
    2110 # 
    2111 # From  orngTree 
    2112 # 
    2113  
    2114 """ 
    2115  
    2116 <P>For a bit more complex example, here's how to write your own stop function. The example itself is more funny than useful. It constructs and prints two trees. For the first one we define the <code>defStop</code> function, which is used by default, and combine it with a random function so that the stop criteria will also be met in additional 20% of the cases when <code>defStop</code> is false. The second tree is build such that it considers only the random function as the stopping criteria. Note that in the second case lambda function still has three parameters, since this is a necessary number of parameters for the stop function (for more, see section on <a href="../reference/TreeLearner.htm">Orange Trees</a> in Orange Reference). 
    2117 </p> 
    2118  
    2119 <p class="header"><a href="tree3.py">tree3.py</a> (uses <a href= 
    2120 "iris.tab">iris.tab</a>)</p> 
    2121  
    2122 <XMP class=code>import orange, orngTree 
    2123 from whrandom import randint, random 
    2124  
    2125 data = orange.ExampleTable("iris.tab") 
    2126  
    2127 defStop = orange.TreeStopCriteria() 
    2128 f = lambda examples, weightID, contingency: defStop(examples, weightID, contingency) or randint(1, 5)==1 
    2129 l = orngTree.TreeLearner(data, stop=f) 
    2130 orngTree.printTxt(l, leafFields=['major', 'contingency']) 
    2131  
    2132 f = lambda x,y,z: randint(1, 5)==1 
    2133 l = orngTree.TreeLearner(data, stop=f) 
    2134 orngTree.printTxt(l, leafFields=['major', 'contingency']) 
    2135 </XMP> 
    2136  
    2137 <p>The output is not shown here since the resulting trees are rather 
    2138 big.</p> 
    2139  
    2140  
    2141  
    2142  
    2143  
    2144  
    2145  
    2146 <index name="classification trees/printing"> 
    2147  
    2148 References 
    2149 ========== 
    2150  
    2151 E Koutsofios, SC North. Drawing Graphs with dot. AT&T Bell Laboratories, 
    2152 Murray Hill NJ, U.S.A., October 1993. 
    2153  
    2154 <p><a href="http://www.research.att.com/sw/tools/graphviz/">Graphviz - 
    2155 open source graph drawing software</a>. A home page of AT&T's dot and 
    2156 similar software packages.</p> 
    2157  
    2158 """ 
    2159  
    2160 import orange 
    2161 import base64 
    2162 from warnings import warn 
    2163  
    21642125class TreeLearner(orange.Learner): 
    21652126    """ 
     
    24622423# the following is for the output 
    24632424 
    2464 import re 
    24652425 
    24662426fs = r"(?P<m100>\^?)(?P<fs>(\d*\.?\d*)?)" 
     
    26182578 
    26192579 
    2620 import operator 
    26212580__opdict = {"<": operator.lt, "<=": operator.le, ">": operator.gt, ">=": operator.ge, "=": operator.eq, "!=": operator.ne} 
    26222581 
     
    29852944 
    29862945 
    2987 def dotTree(tree, fileName, leafStr = "", nodeStr = "", leafShape="plaintext", nodeShape="plaintext", **argkw): 
     2946def printDot(tree, fileName, leafStr = "", nodeStr = "", leafShape="plaintext", nodeShape="plaintext", **argkw): 
     2947    """ Prints the tree to a file in a format used by  
     2948    `GraphViz <http://www.research.att.com/sw/tools/graphviz>`_. 
     2949    Uses the same parameters as :func:`printTxt` defined above 
     2950    plus two parameters which define the shape used for internal 
     2951    nodes and laves of the tree: 
     2952 
     2953    :param leafShape: Shape of the outline around leves of the tree.  
     2954        If "plaintext", no outline is used (default: "plaintext"). 
     2955    :type leafShape: string 
     2956    :param internalNodeShape: Shape of the outline around internal nodes  
     2957        of the tree. If "plaintext", no outline is used (default: "box") 
     2958    :type leafShape: string 
     2959 
     2960    Check `Polygon-based Nodes <http://www.graphviz.org/doc/info/shapes.html>`_  
     2961    for various outlines supported by GraphViz. 
     2962    """ 
    29882963    fle = type(fileName) == str and file(fileName, "wt") or fileName 
    29892964 
    2990     __TreeDumper(leafStr, nodeStr, argkw.get("userFormats", []) + __TreeDumper.defaultStringFormats, 
    2991                  argkw.get("minExamples", 0), argkw.get("maxDepth", 1e10), argkw.get("simpleFirst", True), 
    2992                 tree, 
    2993                  leafShape = leafShape, nodeShape = nodeShape, fle = fle).dotTree() 
     2965    __TreeDumper(leafStr, nodeStr, argkw.get("userFormats", []) +  
     2966        __TreeDumper.defaultStringFormats, argkw.get("minExamples", 0),  
     2967        argkw.get("maxDepth", 1e10), argkw.get("simpleFirst", True), tree, 
     2968        leafShape=leafShape, nodeShape=nodeShape, fle=fle).dotTree() 
    29942969                         
    2995 printDot = dotTree 
    2996          
    2997  
     2970dotTree = printDot 
     2971""" An alias for :func:`printDot`. Left for compatibility. """ 
  • orange/doc/Orange/rst/code/orngTree2.py

    r7439 r7474  
    1 import orange, orngTree, re 
    2 reload(orngTree) 
     1import Orange 
     2import re 
    33 
    4 data = orange.ExampleTable("iris") 
    5 tree = orngTree.TreeLearner(data, maxDepth=3) 
     4data = Orange.data.Table("iris") 
     5tree = Orange.classification.tree.TreeLearner(data, maxDepth=3) 
    66 
    77def getMargin(dist): 
     
    1717    by = mo.group("by") 
    1818    if margin and by: 
    19         whom = orngTree.byWhom(by, parent, tree) 
     19        whom = Orange.classification.tree.byWhom(by, parent, tree) 
    2020        if whom and whom.distribution: 
    2121            divMargin = getMargin(whom.distribution) 
     
    2323                margin /= divMargin 
    2424            else: 
    25                 orngTree.insertDot(strg, mo) 
     25                Orange.classification.tree.insertDot(strg, mo) 
    2626        else: 
    27             return orngTree.insertDot(strg, mo) 
    28     return orngTree.insertNum(strg, mo, margin) 
     27            return Orange.classification.tree.insertDot(strg, mo) 
     28    return Orange.classification.tree.insertNum(strg, mo, margin) 
    2929     
    30 myFormat = [(re.compile("%"+orngTree.fs+"B"+orngTree.by), replaceB)] 
     30myFormat = [(re.compile("%"+Orange.classification.tree.fs 
     31    +"B"+Orange.classification.tree.by), replaceB)] 
    3132             
    32 orngTree.printTree(tree, leafStr="%V %^B% (%^3.2BbP%)", userFormats=myFormat) 
     33Orange.classification.tree.printTree(tree, leafStr="%V %^B% (%^3.2BbP%)", userFormats=myFormat) 
  • orange/doc/Orange/rst/code/tree_c45.py

    r7326 r7474  
    33# Classes:     C45Learner, C45Classifier 
    44# Uses:        iris 
    5 # Referenced:  C45Learner.htm 
     5# Referenced:  TODO 
    66 
    7 import orange 
     7import Orange 
    88 
    9 data = orange.ExampleTable("iris") 
    10 tree = orange.C45Learner(data) 
     9data = Orange.data.Table("iris") 
     10tree = Orange.classification.tree.C45Learner(data) 
    1111 
    1212print "\n\nC4.5 with default arguments" 
     
    1515 
    1616print "\n\nC4.5 with m=100" 
    17 tree = orange.C45Learner(data, m=100) 
     17tree = Orange.classification.tree.C45Learner(data, m=100) 
    1818for i in data[:5]: 
    1919    print tree(i), i.getclass() 
    2020 
    2121print "\n\nC4.5 with minObjs=100" 
    22 tree = orange.C45Learner(data, minObjs=100) 
     22tree = Orange.classification.tree.C45Learner(data, minObjs=100) 
    2323for i in data[:5]: 
    2424    print tree(i), i.getclass() 
    2525 
    2626print "\n\nC4.5 with -m 1 and -s" 
    27 lrn = orange.C45Learner() 
     27lrn = Orange.classification.tree.C45Learner() 
    2828lrn.commandline("-m 1 -s") 
    2929tree = lrn(data) 
     
    3232        print i, tree(i) 
    3333 
    34  
    35 import orngC45 
    36 tree = orange.C45Learner(data) 
    37 orngC45.printTree(tree) 
     34tree = Orange.classification.tree.C45Learner(data) 
     35Orange.classification.tree.printTreeC45(tree) 
    3836print 
    3937 
    4038import orngStat, orngTest 
    41 res = orngTest.crossValidation([orange.C45Learner(), orange.C45Learner(convertToOrange=1)], data) 
     39res = orngTest.crossValidation([Orange.classification.tree.C45Learner(),  
     40    Orange.classification.tree.C45Learner(convertToOrange=1)], data) 
    4241print "Classification accuracy: %5.3f (converted to tree: %5.3f)" % tuple(orngStat.CA(res)) 
    4342print "Brier score: %5.3f (converted to tree: %5.3f)" % tuple(orngStat.BrierScore(res)) 
  • orange/doc/Orange/rst/code/treelearner.py

    r7439 r7474  
    55# Referenced:  TreeLearner.htm 
    66 
    7 import orange 
     7import Orange 
    88 
    9 data = orange.ExampleTable("lenses") 
    10 learner = orange.TreeLearner() 
     9data = Orange.data.Table("lenses") 
     10learner = Orange.classification.tree.TreeLearnerBase() 
    1111 
    1212def printTree0(node, level): 
     
    2828 
    2929def printTree(x): 
    30     if type(x) == orange.TreeClassifier: 
     30    if isinstance(x, Orange.classification.tree.TreeClassifier): 
    3131        printTree0(x.tree, 0) 
    32     elif type(x) == orange.TreeNode: 
     32    elif isinstance(x, Orange.classification.tree.Node): 
    3333        printTree0(x, 0) 
    3434    else: 
     
    3939print learner.split 
    4040 
    41 learner.stop = orange.TreeStopCriteria_common() 
     41learner.stop = Orange.classification.tree.StopCriteria_common() 
    4242print learner.stop.maxMajority, learner.stop.minExamples 
    4343 
     
    4545learner.stop.minExamples = 5.0 
    4646tree = learner(data) 
    47 printTree(tree) 
     47Orange.classification.tree.printTree(tree) 
    4848 
    4949print "\n\nTree with maxMajority = 0.5" 
    5050learner.stop.maxMajority = 0.5 
    5151tree = learner(data) 
    52 printTree(tree) 
     52Orange.classification.tree.printTree(tree) 
  • orange/doc/Orange/rst/code/treestructure.py

    r7283 r7474  
    55# Referenced:  TreeLearner.htm 
    66 
    7 import orange 
     7import Orange 
    88 
    9 data = orange.ExampleTable("lenses") 
    10 treeClassifier = orange.TreeLearner(data) 
     9data = Orange.data.Table("lenses") 
     10treeClassifier = Orange.classification.tree.TreeLearnerBase(data) 
    1111 
    1212def treeSize(node): 
     
    4242 
    4343def printTree(x): 
    44     if type(x) == orange.TreeClassifier: 
     44    if isinstance(x, Orange.classification.tree.TreeClassifier): 
    4545        printTree0(x.tree, 0) 
    46     elif type(x) == orange.TreeNode: 
     46    elif isinstance(x, Orange.classification.tree.Node): 
    4747        printTree0(x, 0) 
    4848    else: 
Note: See TracChangeset for help on using the changeset viewer.