Changeset 7710:363cb7f443ec in orange


Ignore:
Timestamp:
02/23/11 13:44:39 (3 years ago)
Author:
markotoplak
Branch:
default
Convert:
bd7c5f173c51ffd792a199c1e57e3aaaa44b94da
Message:

Moved print, dump, dot, and count_* functions to the TreeClassifier class.

File:
1 edited

Legend:

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

    r7708 r7710  
    121121============== 
    122122 
    123 .. class:: TreeClassifier 
     123.. class:: _TreeClassifier 
    124124 
    125125    Classifies examples according to a tree stored in :obj:`tree`. 
     
    14331433    :members: 
    14341434 
     1435.. autoclass:: TreeClassifier 
     1436    :members: 
     1437 
    14351438For a bit more complex example, here's how to write your own stop 
    14361439function. The example itself is more funny than useful. It constructs 
     
    14521455big. 
    14531456 
    1454 Tree size 
    1455 ========= 
    1456  
    1457 .. autofunction:: countNodes 
    1458  
    1459 .. autofunction:: countLeaves 
    1460  
    14611457Printing the Tree 
    14621458================= 
     
    14691465of examples of this class in a parent node. And even more, you can 
    14701466define your own callback functions to be used for printing. 
    1471  
    1472  
    1473 .. autofunction:: dumpTree 
    1474  
    1475 .. autofunction:: printTree 
    1476  
    1477 .. autofunction:: printTxt 
    14781467 
    14791468Before we go on: you can read all about the function and use it to its 
     
    24082397        return learner 
    24092398 
    2410 #counting 
    2411  
    2412 def __countNodes(node): 
    2413     count = 0 
    2414     if node: 
    2415         count += 1 
    2416         if node.branches: 
    2417             for node in node.branches: 
    2418                 count += __countNodes(node) 
    2419     return count 
    2420  
    2421 def countNodes(tree): 
    2422     """ 
    2423     Return the number of nodes of tree. 
    2424  
    2425     :param tree: The tree for which to count the nodes. 
    2426     :type tree: :class:`TreeClassifier` 
    2427     """ 
    2428     return __countNodes(tree.tree if isinstance(tree, _TreeClassifier) or \ 
    2429         isinstance(tree, TreeClassifier) else tree) 
    2430  
    2431  
    2432 def __countLeaves(node): 
    2433     count = 0 
    2434     if node: 
    2435         if node.branches: # internal node 
    2436             for node in node.branches: 
    2437                 count += __countLeaves(node) 
    2438         else: 
    2439             count += 1 
    2440     return count 
    2441  
    2442 def countLeaves(tree): 
    2443     """ 
    2444     Return the number of leaves in the tree. 
    2445  
    2446     :param tree: The tree for which to count the leaves. 
    2447     :type tree: :class:`TreeClassifier` 
    2448     """ 
    2449     return __countLeaves(tree.tree if isinstance(tree, _TreeClassifier) or \ 
    2450         isinstance(tree, TreeClassifier) else tree) 
    2451  
    24522399# the following is for the output 
    24532400 
     
    27922739# that they don't need to transfer too many arguments. It will be  
    27932740# constructed, used and discarded, it is not meant to store any information. 
    2794 class __TreeDumper: 
     2741class _TreeDumper: 
    27952742    defaultStringFormats = [(re_V, replaceV), (re_N, replaceN), 
    27962743         (re_M, replaceM), (re_m, replacem),  
     
    29442891    return '"%s"' % (base64.b64encode(x)) 
    29452892 
    2946 def dumpTree(tree, leafStr = "", nodeStr = "", **argkw): 
    2947     """ 
    2948     Return a string representation of a tree. 
    2949  
    2950     :arg tree: The tree to dump to string. 
    2951     :type tree: class:`TreeClassifier` 
    2952     :arg leafStr: The format string for printing the tree leaves. If  
    2953       left empty, "%V (%^.2m%)" will be used for classification trees 
    2954       and "%V" for regression trees. 
    2955     :type leafStr: string 
    2956     :arg nodeStr: The format string for printing out the internal nodes. 
    2957       If left empty (as it is by default), no data is printed out for 
    2958       internal nodes. If set to :samp:`"."`, the same string is 
    2959       used as for leaves. 
    2960     :type nodeStr: string 
    2961     :arg maxDepth: If set, it limits the depth to which the tree is 
    2962       printed out. 
    2963     :type maxDepth: integer 
    2964     :arg minExamples: If set, the subtrees with less than the given  
    2965       number of examples are not printed. 
    2966     :type minExamples: integer 
    2967     :arg simpleFirst: If True (default), the branches with a single  
    2968       node are printed before the branches with larger subtrees.  
    2969       If False, the branches are printed in order of 
    2970       appearance. 
    2971     :type simpleFirst: boolean 
    2972     :arg userFormats: A list of regular expressions and callback  
    2973       function through which the user can print out other specific  
    2974       information in the nodes. 
    2975     """ 
    2976     return __TreeDumper(leafStr, nodeStr, argkw.get("userFormats", []) +  
    2977         __TreeDumper.defaultStringFormats, argkw.get("minExamples", 0),  
    2978         argkw.get("maxDepth", 1e10), argkw.get("simpleFirst", True), 
    2979         tree).dumpTree() 
    2980  
    2981  
    2982 def printTree(*a, **aa): 
    2983     """ 
    2984     Print out the tree (call :func:`dumpTree` with the same 
    2985     arguments and print out the result). 
    2986     """ 
    2987     print dumpTree(*a, **aa) 
    2988  
    2989 printTxt = printTree 
    2990 """ An alias for :func:`printTree`. Left for compatibility. """ 
    2991  
    2992 def printDot(tree, fileName, leafStr = "", nodeStr = "", leafShape="plaintext", nodeShape="plaintext", **argkw): 
    2993     """ Prints the tree to a file in a format used by  
    2994     `GraphViz <http://www.research.att.com/sw/tools/graphviz>`_. 
    2995     Uses the same parameters as :func:`printTxt` defined above 
    2996     plus two parameters which define the shape used for internal 
    2997     nodes and laves of the tree: 
    2998  
    2999     :param leafShape: Shape of the outline around leves of the tree.  
    3000         If "plaintext", no outline is used (default: "plaintext"). 
    3001     :type leafShape: string 
    3002     :param internalNodeShape: Shape of the outline around internal nodes  
    3003         of the tree. If "plaintext", no outline is used (default: "box") 
    3004     :type leafShape: string 
    3005  
    3006     Check `Polygon-based Nodes <http://www.graphviz.org/doc/info/shapes.html>`_  
    3007     for various outlines supported by GraphViz. 
    3008     """ 
    3009     fle = type(fileName) == str and file(fileName, "wt") or fileName 
    3010  
    3011     __TreeDumper(leafStr, nodeStr, argkw.get("userFormats", []) +  
    3012         __TreeDumper.defaultStringFormats, argkw.get("minExamples", 0),  
    3013         argkw.get("maxDepth", 1e10), argkw.get("simpleFirst", True), tree, 
    3014         leafShape=leafShape, nodeShape=nodeShape, fle=fle).dotTree() 
    3015   
    30162893class TreeClassifier(Orange.classification.Classifier): 
    30172894    """ 
    3018     Wraps :class:`Orange.core.TreeClassifier`. 
     2895    Wraps :class:`_TreeClassifier`. 
    30192896    """ 
    30202897     
     
    30492926        self.__dict__[name] = value 
    30502927     
    3051     
     2928    def dump(self, leafStr = "", nodeStr = "", **argkw):   
     2929        """ 
     2930        Return a string representation of a tree. 
     2931 
     2932        :arg tree: The tree to dump to string. 
     2933        :type tree: class:`TreeClassifier` 
     2934        :arg leafStr: The format string for printing the tree leaves. If  
     2935          left empty, "%V (%^.2m%)" will be used for classification trees 
     2936          and "%V" for regression trees. 
     2937        :type leafStr: string 
     2938        :arg nodeStr: The format string for printing out the internal nodes. 
     2939          If left empty (as it is by default), no data is printed out for 
     2940          internal nodes. If set to :samp:`"."`, the same string is 
     2941          used as for leaves. 
     2942        :type nodeStr: string 
     2943        :arg maxDepth: If set, it limits the depth to which the tree is 
     2944          printed out. 
     2945        :type maxDepth: integer 
     2946        :arg minExamples: If set, the subtrees with less than the given  
     2947          number of examples are not printed. 
     2948        :type minExamples: integer 
     2949        :arg simpleFirst: If True (default), the branches with a single  
     2950          node are printed before the branches with larger subtrees.  
     2951          If False, the branches are printed in order of 
     2952          appearance. 
     2953        :type simpleFirst: boolean 
     2954        :arg userFormats: A list of regular expressions and callback  
     2955          function through which the user can print out other specific  
     2956          information in the nodes. 
     2957        """ 
     2958        return _TreeDumper(leafStr, nodeStr, argkw.get("userFormats", []) +  
     2959            _TreeDumper.defaultStringFormats, argkw.get("minExamples", 0),  
     2960            argkw.get("maxDepth", 1e10), argkw.get("simpleFirst", True), 
     2961            self).dumpTree() 
     2962 
     2963    def dot(self, fileName, leafStr = "", nodeStr = "", leafShape="plaintext", nodeShape="plaintext", **argkw): 
     2964        """ Prints the tree to a file in a format used by  
     2965        `GraphViz <http://www.research.att.com/sw/tools/graphviz>`_. 
     2966        Uses the same parameters as :func:`printTxt` defined above 
     2967        plus two parameters which define the shape used for internal 
     2968        nodes and laves of the tree: 
     2969 
     2970        :param leafShape: Shape of the outline around leves of the tree.  
     2971            If "plaintext", no outline is used (default: "plaintext"). 
     2972        :type leafShape: string 
     2973        :param internalNodeShape: Shape of the outline around internal nodes  
     2974            of the tree. If "plaintext", no outline is used (default: "box") 
     2975        :type leafShape: string 
     2976 
     2977        Check `Polygon-based Nodes <http://www.graphviz.org/doc/info/shapes.html>`_  
     2978        for various outlines supported by GraphViz. 
     2979        """ 
     2980        fle = type(fileName) == str and file(fileName, "wt") or fileName 
     2981 
     2982        _TreeDumper(leafStr, nodeStr, argkw.get("userFormats", []) +  
     2983            _TreeDumper.defaultStringFormats, argkw.get("minExamples", 0),  
     2984            argkw.get("maxDepth", 1e10), argkw.get("simpleFirst", True), self, 
     2985            leafShape=leafShape, nodeShape=nodeShape, fle=fle).dotTree() 
     2986 
     2987    def count_nodes(self): 
     2988        """ 
     2989        Return the number of nodes of tree. 
     2990        """ 
     2991        return _countNodes(self.tree if isinstance(self, _TreeClassifier) or \ 
     2992            isinstance(self, TreeClassifier) else self) 
     2993 
     2994    def count_leaves(self): 
     2995        """ 
     2996        Return the number of leaves in the tree. 
     2997        """ 
     2998        return _countLeaves(self.tree if isinstance(self, _TreeClassifier) or \ 
     2999            isinstance(self, TreeClassifier) else self) 
     3000 
     3001dumpTree = TreeClassifier.dump 
     3002""" An alias for :obj:`TreeClassifier.dump`. """ 
     3003 
     3004def printTree(*a, **aa): 
     3005    """ 
     3006    Print out the tree (call :func:`dumpTree` with the same 
     3007    arguments and print out the result). 
     3008    """ 
     3009    print dumpTree(*a, **aa) 
     3010 
     3011printTxt = printTree 
     3012""" An alias for :func:`printTree`. Left for compatibility. """ 
     3013 
     3014printDot = TreeClassifier.dot 
     3015""" An alias for :obj:`TreeClassifier.dot` """ 
    30523016 
    30533017dotTree = printDot 
    30543018""" An alias for :func:`printDot`. Left for compatibility. """ 
     3019 
     3020countNodes = TreeClassifier.count_nodes 
     3021countLeaves = TreeClassifier.count_leaves 
     3022 
     3023def _countNodes(node): 
     3024    count = 0 
     3025    if node: 
     3026        count += 1 
     3027        if node.branches: 
     3028            for node in node.branches: 
     3029                count += _countNodes(node) 
     3030    return count 
     3031 
     3032def _countLeaves(node): 
     3033    count = 0 
     3034    if node: 
     3035        if node.branches: # internal node 
     3036            for node in node.branches: 
     3037                count += _countLeaves(node) 
     3038        else: 
     3039            count += 1 
     3040    return count 
     3041 
Note: See TracChangeset for help on using the changeset viewer.