Changeset 10963:44453b134698 in orange


Ignore:
Timestamp:
08/16/12 11:30:04 (20 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Set the 'distribution.variable' in BaggingClassifier and ProbabilityEstimator_loess

The retured distribution of BaggedClassifier.call would raise an
'invalid distribution (no variable)' when calling '.items()', Similary any
classifier where ProbabilityEstimator_loess can be used (for instance
MajorityClassifier).

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Orange/ensemble/bagging.py

    r10580 r10963  
    55import Orange 
    66 
     7 
    78class BaggedLearner(orange.Learner): 
    89    """ 
    9     BaggedLearner takes a learner and returns a bagged learner, which is  
    10     essentially a wrapper around the learner passed as an argument. If  
    11     instances are passed in arguments, BaggedLearner returns a bagged  
    12     classifier. Both learner and classifier then behave just like any  
     10    BaggedLearner takes a learner and returns a bagged learner, which is 
     11    essentially a wrapper around the learner passed as an argument. If 
     12    instances are passed in arguments, BaggedLearner returns a bagged 
     13    classifier. Both learner and classifier then behave just like any 
    1314    other learner and classifier in Orange. 
    1415 
    15     Bagging, in essence, takes training data and a learner, and builds *t*  
    16     classifiers, each time presenting a learner a bootstrap sample from the  
    17     training data. When given a test instance, classifiers vote on class,  
    18     and a bagged classifier returns a class with the highest number of votes.  
    19     As implemented in Orange, when class probabilities are requested, these  
     16    Bagging, in essence, takes training data and a learner, and builds *t* 
     17    classifiers, each time presenting a learner a bootstrap sample from the 
     18    training data. When given a test instance, classifiers vote on class, 
     19    and a bagged classifier returns a class with the highest number of votes. 
     20    As implemented in Orange, when class probabilities are requested, these 
    2021    are proportional to the number of votes for a particular class. 
    21      
     22 
    2223    :param learner: learner to be bagged. 
    2324    :type learner: :class:`Orange.core.Learner` 
     
    2728    :param name: name of the resulting learner. 
    2829    :type name: str 
    29     :rtype: :class:`Orange.ensemble.bagging.BaggedClassifier` or  
     30    :rtype: :class:`Orange.ensemble.bagging.BaggedClassifier` or 
    3031            :class:`Orange.ensemble.bagging.BaggedLearner` 
    3132    """ 
     
    3738        else: 
    3839            return self 
    39          
     40 
    4041    def __init__(self, learner, t=10, name='Bagging'): 
    4142        self.t = t 
     
    4647        """ 
    4748        Learn from the given table of data instances. 
    48          
     49 
    4950        :param instances: data instances to learn from. 
    5051        :type instances: Orange.data.Table 
     
    5253        :type weight: int 
    5354        :rtype: :class:`Orange.ensemble.bagging.BaggedClassifier` 
    54          
     55 
    5556        """ 
    5657        r = random.Random() 
    5758        r.seed(0) 
    58          
     59 
    5960        n = len(instances) 
    6061        classifiers = [] 
     
    6667            data = instances.getitems(selection) 
    6768            classifiers.append(self.learner(data, weight)) 
    68         return BaggedClassifier(classifiers = classifiers, name=self.name,\ 
     69        return BaggedClassifier(classifiers=classifiers, name=self.name, \ 
    6970                    class_var=instances.domain.class_var) 
    70 BaggedLearner = Orange.utils.deprecated_members({"weightId":"weight_id", "examples":"instances"})(BaggedLearner) 
     71 
     72BaggedLearner = Orange.utils.deprecated_members( 
     73    {"weightId": "weight_id", "examples": "instances"} 
     74    )(BaggedLearner) 
     75 
    7176 
    7277class BaggedClassifier(orange.Classifier): 
     
    7580    (:class:`Orange.ensemble.bagging.BaggedLearner`) is used to construct the 
    7681    classifier. 
    77      
     82 
    7883    When constructing the classifier manually, the following parameters can 
    7984    be passed: 
     
    8186    :param classifiers: a list of boosted classifiers. 
    8287    :type classifiers: list 
    83      
     88 
    8489    :param name: name of the resulting classifier. 
    8590    :type name: str 
    86      
     91 
    8792    :param class_var: the class feature. 
    8893    :type class_var: :class:`Orange.feature.Descriptor` 
     
    96101        self.__dict__.update(kwds) 
    97102 
    98     def __call__(self, instance, result_type = orange.GetValue): 
     103    def __call__(self, instance, result_type=orange.GetValue): 
    99104        """ 
    100105        :param instance: instance to be classified. 
    101106        :type instance: :class:`Orange.data.Instance` 
    102          
    103         :param result_type: :class:`Orange.classification.Classifier.GetValue` or \ 
    104               :class:`Orange.classification.Classifier.GetProbabilities` or 
     107 
     108        :param result_type: :class:`Orange.classification.Classifier.GetValue` 
     109              or :class:`Orange.classification.Classifier.GetProbabilities` or 
    105110              :class:`Orange.classification.Classifier.GetBoth` 
    106          
    107         :rtype: :class:`Orange.data.Value`,  
     111 
     112        :rtype: :class:`Orange.data.Value`, 
    108113              :class:`Orange.statistics.Distribution` or a tuple with both 
    109114        """ 
     
    117122                return value 
    118123            for i in range(len(freq)): 
    119                 freq[i] = freq[i]/len(self.classifiers) 
    120             freq = Orange.statistics.distribution.Discrete(freq) 
     124                freq[i] = freq[i] / len(self.classifiers) 
     125            freq = Orange.statistics.distribution.Discrete( 
     126                    freq, variable=self.class_var) 
     127 
    121128            if result_type == orange.GetProbabilities: 
    122129                return freq 
     
    125132            else: 
    126133                return value 
    127              
    128         elif self.class_var.var_type ==Orange.feature.Type.Continuous: 
    129             votes = [c(instance, orange.GetBoth if result_type==\ 
    130                 orange.GetProbabilities else result_type) \ 
    131                 for c in self.classifiers] 
     134 
     135        elif self.class_var.var_type == Orange.feature.Type.Continuous: 
     136            votes = [c(instance, orange.GetBoth \ 
     137                       if result_type == orange.GetProbabilities \ 
     138                       else result_type) \ 
     139                     for c in self.classifiers] 
     140 
    132141            wsum = float(len(self.classifiers)) 
    133142            if result_type in [orange.GetBoth, orange.GetProbabilities]: 
     
    139148                    for val, val_p in p.items(): 
    140149                        prob[float(val)] += val_p / wsum 
    141                      
    142                 prob = Orange.statistics.distribution.Continuous(prob) 
    143                 return (self.class_var(pred), prob) if result_type == orange.GetBoth\ 
    144                     else prob 
     150 
     151                prob = Orange.statistics.distribution.Continuous( 
     152                            prob, variable=self.class_var) 
     153 
     154                return (self.class_var(pred), prob) \ 
     155                        if result_type == orange.GetBoth\ 
     156                        else prob 
     157 
    145158            elif result_type == orange.GetValue: 
    146159                pred = sum([float(c) for c in votes]) / wsum 
    147160                return self.class_var(pred) 
    148              
     161 
    149162    def __reduce__(self): 
    150         return type(self), (self.classifiers, self.name, self.class_var), dict(self.__dict__) 
    151 BaggedClassifier = Orange.utils.deprecated_members({"example":"instance", "classVar":"class_var","resultType":"result_type"})(BaggedClassifier) 
     163        return (type(self), (self.classifiers, self.name, self.class_var), 
     164                dict(self.__dict__)) 
     165 
     166BaggedClassifier = Orange.utils.deprecated_members( 
     167    {"example": "instance", "classVar": "class_var", 
     168     "resultType": "result_type"} 
     169    )(BaggedClassifier) 
  • source/orange/estimateprob.cpp

    r10960 r10963  
    273273  map<float, float> loesscurve; 
    274274  loess(cdist->distribution, nPoints, windowProportion, loesscurve, distributionMethod); 
    275   return mlnew TProbabilityEstimator_FromDistribution(mlnew TContDistribution(loesscurve)); 
     275 
     276  TContDistribution *prob = mlnew TContDistribution(loesscurve); 
     277  prob->variable = frequencies->variable; 
     278  return mlnew TProbabilityEstimator_FromDistribution(prob); 
    276279} 
    277280 
Note: See TracChangeset for help on using the changeset viewer.