orngBayes: A Helper Module for Naive Bayesian Classifier
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.
orngBayes.BayesLearner is similar to
orange.BayesLearner (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.
part of bayes.py
All the attributes listed here are optional in the sense that they appear and are used only if you set them.
- m for m-estimate. If you set it, m-estimation of probabilities will be used (through class
orange.ProbabilityEstimatorConstructor_m). This attribute is ignored if you also set
- Probability estimator constructor to be used for a priori class probabilities. If set, it should be an instance of a class derived from
orange.ProbabilityEstimatorConstructor. Setting this attribute disable the above described attribute
- 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.
- As above, but for continuous attributes.
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.
- 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
orngBayes.BayesLearner. Or it can be given learning examples (and possibly a weight meta attribute), it will construct and return a classifier (an instance of
- __call__(self, examples, weightID = 0)
- 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
orngBayesdoesn't provide a special classifier.
- This function returns an instance of
orange.BayesLearnerwith the components set as defined by the attributes. Actually, when you call
createInstanceto construct an appropriate
orange.BayesLearnerand forwards the call to it.
Printing out the model
To print out the model in form of contingency matrices, call function
orngBayes.printModel(model), like the code below:
part of bayes.py
The output will start with
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).