# source:orange/orange/doc/modules/orngBayes.htm@6538:a5f65d7f0b2c

Revision 6538:a5f65d7f0b2c, 5.0 KB checked in by Mitar <Mitar@…>, 4 years ago (diff)

Made XPM version of the icon 32x32.

Line
4<body>
5<h1>orngBayes: A Helper Module for Naive Bayesian Classifier</h1>
6<index name="classifiers+naive Bayesian classifier">
7<index name="modules+bayes learner">
8<index name="classes/BayesLearner (in orngBayes)">
9
10<p>Module orngBayes contains a Bayesian learner which uses the one from Orange, with the only difference that using the m-estimate is much simpler than with the learner built in Orange. Besides, the module has a function for printing out the classifier in a textual form.</p>
11
12<h2>Bayes Learner</h2>
13
14<P>Class <code>orngBayes.BayesLearner</code> is similar to <code>orange.BayesLearner</code> (the former in fact uses the latter) except that for estimating the probabilities with m-estimates you don't need to set a bunch of probability estimators, you can simply specify the value of m. To show how, let's compare a naive Bayesian classifier with m-estimate (m=2) with one that uses relative frequencies to estimate probabilities.</P>
15
17<xmp class="code">import orange, orngBayes, orngTest, orngStat
18
19data = orange.ExampleTable("lung-cancer")
20
21bayes = orngBayes.BayesLearner()
22bayes_m = orngBayes.BayesLearner(m=2)
23
24res = orngTest.crossValidation([bayes, bayes_m], data)
25CAs = orngStat.CA(res)
26print
27print "Without m: %5.3f" % CAs[0]
28print "With m=2: %5.3f" % CAs[1]
29</xmp>
30
31<p class="section">Attributes</p>
32
33<P>All the attributes listed here are optional in the sense that they appear and are used only if you set them.</P>
34
35<dl class="attributes">
36<dt>m</dt>
37<dd>m for m-estimate. If you set it, m-estimation of probabilities will be used (through class <a href="../reference/ProbabilityEstimation.htm#mestimate"><code>orange.ProbabilityEstimatorConstructor_m</code></a>). This attribute is ignored if you also set <code>estimatorConstructor</code>.</dd>
38
39<dt>estimatorConstructor</dt>
40<dd>Probability estimator constructor to be used for a priori class probabilities. If set, it should be an instance of a class derived from <a href="../reference/ProbabilityEstimation.htm"><code>orange.ProbabilityEstimatorConstructor</code></a>. Setting this attribute disable the above described attribute <code>m</code>.</dd>
41
42<dt>conditionalEstimatorConstructor</dt>
43<dd>Probability estimator constructor for conditional probabilities for discrete attributes. If the attribute is omitted, the same estimator will be used as for a priori class probabilities.</dd>
44
45<dt>conditionalEstimatorConstructorContinuous</dt>
46<dd>As above, but for continuous attributes.</dd>
47</dl>
48
49<P>If none of these attributes is given, relative frequencies are used for a priori class probabilities and conditional probabilities of discrete attributes, and loess is used for continuous attributes.</P>
50
51<p class="section">Methods</p>
52<dl class="attributes">
53<dt>Constructor</dt>
54<dd>As usual, the constructor can be given no arguments or keyword arguments (such as m=2) which are copied to the object's attributes; the constructor will then return an instance of <code>orngBayes.BayesLearner</code>. Or it can be given learning examples (and possibly a weight meta attribute), it will construct and return a classifier (an instance of <code>orange.BayesClassifier</code>).</dd>
55
56<dt>__call__(self, examples, weightID = 0)</dt>
57<dd>Call the learner with examples as an argument (and, optionally the id of meta attribute with weights) and you'll get the classifier, as usual. The classifier will be an instance of <code>orange.BayesClassifier</code>, module <code>orngBayes</code> doesn't provide a special classifier.</dd>
58
59<dt>createInstance(self)</dt>
60<dd>This function returns an instance of <code>orange.BayesLearner</code> with the components set as defined by the attributes. Actually, when you call <code>orngBayes.BayesLearner</code> it calls <code>createInstance</code> to construct an appropriate <code>orange.BayesLearner</code> and forwards the call to it.</dd>
61</dl>
62
63<h2>Printing out the model</h2>
64
65<p>To print out the model in form of contingency matrices, call function <code>orngBayes.printModel(model)</code>, like the code below:</p>
67<xmp class="code">import orange, orngBayes
68
69data = orange.ExampleTable("voting")
70model = orngBayes.BayesLearner(data)
71orngBayes.printModel(model)
72</xmp>
73
75<xmp>
76                     republican   democrat
77class probabilities       0.386      0.614
78
79Attribute handicapped-infants
80                     republican   democrat
81                   n      0.568      0.432
82                   y      0.166      0.834
83
84Attribute water-project-cost-sharing
85                     republican   democrat
86                   y      0.385      0.615
87                   n      0.380      0.620
88</xmp>
89... and so on through all the attributes.</P>
90
91<P>This function is unable to print out the model if it contains continuous attributes or advanced probability estimators which wouldn't store the pre-computed probabilities (we have none like this at the moment).</P>
92
93</body> </html>
Note: See TracBrowser for help on using the repository browser.