Changeset 7919:16d9cd7a66d6 in orange


Ignore:
Timestamp:
05/19/11 11:55:14 (3 years ago)
Author:
anze <anze.staric@…>
Branch:
default
Convert:
e3f8bec54720c76459fc0b221effa6dc77e7f00b
Message:

Fixed parameter names in NaiveLearner (replaced camel case with underscores).

Location:
orange
Files:
5 edited

Legend:

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

    r7810 r7919  
    1717 
    1818The following example demonstrates a straightforward invocation of 
    19 this algorithm (`bayes-run.py`_, uses `iris.tab`_): 
     19this algorithm (`bayes-run.py`_, uses `titanic.tab`_): 
    2020 
    2121.. literalinclude:: code/bayes-run.py 
     
    7878.. _adult-sample.tab: code/adult-sample.tab 
    7979.. _iris.tab: code/iris.tab 
     80.. _titanic.tab: code/iris.tab 
    8081.. _lenses.tab: code/lenses.tab 
    8182 
     
    174175from Orange.core import BayesClassifier as _BayesClassifier 
    175176from Orange.core import BayesLearner as _BayesLearner 
     177 
    176178 
    177179class NaiveLearner(Orange.classification.Learner): 
     
    237239    """ 
    238240     
    239     def __new__(cls, instances = None, weightID = 0, **argkw): 
     241    def __new__(cls, instances = None, weight_id = 0, **argkw): 
    240242        self = Orange.classification.Learner.__new__(cls, **argkw) 
    241243        if instances: 
    242244            self.__init__(**argkw) 
    243             return self.__call__(instances, weightID) 
     245            return self.__call__(instances, weight_id) 
    244246        else: 
    245247            return self 
    246248         
    247     def __init__(self, adjustTreshold=False, m=0, estimatorConstructor=None, 
    248                  conditionalEstimatorConstructor=None, 
    249                  conditionalEstimatorConstructorContinuous=None,**argkw): 
    250         self.adjustThreshold = adjustTreshold 
     249    def __init__(self, adjust_threshold=False, m=0, estimator_constructor=None, 
     250                 conditional_estimator_constructor=None, 
     251                 conditional_estimator_constructor_continuous=None,**argkw): 
     252        self.adjust_threshold = adjust_threshold 
    251253        self.m = m 
    252         self.estimatorConstructor = estimatorConstructor 
    253         self.conditionalEstimatorConstructor = conditionalEstimatorConstructor 
    254         self.conditionalEstimatorConstructorContinuous = conditionalEstimatorConstructorContinuous 
     254        self.estimator_constructor = estimator_constructor 
     255        self.conditional_estimator_constructor = conditional_estimator_constructor 
     256        self.conditional_estimator_constructor_continuous = conditional_estimator_constructor_continuous 
    255257        self.__dict__.update(argkw) 
    256258 
     
    265267        """ 
    266268        bayes = _BayesLearner() 
    267         if self.estimatorConstructor: 
    268             bayes.estimatorConstructor = self.estimatorConstructor 
     269        if self.estimator_constructor: 
     270            bayes.estimator_constructor = self.estimator_constructor 
    269271            if self.m: 
    270                 if not hasattr(bayes.estimatorConstructor, "m"): 
    271                     raise AttributeError, "invalid combination of attributes: 'estimatorConstructor' does not expect 'm'" 
     272                if not hasattr(bayes.estimator_constructor, "m"): 
     273                    raise AttributeError, "invalid combination of attributes: 'estimator_constructor' does not expect 'm'" 
    272274                else: 
    273                     self.estimatorConstructor.m = self.m 
     275                    self.estimator_constructor.m = self.m 
    274276        elif self.m: 
    275             bayes.estimatorConstructor = Orange.core.ProbabilityEstimatorConstructor_m(m = self.m) 
    276         if self.conditionalEstimatorConstructor: 
    277             bayes.conditionalEstimatorConstructor = self.conditionalEstimatorConstructor 
    278         elif bayes.estimatorConstructor: 
    279             bayes.conditionalEstimatorConstructor = Orange.core.ConditionalProbabilityEstimatorConstructor_ByRows() 
    280             bayes.conditionalEstimatorConstructor.estimatorConstructor=bayes.estimatorConstructor 
    281         if self.conditionalEstimatorConstructorContinuous: 
    282             bayes.conditionalEstimatorConstructorContinuous = self.conditionalEstimatorConstructorContinuous 
    283         if self.adjustThreshold: 
    284             bayes.adjustThreshold = self.adjustThreshold 
     277            bayes.estimator_constructor = Orange.core.ProbabilityEstimatorConstructor_m(m = self.m) 
     278        if self.conditional_estimator_constructor: 
     279            bayes.conditional_estimator_constructor = self.conditional_estimator_constructor 
     280        elif bayes.estimator_constructor: 
     281            bayes.conditional_estimator_constructor = Orange.core.ConditionalProbabilityEstimatorConstructor_ByRows() 
     282            bayes.conditional_estimator_constructor.estimator_constructor=bayes.estimator_constructor 
     283        if self.conditional_estimator_constructor_continuous: 
     284            bayes.conditional_estimator_constructor_continuous = self.conditional_estimator_constructor_continuous 
     285        if self.adjust_threshold: 
     286            bayes.adjust_threshold = self.adjust_threshold 
    285287        return NaiveClassifier(bayes(instances, weight)) 
    286              
     288NaiveLearner = Orange.misc.deprecated_members( 
     289{     "adjustThreshold": "adjust_threshold", 
     290      "estimatorConstructor": "estimator_constructor", 
     291      "conditionalEstimatorConstructor": "conditional_estimator_constructor", 
     292      "conditionalEstimatorConstructorContinuous":"conditional_estimator_constructor_continuous", 
     293      "weightID": "weight_id" 
     294}, in_place=False)(NaiveLearner) 
     295 
     296 
    287297class NaiveClassifier(Orange.classification.Classifier): 
    288298    """ 
  • orange/doc/Orange/rst/code/bayes-mestimate.py

    r7534 r7919  
    1414bayesWithM = bayesWithM_L(table) 
    1515 
    16 print bayes.conditionalDistributions 
     16print bayes.conditional_distributions 
    1717# prints: <<'pre-presbyopic': <0.625, 0.125, 0.250>, 'presbyopic': <0.750, 0.125, 0.125>, ...>> 
    18 print bayesWithM.conditionalDistributions 
     18print bayesWithM.conditional_distributions 
    1919# prints: <<'pre-presbyopic': <0.625, 0.133, 0.242>, 'presbyopic': <0.725, 0.133, 0.142>, ...>> 
    2020 
  • orange/doc/Orange/rst/code/bayes-run.py

    r7418 r7919  
    11# Description: Naive Bayes Learner on iris data set 
    22# Category:    classification 
    3 # Uses:        iris 
     3# Uses:        titanic 
    44# Referenced:  Orange.classification.bayes 
    55# Classes:     Orange.classification.bayes.NaiveLearner, Orange.classification.bayes.NaiveClassifier 
    66 
    77import Orange 
    8 table = Orange.data.Table("iris.tab") 
     8table = Orange.data.Table("titanic.tab") 
    99 
    1010learner = Orange.classification.bayes.NaiveLearner() 
  • orange/doc/Orange/rst/code/bayes-thresholdAdjustment.py

    r7477 r7919  
    1010 
    1111bayes = Orange.classification.bayes.NaiveLearner(name="Naive Bayes") 
    12 adjustedBayes = Orange.classification.bayes.NaiveLearner(adjustThreshold=True, name="Adjusted Naive Bayes") 
     12adjusted_bayes = Orange.classification.bayes.NaiveLearner(adjust_threshold=True, name="Adjusted Naive Bayes") 
    1313 
    14 results = Orange.evaluation.testing.crossValidation([bayes, adjustedBayes], table) 
     14results = Orange.evaluation.testing.cross_validation([bayes, adjusted_bayes], table) 
    1515print orngStat.CA(results) 
  • orange/orngBayes.py

    r6538 r7919  
    1 import orange, time 
    2  
    3 class BayesLearner(orange.Learner): 
    4     def __new__(cls, examples = None, weightID = 0, **argkw): 
    5         self = orange.Learner.__new__(cls, **argkw) 
    6         if examples: 
    7             self.__init__(**argkw) 
    8             return self.__call__(examples, weightID) 
    9         else: 
    10             return self 
    11          
    12     def __init__(self, **argkw): 
    13         self.learner = None 
    14         self.__dict__.update(argkw) 
    15  
    16     def __setattr__(self, name, value): 
    17         if name in ["m", "estimatorConstructor", "conditionalEstimatorConstructor", "conditionalEstimatorConstructorContinuous"]: 
    18             self.learner = None 
    19         self.__dict__[name] = value 
    20  
    21     def __call__(self, examples, weight=0): 
    22         if not self.learner: 
    23             self.learner = self.createInstance() 
    24         return self.learner(examples, weight) 
    25  
    26     def createInstance(self): 
    27         bayes = orange.BayesLearner() 
    28         if hasattr(self, "estimatorConstructor"): 
    29             bayes.estimatorConstructor = self.estimatorConstructor 
    30             if hasattr(self, "m"): 
    31                 if hasattr(bayes.estimatorConstructor, "m"): 
    32                     raise AttributeError, "invalid combination of attributes: 'estimatorConstructor' does not expect 'm'" 
    33                 else: 
    34                     self.estimatorConstructor.m = self.m 
    35         elif hasattr(self, "m"): 
    36             bayes.estimatorConstructor = orange.ProbabilityEstimatorConstructor_m(m = self.m) 
    37  
    38         if hasattr(self, "conditionalEstimatorConstructor"): 
    39             bayes.conditionalEstimatorConstructor = self.conditionalEstimatorConstructor 
    40         elif bayes.estimatorConstructor: 
    41             bayes.conditionalEstimatorConstructor = orange.ConditionalProbabilityEstimatorConstructor_ByRows() 
    42             bayes.conditionalEstimatorConstructor.estimatorConstructor=bayes.estimatorConstructor 
    43              
    44         if hasattr(self, "conditionalEstimatorConstructorContinuous"): 
    45             bayes.conditionalEstimatorConstructorContinuous = self.conditionalEstimatorConstructorContinuous 
    46              
    47         return bayes 
    48              
     1from Orange import NaiveLearner as BayesLearner, NaiveClassifier as BayesClassifier 
    492 
    503def printModel(bayesclassifier): 
    51     nValues=len(bayesclassifier.classVar.values) 
    52     frmtStr=' %10.3f'*nValues 
    53     classes=" "*20+ ((' %10s'*nValues) % tuple([i[:10] for i in bayesclassifier.classVar.values])) 
    54     print classes 
    55     print "class probabilities "+(frmtStr % tuple(bayesclassifier.distribution)) 
    56     print 
    57  
    58     for i in bayesclassifier.conditionalDistributions: 
    59         print "Attribute", i.variable.name 
    60         print classes 
    61         for v in range(len(i.variable.values)): 
    62             print ("%20s" % i.variable.values[v][:20]) + (frmtStr % tuple(i[v])) 
    63         print 
     4    """ 
     5    DEPRECATED. Replaced by :obj:`BayesClassifier.dump`. 
     6    """ 
     7    print(model if isinstance(model, TreeClassifier) else NaiveClassifier(model)) 
Note: See TracChangeset for help on using the changeset viewer.