Changeset 7275:19c8450c2735 in orange


Ignore:
Timestamp:
02/02/11 23:40:13 (3 years ago)
Author:
matija <matija.polajnar@…>
Branch:
default
Convert:
9d82c1996c09693a5b5a80d91b5b25be6f8b81e7
Message:

Complete documentation of CN2; the rest is unfinished.

File:
1 edited

Legend:

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

    r7230 r7275  
    1515 
    1616Several variations of well-known CN2 rule learning algorithms are implemented. 
    17  
    18 All variations of CN2 are implemented by wrapping 
     17All are implemented by wrapping the 
    1918:class:`Orange.classification.rules.RuleLearner` class. Each CN2 learner class 
    2019in this module changes some of RuleLearner's replaceable components to reflect 
    21 the required behaviour. Thus, in the description of each class, we mention only 
    22 components that differ from default values. 
     20the required behaviour. 
     21 
     22Usage is consistent with typical learner usage in Orange:: 
     23 
     24    # Read some data 
     25    instances =  Orange.data.Table("titanic") 
     26 
     27    # construct the learning algorithm and use it to induce a classifier 
     28    cn2_learner = Orange.classifier.rules.CN2Learner() 
     29    cn2_clasifier = cn2_learner(data) 
     30 
     31    # ... or, in a single step 
     32    cn2_classifier = Orange.classifier.rules.CN2Learner(instances) 
    2333 
    2434.. autoclass:: Orange.classification.rules.CN2Learner 
    2535   :members: 
    2636   :show-inheritance: 
    27    :undoc-members: 
    2837    
    2938.. autoclass:: Orange.classification.rules.CN2Classifier 
     
    3645   :members: 
    3746   :show-inheritance: 
    38    :undoc-members: 
    3947    
    4048.. autoclass:: Orange.classification.rules.CN2UnorderedClassifier 
     
    4856   :members: 
    4957   :show-inheritance: 
    50    :undoc-members: 
    5158    
    5259References 
     
    147154class CN2Learner(RuleLearner): 
    148155    """ 
    149     Classical CN2 (see Clark and Niblett; 1988). It learns a set of ordered 
     156    Classical CN2 (see Clark and Niblett; 1988) induces a set of ordered 
    150157    rules, which means that classificator must try these rules in the same 
    151158    order as they were learned. 
    152159     
    153     """ 
    154      
    155     def __new__(cls, examples=None, weightID=0, **kwargs): 
    156         """ 
    157         :param examples: Data instances to learn from. If not None, an 
    158             :class:`Orange.classification.rules.CN2Classifier` is returned.  
    159         :type examples: :class:`Orange.data.Table` or None 
    160         :param weightId: ID number of weight attribute, default 0 
    161         :type weightId: integer 
    162         :rtype: :class:`Orange.classification.rules.CN2Learner` or 
    163             :class:`Orange.classification.rules.CN2Classifier` 
    164          
    165         Other named parameters may be passed as defined by the ancestor class. 
    166          
    167         """ 
     160    If data instances are provided to the constructor, the learning algorithm 
     161    is called and the resulting classifier is returned instead of the learner. 
     162 
     163    Constructor can be given the following parameters: 
     164     
     165    :param evaluator: an object that evaluates a rule from covered instances. 
     166        By default, entropy is used as a measure.  
     167    :type evaluator: :class:`Orange.classification.rules.RuleEvaluator` 
     168    :param beamWidth: width of the search beam. 
     169    :type beamWidth: int 
     170    :param alpha: significance level of the statistical test to determine 
     171        whether rule is good enough to be returned by rulefinder. Likelihood 
     172        ratio statistics is used that gives an estimate if rule is 
     173        statistically better than the default rule. 
     174    :type alpha: number 
     175 
     176    """ 
     177     
     178    def __new__(cls, instances=None, weightID=0, **kwargs): 
    168179        self = RuleLearner.__new__(cls, **kwargs) 
    169         if examples is not None: 
     180        if instances is not None: 
    170181            self.__init__(**kwargs) 
    171             return self.__call__(examples, weightID) 
     182            return self.__call__(instances, weightID) 
    172183        else: 
    173184            return self 
    174185         
    175186    def __init__(self, evaluator = RuleEvaluator_Entropy(), beamWidth = 5, alpha = 1.0, **kwds): 
    176         """ 
    177         :param evaluator:   
    178         :type evaluator: :class:`Orange.data.Table` 
    179         :param beamWidth:  
    180         :type beamWidth:  
    181         :param alpha: 
    182         :type alpha: 
    183         :rtype: :class:`Orange.classification.rules.CN2Learner` 
    184          
    185         Other named parameters may be passed as defined by the ancestor class. 
    186          
    187         """ 
    188187        self.__dict__.update(kwds) 
    189188        self.ruleFinder = RuleBeamFinder() 
     
    192191        self.ruleFinder.validator = RuleValidator_LRS(alpha = alpha) 
    193192         
    194     def __call__(self, examples, weight=0): 
    195         """ 
    196         :param examples: Data instances to learn from.  
    197         :type examples: :class:`Orange.data.Table` 
    198         :param weight: ID number of weight attribute, default 0 
    199         :type weight: integer 
    200         :rtype: :class:`Orange.classification.rules.CN2Classifier` 
    201          
    202         Learns from the given table of data instances. 
    203          
    204         """ 
    205         supervisedClassCheck(examples) 
    206          
    207         cl = RuleLearner.__call__(self,examples,weight) 
     193    def __call__(self, instances, weight=0): 
     194        supervisedClassCheck(instances) 
     195         
     196        cl = RuleLearner.__call__(self,instances,weight) 
    208197        rules = cl.rules 
    209         return CN2Classifier(rules, examples, weight) 
     198        return CN2Classifier(rules, instances, weight) 
    210199 
    211200 
    212201class CN2Classifier(RuleClassifier): 
    213202    """ 
    214     Classical CN2 (see Clark and Niblett; 1988). Classifies using an ordered 
    215     set of rules. Usually the learner 
     203    Classical CN2 (see Clark and Niblett; 1988) classifies a new instance 
     204    using an ordered set of rules. Usually the learner 
    216205    (:class:`Orange.classification.rules.CN2Learner`) is used to construct the 
    217206    classifier. 
    218      
    219     """ 
    220     def __init__(self, rules=None, examples=None, weightID = 0, **argkw): 
     207         
     208        :param instance: instance to be classifier 
     209        :type instance: :class:`Orange.data.Instance` 
     210        :param result_type: :class:`Orange.core.Classifier.GetValue` or \ 
     211              :class:`Orange.core.Classifier.GetProbabilities` or 
     212              :class:`Orange.core.Classifier.GetBoth` 
     213         
     214        :rtype: :class:`Orange.data.Value`,  
     215              :class:`Orange.statistics.Distribution` or a tuple with both 
     216     
     217    """ 
     218    def __init__(self, rules=None, instances=None, weightID = 0, **argkw): 
    221219        self.rules = rules 
    222         self.examples = examples 
     220        self.instances = instances 
    223221        self.weightID = weightID 
    224         self.classVar = None if examples is None else examples.domain.classVar 
     222        self.classVar = None if instances is None else instances.domain.classVar 
    225223        self.__dict__.update(argkw) 
    226         if examples is not None: 
    227             self.prior = Orange.core.Distribution(examples.domain.classVar, examples) 
    228  
    229     def __call__(self, example, result_type=Orange.core.GetValue): 
     224        if instances is not None: 
     225            self.prior = Orange.core.Distribution(instances.domain.classVar, instances) 
     226 
     227    def __call__(self, instance, result_type=Orange.core.Classifier.GetValue): 
    230228        classifier = None 
    231229        for r in self.rules: 
    232          #   r.filter.domain = example.domain 
    233             if r(example) and r.classifier: 
     230         #   r.filter.domain = instance.domain 
     231            if r(instance) and r.classifier: 
    234232                classifier = r.classifier 
    235233                classifier.defaultDistribution = r.classDistribution 
    236234                break 
    237235        if not classifier: 
    238             classifier = Orange.core.DefaultClassifier(example.domain.classVar, self.prior.modus()) 
     236            classifier = Orange.core.DefaultClassifier(instance.domain.classVar, self.prior.modus()) 
    239237            classifier.defaultDistribution = self.prior 
    240238 
    241         if result_type == Orange.core.GetValue: 
    242           return classifier(example) 
    243         if result_type == Orange.core.GetProbabilities: 
     239        if result_type == Orange.core.Classifier.GetValue: 
     240          return classifier(instance) 
     241        if result_type == Orange.core.Classifier.GetProbabilities: 
    244242          return classifier.defaultDistribution 
    245         return (classifier(example),classifier.defaultDistribution) 
     243        return (classifier(instance),classifier.defaultDistribution) 
    246244 
    247245    def __str__(self): 
     
    254252class CN2UnorderedLearner(RuleLearner): 
    255253    """ 
    256     CN2 unordered (see Clark and Boswell; 1991). It learns a set of unordered 
    257     rules - classification from rules does not assume ordering of rules - and 
    258     returns an :class:`Orange.classification.rules.CN2UnorderedClassifier`. In 
    259     fact, learning rules is quite similar to learning in classical CN2, where 
     254    CN2 unordered (see Clark and Boswell; 1991) induces a set of unordered 
     255    rules - classification from rules does not assume ordering of rules. 
     256    Learning rules is quite similar to learning in classical CN2, where 
    260257    the process of learning of rules is separated to learning rules for each 
    261     class, which is implemented in class' __call__ function. Learning of rules 
    262     for each class uses a slightly changed version of classical CN2 algorithm. 
    263      
    264     """ 
    265     def __new__(cls, examples=None, weightID=0, **kwargs): 
    266         """ 
    267         :param examples: Data instances to learn from. If not None, an 
    268             :class:`Orange.classification.rules.CN2UnorderedClassifier` is 
    269             returned.  
    270         :type examples: :class:`Orange.data.Table` or None 
    271         :param weightId: ID number of weight attribute, default 0 
    272         :type weightId: integer 
    273         :rtype: :class:`Orange.classification.rules.CN2UnorderedLearner` or 
    274             :class:`Orange.classification.rules.CN2UnorderedClassifier` 
    275          
    276         Other named parameters may be passed as defined by the ancestor class. 
    277          
    278         """ 
     258    class. 
     259     
     260    If data instances are provided to the constructor, the learning algorithm 
     261    is called and the resulting classifier is returned instead of the learner. 
     262 
     263    Constructor can be given the following parameters: 
     264     
     265    :param evaluator: an object that evaluates a rule from covered instances. 
     266        By default, Laplace's rule of succession is used as a measure.  
     267    :type evaluator: :class:`Orange.classification.rules.RuleEvaluator` 
     268    :param beamWidth: width of the search beam. 
     269    :type beamWidth: int 
     270    :param alpha: significance level of the statistical test to determine 
     271        whether rule is good enough to be returned by rulefinder. Likelihood 
     272        ratio statistics is used that gives an estimate if rule is 
     273        statistically better than the default rule. 
     274    :type alpha: number 
     275    """ 
     276    def __new__(cls, instances=None, weightID=0, **kwargs): 
    279277        self = RuleLearner.__new__(cls, **kwargs) 
    280         if examples is not None: 
     278        if instances is not None: 
    281279            self.__init__(**kwargs) 
    282             return self.__call__(examples, weightID) 
     280            return self.__call__(instances, weightID) 
    283281        else: 
    284282            return self 
     
    294292        self.dataStopping = RuleDataStoppingCriteria_NoPositives() 
    295293         
    296     def __call__(self, examples, weight=0): 
    297         supervisedClassCheck(examples) 
     294    def __call__(self, instances, weight=0): 
     295        supervisedClassCheck(instances) 
    298296         
    299297        rules = RuleList() 
    300         self.ruleStopping.apriori = Orange.core.Distribution(examples.domain.classVar,examples) 
     298        self.ruleStopping.apriori = Orange.core.Distribution(instances.domain.classVar,instances) 
    301299        progress=getattr(self,"progressCallback",None) 
    302300        if progress: 
    303301            progress.start = 0.0 
    304302            progress.end = 0.0 
    305             distrib = Orange.core.Distribution(examples.domain.classVar, examples, weight) 
     303            distrib = Orange.core.Distribution(instances.domain.classVar, instances, weight) 
    306304            distrib.normalize() 
    307         for targetClass in examples.domain.classVar: 
     305        for targetClass in instances.domain.classVar: 
    308306            if progress: 
    309307                progress.start = progress.end 
    310308                progress.end += distrib[targetClass] 
    311309            self.targetClass = targetClass 
    312             cl = RuleLearner.__call__(self,examples,weight) 
     310            cl = RuleLearner.__call__(self,instances,weight) 
    313311            for r in cl.rules: 
    314312                rules.append(r) 
    315313        if progress: 
    316314            progress(1.0,None) 
    317         return CN2UnorderedClassifier(rules, examples, weight) 
     315        return CN2UnorderedClassifier(rules, instances, weight) 
    318316 
    319317 
    320318class CN2UnorderedClassifier(RuleClassifier): 
    321     def __init__(self, rules = None, examples = None, weightID = 0, **argkw): 
     319    """ 
     320    CN2 unordered (see Clark and Boswell; 1991) induces a set of unordered 
     321    rules. Usually the learner 
     322    (:class:`Orange.classification.rules.CN2UnorderedLearner`) is used to 
     323    construct the classifier. 
     324         
     325        :param instance: instance to be classifier 
     326        :type instance: :class:`Orange.data.Instance` 
     327        :param result_type: :class:`Orange.core.Classifier.GetValue` or \ 
     328              :class:`Orange.core.Classifier.GetProbabilities` or 
     329              :class:`Orange.core.Classifier.GetBoth` 
     330         
     331        :rtype: :class:`Orange.data.Value`,  
     332              :class:`Orange.statistics.Distribution` or a tuple with both 
     333     
     334    """ 
     335    def __init__(self, rules = None, instances = None, weightID = 0, **argkw): 
    322336        self.rules = rules 
    323         self.examples = examples 
     337        self.instances = instances 
    324338        self.weightID = weightID 
    325         self.classVar = examples.domain.classVar if examples is not None else None 
     339        self.classVar = instances.domain.classVar if instances is not None else None 
    326340        self.__dict__.update(argkw) 
    327         if examples is not None: 
    328             self.prior = Orange.core.Distribution(examples.domain.classVar, examples) 
    329  
    330     def __call__(self, example, result_type=Orange.core.GetValue, retRules = False): 
     341        if instances is not None: 
     342            self.prior = Orange.core.Distribution(instances.domain.classVar, instances) 
     343 
     344    def __call__(self, instance, result_type=Orange.core.GetValue, retRules = False): 
    331345        def add(disc1, disc2, sumd): 
    332346            disc = Orange.core.DiscDistribution(disc1) 
     
    338352 
    339353        # create empty distribution 
    340         retDist = Orange.core.DiscDistribution(self.examples.domain.classVar) 
     354        retDist = Orange.core.DiscDistribution(self.instances.domain.classVar) 
    341355        covRules = RuleList() 
    342         # iterate through examples - add distributions 
     356        # iterate through instances - add distributions 
    343357        sumdisc = 0. 
    344358        for r in self.rules: 
    345             if r(example) and r.classDistribution: 
     359            if r(instance) and r.classDistribution: 
    346360                retDist, sumdisc = add(retDist, r.classDistribution, sumdisc) 
    347361                covRules.append(r) 
     
    351365             
    352366        if sumdisc > 0.0: 
    353             for c in self.examples.domain.classVar: 
     367            for c in self.instances.domain.classVar: 
    354368                retDist[c] /= sumdisc 
    355369        else: 
     
    357371         
    358372        if retRules: 
    359             if result_type == Orange.core.GetValue: 
     373            if result_type == Orange.core.Classifier.GetValue: 
    360374              return (retDist.modus(), covRules) 
    361             if result_type == Orange.core.GetProbabilities: 
     375            if result_type == Orange.core.Classifier.GetProbabilities: 
    362376              return (retDist, covRules) 
    363377            return (retDist.modus(),retDist,covRules) 
    364         if result_type == Orange.core.GetValue: 
     378        if result_type == Orange.core.Classifier.GetValue: 
    365379          return retDist.modus() 
    366         if result_type == Orange.core.GetProbabilities: 
     380        if result_type == Orange.core.Classifier.GetProbabilities: 
    367381          return retDist 
    368382        return (retDist.modus(),retDist) 
     
    377391class CN2SDUnorderedLearner(CN2UnorderedLearner): 
    378392    """ 
    379     CN2-SD (see Lavrac et al.; 2004). It learns a set of unordered rules, which 
     393    CN2-SD (see Lavrac et al.; 2004) induces a set of unordered rules, which 
    380394    is the same as :class:`Orange.classification.rules.CN2UnorderedLearner`. 
    381395    The difference between classical CN2 unordered and CN2-SD is selection of 
    382     specific evaluation function and covering function, as mentioned in 
    383     description of 'mult' parameter of __init__ function. 
    384      
    385     """ 
    386     def __new__(cls, examples=None, weightID=0, **kwargs): 
    387         """ 
    388         :param examples: Data instances to learn from. If not None, an 
    389             :class:`Orange.classification.rules.CN2UnorderedClassifier` is 
    390             returned.  
    391         :type examples: :class:`Orange.data.Table` or None 
    392         :param weightId: ID number of weight attribute, default 0 
    393         :type weightId: integer 
    394         :rtype: :class:`Orange.classification.rules.CN2SDUnorderedLearner` or 
    395             :class:`Orange.classification.rules.CN2UnorderedClassifier` 
    396          
    397         Other named parameters may be passed as defined by the ancestor class. 
    398          
    399         """ 
     396    specific evaluation function and covering function: 
     397    :class:`Orange.classifier.rules.WRACCEvaluator` is used to implement 
     398    weight-relative accuracy and  
     399    :class:`Orange.classifier.rules.CovererAndRemover_multWeight` avoids 
     400    excluding covered instances, multiplying their weight by the value of 
     401    mult parameter instead. 
     402     
     403    If data instances are provided to the constructor, the learning algorithm 
     404    is called and the resulting classifier is returned instead of the learner. 
     405 
     406    Constructor can be given the following parameters: 
     407     
     408    :param evaluator: an object that evaluates a rule from covered instances. 
     409        By default, weighted relative accuracy is used. 
     410    :type evaluator: :class:`Orange.classification.rules.RuleEvaluator` 
     411    :param beamWidth: width of the search beam. 
     412    :type beamWidth: int 
     413    :param alpha: significance level of the statistical test to determine 
     414        whether rule is good enough to be returned by rulefinder. Likelihood 
     415        ratio statistics is used that gives an estimate if rule is 
     416        statistically better than the default rule. 
     417    :type alpha: number 
     418    :param mult: multiplicator for weights of covered instances. 
     419    :type mult: number 
     420    """ 
     421    def __new__(cls, instances=None, weightID=0, **kwargs): 
    400422        self = CN2UnorderedLearner.__new__(cls, **kwargs) 
    401         if examples is not None: 
     423        if instances is not None: 
    402424            self.__init__(**kwargs) 
    403             return self.__call__(examples, weightID) 
     425            return self.__call__(instances, weightID) 
    404426        else: 
    405427            return self 
     
    410432        self.coverAndRemove = CovererAndRemover_multWeights(mult=mult) 
    411433 
    412     def __call__(self, examples, weight=0):         
    413         supervisedClassCheck(examples) 
    414          
    415         oldExamples = Orange.core.ExampleTable(examples) 
    416         classifier = CN2UnorderedLearnerClass.__call__(self,examples,weight) 
     434    def __call__(self, instances, weight=0):         
     435        supervisedClassCheck(instances) 
     436         
     437        oldInstances = Orange.data.Table(instances) 
     438        classifier = CN2UnorderedLearnerClass.__call__(self,instances,weight) 
    417439        for r in classifier.rules: 
    418             r.filterAndStore(oldExamples,weight,r.classifier.defaultVal) 
     440            r.filterAndStore(oldInstances,weight,r.classifier.defaultVal) 
    419441        return classifier 
    420442 
     
    483505        self.apriori =  None 
    484506         
    485     def __call__(self,rules,rule,examples,data): 
     507    def __call__(self,rules,rule,instances,data): 
    486508        if not self.apriori: 
    487509            return False 
     
    505527     
    506528 
    507 def supervisedClassCheck(examples): 
    508     if not examples.domain.classVar: 
     529def supervisedClassCheck(instances): 
     530    if not instances.domain.classVar: 
    509531        raise Exception("Class variable is required!") 
    510     if examples.domain.classVar.varType == Orange.core.VarTypes.Continuous: 
     532    if instances.domain.classVar.varType == Orange.core.VarTypes.Continuous: 
    511533        raise Exception("CN2 requires a discrete class!") 
    512534     
     
    516538 
    517539class RuleClassifier_bestRule(RuleClassifier): 
    518     def __init__(self, rules, examples, weightID = 0, **argkw): 
     540    def __init__(self, rules, instances, weightID = 0, **argkw): 
    519541        self.rules = rules 
    520         self.examples = examples 
    521         self.classVar = examples.domain.classVar 
     542        self.instances = instances 
     543        self.classVar = instances.domain.classVar 
    522544        self.__dict__.update(argkw) 
    523         self.prior = Orange.core.Distribution(examples.domain.classVar, examples) 
    524  
    525     def __call__(self, example, result_type=Orange.core.GetValue): 
    526         retDist = Orange.core.Distribution(example.domain.classVar) 
     545        self.prior = Orange.core.Distribution(instances.domain.classVar, instances) 
     546 
     547    def __call__(self, instance, result_type=Orange.core.Classifier.GetValue): 
     548        retDist = Orange.core.Distribution(instance.domain.classVar) 
    527549        bestRule = None 
    528550        for r in self.rules: 
    529             if r(example) and (not bestRule or r.quality>bestRule.quality): 
    530                 for v_i,v in enumerate(example.domain.classVar): 
     551            if r(instance) and (not bestRule or r.quality>bestRule.quality): 
     552                for v_i,v in enumerate(instance.domain.classVar): 
    531553                    retDist[v_i] = r.classDistribution[v_i] 
    532554                bestRule = r 
     
    537559        sumdist = sum(retDist) 
    538560        if sumdist > 0.0: 
    539             for c in self.examples.domain.classVar: 
     561            for c in self.instances.domain.classVar: 
    540562                retDist[c] /= sumdisc 
    541563        else: 
    542564            retDist.normalize() 
    543         # return classifier(example, result_type=result_type) 
    544         if result_type == Orange.core.GetValue: 
     565        # return classifier(instance, result_type=result_type) 
     566        if result_type == Orange.core.Classifier.GetValue: 
    545567          return retDist.modus() 
    546         if result_type == Orange.core.GetProbabilities: 
     568        if result_type == Orange.core.Classifier.GetProbabilities: 
    547569          return retDist 
    548570        return (retDist.modus(),retDist) 
     
    557579    def __init__(self, mult = 0.7): 
    558580        self.mult = mult 
    559     def __call__(self, rule, examples, weights, targetClass): 
     581    def __call__(self, rule, instances, weights, targetClass): 
    560582        if not weights: 
    561583            weights = Orange.core.newmetaid() 
    562             examples.addMetaAttribute(weights,1.) 
    563             examples.domain.addmeta(weights, Orange.core.FloatVariable("weights-"+str(weights)), True) 
     584            instances.addMetaAttribute(weights,1.) 
     585            instances.domain.addmeta(weights, Orange.data.feature.Continuous("weights-"+str(weights)), True) 
    564586        newWeightsID = Orange.core.newmetaid() 
    565         examples.addMetaAttribute(newWeightsID,1.) 
    566         examples.domain.addmeta(newWeightsID, Orange.core.FloatVariable("weights-"+str(newWeightsID)), True) 
    567         for example in examples: 
    568             if rule(example) and example.getclass() == rule.classifier(example,Orange.core.GetValue): 
    569                 example[newWeightsID]=example[weights]*self.mult 
     587        instances.addMetaAttribute(newWeightsID,1.) 
     588        instances.domain.addmeta(newWeightsID, Orange.data.feature.Continuous("weights-"+str(newWeightsID)), True) 
     589        for instance in instances: 
     590            if rule(instance) and instance.getclass() == rule.classifier(instance,Orange.core.Classifier.GetValue): 
     591                instance[newWeightsID]=instance[weights]*self.mult 
    570592            else: 
    571                 example[newWeightsID]=example[weights] 
    572         return (examples,newWeightsID) 
     593                instance[newWeightsID]=instance[weights] 
     594        return (instances,newWeightsID) 
    573595 
    574596class CovererAndRemover_addWeights(RuleCovererAndRemover): 
    575     def __call__(self, rule, examples, weights, targetClass): 
     597    def __call__(self, rule, instances, weights, targetClass): 
    576598        if not weights: 
    577599            weights = Orange.core.newmetaid() 
    578             examples.addMetaAttribute(weights,1.) 
    579             examples.domain.addmeta(weights, Orange.core.FloatVariable("weights-"+str(weights)), True) 
     600            instances.addMetaAttribute(weights,1.) 
     601            instances.domain.addmeta(weights, Orange.data.feature.Continuous("weights-"+str(weights)), True) 
    580602        try: 
    581             coverage = examples.domain.getmeta("Coverage") 
     603            coverage = instances.domain.getmeta("Coverage") 
    582604        except: 
    583             coverage = Orange.core.FloatVariable("Coverage") 
    584             examples.domain.addmeta(Orange.core.newmetaid(),coverage, True) 
    585             examples.addMetaAttribute(coverage,0.0) 
     605            coverage = Orange.data.feature.Continuous("Coverage") 
     606            instances.domain.addmeta(Orange.core.newmetaid(),coverage, True) 
     607            instances.addMetaAttribute(coverage,0.0) 
    586608        newWeightsID = Orange.core.newmetaid() 
    587         examples.addMetaAttribute(newWeightsID,1.) 
    588         examples.domain.addmeta(newWeightsID, Orange.core.FloatVariable("weights-"+str(newWeightsID)), True) 
    589         for example in examples: 
    590             if rule(example) and example.getclass() == rule.classifier(example,Orange.core.GetValue): 
     609        instances.addMetaAttribute(newWeightsID,1.) 
     610        instances.domain.addmeta(newWeightsID, Orange.data.feature.Continuous("weights-"+str(newWeightsID)), True) 
     611        for instance in instances: 
     612            if rule(instance) and instance.getclass() == rule.classifier(instance,Orange.core.Classifier.GetValue): 
    591613                try: 
    592                     example[coverage]+=1.0 
     614                    instance[coverage]+=1.0 
    593615                except: 
    594                     example[coverage]=1.0 
    595                 example[newWeightsID]=1.0/(example[coverage]+1) 
     616                    instance[coverage]=1.0 
     617                instance[newWeightsID]=1.0/(instance[coverage]+1) 
    596618            else: 
    597                 example[newWeightsID]=example[weights] 
    598         return (examples,newWeightsID) 
     619                instance[newWeightsID]=instance[weights] 
     620        return (instances,newWeightsID) 
    599621 
    600622def rule_in_set(rule,rules): 
     
    642664        self.validator = validator 
    643665 
    644     def __call__(self,rules,rule,examples,data):         
    645         ru_st = self.ruleStopping(rules,rule,examples,data) 
     666    def __call__(self,rules,rule,instances,data):         
     667        ru_st = self.ruleStopping(rules,rule,instances,data) 
    646668        if not ru_st: 
    647669            self.validator.rules.append(rule) 
     
    677699 
    678700def createRandomDataSet(data): 
    679     newData = Orange.core.ExampleTable(data) 
     701    newData = Orange.data.Table(data) 
    680702    # shuffle data 
    681703    cl_num = newData.toNumeric("C") 
    682704    random.shuffle(cl_num[0][:,0]) 
    683     clData = Orange.core.ExampleTable(Orange.core.Domain([newData.domain.classVar]),cl_num[0]) 
     705    clData = Orange.data.Table(Orange.data.Domain([newData.domain.classVar]),cl_num[0]) 
    684706    for d_i,d in enumerate(newData): 
    685707        d[newData.domain.classVar] = clData[d_i][newData.domain.classVar] 
     
    746768        self.bestRule = [] 
    747769 
    748     def initialize(self, examples, weightID, targetClass, apriori): 
    749         self.bestRule = [None]*len(examples) 
     770    def initialize(self, instances, weightID, targetClass, apriori): 
     771        self.bestRule = [None]*len(instances) 
    750772        self.probAttribute = Orange.core.newmetaid() 
    751         examples.addMetaAttribute(self.probAttribute,-1.e-6) 
    752         examples.domain.addmeta(self.probAttribute, Orange.core.FloatVariable("Probs")) 
    753         for example in examples: 
    754 ##            if targetClass<0 or (example.getclass() == targetClass): 
    755             example[self.probAttribute] = apriori[targetClass]/apriori.abs 
    756         return examples 
    757  
    758     def getBestRules(self, currentRules, examples, weightID): 
     773        instances.addMetaAttribute(self.probAttribute,-1.e-6) 
     774        instances.domain.addmeta(self.probAttribute, Orange.data.feature.Continuous("Probs")) 
     775        for instance in instances: 
     776##            if targetClass<0 or (instance.getclass() == targetClass): 
     777            instance[self.probAttribute] = apriori[targetClass]/apriori.abs 
     778        return instances 
     779 
     780    def getBestRules(self, currentRules, instances, weightID): 
    759781        bestRules = RuleList() 
    760782        for r in currentRules: 
     
    762784                bestRules.append(r) 
    763785        for r_i,r in enumerate(self.bestRule): 
    764             if r and not rule_in_set(r,bestRules) and examples[r_i].getclass()==r.classifier.defaultValue: 
     786            if r and not rule_in_set(r,bestRules) and instances[r_i].getclass()==r.classifier.defaultValue: 
    765787                bestRules.append(r) 
    766788        return bestRules 
    767789 
    768     def remainingExamplesP(self, examples, targetClass): 
     790    def remainingInstancesP(self, instances, targetClass): 
    769791        pSum, pAll = 0.0, 0.0 
    770         for ex in examples: 
     792        for ex in instances: 
    771793            if ex.getclass() == targetClass: 
    772794                pSum += ex[self.probAttribute] 
     
    774796        return pSum/pAll 
    775797 
    776     def __call__(self, rule, examples, weights, targetClass): 
     798    def __call__(self, rule, instances, weights, targetClass): 
    777799        if targetClass<0: 
    778             for example_i, example in enumerate(examples): 
    779                 if rule(example) and rule.quality>example[self.probAttribute]-0.01: 
    780                     example[self.probAttribute] = rule.quality+0.01 
    781                     self.bestRule[example_i]=rule 
    782         else: 
    783             for example_i, example in enumerate(examples): #rule.classifier.defaultVal == example.getclass() and 
    784                 if rule(example) and rule.quality>example[self.probAttribute]: 
    785                     example[self.probAttribute] = rule.quality+0.001 
    786                     self.bestRule[example_i]=rule 
    787 ##                if rule.classifier.defaultVal == example.getclass(): 
    788 ##                    print example[self.probAttribute] 
     800            for instance_i, instance in enumerate(instances): 
     801                if rule(instance) and rule.quality>instance[self.probAttribute]-0.01: 
     802                    instance[self.probAttribute] = rule.quality+0.01 
     803                    self.bestRule[instance_i]=rule 
     804        else: 
     805            for instance_i, instance in enumerate(instances): #rule.classifier.defaultVal == instance.getclass() and 
     806                if rule(instance) and rule.quality>instance[self.probAttribute]: 
     807                    instance[self.probAttribute] = rule.quality+0.001 
     808                    self.bestRule[instance_i]=rule 
     809##                if rule.classifier.defaultVal == instance.getclass(): 
     810##                    print instance[self.probAttribute] 
    789811        # compute factor 
    790         return (examples,weights) 
    791  
    792 def add_sub_rules(rules, examples, weight, learner, dists): 
    793     apriori = Orange.core.Distribution(examples.domain.classVar,examples,weight) 
     812        return (instances,weights) 
     813 
     814def add_sub_rules(rules, instances, weight, learner, dists): 
     815    apriori = Orange.core.Distribution(instances.domain.classVar,instances,weight) 
    794816    newRules = RuleList() 
    795817    for r in rules: 
     
    802824        tmpRle.filter.conditions = [] 
    803825        tmpRle.parentRule = None 
    804         tmpRle.filterAndStore(examples,weight,r.classifier.defaultVal) 
     826        tmpRle.filterAndStore(instances,weight,r.classifier.defaultVal) 
    805827        tmpList.append(tmpRle) 
    806828        while tmpList and len(tmpList[0].filter.conditions) <= len(r.filter.conditions): 
     
    811833                learner.ruleFinder.evaluator.returnExpectedProb = False 
    812834                learner.ruleFinder.evaluator.evDistGetter.dists = createEVDistList(dists[int(r.classifier.defaultVal)]) 
    813                 tmpRule.quality = learner.ruleFinder.evaluator(tmpRule,examples,weight,r.classifier.defaultVal,apriori) 
     835                tmpRule.quality = learner.ruleFinder.evaluator(tmpRule,instances,weight,r.classifier.defaultVal,apriori) 
    814836                learner.ruleFinder.evaluator.returnExpectedProb = oldREP 
    815837                # if rule not in rules already, add it to the list 
     
    822844                        tmpRule2.parentRule = tmpRule 
    823845                        tmpRule2.filter.conditions.append(c) 
    824                         tmpRule2.filterAndStore(examples,weight,r.classifier.defaultVal) 
     846                        tmpRule2.filterAndStore(instances,weight,r.classifier.defaultVal) 
    825847                        if tmpRule2.classDistribution.abs < tmpRule.classDistribution.abs: 
    826848                            tmpList2.append(tmpRule2) 
    827849            tmpList = tmpList2 
    828     for cl in examples.domain.classVar: 
     850    for cl in instances.domain.classVar: 
    829851        tmpRle = Rule() 
    830         tmpRle.filter = Orange.core.Filter_values(domain = examples.domain) 
     852        tmpRle.filter = Orange.core.Filter_values(domain = instances.domain) 
    831853        tmpRle.parentRule = None 
    832         tmpRle.filterAndStore(examples,weight,int(cl)) 
     854        tmpRle.filterAndStore(instances,weight,int(cl)) 
    833855        tmpRle.quality = tmpRle.classDistribution[int(cl)]/tmpRle.classDistribution.abs 
    834856        newRules.append(tmpRle) 
    835857    return newRules 
    836858 
    837 #def CN2EVCUnorderedLearner(examples = None, weightID=0, **kwds): 
     859#def CN2EVCUnorderedLearner(instances = None, weightID=0, **kwds): 
    838860#    cn2 = CN2EVCUnorderedLearnerClass(**kwds) 
    839 #    if examples: 
    840 #        return cn2(examples, weightID) 
     861#    if instances: 
     862#        return cn2(instances, weightID) 
    841863#    else: 
    842864#        return cn2 
Note: See TracChangeset for help on using the changeset viewer.