Changeset 7534:287405691042 in orange


Ignore:
Timestamp:
02/04/11 21:14:41 (3 years ago)
Author:
matija <matija.polajnar@…>
Branch:
default
Convert:
0d225967ebec449806ed88da5d1b4533519b17e8
Message:

Corrections to classification.bayes documentation.

Location:
orange
Files:
2 edited

Legend:

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

    r7498 r7534  
    11"""  
    2   index:: naive Bayes classifier 
     2.. index:: naive Bayes classifier 
    33    
    44.. index::  
    55   single: classification; naive Bayes classifier 
    66 
    7 ====================== 
     7********************** 
    88Naive Bayes Classifier 
    9 ====================== 
     9********************** 
    1010 
    1111The most primitive bayesian classifier is :obj:`NaiveLearner`  
    1212(http://en.wikipedia.org/wiki/Naive_Bayes_classifier). 
    1313The class estimates conditional probabilities from training data and uses them 
    14 for classification of new examples.  
     14for classification of new data instances.  
    1515 
    1616Example (`bayes-run.py`_, uses `iris.tab`_) 
     
    3333m-estimate. 
    3434 
    35 Example (`bayes-mestimate.py`_, uses `iris.tab`_) 
     35Example (`bayes-mestimate.py`_, uses `lenses.tab`_) 
    3636 
    3737.. literalinclude:: code/bayes-mestimate.py 
    3838    :lines: 7- 
    3939 
    40 Observing probabilities shows a shift towards the second class - 
    41 as compared to probabilities above, where relative frequencies were used. 
    42 Note that the change in error estimation did not have any effect on apriori 
     40Observing conditional probabilities in an m-estimate based classifier shows a 
     41shift towards the second class - as compared to probabilities above, where 
     42relative frequencies were used. Note that the change in error estimation did 
     43not have any effect on apriori 
    4344probabilities. 
    4445 
     
    4849    :lines: 7- 
    4950     
    50 Setting adjustThreshold paramater can sometimes improve the results. :: 
     51Setting adjustThreshold parameter can sometimes improve the results. Those are 
     52the classification accuracies of 10-fold cross-validation of a normal naive 
     53bayesian classifier, and one with an adjusted threshold:: 
    5154 
    5255    [0.7901746265516516, 0.8280138859667578] 
     
    5760.. _adult-sample.tab: code/adult-sample.tab 
    5861.. _iris.tab: code/iris.tab 
    59  
    60 ====================== 
     62.. _lenses.tab: code/lenses.tab 
     63 
    6164Implementation Details 
    6265====================== 
    6366 
     67The following two classes are implemented in C++ (*bayes.cpp*). They are not 
     68intended to be used directly. Here we give implementation details for those 
     69interested. 
     70 
    6471Orange.core.BayesLearner 
    65 ======================== 
    66 The first three fields are empty (None) by default. 
     72------------------------ 
     73Fields estimatorConstructor, conditionalEstimatorConstructor and 
     74conditionalEstimatorConstructorContinuous are empty (None) by default. 
    6775 
    6876If estimatorConstructor is left undefined, p(C) will be estimated by relative 
     
    7078When conditionalEstimatorConstructor is left undefined, it will use the same 
    7179constructor as for estimating unconditional probabilities (estimatorConstructor 
    72 is used as an estimator in (ConditionalProbabilityEstimatorConstructor_ByRows). 
     80is used as an estimator in ConditionalProbabilityEstimatorConstructor_ByRows). 
    7381That is, by default, both will use relative frequencies. But when 
    7482estimatorConstructor is set to, for instance, estimate probabilities by 
    75 m-estimate with m=2.0, m-estimates with m=2.0 will be used for estimation of 
     83m-estimate with m=2.0, the same estimator will be used for estimation of 
    7684conditional probabilities, too. 
    77 P(c|vi) for continuous attributes are, by default estimated with loess (a 
     85P(c|vi) for continuous attributes are, by default, estimated with loess (a 
    7886variant of locally weighted linear regression), using 
    7987ConditionalProbabilityEstimatorConstructor_loess. 
     
    96104 
    97105Orange.core.BayesClassifier 
    98 =========================== 
    99 Class NaiveClassifier represents a naive Bayesian classifier. Probability of 
     106--------------------------- 
     107Class NaiveClassifier represents a naive bayesian classifier. Probability of 
    100108class C, knowing that values of features :math:`F_1, F_2, ..., F_n` are 
    101109:math:`v_1, v_2, ..., v_n`, is computed as :math:`p(C|v_1, v_2, ..., v_n) = \ 
     
    110118m-estimate. In this case, the above formula is much more appropriate.  
    111119 
    112 When computing the formula, probabilities p(C) are read from distribution which 
    113 is of type Distribution and stores a (normalized) probability of each class. 
     120When computing the formula, probabilities p(C) are read from distribution, which 
     121is of type Distribution, and stores a (normalized) probability of each class. 
    114122When distribution is None, BayesClassifier calls estimator to assess the 
    115123probability. The former method is faster and is actually used by all existing 
     
    132140conditionalEstimators) can be None. For instance, BayesLearner normally 
    133141constructs a classifier which has either distribution or estimator defined. 
    134 While it is not an error, to have both, only distribution will be used in that 
     142While it is not an error to have both, only distribution will be used in that 
    135143case. As for the other two fields, they can be both defined and used 
    136144complementarily; the elements which are missing in one are defined in the 
     
    156164    is called and the resulting classifier is returned instead of the learner. 
    157165     
    158     :param adjustTreshold: sets the corresponding attribute 
    159     :type adjustTreshold: boolean 
    160     :param m: sets the estimatorConstructor to \ 
    161     :class:`orange.ProbabilityEstimatorConstructor_m` with specified m  
    162     :type m: integer 
    163     :param estimatorConstructor: sets the corresponding attribute 
    164     :type estimatorConstructor: orange.ProbabilityEstimatorConstructor 
    165     :param conditionalEstimatorConstructor: sets the corresponding attribute 
    166     :type conditionalEstimatorConstructor: 
    167             :class:`orange.ConditionalProbabilityEstimatorConstructor` 
    168     :param conditionalEstimatorConstructorContinuous: sets the corresponding 
    169             attribute 
    170     :type conditionalEstimatorConstructorContinuous:  
    171             :class:`orange.ConditionalProbabilityEstimatorConstructor` 
    172     :rtype: :class:`Orange.classification.bayes.NaiveBayesLearner` or 
    173             :class:`Orange.classification.bayes.NaiveBayesClassifier`  
    174      
    175     All attributes can also be set as constructor parameters. 
     166    .. 
     167        :param adjustTreshold: sets the corresponding attribute 
     168        :type adjustTreshold: boolean 
     169        :param m: sets the :obj:`estimatorConstructor` to 
     170            :class:`orange.ProbabilityEstimatorConstructor_m` with specified m 
     171        :type m: integer 
     172        :param estimatorConstructor: sets the corresponding attribute 
     173        :type estimatorConstructor: orange.ProbabilityEstimatorConstructor 
     174        :param conditionalEstimatorConstructor: sets the corresponding attribute 
     175        :type conditionalEstimatorConstructor: 
     176                :class:`orange.ConditionalProbabilityEstimatorConstructor` 
     177        :param conditionalEstimatorConstructorContinuous: sets the corresponding 
     178                attribute 
     179        :type conditionalEstimatorConstructorContinuous:  
     180                :class:`orange.ConditionalProbabilityEstimatorConstructor` 
     181                 
     182    :rtype: :class:`Orange.classification.bayes.NaiveLearner` or 
     183            :class:`Orange.classification.bayes.NaiveClassifier` 
     184             
     185    Constructor parameters set the corresponding attributes. 
    176186     
    177187    .. attribute:: adjustTreshold 
     
    186196     
    187197        m for m-estimate. If set, m-estimation of probabilities 
    188         will be used using :class:`orange.ProbabilityEstimatorConstructor_m` 
     198        will be used using :class:`orange.ProbabilityEstimatorConstructor_m`. 
    189199        This attribute is ignored if you also set estimatorConstructor. 
    190200         
     
    193203        Probability estimator constructor for 
    194204        prior class probabilities. Defaults to 
    195         :class:`orange.ProbabilityEstimatorConstructor_relative` 
     205        :class:`orange.ProbabilityEstimatorConstructor_relative`. 
    196206        Setting this attribute disables the above described attribute m. 
    197207         
     
    206216        Probability estimator constructor for conditional probabilities for 
    207217        continuous features. Defaults to  
    208         :class:`orange.ConditionalProbabilityEstimatorConstructor_loess`  
     218        :class:`orange.ConditionalProbabilityEstimatorConstructor_loess`. 
    209219    """ 
    210220     
     
    280290    .. attribute:: conditionalEstimators 
    281291     
    282         A list of estimators for conditional probabilities 
     292        A list of estimators for conditional probabilities. 
    283293         
    284294    .. attribute:: adjustThreshold 
     
    298308    def __call__(self, instance, result_type=Orange.classification.Classifier.GetValue, 
    299309                 *args, **kwdargs): 
    300         """Classify a new instance 
    301          
    302         :param instance: instance to be classifier 
     310        """Classify a new instance. 
     311         
     312        :param instance: instance to be classified. 
    303313        :type instance: :class:`Orange.data.Instance` 
    304314        :param result_type: :class:`Orange.classification.Classifier.GetValue` or \ 
     
    320330     
    321331    def p(self, class_, instance): 
    322         """Return probability of single class 
     332        """ 
     333        Return probability of a single class. 
    323334        Probability is not normalized and can be different from probability 
    324         returned from __call__ 
     335        returned from __call__. 
    325336         
    326337        :param class_: class variable for which the probability should be 
    327                 outputed 
     338                output. 
    328339        :type class_: :class:`Orange.data.Variable` 
    329         :param instance: instance to be classified 
     340        :param instance: instance to be classified. 
    330341        :type instance: :class:`Orange.data.Instance` 
    331342         
     
    334345     
    335346    def printModel(self): 
    336         """Print classificator in human friendly format""" 
     347        """Print classifier in human friendly format.""" 
    337348        nValues=len(self.classVar.values) 
    338349        frmtStr=' %10.3f'*nValues 
  • orange/doc/Orange/rst/code/bayes-mestimate.py

    r7477 r7534  
    1515 
    1616print bayes.conditionalDistributions 
    17 """<<'pre-presbyopic': <0.625, 0.125, 0.250>, 'presbyopic': <0.750, 0.125, 0.125>, ...>>""" 
     17# prints: <<'pre-presbyopic': <0.625, 0.125, 0.250>, 'presbyopic': <0.750, 0.125, 0.125>, ...>> 
    1818print bayesWithM.conditionalDistributions 
    19 """<<'pre-presbyopic': <0.625, 0.133, 0.242>, 'presbyopic': <0.725, 0.133, 0.142>, ...>>""" 
     19# prints: <<'pre-presbyopic': <0.625, 0.133, 0.242>, 'presbyopic': <0.725, 0.133, 0.142>, ...>> 
    2020 
    2121print bayes.distribution 
    22 """<0.625, 0.167, 0.208>""" 
     22# prints: <0.625, 0.167, 0.208> 
    2323print bayesWithM.distribution 
    24 """<0.625, 0.167, 0.208>""" 
     24# prints: <0.625, 0.167, 0.208> 
Note: See TracChangeset for help on using the changeset viewer.