Changeset 9534:2fb4bdb46798 in orange


Ignore:
Timestamp:
01/16/12 16:49:10 (2 years ago)
Author:
lanz <lan.zagar@…>
Branch:
default
Convert:
eaa3528c09cdac41ea43d2a898f934c90a464db9
Message:

Documentation for Orange.multitarget.tree.

Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • docs/reference/rst/index.rst

    r9508 r9534  
    2323 
    2424   Orange.multilabel 
     25    
     26   Orange.multitarget 
    2527 
    2628   Orange.network 
  • orange/Orange/multitarget/__init__.py

    r9414 r9534  
    11import Orange 
    22 
     3import tree 
    34 
    45# Other algorithms which also work with multitarget data 
  • orange/Orange/multitarget/tree.py

    r9416 r9534  
     1""" 
     2.. index:: Multi-target Tree Learner 
     3 
     4*************************************** 
     5Multi-target Tree Learner 
     6*************************************** 
     7 
     8To use the tree learning algorithm for multi-target data, standard 
     9orange trees (:class:`Orange.classification.tree.TreeLearner`) can be used. 
     10Only the :obj:`Orange.classification.tree.TreeLearner.measure` for feature 
     11scoring and the :obj:`Orange.classification.tree.TreeLearner.node_learner` 
     12components have to be chosen so that they work on multi-target data domains. 
     13 
     14This module provides one such measure (:class:`MultitargetVariance`) that 
     15can be used and a helper class :class:`MultiTreeLearner` which extends 
     16:class:`Orange.classification.tree.TreeLearner` and is the same in all 
     17aspects except for different (multi-target) defaults for `measure` and 
     18`node_learner`. 
     19 
     20.. index:: Multi-target Variance  
     21.. autoclass:: Orange.multitarget.tree.MultitargetVariance 
     22   :members: 
     23   :show-inheritance: 
     24 
     25.. index:: Multi-target Tree Learner 
     26.. autoclass:: Orange.multitarget.tree.MultiTreeLearner 
     27   :members: 
     28   :show-inheritance: 
     29 
     30.. index:: Multi-target Tree Classifier 
     31.. autoclass:: Orange.multitarget.tree.MultiTree 
     32   :members: 
     33   :show-inheritance: 
     34 
     35 
     36Examples 
     37======== 
     38 
     39The following example demonstrates how to build a prediction model with 
     40MultitargetTreeLearner and use it to predict (multiple) class values for 
     41a given instance (:download:`multitarget.py <code/multitarget.py>`, 
     42uses :download:`emotions.tab <code/emotions.tab>`): 
     43 
     44.. literalinclude:: code/multitarget.py 
     45   :lines: 1-4, 10-12 
     46 
     47""" 
     48 
    149from operator import itemgetter 
    250 
     
    654 
    755def weighted_variance(X, weights=None): 
    8     """Computes the variance using a weighted distance between vectors""" 
    9     global foo 
    10     foo = X 
    11  
     56    """Computes the variance using a weighted distance to the centroid.""" 
    1257    if not weights: 
    1358        weights = [1] * len(X[0]) 
    14  
    1559    X = X * np.array(weights) 
    16     return np.sum((X - np.mean(X, 0))**2, 1).sum() 
     60    return np.sum(np.sum((X - np.mean(X, 0))**2, 1)) 
    1761 
    1862class MultitargetVariance(Orange.feature.scoring.Score): 
     63    """ 
     64    A multi-target score that ranks features based on the variance of the 
     65    subsets. A weighted distance can be used to compute the variance. 
     66    """ 
     67 
    1968    def __init__(self, weights=None): 
     69        """ 
     70        :param weights: Weights of the features used when computing distances. 
     71                        If None, all weights are set to 1. 
     72        :type weigts: list 
     73        """ 
     74 
    2075        # Types of classes allowed 
    2176        self.handles_discrete = True 
     77        ### TODO: for discrete classes with >2 values entropy should be used instead of variance 
    2278        self.handles_continuous = True 
    2379        # Can handle continuous features 
     
    3086 
    3187    def threshold_function(self, feature, data, cont_distrib=None, weightID=0): 
     88        """ 
     89        Evaluates possible splits of a continuous feature into a binary one 
     90        and scores them. 
     91         
     92        :param feature: Continuous feature to be split. 
     93        :type feature: :class:`Orange.data.variable` 
     94 
     95        :param data: The data set to be split using the given continuous feature. 
     96        :type data: :class:`Orange.data.Table` 
     97 
     98        :return: :obj:`list` of tuples (threshold, score, None) 
     99        """ 
     100 
    32101        f = data.domain[feature] 
    33102        values = sorted(set(ins[f].value for ins in data)) 
     
    45114 
    46115    def best_threshold(self, feature, data): 
     116        """ 
     117        Computes the best threshold for a split of a continuous feature. 
     118 
     119        :param feature: Continuous feature to be split. 
     120        :type feature: :class:`Orange.data.variable` 
     121 
     122        :param data: The data set to be split using the given continuous feature. 
     123        :type data: :class:`Orange.data.Table` 
     124 
     125        :return: :obj:`tuple` (threshold, score, None) 
     126        """ 
     127 
    47128        scores = self.threshold_function(feature, data) 
    48129        threshold, score = max(scores, key=itemgetter(1)) 
     
    50131 
    51132    def __call__(self, feature, data, apriori_class_distribution=None, weightID=0): 
     133        """ 
     134        :param feature: The feature to be scored. 
     135        :type feature: :class:`Orange.data.variable` 
     136 
     137        :param data: The data set on which to score the feature. 
     138        :type data: :class:`Orange.data.Table` 
     139 
     140        :return: :obj:`float` 
     141        """ 
     142 
    52143        split = dict((ins[feature].value, []) for ins in data) 
    53144        for ins in data: 
     
    59150class MultiTreeLearner(Orange.classification.tree.TreeLearner): 
    60151    """ 
    61     MultiTreeLearner is a multitarget equivalent of the TreeLearner. 
    62     It is the same as Orange.classification.tree.TreeLearner, except for 
    63     the default values of two parameters: 
    64     measure: MultitargetVariance 
    65     node_learner: Orange.multitarget.MultitargetLearner(Orange.classification.majority.MajorityLearner()) 
    66     """ 
    67  
    68     def __init__(self, measure=MultitargetVariance(),  
    69                  node_learner=Orange.multitarget.MultitargetLearner( 
    70                      Orange.classification.majority.MajorityLearner()), 
    71                  **kwargs): 
     152    MultiTreeLearner is a multi-target version of a tree learner. It is the 
     153    same as :class:`Orange.classification.tree.TreeLearner`, except for the 
     154    default values of two parameters: 
     155     
     156    .. attribute:: measure 
     157         
     158        A multi-target score is used by default: :class:`Orange.multitarget.tree.MultitargetVariance`. 
     159 
     160    .. attribute:: node_learner 
     161         
     162        Standard trees use :class:`Orange.classification.majority.MajorityLearner` 
     163        to construct prediction models in the leaves of the tree. 
     164        MultiTreeLearner uses the multi-target equivalent which can be  
     165        obtained simply by wrapping the majority learner: 
     166 
     167        :class:`Orange.multitarget.MultitargetLearner` (:class:`Orange.classification.majority.MajorityLearner()`). 
     168 
     169    """ 
     170 
     171    def __init__(self, **kwargs): 
     172        """ 
     173        The constructor simply passes all given arguments to TreeLearner's constructor 
     174        :obj:`Orange.classification.tree.TreeLearner.__init__`. 
     175        """ 
     176         
     177        measure = MultitargetVariance() 
     178        node_learner = Orange.multitarget.MultitargetLearner( 
     179            Orange.classification.majority.MajorityLearner()) 
    72180        Orange.classification.tree.TreeLearner.__init__( 
    73181            self, measure=measure, node_learner=node_learner, **kwargs) 
    74182 
    75183    def __call__(self, data, weight=0): 
     184        """ 
     185        :param data: Data instances to learn from. 
     186        :type data: :class:`Orange.data.Table` 
     187 
     188        :param weight: Id of meta attribute with weights of instances. 
     189        :type weight: :obj:`int` 
     190        """ 
     191         
    76192        # TreeLearner does not work on class-less domains, 
    77193        # so we set the class if necessary 
     
    80196                data.domain.attributes, data.domain.class_vars[0], 
    81197                class_vars=data.domain.class_vars), data) 
    82         tree = Orange.classification.tree.TreeLearner.__call__(self, data2, weight) 
     198        tree = Orange.classification.tree.TreeLearner.__call__( 
     199            self, data2, weight) 
    83200        return MultiTree(base_classifier=tree) 
    84201 
    85202class MultiTree(Orange.classification.tree.TreeClassifier): 
    86     """MultiTree classifier""" 
     203    """ 
     204    MultiTree classifier is almost the same as the base class it extends 
     205    (:class:`Orange.classification.tree.TreeClassifier`). Only the 
     206    :obj:`__call__` method is modified so it works with multi-target data. 
     207    """ 
    87208 
    88209    def __call__(self, instance, return_type=Orange.core.GetValue): 
     210        """ 
     211        :param instance: Instance to be classified. 
     212        :type instance: :class:`Orange.data.Instance` 
     213 
     214        :param return_type: One of 
     215            :class:`Orange.classification.Classifier.GetValue`, 
     216            :class:`Orange.classification.Classifier.GetProbabilities` or 
     217            :class:`Orange.classification.Classifier.GetBoth` 
     218        """ 
     219 
    89220        node = self.descender(self.tree, instance)[0] 
    90221        return node.node_classifier(instance, return_type) 
     
    92223 
    93224if __name__ == '__main__': 
    94     data = Orange.data.Table('emotions') 
     225    data = Orange.data.Table('test-pls') 
    95226    print 'Actual classes:\n', data[0].get_classes() 
    96     mt = MultiTreeLearner(max_depth=2) 
    97     c = mt(data) 
    98     print 'Predicted classes:\n', c(data[0]) 
    99  
     227     
     228    majority = Orange.classification.majority.MajorityLearner() 
     229    mt_majority = Orange.multitarget.MultitargetLearner(majority) 
     230    c_mtm = mt_majority(data) 
     231    print 'Majority predictions:\n', c_mtm(data[0]) 
     232 
     233    mt_tree = MultiTreeLearner(max_depth=3) 
     234    c_mtt = mt_tree(data) 
     235    print 'Multi-target Tree predictions:\n', c_mtt(data[0]) 
Note: See TracChangeset for help on using the changeset viewer.