Changeset 8197:46e21cf02551 in orange


Ignore:
Timestamp:
08/17/11 15:32:45 (3 years ago)
Author:
markotoplak
Branch:
default
Convert:
40a39f12b4713e4973bf32b8c86018638f0d5d52
Message:

Orange.classification.tree: merged TreeClassifier with _TreeClassifier,
keeping only the former.

File:
1 edited

Legend:

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

    r8192 r8197  
    3232.. autoclass:: TreeClassifier 
    3333    :members: 
    34  
    35     .. attribute:: tree 
    36  
    37         The root of the tree, represented as a :class:`Node`. 
    3834 
    3935 
     
    119115Finally, if the node is a leaf, we print out the distribution of learning 
    120116examples in the node and the class to which the examples in the node 
    121 would be classified. We again assume that the :obj:`node_classifier` is 
     117would be classified. We again assume that the :obj:`~Node.node_classifier` is 
    122118the default one - a :obj:`DefaultClassifier`. A better print function 
    123119should be aware of possible alternatives. 
     
    348344 
    349345 
    350 .. class:: _TreeClassifier 
    351  
    352     Classifies examples according to a tree stored in :obj:`tree`. 
    353     This class in not to be used directly. The :class:`TreeLearner` 
    354     constructs a :class:`TreeClassifier`, which uses this class. 
    355  
    356     .. attribute:: tree 
    357  
    358         The root of the tree, represented as a :class:`Node`. 
    359      
    360     Classification would be straightforward if there were no unknown 
    361     values or, in general, examples that cannot be placed into a single 
    362     branch. The response in such cases is determined by a component 
    363     :obj:`descender`. 
    364  
    365     :obj:`Descender` is an abstract object which is given an example 
    366     and whose basic job is to descend as far down the tree as possible, 
    367     according to the values of example's attributes. The :obj:`Descender`: 
    368     calls the node's :obj:`branch_selector` to get the branch index. If 
    369     it's a simple index, the corresponding branch is followed. If not, 
    370     it's up to descender to decide what to do, and that's where descenders 
    371     differ. A :obj:`descender` can choose a single branch (for instance, 
    372     the one that is the most recommended by the :obj:`branch_selector`) 
    373     or it can let the branches vote. 
    374  
    375     In general there are three possible outcomes of a descent. 
    376  
    377     #. Descender reaches a leaf. This happens when nothing went wrong 
    378        (there are no unknown or out-of-range values in the example) 
    379        or when things went wrong, but the descender smoothed them by 
    380        selecting a single branch and continued the descend. In this case, 
    381        the descender returns the reached :obj:`Node`. 
    382     #. :obj:`branch_selector` returned a distribution and the 
    383        :obj:`Descender` decided to stop the descend at this (internal) 
    384        node.  Again, descender returns the current :obj:`Node` and 
    385        nothing else. 
    386     #. :obj:`branch_selector` returned a distribution and the 
    387        :obj:`Node` wants to split the example (i.e., to decide the class 
    388        by voting). 
    389  
    390     It returns a :obj:`Node` and the vote-weights for the branches. 
    391     The weights can correspond to the distribution returned by 
    392     :obj:`branch_selector`, to the number of learning examples that were 
    393     assigned to each branch, or to something else. 
    394  
    395     :obj:`TreeClassifier` uses the descender to descend from the root. 
    396     If it returns only a :obj:`Node` and no distribution, the descend 
    397     should stop; it does not matter whether it's a leaf (the first 
    398     case above) or an internal node (the second case). The node's 
    399     :obj:`node_classifier` is used to decide the class. If the descender 
    400     returns a :obj:`Node` and a distribution, the :obj:`TreeClassifier` 
    401     recursively calls itself for each of the subtrees and the predictions 
    402     are weighted as requested by the descender. 
    403  
    404     When voting, subtrees do not predict the class but probabilities of 
    405     classes. The predictions are multiplied by weights, summed and the 
    406     most probable class is returned. 
    407  
    408     .. method:: vote() 
    409  
    410         It gets a :obj:`Node`, an :obj:`Orange.data.Instance` and a 
    411         distribution of vote weights. For each node, it calls the 
    412         :obj:`class_distribution` and then multiplies and sums the 
    413         distribution. :obj:`vote` returns a normalized distribution 
    414         of predictions. 
    415  
    416     .. method:: class_distribution() 
    417  
    418         Gets an additional parameter, a :obj:`Node` (default tree root). 
    419         :obj:`class_distribution` uses :obj:`descender`. If descender 
    420         reaches a leaf, it calls :obj:`node_classifier`, otherwise it 
    421         calls :obj:`vote`. 
    422  
    423         Thus, the :obj:`vote` and :obj:`class_distribution` are written 
    424         in a form of double recursion. The recursive calls do not happen 
    425         at each node of the tree but only at nodes where a vote is needed 
    426         (that is, at nodes where the descender halts). 
    427  
    428     .. method:: __call__ 
    429  
    430         Calls the descender. If it reaches a leaf, the class is 
    431         predicted by the leaf's :obj:`node_classifier`. Otherwise, it calls 
    432         :obj:`vote`. From now on, :obj:`vote` and :obj:`class_distribution` 
    433         interweave down the tree and return a distribution of 
    434         predictions. This method simply chooses the most probable class. 
    435  
    436346Split constructors 
    437347===================== 
     
    823733be unambiguously put in a branch. 
    824734 
     735 
    825736.. class:: Descender 
    826737 
    827738    An abstract base object for tree descenders. 
     739 
     740    It descends a given instance as far deep as possible, 
     741    according to the values of instance's attributes. The :obj:`Descender`: 
     742    calls the node's :obj:`Node.branch_selector` to get the branch index. If 
     743    it's a simple index, the corresponding branch is followed. If not, 
     744    it's up to descender to decide what to do, and that's where descenders 
     745    differ. A descender can choose a single branch (for instance, 
     746    the one that is the most recommended by the :obj:`Node.branch_selector`) 
     747    or it can let the branches vote. 
     748 
     749    Three are possible outcomes of a descent: 
     750 
     751    #. Descender reaches a leaf. This happens when 
     752       there were no unknown or out-of-range values, or when the descender 
     753       selected a single branch and continued the descend despite them. In 
     754       this case, the descender returns the reached :obj:`Node`. 
     755    #. Node's :obj:`~Node.branch_selector` returned a distribution and the 
     756       :obj:`Descender` decided to stop the descend at this (internal) 
     757       node.  The descender returns the current :obj:`Node`. 
     758    #. Node's :obj:`~Node.branch_selector` returned a distribution and the 
     759       :obj:`Node` wants to split the instance (i.e., to decide the class 
     760       by voting). It returns a :obj:`Node` and the vote-weights for the 
     761       branches.  The weights can correspond to the distribution returned 
     762       by node's :obj:`~Node.branch_selector`, to the number of learning 
     763       instances that were assigned to each branch, or to something else. 
    828764 
    829765    .. method:: __call__(node, example) 
     
    904840classifiers, branch selectors, ...  as the original tree. Thus, you may 
    905841modify a pruned tree structure (manually cut it, add new nodes, replace 
    906 components) but modifying, for instance, some node's :obj:`node_classifier` 
    907 (a :obj:`node_classifier` itself, not a reference to it!) would modify 
    908 the node's :obj:`node_classifier` in the corresponding node of the 
     842components) but modifying, for instance, some node's :obj:`~Node.node_classifier` 
     843(a :obj:`~Node.node_classifier` itself, not a reference to it!) would modify 
     844the node's :obj:`~Node.node_classifier` in the corresponding node of the 
    909845original tree. 
    910846 
    911 Pruners cannot construct a :obj:`node_classifier` nor merge 
    912 :obj:`node_classifier` of the pruned subtrees into classifiers for new 
     847Pruners cannot construct a :obj:`~Node.node_classifier` nor merge 
     848:obj:`~Node.node_classifier` of the pruned subtrees into classifiers for new 
    913849leaves. Thus, if you want to build a prunable tree, internal nodes 
    914 must have their :obj:`node_classifier` defined. Fortunately, this is 
     850must have their :obj:`~Node.node_classifier` defined. Fortunately, this is 
    915851the default. 
    916852 
     
    19781914    #. A :obj:`stop` is called 
    19791915       to see whether it is worth to continue. If not, a 
    1980        :obj:`node_classifier` is built and the :obj:`Node` is 
    1981        returned. Otherwise, a :obj:`node_classifier` is only built if 
    1982        :obj:`store_node_classifier` is `True`.  The :obj:`node_classifier` 
     1916       :obj:`~Node.node_classifier` is built and the :obj:`Node` is 
     1917       returned. Otherwise, a :obj:`~Node.node_classifier` is only built if 
     1918       :obj:`store_node_classifier` is `True`.  The :obj:`~Node.node_classifier` 
    19831919       is build by calling :obj:`node_learner`'s :obj:`smart_learn` 
    19841920       function with the given instances, weight ID and the just computed 
     
    19861922       :obj:`~Orange.classification.majority.MajorityLearner`, can), it 
    19871923       won't touch the instances. Therefore a :obj:`contingency_computer` 
    1988        will, in many cases, affect the :obj:`node_classifier`. The 
     1924       will, in many cases, affect the :obj:`~Node.node_classifier`. The 
    19891925       :obj:`node_learner` can return no classifier; if so and 
    19901926       if the classifier would be needed for classification, the 
     
    21572093 
    21582094        Determines whether to store class distributions, contingencies and 
    2159         examples in :class:`Node`, and whether the :obj:`node_classifier` 
     2095        examples in :class:`Node`, and whether the :obj:`Node.node_classifier` 
    21602096        should be build for internal nodes.  No memory will be saved  
    21612097        by not storing distributions but storing contingencies, since 
     
    28572793class TreeClassifier(Orange.classification.Classifier): 
    28582794    """ 
    2859     Wraps :class:`_TreeClassifier`. 
     2795 
     2796    Classifies instances according to the tree stored in :obj:`tree`. 
     2797 
     2798    **The classification process** 
     2799 
     2800    :obj:`TreeClassifier` uses the :obj:`descender` to descend from 
     2801    the root.  If it returns only a :obj:`Node` and no distribution, 
     2802    the descend should stop; it does not matter whether it's a leaf 
     2803    (the first case above) or an internal node (the second case). The 
     2804    node's :obj:`~Node.node_classifier` is used to decide the class. 
     2805 
     2806    If the descender returns a :obj:`Node` and a distribution, 
     2807    the :obj:`TreeClassifier` recursively calls itself for each of 
     2808    the subtrees and the predictions are weighted as requested by 
     2809    the descender. From now on, ``vote`` and ``class_distribution`` 
     2810    (private methods) interweave down the tree. 
     2811 
     2812    ``vote`` returns a normalized distribution  of predictions: for each 
     2813    node, it calls the  :obj:`class_distribution` and then multiplies 
     2814    and sums the distribution.  ``class_distribution`` gets an additional  
     2815    parameter, a default 
     2816    tree root.  If :obj:`descender` reaches a leaf, it calls 
     2817    :obj:`~Node.node_classifier`, otherwise it calls ``vote``. Thus, 
     2818    ``vote`` and ``class_distribution`` form a double recursion. The 
     2819    recursive calls only happen at nodes where a vote is needed (that is, 
     2820    at nodes where the descender halts). 
     2821 
     2822    **Attributes** 
     2823 
     2824    .. attribute:: tree 
     2825 
     2826        The root of the tree, represented as a :class:`Node`. 
     2827 
     2828    .. attribute:: descender 
     2829 
     2830        A :obj:`Descender`. It is used to descend an instance from the 
     2831        root of the tree (:obj:`tree`) as deeply as possible according 
     2832        to the instance's feature values. 
     2833 
     2834 
    28602835    """ 
    28612836     
     
    28692844                 *args, **kwdargs): 
    28702845        """Classify a new instance. 
    2871          
     2846 
     2847       
    28722848        :param instance: instance to be classified. 
    28732849        :type instance: :class:`Orange.data.Instance` 
Note: See TracChangeset for help on using the changeset viewer.