Changeset 7793:0acc86b21910 in orange


Ignore:
Timestamp:
03/31/11 12:47:11 (3 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
a6802111b7af5a733ec2d7fc2f914faeca8f2918
Message:

Removed the redundant ThresholdLearner_fixed function.
Added explicit init methods to ThresholdLearner classes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/optimization/__init__.py

    r7767 r7793  
    415415    .. attribute:: storeCurve 
    416416     
    417         If set, the resulting classifier will contain an attribute curve, with 
     417        If `True`, the resulting classifier will contain an attribute curve, with 
    418418        a list of tuples containing thresholds and classification accuracies at 
    419         that threshold. 
     419        that threshold (default `False`). 
    420420     
    421421    """ 
     
    423423    def __new__(cls, examples = None, weightID = 0, **kwds): 
    424424        self = Orange.classification.Learner.__new__(cls, **kwds) 
     425        if examples: 
     426            self.__init__(**kwargs) 
     427            return self.__call__(examples, weightID) 
     428        else: 
     429            return self 
     430         
     431    def __init__(self, learner=None, storeCurve=False, **kwds): 
     432        self.learner = learner 
     433        self.storeCurve = storeCurve 
    425434        self.__dict__.update(kwds) 
    426         if examples: 
    427             return self.__call__(examples, weightID) 
    428         else: 
    429             return self 
    430435 
    431436    def __call__(self, examples, weightID = 0): 
    432         if not hasattr(self, "learner"): 
    433             raise AttributeError("learner not set") 
     437        if self.learner is None: 
     438            raise AttributeError("Learner not set.") 
    434439         
    435440        classifier = self.learner(examples, weightID) 
     
    437442                                                          examples,  
    438443                                                          weightID) 
    439         if getattr(self, "storeCurve", 0): 
     444        if self.storeCurve: 
    440445            return ThresholdClassifier(classifier, threshold, curve = curve) 
    441446        else: 
     
    482487        else: 
    483488            return (value, probs) 
    484  
    485 def ThresholdLearner_fixed(learner, threshold,  
    486                            examples=None, weightId=0, **kwds): 
    487      
    488     """There's also a dumb variant of  
     489         
     490     
     491class ThresholdLearner_fixed(Orange.classification.Learner): 
     492    """ There's also a dumb variant of  
    489493    :obj:`Orange.optimization.ThresholdLearner`, a class called 
    490494    :obj:`Orange.optimization.ThreshholdLearner_fixed`. Instead of finding the 
     
    492496    attributes. 
    493497     
    494     .. attriute:: learner 
     498    .. attribute:: learner 
    495499     
    496500    The wrapped learner, for example an instance of 
    497501    :obj:`Orange.classification.bayes.NaiveLearner`. 
    498502     
    499     .. attriute:: threshold 
     503    .. attribute:: threshold 
    500504     
    501505    Threshold to use in classification. 
     
    506510     
    507511    """ 
    508      
    509     lr = apply(ThresholdLearner_fixed_Class, (learner, threshold), kwds) 
    510     if examples: 
    511         return lr(examples, weightId) 
    512     else: 
    513         return lr 
    514      
    515 class ThresholdLearner_fixed(Orange.classification.Learner): 
    516512    def __new__(cls, examples = None, weightID = 0, **kwds): 
    517513        self = Orange.classification.Learner.__new__(cls, **kwds) 
     514        if examples: 
     515            self.__init__(**kwds) 
     516            return self.__call__(examples, weightID) 
     517        else: 
     518            return self 
     519         
     520    def __init__(self, learner=None, threshold=None, **kwds): 
     521        self.learner = learner 
     522        self.threshold = threshold 
    518523        self.__dict__.update(kwds) 
    519         if examples: 
    520             return self.__call__(examples, weightID) 
    521         else: 
    522             return self 
    523524 
    524525    def __call__(self, examples, weightID = 0): 
    525         if not hasattr(self, "learner"): 
    526             raise AttributeError("learner not set") 
    527         if not hasattr(self, "threshold"): 
    528             raise AttributeError("threshold not set") 
    529         if len(examples.domain.classVar.values)!=2: 
    530             raise ValueError("ThresholdLearner handles binary classes only") 
     526        if self.learner is None: 
     527            raise AttributeError("Learner not set.") 
     528        if self.threshold is None: 
     529            raise AttributeError("Threshold not set.") 
     530        if len(examples.domain.classVar.values) != 2: 
     531            raise ValueError("ThresholdLearner handles binary classes only.") 
    531532         
    532533        return ThresholdClassifier(self.learner(examples, weightID),  
Note: See TracChangeset for help on using the changeset viewer.