Changeset 7368:2627d45ef4d0 in orange


Ignore:
Timestamp:
02/04/11 00:35:10 (3 years ago)
Author:
anze <anze.staric@…>
Branch:
default
Convert:
b7b028f5dd7d3a78f8b282b75d2f5710cca80a5a
Message:

Updated bayes doc examples

Location:
orange
Files:
2 edited

Legend:

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

    r7345 r7368  
    66========================= 
    77 
     8The most primitive bayesian classifier is :obj:`NaiveLearner`. The class 
     9estimates conditional probabilities from train data and uses them for 
     10prediction of new examples.  
     11 
     12Example (`bayes-run.py`_, uses `iris.tab`_) 
     13 
     14.. literalinclude:: code/bayes-run.py 
     15    :lines: 7- 
     16 
    817.. index:: Naive Bayesian Learner 
    9 .. autoclass:: Orange.classification.bayes.NaiveBayesLearner 
     18.. autoclass:: Orange.classification.bayes.NaiveLearner 
    1019   :members: 
     20   :show-inheritance: 
    1121  
    12 .. autoclass:: Orange.classification.bayes.NaiveBayesClassifier 
     22.. autoclass:: Orange.classification.bayes.NaiveClassifier 
    1323   :members: 
     24   :show-inheritance: 
    1425    
    1526Examples 
     
    1829five examples. 
    1930 
    20 >>> data = orange.ExampleTable("lenses") 
    21 >>> bayes = orange.BayesLearner(data) 
     31>>> from Orange import * 
     32>>> table = data.Table("lenses") 
     33>>> bayes = classification.bayes.NaiveLearner(table) 
    2234>>> 
    23 >>> for ex in data[:5]: 
     35>>> for ex in table[:5]: 
    2436...    print ex.getclass(), bayes(ex) 
    2537no no 
     
    3244maybe? 
    3345 
    34 >>> for ex in data[:5]: 
    35 ...     print ex.getclass(), bayes(ex, orange.Classifier.GetProbabilities) 
     46>>> for ex in table[:5]: 
     47...     print ex.getclass(), bayes(ex, Orange.classification.Classifier.GetProbabilities) 
    3648no <0.423, 0.000, 0.577> 
    3749no <0.000, 0.000, 1.000> 
     
    6779Let us now use m-estimate instead of relative frequencies. 
    6880 
    69 >>> bayesl = orange.BayesLearner() 
    70 >>> bayesl.estimatorConstructor = orange.ProbabilityEstimatorConstructor_m(m=2.0) 
    71 >>> bayes = bayesl(data) 
     81>>> bayesl = classification.bayes.NaiveLearner(m=2.0) 
     82>>> bayes = bayesl(table) 
    7283 
    7384The classifier is still correct for all examples. 
    7485 
    75 >>> for ex in data[:5]: 
    76 ...     print ex.getclass(), bayes(ex, no &lt;0.375, 0.063, 0.562&gt; 
     86>>> for ex in table[:5]: 
     87...     print ex.getclass(), bayes(ex, Orange.classification.Classifier.GetBoth)  
     88no <0.375, 0.063, 0.562>; 
    7789no <0.016, 0.003, 0.981> 
    7890soft <0.021, 0.607, 0.372> 
     
    100112dataset that contains four continuous and no discrete attributes. 
    101113 
    102 >>> data = orange.ExampleTable("iris") 
    103 >>> bayes = orange.BayesLearner(data) 
    104 >>> for exi in range(0, len(data), 20): 
    105 ...     print data[exi].getclass(), bayes(data[exi], orange.Classifier.GetBoth) 
     114>>> table = data.Table("iris") 
     115>>> bayes = orange.BayesLearner(table) 
     116>>> for exi in range(0, len(table), 20): 
     117...     print data[exi].getclass(), bayes(table[exi], orange.Classifier.GetBoth) 
    106118 
    107119The classifier works well. To see a glimpse of how it works, let us observe 
     
    129141It is important to stress that the curves are relatively smooth although no fitting (either manual or automatic) of parameters took place. 
    130142 
     143 
     144.. _bayes-run.py: code/bayes-run.py 
     145.. _iris.tab: code/iris.tab 
    131146""" 
    132147 
     
    135150from Orange.core import BayesLearner as _BayesLearner 
    136151 
    137 class NaiveBayesLearner(Orange.core.Learner): 
     152class NaiveLearner(Orange.classification.Learner): 
    138153    """ 
    139154    Probabilistic classifier based on applying Bayes' theorem (from Bayesian 
     
    183198     
    184199    def __new__(cls, examples = None, weightID = 0, **argkw): 
    185         self = Orange.core.Learner.__new__(cls, **argkw) 
     200        self = Orange.classification.Learner.__new__(cls, **argkw) 
    186201        if examples: 
    187202            self.__init__(**argkw) 
     
    230245        return NaiveBayesClassifier(bayes(instances, weight)) 
    231246             
    232 class NaiveBayesClassifier(Orange.core.Classifier): 
     247class NaiveClassifier(Orange.classification.Classifier): 
    233248    """ 
    234249    Predictor based on calculated probabilities 
     
    258273            self.__dict__[k] = v 
    259274   
    260     def __call__(self, instance, result_type=Orange.core.Classifier.GetValue, 
     275    def __call__(self, instance, result_type=Orange.classification.Classifier.GetValue, 
    261276                 *args, **kwdargs): 
    262277        """Classify a new instance 
     
    264279        :param instance: instance to be classifier 
    265280        :type instance: :class:`Orange.data.Instance` 
    266         :param result_type: :class:`Orange.core.Classifier.GetValue` or \ 
    267               :class:`Orange.core.Classifier.GetProbabilities` or 
    268               :class:`Orange.core.Classifier.GetBoth` 
     281        :param result_type: :class:`Orange.classification.Classifier.GetValue` or \ 
     282              :class:`Orange.classification.Classifier.GetProbabilities` or 
     283              :class:`Orange.classification.Classifier.GetBoth` 
    269284         
    270285        :rtype: :class:`Orange.data.Value`,  
  • orange/doc/Orange/rst/code/bayes-run.py

    r7252 r7368  
     1# Description: Naive Bayes Learner on iris data set 
     2# Category:    classification 
     3# Uses:        iris 
     4# Referenced:  Orange.classification.bayes 
     5# Classes:     Orange.classification.bayes.NaiveLearner, Orange.classification.bayes.NaiveClassifier 
     6 
    17import Orange 
    2 som = Orange.projection.som.SOMLearner(map_shape=(10, 20), initialize=Orange.projection.som.InitializeRandom) 
    3 map = som(Orange.data.Table("iris.tab")) 
    4 for n in map: 
    5     print "node:", n.pos[0], n.pos[1] 
    6     for e in n.examples: 
    7         print "\t",e 
     8table = Orange.data.Table("iris.tab") 
     9 
     10learner = Orange.classification.bayes.NaiveLearner() 
     11classifier = learner(table) 
     12prediction = classifier(table[0]) 
Note: See TracChangeset for help on using the changeset viewer.