source: orange/docs/reference/rst/code/selection-bayes.py @ 9372:aef193695ea9

Revision 9372:aef193695ea9, 1.5 KB checked in by mitar, 2 years ago (diff)

Moved documentation to the separate directory.

Line 
1# Description: Compares naive Bayes with and without feature subset selection
2# Category:    feature selection
3# Uses:        voting
4# Referenced:  Orange.feature.html#selection
5# Classes:     Orange.feature.scoring.score_all, Orange.feature.selection.bestNAtts
6
7import Orange
8
9
10class BayesFSS(object):
11    def __new__(cls, examples=None, **kwds):
12        learner = object.__new__(cls, **kwds)
13        if examples:
14            return learner(examples)
15        else:
16            return learner
17   
18    def __init__(self, name='Naive Bayes with FSS', N=5):
19        self.name = name
20        self.N = 5
21     
22    def __call__(self, table, weight=None):
23        ma = Orange.feature.scoring.score_all(table)
24        filtered = Orange.feature.selection.selectBestNAtts(table, ma, self.N)
25        model = Orange.classification.bayes.NaiveLearner(filtered)
26        return BayesFSS_Classifier(classifier=model, N=self.N, name=self.name)
27
28class BayesFSS_Classifier:
29    def __init__(self, **kwds):
30        self.__dict__.update(kwds)
31   
32    def __call__(self, example, resultType = Orange.core.GetValue):
33        return self.classifier(example, resultType)
34
35
36# test above wraper on a data set
37table = Orange.data.Table("voting")
38learners = (Orange.classification.bayes.NaiveLearner(name='Naive Bayes'),
39            BayesFSS(name="with FSS"))
40results = Orange.evaluation.testing.cross_validation(learners, table)
41
42# output the results
43print "Learner      CA"
44for i in range(len(learners)):
45    print "%-12s %5.3f" % (learners[i].name, Orange.evaluation.scoring.CA(results)[i])
Note: See TracBrowser for help on using the repository browser.