Changeset 7366:12ef69f24d36 in orange


Ignore:
Timestamp:
02/04/11 00:30:15 (3 years ago)
Author:
matija <matija.polajnar@…>
Branch:
default
Convert:
c0668e7f203949cb72bd1d8160196dbf30cc6cea
Message:

Finished documentation of rules module.

Location:
orange
Files:
2 added
1 edited

Legend:

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

    r7288 r7366  
    66 
    77First, the commonly used CN2 learner's description is given. That is followed 
    8 by documentation of common classes and functions of the module. 
    9  
    10 === 
    11 CN2 
    12 === 
     8by documentation of the common framework of classes that enable customization 
     9of the rule induction process. 
     10 
     11============================ 
     12CN2 Rule Induction Algorithm 
     13============================ 
    1314 
    1415.. index:: CN2 
     
    2021the required behaviour. 
    2122 
    22 Usage 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) 
    33  
     23Usage is consistent with typical learner usage in Orange: 
     24 
     25.. literalinclude:: code/rules-cn2.py 
     26    :lines: 7- 
     27 
     28This is the resulting printout:: 
     29     
     30    IF sex=['female'] AND status=['first'] AND age=['child'] THEN survived=yes<0.000, 1.000> 
     31    IF sex=['female'] AND status=['second'] AND age=['child'] THEN survived=yes<0.000, 13.000> 
     32    IF sex=['male'] AND status=['second'] AND age=['child'] THEN survived=yes<0.000, 11.000> 
     33    IF sex=['female'] AND status=['first'] THEN survived=yes<4.000, 140.000> 
     34    IF status=['first'] AND age=['child'] THEN survived=yes<0.000, 5.000> 
     35    IF sex=['male'] AND status=['second'] THEN survived=no<154.000, 14.000> 
     36    IF status=['crew'] AND sex=['female'] THEN survived=yes<3.000, 20.000> 
     37    IF status=['second'] THEN survived=yes<13.000, 80.000> 
     38    IF status=['third'] AND sex=['male'] AND age=['adult'] THEN survived=no<387.000, 75.000> 
     39    IF status=['crew'] THEN survived=no<670.000, 192.000> 
     40    IF age=['child'] AND sex=['male'] THEN survived=no<35.000, 13.000> 
     41    IF sex=['male'] THEN survived=no<118.000, 57.000> 
     42    IF age=['child'] THEN survived=no<17.000, 14.000> 
     43    IF TRUE THEN survived=no<89.000, 76.000> 
     44     
    3445.. autoclass:: Orange.classification.rules.CN2Learner 
    3546   :members: 
     
    5465    
    5566.. autoclass:: Orange.classification.rules.CN2SDUnorderedLearner 
     67   :members: 
     68   :show-inheritance: 
     69    
     70.. autoclass:: Orange.classification.rules.CN2EVCUnorderedLearner 
    5671   :members: 
    5772   :show-inheritance: 
     
    7186  of Machine Learning Research 5: 153-188, 2004. 
    7287 
    73 =========== 
    74 All the rest ... 
    75 =========== 
     88================= 
     89General Framework 
     90================= 
     91 
     92A general framework of classes supports the described CN2 implementation, and 
     93can in fact be fine-tuned to specific needs by replacing individual components. 
     94Here is a simple example, while a detailed architecture can be observed 
     95in description of classes that follows it: 
     96 
     97.. literalinclude:: code/rules-customized.py 
     98    :lines: 7-17 
     99 
     100In the example, the rule evaluation function was set to an m-estimate of 
     101probability with m=50. The result is:: 
     102 
     103    IF sex=['male'] AND status=['second'] AND age=['adult'] THEN survived=no<154.000, 14.000> 
     104    IF sex=['male'] AND status=['third'] AND age=['adult'] THEN survived=no<387.000, 75.000> 
     105    IF sex=['female'] AND status=['first'] THEN survived=yes<4.000, 141.000> 
     106    IF status=['crew'] AND sex=['male'] THEN survived=no<670.000, 192.000> 
     107    IF status=['second'] THEN survived=yes<13.000, 104.000> 
     108    IF status=['third'] AND sex=['male'] THEN survived=no<35.000, 13.000> 
     109    IF status=['first'] AND age=['adult'] THEN survived=no<118.000, 57.000> 
     110    IF status=['crew'] THEN survived=yes<3.000, 20.000> 
     111    IF sex=['female'] THEN survived=no<106.000, 90.000> 
     112    IF TRUE THEN survived=yes<0.000, 5.000> 
     113 
     114Notice that we first need to set the ruleFinder 
     115component, because the default components are not constructed when the learner 
     116is constructed, but only when we run it on data. At that time, the algorithm 
     117checks which components are necessary and sets defaults. Similarly, when the 
     118learner finishes, it destructs all default components. Continuing with our 
     119example, assume that we wish to set a different validation function and a 
     120different bean width. This is simply written as: 
     121 
     122.. literalinclude:: code/rules-customized.py 
     123    :lines: 19-23 
     124 
     125 
     126.. py:class:: Orange.classification.rules.Rule(filter, classifier, lr, dist, ce, w = 0, qu = -1) 
     127    
     128   Base class for presentation of a single induced rule. 
     129    
     130   Parameters, that can be passed to the constructor, correspond to the first 
     131   7 attributes. All attributes are: 
     132    
     133   .. attribute:: filter 
     134    
     135      contents of the rule; this is the basis of the Rule class. Must be of 
     136      type :class:`Orange.core.Filter`; an instance of 
     137      :class:`Orange.core.Filter_values` is set as a default. 
     138    
     139   .. attribute:: classifier 
     140       
     141      each rule can be used as a classical Orange like 
     142      classifier. Must be of type :class:`Orange.classification.Classifier`. By default, 
     143      an instance of :class:`Orange.core.DefaultClassifier` is used. 
     144    
     145   .. attribute:: learner 
     146       
     147      learner to be used for making a classifier. Must be of type 
     148      :class:`Orange.core.learner`. By default, 
     149      :class:`Orange.core.MajorityLearner` is used. 
     150    
     151   .. attribute:: classDistribution 
     152       
     153      distribution of class in data instances covered by this rule 
     154      (:class:`Orange.core.Distribution`). 
     155    
     156   .. attribute:: examples 
     157       
     158      data instances covered by this rule (:class:`Orange.data.Table`). 
     159    
     160   .. attribute:: weightID 
     161    
     162      ID of the weight meta-attribute for the stored data instances (int). 
     163    
     164   .. attribute:: quality 
     165       
     166      quality of the rule. Rules with higher quality are better (float). 
     167    
     168   .. attribute:: complexity 
     169    
     170      complexity of the rule (float). Complexity is used for 
     171      selecting between rules with equal quality, where rules with lower 
     172      complexity are preferred. Typically, complexity corresponds to the 
     173      number of selectors in rule (actually to number of conditions in filter), 
     174      but, obviously, any other measure can be applied. 
     175    
     176   .. method:: filterAndStore(instances, weightID=0, targetClass=-1) 
     177    
     178      Filter passed data instances and store them in the attribute 'examples'. 
     179      Also, compute classDistribution, set weight of stored examples and create 
     180      a new classifier using 'learner' attribute. 
     181       
     182      :param weightID: ID of the weight meta-attribute. 
     183      :type weightID: int 
     184      :param targetClass: index of target class; -1 for all. 
     185      :type targetClass: int 
     186    
     187   Objects of this class can be invoked: 
     188 
     189   .. method:: __call__(instance, instances, weightID=0, targetClass=-1) 
     190    
     191      There are two ways of invoking this method. One way is only passing the 
     192      data instance; then the Rule object returns True if the given instance is 
     193      covered by the rule's filter. 
     194       
     195      :param instance: data instance. 
     196      :type instance: :class:`Orange.data.Instance` 
     197       
     198      Another way of invocation is passing a table of data instances, 
     199      in which case a table of instances, covered by this rule, is returned. 
     200       
     201      :param instances: a table of data instances. 
     202      :type instances: :class:`Orange.data.Table` 
     203      :param ref: TODO 
     204      :type ref: bool 
     205      :param negate: if set to True, the result is inverted: the resulting 
     206          table contains instances *not* covered by the rule. 
     207      :type negate: bool 
     208 
     209.. py:class:: Orange.classification.rules.RuleLearner(storeInstances = true, targetClass = -1, baseRules = Orange.classification.rules.RuleList()) 
     210    
     211   Bases: :class:`Orange.core.Learner` 
     212    
     213   A base rule induction learner. The algorithm follows separate-and-conquer 
     214   strategy, which has its origins in the AQ family of algorithms 
     215   (Fuernkranz J.; Separate-and-Conquer Rule Learning, Artificial Intelligence 
     216   Review 13, 3-54, 1999). Basically, such algorithms search for the "best" 
     217   possible rule in learning instancess, remove covered data from learning 
     218   instances (separate) and repeat the process (conquer) on the remaining 
     219   instances. 
     220    
     221   The class' functionality can be best explained by showing its __call__ 
     222   function:: 
     223    
     224        def __call__(self, instances, weightID=0): 
     225            ruleList = Orange.classification.rules.RuleList() 
     226            allInstances = Orange.data.Table(instances) 
     227            while not self.dataStopping(instances, weightID, self.targetClass): 
     228                newRule = self.ruleFinder(instances, weightID, self.targetClass, 
     229                                          self.baseRules) 
     230                if self.ruleStopping(ruleList, newRule, instances, weightID): 
     231                    break 
     232                instances, weightID = self.coverAndRemove(newRule, instances, 
     233                                                        weightID, self.targetClass) 
     234                ruleList.append(newRule) 
     235            return Orange.classification.rules..RuleClassifier_FirstRule( 
     236                rules=ruleList, instances=allInstances) 
     237                 
     238   The four customizable components here are the invoked dataStopping, 
     239   ruleFinder, coverAndRemove and ruleStopping objects. By default, components 
     240   of the original CN2 algorithm will be used, but this can be changed by 
     241   modifying those attributes: 
     242    
     243   .. attribute:: dataStopping 
     244    
     245      an object of class 
     246      :class:`Orange.classification.rules.RuleDataStoppingCriteria` 
     247      that determines whether there will be any benefit from further learning 
     248      (ie. if there is enough data to continue learning). The default 
     249      implementation 
     250      (:class:`Orange.classification.rules.RuleDataStoppingCriteria_NoPositives`) 
     251      returns True if there are no more instances of given class.  
     252    
     253   .. attribute:: ruleStopping 
     254       
     255      an object of class  
     256      :class:`Orange.classification.rules.RuleStoppingCriteria` 
     257      that decides from the last rule learned if it is worthwhile to use the 
     258      rule and learn more rules. By default, no rule stopping criteria is 
     259      used (ruleStopping==None), thus accepting all rules. 
     260        
     261   .. attribute:: coverAndRemove 
     262        
     263      an object of class 
     264      :class:`Orange.classification.rules.RuleCovererAndRemover` 
     265      that removes instances covered by the rule and returns remaining 
     266      instances. The default implementation 
     267      (:class:`Orange.classification.rules.RuleCovererAndRemover_Default`) 
     268      only removes the instances that belong to given target class, except if 
     269      it is not given (ie. targetClass==-1). 
     270     
     271   .. attribute:: ruleFinder 
     272       
     273      an object of class 
     274      :class:`Orange.classification.rules.RuleFinder` that learns a single 
     275      rule from instances. Default implementation is 
     276      :class:`Orange.classification.rules.RuleBeamFinder`. 
     277 
     278   Constructor can be given the following parameters: 
     279     
     280   :param storeInstances: if set to True, the rules will have data instances 
     281       stored. 
     282   :type storeInstances: bool 
     283     
     284   :param targetClass: index of a specific class being learned; -1 for all. 
     285   :type targetClass: int 
     286    
     287   :param baseRules: Rules that we would like to use in ruleFinder to 
     288       constrain the learning space. If not set, it will be set to a set 
     289       containing only an empty rule. 
     290   :type baseRules: :class:`Orange.classification.rules.RuleList` 
     291 
     292Rule Finders 
     293------------ 
     294 
     295.. class:: Orange.classification.rules.RuleFinder 
     296 
     297   Base class for all rule finders. These are used to learn a single rule from 
     298   instances. 
     299    
     300   Rule finders are invokable in the following manner: 
     301    
     302   .. method:: __call__(table, weightID, targetClass, baseRules) 
     303    
     304      Return a new rule, induced from instances in the given table. 
     305       
     306      :param table: data instances to learn from. 
     307      :type table: :class:`Orange.data.Table` 
     308       
     309      :param weightID: ID of the weight meta-attribute for the stored data 
     310          instances. 
     311      :type weightID: int 
     312       
     313      :param targetClass: index of a specific class being learned; -1 for all. 
     314      :type targetClass: int  
     315       
     316      :param baseRules: Rules that we would like to use in ruleFinder to 
     317          constrain the learning space. If not set, it will be set to a set 
     318          containing only an empty rule. 
     319      :type baseRules: :class:`Orange.classification.rules.RuleList` 
     320 
     321.. class:: Orange.classification.rules.RuleBeamFinder 
     322    
     323   Bases: :class:`Orange.classification.rules.RuleFinder` 
     324    
     325   Beam search for the best rule. This is the default class used in RuleLearner 
     326   to find the best rule. Pseudo code of the algorithm is shown here: 
     327 
     328   .. parsed-literal:: 
     329 
     330      def \_\_call\_\_(self, table, weightID, targetClass, baseRules): 
     331          prior = orange.Distribution(table.domain.classVar, table, weightID) 
     332          rulesStar, bestRule = self.\ **initializer**\ (table, weightID, targetClass, baseRules, self.evaluator, prior) 
     333          \# compute quality of rules in rulesStar and bestRule 
     334          ... 
     335          while len(rulesStar) \> 0: 
     336              candidates, rulesStar = self.\ **candidateSelector**\ (rulesStar, table, weightID) 
     337              for cand in candidates: 
     338                  newRules = self.\ **refiner**\ (cand, table, weightID, targetClass) 
     339                  for newRule in newRules: 
     340                      if self.\ **ruleStoppingValidator**\ (newRule, table, weightID, targetClass, cand.classDistribution): 
     341                          newRule.quality = self.\ **evaluator**\ (newRule, table, weightID, targetClass, prior) 
     342                          rulesStar.append(newRule) 
     343                          if self.\ **validator**\ (newRule, table, weightID, targetClass, prior) and 
     344                              newRule.quality \> bestRule.quality: 
     345                              bestRule = newRule 
     346              rulesStar = self.\ **ruleFilter**\ (rulesStar, table, weightID) 
     347          return bestRule 
     348 
     349   Bolded in the pseudo-code are several exchangeable components, exposed as 
     350   attributes. These are: 
     351 
     352   .. attribute:: initializer 
     353    
     354      an object of class 
     355      :class:`Orange.classification.rules.RuleBeamInitializer` 
     356      used to initialize rulesStar and for selecting the 
     357      initial best rule. By default 
     358      (:class:`Orange.classification.rules.RuleBeamInitializer_Default`), 
     359      baseRules are returned as starting rulesSet and the best from baseRules 
     360      is set as bestRule. If baseRules are not set, this class will return 
     361      rulesStar with rule that covers all instances (has no selectors) and 
     362      this rule will be also used as bestRule. 
     363    
     364   .. attribute:: candidateSelector 
     365    
     366      an object of class 
     367      :class:`Orange.classification.rules.RuleBeamCandidateSelector` 
     368      used to separate a subset from the current 
     369      rulesStar and return it. These rules will be used in the next 
     370      specification step. Default component (an instance of 
     371      :class:`Orange.classification.rules.RuleBeamCandidateSelector_TakeAll`) 
     372      takes all rules in rulesStar 
     373     
     374   .. attribute:: refiner 
     375    
     376      an object of class 
     377      :class:`Orange.classification.rules.RuleBeamRefiner` 
     378      used to refine given rule. New rule should cover a 
     379      strict subset of examples covered by given rule. Default component 
     380      (:class:`Orange.classification.rules.RuleBeamRefiner_Selector`) adds 
     381      a conjunctive selector to selectors present in the rule. 
     382     
     383   .. attribute:: ruleFilter 
     384    
     385      an object of class 
     386      :class:`Orange.classification.rules.RuleBeamFilter` 
     387      used to filter rules to keep beam relatively small 
     388      to contain search complexity. By default, it takes five best rules: 
     389      :class:`Orange.classification.rules.RuleBeamFilter_Width`\ *(m=5)*\ . 
     390 
     391   .. method:: __call__(data, weightID, targetClass, baseRules) 
     392 
     393   Determines the next best rule to cover the remaining data instances. 
     394    
     395   :param data: data instances. 
     396   :type data: :class:`Orange.data.Table` 
     397    
     398   :param weightID: index of the weight meta-attribute. 
     399   :type weightID: int 
     400    
     401   :param targetClass: index of the target class. 
     402   :type targetClass: int 
     403    
     404   :param baseRules: existing rules. 
     405   :type baseRules: :class:`Orange.classification.rules.RuleList` 
     406 
     407Rule Evaluators 
     408--------------- 
     409 
     410.. class:: Orange.classification.rules.RuleEvaluator 
     411 
     412   Base class for rule evaluators that evaluate the quality of the rule based 
     413   on covered data instances. All evaluators support being invoked in the 
     414   following manner: 
     415    
     416   .. method:: __call__(rule, instances, weightID, targetClass, prior) 
     417    
     418      Calculates a non-negative rule quality. 
     419       
     420      :param rule: rule to evaluate. 
     421      :type rule: :class:`Orange.classification.rules.Rule` 
     422       
     423      :param instances: a table of instances, covered by the rule. 
     424      :type instances: :class:`Orange.data.Table` 
     425       
     426      :param weightID: index of the weight meta-attribute. 
     427      :type weightID: int 
     428       
     429      :param targetClass: index of target class of this rule. 
     430      :type targetClass: int 
     431       
     432      :param prior: prior class distribution. 
     433      :type prior: :class:`Orange.core.Distribution` 
     434 
     435.. autoclass:: Orange.classification.rules.LaplaceEvaluator 
     436   :members: 
     437   :show-inheritance: 
     438 
     439.. autoclass:: Orange.classification.rules.WRACCEvaluator 
     440   :members: 
     441   :show-inheritance: 
     442    
     443.. class:: Orange.classification.rules.RuleEvaluator_Entropy 
     444 
     445   Bases: :class:`Orange.classification.rules.RuleEvaluator` 
     446     
     447.. class:: Orange.classification.rules.RuleEvaluator_LRS 
     448 
     449   Bases: :class:`Orange.classification.rules.RuleEvaluator` 
     450 
     451.. class:: Orange.classification.rules.RuleEvaluator_Laplace 
     452 
     453   Bases: :class:`Orange.classification.rules.RuleEvaluator` 
     454 
     455.. class:: Orange.classification.rules.RuleEvaluator_mEVC 
     456 
     457   Bases: :class:`Orange.classification.rules.RuleEvaluator` 
     458    
     459Instance Coverers and Removers 
     460------------------------------ 
     461 
     462.. class:: RuleCovererAndRemover 
     463 
     464   Base class for rule coverers and removers that, when invoked, remove 
     465   instances covered by the rule and return remaining instances. 
     466 
     467.. autoclass:: CovererAndRemover_MultWeights 
     468 
     469.. autoclass:: CovererAndRemover_AddWeights 
     470    
     471Miscellaneous Functions 
     472----------------------- 
     473 
     474.. automethod:: Orange.classification.rules.ruleToString 
     475 
     476   .. method:: __call__(rule, instances, weights, targetClass) 
     477    
     478      Calculates a non-negative rule quality. 
     479       
     480      :param rule: rule to evaluate. 
     481      :type rule: :class:`Orange.classification.rules.Rule` 
     482       
     483      :param instances: a table of instances, covered by the rule. 
     484      :type instances: :class:`Orange.data.Table` 
     485       
     486      :param weights: index of the weight meta-attribute. 
     487      :type weights: int 
     488       
     489      :param targetClass: index of target class of this rule. 
     490      :type targetClass: int 
     491 
     492.. 
     493    Undocumented are: 
     494    Data-based Stopping Criteria 
     495    ---------------------------- 
     496    Rule-based Stopping Criteria 
     497    ---------------------------- 
     498    Rule-based Stopping Criteria 
     499    ---------------------------- 
    76500 
    77501""" 
     
    84508    RuleClassifier_logit, \ 
    85509    RuleLearner, \ 
     510    Rule, \ 
    86511    RuleBeamCandidateSelector, \ 
    87512    RuleBeamCandidateSelector_TakeAll, \ 
     
    117542 
    118543class LaplaceEvaluator(RuleEvaluator): 
     544    """ 
     545    Laplace's rule of succession. 
     546    """ 
    119547    def __call__(self, rule, data, weightID, targetClass, apriori): 
    120548        if not rule.classDistribution: 
     
    131559 
    132560class WRACCEvaluator(RuleEvaluator): 
     561    """ 
     562    Weighted relative accuracy. 
     563    """ 
    133564    def __call__(self, rule, data, weightID, targetClass, apriori): 
    134565        if not rule.classDistribution: 
     
    150581        else: return (pTruePositive-pClass)/max(pRule,1e-6) 
    151582 
    152 class Rule(Orange.core.Rule): 
    153     """ 
    154     Wrapper around a native Rule class to add print method. 
    155     """ 
    156      
    157     def __init__(self, nativeBayesClassifier): 
    158         self.nativeBayesClassifier = nativeBayesClassifier 
    159         for k, v in self.nativeBayesClassifier.__dict__.items(): 
    160             self.__dict__[k] = v 
    161    
    162     def __call__(self, instance, result_type=Orange.core.Classifier.GetValue, 
    163                  *args, **kwdargs): 
    164         """Classify a new instance 
    165          
    166         :param instance: instance to be classifier 
    167         :type instance: :class:`Orange.data.Instance` 
    168         :param result_type: :class:`Orange.core.Classifier.GetValue` or \ 
    169               :class:`Orange.core.Classifier.GetProbabilities` or 
    170               :class:`Orange.core.Classifier.GetBoth` 
    171          
    172         :rtype: :class:`Orange.data.Value`,  
    173               :class:`Orange.statistics.Distribution` or a tuple with both 
    174         """ 
    175         self.nativeBayesClassifier(instance, *args, **kwdargs) 
    176  
    177     def __setattr__(self, name, value): 
    178         if name == "nativeBayesClassifier": 
    179             self.__dict__[name] = value 
    180             return 
    181         if name in self.nativeBayesClassifier.__dict__: 
    182             self.nativeBayesClassifier.__dict__[name] = value 
    183         self.__dict__[name] = value 
    184      
    185     def p(self, class_, instance): 
    186         """Return probability of single class 
    187         Probability is not normalized and can be different from probability 
    188         returned from __call__ 
    189          
    190         :param class_: 
    191         :type class_: 
    192         :param instance: instance to be classified 
    193         :type instance: :class:`Orange.data.Instance` 
    194          
    195         """ 
    196         return self.nativeBayesClassifier.p(class_, instance) 
    197      
    198     def printModel(self): 
    199         """Print classificator in human friendly format""" 
    200         nValues=len(self.classVar.values) 
    201         frmtStr=' %10.3f'*nValues 
    202         classes=" "*20+ ((' %10s'*nValues) % tuple([i[:10] for i in self.classVar.values])) 
    203         print classes 
    204         print "class probabilities "+(frmtStr % tuple(self.distribution)) 
    205         print 
    206      
    207         for i in self.conditionalDistributions: 
    208             print "Attribute", i.variable.name 
    209             print classes 
    210             for v in range(len(i.variable.values)): 
    211                 print ("%20s" % i.variable.values[v][:20]) + (frmtStr % tuple(i[v])) 
    212             print 
     583 
     584class MEstimateEvaluator(RuleEvaluator): 
     585    """ 
     586    Rule evaluator using m-estimate of probability rule evaluation function. 
     587     
     588    :param m: m-value for m-estimate 
     589    :type m: int 
     590     
     591    """ 
     592    def __init__(self, m=2): 
     593        self.m = m 
     594    def __call__(self, rule, data, weightID, targetClass, apriori): 
     595        if not rule.classDistribution: 
     596            return 0. 
     597        sumDist = rule.classDistribution.abs 
     598        if self.m == 0 and not sumDist: 
     599            return 0. 
     600        # get distribution 
     601        if targetClass>-1: 
     602            p = rule.classDistribution[targetClass]+self.m*apriori[targetClass]/apriori.abs 
     603            p = p / (rule.classDistribution.abs + self.m) 
     604        else: 
     605            p = max(rule.classDistribution)+self.m*apriori[rule.classDistribution.modus()]/apriori.abs 
     606            p = p / (rule.classDistribution.abs + self.m)       
     607        return p 
     608 
    213609 
    214610class CN2Learner(RuleLearner): 
     
    232628        ratio statistics is used that gives an estimate if rule is 
    233629        statistically better than the default rule. 
    234     :type alpha: number 
     630    :type alpha: float 
    235631 
    236632    """ 
     
    268664        :param instance: instance to be classifier 
    269665        :type instance: :class:`Orange.data.Instance` 
    270         :param result_type: :class:`Orange.core.Classifier.GetValue` or \ 
    271               :class:`Orange.core.Classifier.GetProbabilities` or 
    272               :class:`Orange.core.Classifier.GetBoth` 
     666        :param result_type: :class:`Orange.classification.Classifier.GetValue` or \ 
     667              :class:`Orange.classification.Classifier.GetProbabilities` or 
     668              :class:`Orange.classification.Classifier.GetBoth` 
    273669         
    274670        :rtype: :class:`Orange.data.Value`,  
     
    285681            self.prior = Orange.core.Distribution(instances.domain.classVar, instances) 
    286682 
    287     def __call__(self, instance, result_type=Orange.core.Classifier.GetValue): 
     683    def __call__(self, instance, result_type=Orange.classification.Classifier.GetValue): 
    288684        classifier = None 
    289685        for r in self.rules: 
     
    297693            classifier.defaultDistribution = self.prior 
    298694 
    299         if result_type == Orange.core.Classifier.GetValue: 
     695        if result_type == Orange.classification.Classifier.GetValue: 
    300696          return classifier(instance) 
    301         if result_type == Orange.core.Classifier.GetProbabilities: 
     697        if result_type == Orange.classification.Classifier.GetProbabilities: 
    302698          return classifier.defaultDistribution 
    303699        return (classifier(instance),classifier.defaultDistribution) 
     
    332728        ratio statistics is used that gives an estimate if rule is 
    333729        statistically better than the default rule. 
    334     :type alpha: number 
     730    :type alpha: float 
    335731    """ 
    336732    def __new__(cls, instances=None, weightID=0, **kwargs): 
     
    385781        :param instance: instance to be classifier 
    386782        :type instance: :class:`Orange.data.Instance` 
    387         :param result_type: :class:`Orange.core.Classifier.GetValue` or \ 
    388               :class:`Orange.core.Classifier.GetProbabilities` or 
    389               :class:`Orange.core.Classifier.GetBoth` 
     783        :param result_type: :class:`Orange.classification.Classifier.GetValue` or \ 
     784              :class:`Orange.classification.Classifier.GetProbabilities` or 
     785              :class:`Orange.classification.Classifier.GetBoth` 
    390786         
    391787        :rtype: :class:`Orange.data.Value`,  
     
    431827         
    432828        if retRules: 
    433             if result_type == Orange.core.Classifier.GetValue: 
     829            if result_type == Orange.classification.Classifier.GetValue: 
    434830              return (retDist.modus(), covRules) 
    435             if result_type == Orange.core.Classifier.GetProbabilities: 
     831            if result_type == Orange.classification.Classifier.GetProbabilities: 
    436832              return (retDist, covRules) 
    437833            return (retDist.modus(),retDist,covRules) 
    438         if result_type == Orange.core.Classifier.GetValue: 
     834        if result_type == Orange.classification.Classifier.GetValue: 
    439835          return retDist.modus() 
    440         if result_type == Orange.core.Classifier.GetProbabilities: 
     836        if result_type == Orange.classification.Classifier.GetProbabilities: 
    441837          return retDist 
    442838        return (retDist.modus(),retDist) 
     
    457853    :class:`Orange.classifier.rules.WRACCEvaluator` is used to implement 
    458854    weight-relative accuracy and  
    459     :class:`Orange.classifier.rules.CovererAndRemover_multWeight` avoids 
     855    :class:`Orange.classifier.rules.CovererAndRemover_MultWeight` avoids 
    460856    excluding covered instances, multiplying their weight by the value of 
    461857    mult parameter instead. 
     
    475871        ratio statistics is used that gives an estimate if rule is 
    476872        statistically better than the default rule. 
    477     :type alpha: number 
     873    :type alpha: float 
    478874    :param mult: multiplicator for weights of covered instances. 
    479     :type mult: number 
     875    :type mult: float 
    480876    """ 
    481877    def __new__(cls, instances=None, weightID=0, **kwargs): 
     
    502898 
    503899 
    504 class MEstimate(RuleEvaluator): 
    505     """ 
    506     Rule evaluator using m-estimate of probability rule evaluation function. 
    507     :param m: m-value for m-estimate 
    508     :type m: integer 
    509      
    510     """ 
    511     def __init__(self, m=2): 
    512         self.m = m 
    513     def __call__(self, rule, data, weightID, targetClass, apriori): 
    514         if not rule.classDistribution: 
    515             return 0. 
    516         sumDist = rule.classDistribution.abs 
    517         if self.m == 0 and not sumDist: 
    518             return 0. 
    519         # get distribution 
    520         if targetClass>-1: 
    521             p = rule.classDistribution[targetClass]+self.m*apriori[targetClass]/apriori.abs 
    522             p = p / (rule.classDistribution.abs + self.m) 
    523         else: 
    524             p = max(rule.classDistribution)+self.m*apriori[rule.classDistribution.modus()]/apriori.abs 
    525             p = p / (rule.classDistribution.abs + self.m)       
    526         return p 
     900class CN2EVCUnorderedLearner(ABCN2): 
     901    """ 
     902    CN2 + EVC as evaluation + LRC classification. 
     903    """ 
     904    def __init__(self, width=5, nsampling=100, rule_sig=1.0, att_sig=1.0, min_coverage = 1., max_rule_complexity = 5.): 
     905        ABCN2.__init__(self, width=width, nsampling=nsampling, rule_sig=rule_sig, att_sig=att_sig, 
     906                       min_coverage=int(min_coverage), max_rule_complexity = int(max_rule_complexity)) 
     907 
    527908 
    528909class RuleStopping_Apriori(RuleStoppingCriteria): 
     
    541922        return True 
    542923 
     924 
     925class RuleStopping_SetRules(RuleStoppingCriteria): 
     926    def __init__(self,validator): 
     927        self.ruleStopping = RuleStoppingCriteria_NegativeDistribution() 
     928        self.validator = validator 
     929 
     930    def __call__(self,rules,rule,instances,data):         
     931        ru_st = self.ruleStopping(rules,rule,instances,data) 
     932        if not ru_st: 
     933            self.validator.rules.append(rule) 
     934        return bool(ru_st) 
     935 
     936 
    543937class LengthValidator(RuleValidator): 
    544938    """ prune rules with more conditions than self.length. """ 
     
    562956        return bool(self.validator(rule,data,weightID,targetClass,apriori)) 
    563957                 
    564 class RuleStopping_SetRules(RuleStoppingCriteria): 
    565     def __init__(self,validator): 
    566         self.ruleStopping = RuleStoppingCriteria_NegativeDistribution() 
    567         self.validator = validator 
    568  
    569     def __call__(self,rules,rule,instances,data):         
    570         ru_st = self.ruleStopping(rules,rule,instances,data) 
    571         if not ru_st: 
    572             self.validator.rules.append(rule) 
    573         return bool(ru_st) 
    574958 
    575959 
     
    582966        self.prior = Orange.core.Distribution(instances.domain.classVar, instances) 
    583967 
    584     def __call__(self, instance, result_type=Orange.core.Classifier.GetValue): 
     968    def __call__(self, instance, result_type=Orange.classification.Classifier.GetValue): 
    585969        retDist = Orange.core.Distribution(instance.domain.classVar) 
    586970        bestRule = None 
     
    601985            retDist.normalize() 
    602986        # return classifier(instance, result_type=result_type) 
    603         if result_type == Orange.core.Classifier.GetValue: 
     987        if result_type == Orange.classification.Classifier.GetValue: 
    604988          return retDist.modus() 
    605         if result_type == Orange.core.Classifier.GetProbabilities: 
     989        if result_type == Orange.classification.Classifier.GetProbabilities: 
    606990          return retDist 
    607991        return (retDist.modus(),retDist) 
     
    613997        return retStr     
    614998 
     999 
    6151000class CovererAndRemover_MultWeights(RuleCovererAndRemover): 
     1001    """ 
     1002    Covering and removing of instances using weight multiplication. 
     1003    :param mult: weighting multiplication factor 
     1004    :type mult: float 
     1005     
     1006    """ 
     1007     
    6161008    def __init__(self, mult = 0.7): 
    6171009        self.mult = mult 
     
    6251017        instances.domain.addmeta(newWeightsID, Orange.data.feature.Continuous("weights-"+str(newWeightsID)), True) 
    6261018        for instance in instances: 
    627             if rule(instance) and instance.getclass() == rule.classifier(instance,Orange.core.Classifier.GetValue): 
     1019            if rule(instance) and instance.getclass() == rule.classifier(instance,Orange.classification.Classifier.GetValue): 
    6281020                instance[newWeightsID]=instance[weights]*self.mult 
    6291021            else: 
     
    6311023        return (instances,newWeightsID) 
    6321024 
     1025 
    6331026class CovererAndRemover_AddWeights(RuleCovererAndRemover): 
     1027    """ 
     1028    Covering and removing of instances using weight addition. 
     1029     
     1030    """ 
     1031     
    6341032    def __call__(self, rule, instances, weights, targetClass): 
    6351033        if not weights: 
     
    6471045        instances.domain.addmeta(newWeightsID, Orange.data.feature.Continuous("weights-"+str(newWeightsID)), True) 
    6481046        for instance in instances: 
    649             if rule(instance) and instance.getclass() == rule.classifier(instance,Orange.core.Classifier.GetValue): 
     1047            if rule(instance) and instance.getclass() == rule.classifier(instance,Orange.classification.Classifier.GetValue): 
    6501048                try: 
    6511049                    instance[coverage]+=1.0 
     
    7101108 
    7111109 
    712 class CN2EVCUnorderedLearner(ABCN2): 
    713     """This is implementation of CN2 + EVC as evaluation + LRC classification. 
    714         Main parameters: 
    715           -- ... 
    716     """ 
    717     def __init__(self, width=5, nsampling=100, rule_sig=1.0, att_sig=1.0, min_coverage = 1., max_rule_complexity = 5.): 
    718         ABCN2.__init__(self, width=width, nsampling=nsampling, rule_sig=rule_sig, att_sig=att_sig, 
    719                        min_coverage=int(min_coverage), max_rule_complexity = int(max_rule_complexity)) 
    720  
    7211110def ruleToString(rule, showDistribution = True): 
    7221111    """ 
    7231112    Write a string presentation of rule in human readable format. 
     1113     
    7241114    :param rule: rule to pretty-print. 
    725     :type rule: :class:`Orange.classification.rules.Rules` 
     1115    :type rule: :class:`Orange.classification.rules.Rule` 
     1116     
    7261117    :param showDistribution: determines whether presentation should also 
    7271118        contain the distribution of covered instances 
    728     :type showDistribution: boolean 
     1119    :type showDistribution: bool 
    7291120     
    7301121    """ 
     
    7741165        raise Exception("CN2 requires a discrete class!") 
    7751166     
    776  
    777  
    778  
    779  
    7801167 
    7811168def rule_in_set(rule,rules): 
     
    8081195    return True 
    8091196 
    810  
    8111197# Miscellaneous - utility functions 
    8121198def avg(l): 
     
    8591245    return mi, beta, percs 
    8601246 
    861  
    8621247def computeDists(data, weight=0, targetClass=0, N=100, learner=None): 
    8631248    """ Compute distributions of likelihood ratio statistics of extreme (best) rules.  """ 
     
    8981283        l.append(Orange.core.EVDist(mu=el[0],beta=el[1],percentiles=el[2])) 
    8991284    return l 
    900  
    901  
    902  
    9031285 
    9041286def add_sub_rules(rules, instances, weight, learner, dists): 
Note: See TracChangeset for help on using the changeset viewer.