Changeset 9733:20f29544e12d in orange


Ignore:
Timestamp:
02/06/12 13:25:39 (2 years ago)
Author:
crt.gorup@…
Branch:
default
rebase_source:
52ef560e460d6dbc43513f55f66381fafdd802f1
Message:

Removed camelCase and added deprecated decorators.

Location:
Orange/ensemble
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • Orange/ensemble/bagging.py

    r9671 r9733  
    3030            :class:`Orange.ensemble.bagging.BaggedLearner` 
    3131    """ 
    32     def __new__(cls, learner, instances=None, weightId=None, **kwargs): 
     32    def __new__(cls, learner, instances=None, weight_id=None, **kwargs): 
    3333        self = orange.Learner.__new__(cls, **kwargs) 
    3434        if instances is not None: 
    3535            self.__init__(self, learner, **kwargs) 
    36             return self.__call__(instances, weightId) 
     36            return self.__call__(instances, weight_id) 
    3737        else: 
    3838            return self 
     
    6767            classifiers.append(self.learner(data, weight)) 
    6868        return BaggedClassifier(classifiers = classifiers, name=self.name,\ 
    69                     classVar=instances.domain.classVar) 
     69                    class_var=instances.domain.class_var) 
     70BaggedLearner = Orange.misc.deprecated_members({"weightId":"weight_id", "examples":"instances"})(BaggedLearner) 
    7071 
    7172class BaggedClassifier(orange.Classifier): 
     
    8485    :type name: str 
    8586     
    86     :param classVar: the class feature. 
    87     :type classVar: :class:`Orange.data.variable.Variable` 
     87    :param class_var: the class feature. 
     88    :type class_var: :class:`Orange.data.variable.Variable` 
    8889 
    8990    """ 
    9091 
    91     def __init__(self, classifiers, name, classVar, **kwds): 
     92    def __init__(self, classifiers, name, class_var, **kwds): 
    9293        self.classifiers = classifiers 
    9394        self.name = name 
    94         self.classVar = classVar 
     95        self.class_var = class_var 
    9596        self.__dict__.update(kwds) 
    9697 
    97     def __call__(self, instance, resultType = orange.GetValue): 
     98    def __call__(self, instance, result_type = orange.GetValue): 
    9899        """ 
    99100        :param instance: instance to be classified. 
     
    107108              :class:`Orange.statistics.Distribution` or a tuple with both 
    108109        """ 
    109         if self.classVar.varType == Orange.data.Type.Discrete: 
    110             freq = [0.] * len(self.classVar.values) 
     110        if self.class_var.var_type == Orange.data.Type.Discrete: 
     111            freq = [0.] * len(self.class_var.values) 
    111112            for c in self.classifiers: 
    112113                freq[int(c(instance))] += 1 
    113114            index = freq.index(max(freq)) 
    114             value = Orange.data.Value(self.classVar, index) 
    115             if resultType == orange.GetValue: 
     115            value = Orange.data.Value(self.class_var, index) 
     116            if result_type == orange.GetValue: 
    116117                return value 
    117118            for i in range(len(freq)): 
    118119                freq[i] = freq[i]/len(self.classifiers) 
    119120            freq = Orange.statistics.distribution.Discrete(freq) 
    120             if resultType == orange.GetProbabilities: 
     121            if result_type == orange.GetProbabilities: 
    121122                return freq 
    122             elif resultType == orange.GetBoth: 
     123            elif result_type == orange.GetBoth: 
    123124                return (value, freq) 
    124125            else: 
    125126                return value 
    126127             
    127         elif self.classVar.varType ==Orange.data.Type.Continuous: 
    128             votes = [c(instance, orange.GetBoth if resultType==\ 
    129                 orange.GetProbabilities else resultType) \ 
     128        elif self.class_var.var_type ==Orange.data.Type.Continuous: 
     129            votes = [c(instance, orange.GetBoth if result_type==\ 
     130                orange.GetProbabilities else result_type) \ 
    130131                for c in self.classifiers] 
    131132            wsum = float(len(self.classifiers)) 
    132             if resultType in [orange.GetBoth, orange.GetProbabilities]: 
     133            if result_type in [orange.GetBoth, orange.GetProbabilities]: 
    133134                pred = sum([float(c) for c, p in votes]) / wsum 
    134135#               prob = sum([float(p.modus()) for c, p in votes]) / wsum 
     
    140141                     
    141142                prob = Orange.statistics.distribution.Continuous(prob) 
    142                 return (self.classVar(pred), prob) if resultType == orange.GetBoth\ 
     143                return (self.class_var(pred), prob) if result_type == orange.GetBoth\ 
    143144                    else prob 
    144             elif resultType == orange.GetValue: 
     145            elif result_type == orange.GetValue: 
    145146                pred = sum([float(c) for c in votes]) / wsum 
    146                 return self.classVar(pred) 
     147                return self.class_var(pred) 
    147148             
    148149    def __reduce__(self): 
    149         return type(self), (self.classifiers, self.name, self.classVar), dict(self.__dict__) 
    150      
     150        return type(self), (self.classifiers, self.name, self.class_var), dict(self.__dict__) 
     151BaggedClassifier = Orange.misc.deprecated_members({"example":"instance", "classVar":"class_var","resultType":"result_type"})(BaggedClassifier) 
  • Orange/ensemble/boosting.py

    r9671 r9733  
    2727            :class:`Orange.ensemble.boosting.BoostedLearner` 
    2828    """ 
    29     def __new__(cls, learner, instances=None, weightId=None, **kwargs): 
     29    def __new__(cls, learner, instances=None, weight_id=None, **kwargs): 
    3030        self = orange.Learner.__new__(cls, **kwargs) 
    3131        if instances is not None: 
    3232            self.__init__(self, learner, **kwargs) 
    33             return self.__call__(instances, weightId) 
     33            return self.__call__(instances, weight_id) 
    3434        else: 
    3535            return self 
     
    4040        self.learner = learner 
    4141 
    42     def __call__(self, instances, origWeight = 0): 
     42    def __call__(self, instances, orig_weight = 0): 
    4343        """ 
    4444        Learn from the given table of data instances. 
     
    4646        :param instances: data instances to learn from. 
    4747        :type instances: Orange.data.Table 
    48         :param origWeight: weight. 
    49         :type origWeight: int 
     48        :param orig_weight: weight. 
     49        :type orig_weight: int 
    5050        :rtype: :class:`Orange.ensemble.boosting.BoostedClassifier` 
    5151         
     
    5353        import math 
    5454        weight = Orange.data.new_meta_id() 
    55         if origWeight: 
     55        if orig_weight: 
    5656            for i in instances: 
    57                 i.setweight(weight, i.getweight(origWeight)) 
     57                i.setweight(weight, i.getweight(orig_weight)) 
    5858        else: 
    5959            instances.addMetaAttribute(weight, 1.0) 
     
    8080                instances.removeMetaAttribute(weight) 
    8181                return BoostedClassifier(classifiers = classifiers,  
    82                     name=self.name, classVar=instances.domain.classVar) 
     82                    name=self.name, class_var=instances.domain.class_var) 
    8383            beta = epsilon/(1-epsilon) 
    8484            for e in range(n): 
     
    9191        instances.removeMetaAttribute(weight) 
    9292        return BoostedClassifier(classifiers = classifiers, name=self.name,  
    93             classVar=instances.domain.classVar) 
     93            class_var=instances.domain.class_var) 
     94BoostedLearner = Orange.misc.deprecated_members({"examples":"instances", "classVar":"class_var", "weightId":"weigth_id", "origWeight":"orig_weight"})(BoostedLearner) 
    9495 
    9596class BoostedClassifier(orange.Classifier): 
     
    108109    :type name: str 
    109110     
    110     :param classVar: the class feature. 
    111     :type classVar: :class:`Orange.data.variable.Variable` 
     111    :param class_var: the class feature. 
     112    :type class_var: :class:`Orange.data.variable.Variable` 
    112113     
    113114    """ 
    114115 
    115     def __init__(self, classifiers, name, classVar, **kwds): 
     116    def __init__(self, classifiers, name, class_var, **kwds): 
    116117        self.classifiers = classifiers 
    117118        self.name = name 
    118         self.classVar = classVar 
     119        self.class_var = class_var 
    119120        self.__dict__.update(kwds) 
    120121 
    121     def __call__(self, instance, resultType = orange.GetValue): 
     122    def __call__(self, instance, result_type = orange.GetValue): 
    122123        """ 
    123124        :param instance: instance to be classified. 
     
    131132              :class:`Orange.statistics.Distribution` or a tuple with both 
    132133        """ 
    133         votes = Orange.statistics.distribution.Discrete(self.classVar) 
     134        votes = Orange.statistics.distribution.Discrete(self.class_var) 
    134135        for c, e in self.classifiers: 
    135136            votes[int(c(instance))] += e 
    136         index = Orange.misc.selection.selectBestIndex(votes) 
     137        index = Orange.misc.selection.select_best_index(votes) 
    137138        # TODO 
    138         value = Orange.data.Value(self.classVar, index) 
    139         if resultType == orange.GetValue: 
     139        value = Orange.data.Value(self.class_var, index) 
     140        if result_type == orange.GetValue: 
    140141            return value 
    141142        sv = sum(votes) 
    142143        for i in range(len(votes)): 
    143144            votes[i] = votes[i]/sv 
    144         if resultType == orange.GetProbabilities: 
     145        if result_type == orange.GetProbabilities: 
    145146            return votes 
    146         elif resultType == orange.GetBoth: 
     147        elif result_type == orange.GetBoth: 
    147148            return (value, votes) 
    148149        else: 
     
    150151         
    151152    def __reduce__(self): 
    152         return type(self), (self.classifiers, self.name, self.classVar), dict(self.__dict__) 
    153      
     153        return type(self), (self.classifiers, self.name, self.class_var), dict(self.__dict__) 
     154 
     155BoostedClassifier = Orange.misc.deprecated_members({"classVar":"class_var", "resultType":"result_type"})(BoostedClassifier) 
  • Orange/ensemble/forest.py

    r9697 r9733  
    55import random 
    66import copy 
     7from Orange.misc import deprecated_keywords 
    78 
    89def _default_small_learner(attributes=None, rand=None, base=None): 
     
    2930  
    3031class _RandomForestSimpleTreeLearner(Orange.core.Learner): 
    31     """ A learner which wraps an ordinary SimpleTreeLearner.  Sets the 
     32    """A learner which wraps an ordinary SimpleTreeLearner.  Sets the 
    3233    skip_prob so that the number of randomly chosen features for each 
    33     split is  (on average) as specified.  """ 
    34  
    35     def __new__(cls, examples = None, weightID = 0, **argkw): 
     34    split is  (on average) as specified.""" 
     35 
     36    def __new__(cls, instances = None, weight_id = 0, **argkw): 
    3637        self = Orange.core.Learner.__new__(cls, **argkw) 
    37         if examples: 
     38        if instances: 
    3839            self.__init__(**argkw) 
    39             return self.__call__(examples, weightID) 
     40            return self.__call__(instances, weight_id) 
    4041        else: 
    4142            return self 
     
    4647        self.rand = rand 
    4748     
    48     def __call__(self, examples, weight=0): 
     49    def __call__(self, instances, weight=0): 
    4950        osp,orand = self.base.skip_prob, self.base.random_generator 
    50         self.base.skip_prob = 1-float(self.attributes)/len(examples.domain.attributes) 
     51        self.base.skip_prob = 1-float(self.attributes)/len(instances.domain.attributes) 
    5152        self.base.random_generator = self.rand 
    52         r = self.base(examples, weight) 
     53        r = self.base(instances, weight) 
    5354        self.base.skip_prob, self.base.random_generator = osp, orand 
    5455        return r 
     56_RandomForestSimpleTreeLearner = Orange.misc.deprecated_members({"weightID":"weight_id", "examples":"instances"})(_RandomForestSimpleTreeLearner) 
    5557    
    5658class RandomForestLearner(orange.Learner): 
     
    170172        return RandomForestClassifier(classifiers = classifiers, name=self.name,\ 
    171173                    domain=instances.domain, class_var=instances.domain.class_var) 
    172   
     174RandomForestLearner = Orange.misc.deprecated_members({"examples":"instances"})(RandomForestLearner) 
    173175 
    174176class RandomForestClassifier(orange.Classifier): 
     
    202204        self.__dict__.update(kwds) 
    203205 
    204     def __call__(self, instance, resultType = orange.GetValue): 
     206    def __call__(self, instance, result_type = orange.GetValue): 
    205207        """ 
    206208        :param instance: instance to be classified. 
     
    221223         
    222224            # voting for class probabilities 
    223             if resultType == orange.GetProbabilities or resultType == orange.GetBoth: 
     225            if result_type == orange.GetProbabilities or result_type == orange.GetBoth: 
    224226                prob = [0.] * len(self.domain.class_var.values) 
    225227                for c in self.classifiers: 
     
    234236            # this may not be the same class as one obtaining the 
    235237            # highest probability through probability voting 
    236             if resultType == orange.GetValue or resultType == orange.GetBoth: 
     238            if result_type == orange.GetValue or result_type == orange.GetBoth: 
    237239                cfreq = [0] * len(self.domain.class_var.values) 
    238240                for c in self.classifiers: 
     
    241243                cvalue = Orange.data.Value(self.domain.class_var, index) 
    242244     
    243             if resultType == orange.GetValue: return cvalue 
    244             elif resultType == orange.GetProbabilities: return cprob 
     245            if result_type == orange.GetValue: return cvalue 
     246            elif result_type == orange.GetProbabilities: return cprob 
    245247            else: return (cvalue, cprob) 
    246248         
     
    249251         
    250252            # voting for class probabilities 
    251             if resultType == orange.GetProbabilities or resultType == orange.GetBoth: 
     253            if result_type == orange.GetProbabilities or result_type == orange.GetBoth: 
    252254                probs = [c(instance, orange.GetBoth) for c in self.classifiers] 
    253255                cprob = dict() 
     
    262264                 
    263265            # gather average class value 
    264             if resultType == orange.GetValue or resultType == orange.GetBoth: 
     266            if result_type == orange.GetValue or result_type == orange.GetBoth: 
    265267                values = [c(instance).value for c in self.classifiers] 
    266268                cvalue = Orange.data.Value(self.domain.class_var, sum(values) / len(self.classifiers)) 
    267269             
    268             if resultType == orange.GetValue: return cvalue 
    269             elif resultType == orange.GetProbabilities: return cprob 
     270            if result_type == orange.GetValue: return cvalue 
     271            elif result_type == orange.GetProbabilities: return cprob 
    270272            else: return (cvalue, cprob) 
    271273             
    272274    def __reduce__(self): 
    273275        return type(self), (self.classifiers, self.name, self.domain, self.class_var), dict(self.__dict__) 
    274  
     276RandomForestClassifier = Orange.misc.deprecated_members({"resultType":"result_type", "classVar":"class_var", "example":"instance"})(RandomForestClassifier) 
    275277### MeasureAttribute_randomForests 
    276278 
     
    326328        else: 
    327329            self.learner = learner 
    328    
    329     def __call__(self, feature, instances, apriorClass=None): 
     330 
     331    @deprecated_keywords({"apriorClass":"aprior_class"}) 
     332    def __call__(self, feature, instances, aprior_class=None): 
    330333        """ 
    331334        Return importance of a given feature. 
     
    339342        :type instances: :class:`Orange.data.Table` 
    340343         
    341         :param apriorClass: not used! 
     344        :param aprior_class: not used! 
    342345         
    343346        """ 
     
    354357        else: 
    355358          raise Exception("MeasureAttribute_rf can not be called with (\ 
    356                 contingency,classDistribution, apriorClass) as fuction arguments.") 
     359                contingency,classDistribution, aprior_class) as fuction arguments.") 
    357360 
    358361        self._buffer(instances) 
     
    482485    a new split constructor. 
    483486    """ 
    484  
    485     def __new__(cls, examples = None, weightID = 0, **argkw): 
     487    @deprecated_keywords({"weightID":"weight_id", "examples":"instances"}) 
     488    def __new__(cls, instances = None, weight_id = 0, **argkw): 
    486489        self = Orange.core.Learner.__new__(cls, **argkw) 
    487         if examples: 
     490        if instances: 
    488491            self.__init__(**argkw) 
    489             return self.__call__(examples, weightID) 
     492            return self.__call__(instances, weight_id) 
    490493        else: 
    491494            return self 
     
    497500        if not self.rand: #for all the built trees 
    498501            self.rand = random.Random(0) 
    499      
    500     def __call__(self, examples, weight=0): 
     502 
     503    @deprecated_keywords({"examples":"instances"}) 
     504    def __call__(self, instances, weight=0): 
    501505        """ A current tree learner is copied, modified and then used. 
    502506        Modification: set a different split constructor, which uses 
     
    508512        if not bcopy.measure: 
    509513            bcopy.measure = Orange.feature.scoring.Gini() \ 
    510                 if isinstance(examples.domain.class_var, Orange.data.variable.Discrete) \ 
     514                if isinstance(instances.domain.class_var, Orange.data.variable.Discrete) \ 
    511515                else Orange.feature.scoring.MSE() 
    512516 
     
    514518            bcopy.split, self.attributes, self.rand) 
    515519 
    516         return bcopy(examples, weight=weight) 
     520        return bcopy(instances, weight=weight) 
    517521 
    518522class SplitConstructor_AttributeSubset(orange.TreeSplitConstructor): 
     
    524528            self.rand = random.Random(0) 
    525529 
    526     def __call__(self, gen, weightID, contingencies, apriori, candidates, clsfr): 
     530    @deprecated_keywords({"weightID":"weight_id"}) 
     531    def __call__(self, gen, weight_id, contingencies, apriori, candidates, clsfr): 
    527532        # if number of features for subset is not set, use square root 
    528533        cand = [1]*int(self.attributes) + [0]*(len(candidates) - int(self.attributes)) 
     
    530535        # instead with all features, we will invoke split constructor  
    531536        # only for the subset of a features 
    532         t = self.scons(gen, weightID, contingencies, apriori, cand, clsfr) 
     537        t = self.scons(gen, weight_id, contingencies, apriori, cand, clsfr) 
    533538        return t 
Note: See TracChangeset for help on using the changeset viewer.