source: orange/docs/reference/rst/code/selection-filtered-learner.py @ 9661:674777da09b9

Revision 9661:674777da09b9, 1.4 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

To Orange25.

Line 
1# Description: Demonstrates the use of Orange.feature.selection.FilteredLearner
2#              to compare naive Bayesian learner when all or just the most
3#              important attribute is used. Shows how to find out which (in
4#              ten-fold cross validation) attributes was used the most.
5# Category:    feature selection
6# Uses:        voting
7# Referenced:  Orange.feature.html#selection
8# Classes:     Orange.feature.selection.FilteredLearner
9
10import Orange
11
12voting = Orange.data.Table("voting")
13
14nb = Orange.classification.bayes.NaiveLearner()
15fl = Orange.feature.selection.FilteredLearner(nb,
16     filter=Orange.feature.selection.FilterBestN(n=1), name='filtered')
17learners = (Orange.classification.bayes.NaiveLearner(name='bayes'), fl)
18results = Orange.evaluation.testing.cross_validation(learners, voting, storeClassifiers=1)
19
20# output the results
21print "Learner      CA"
22for i in range(len(learners)):
23    print "%-12s %5.3f" % (learners[i].name, Orange.evaluation.scoring.CA(results)[i])
24
25# find out which attributes were retained by filtering
26
27print "\nNumber of times attributes were used in cross-validation:"
28attsUsed = {}
29for i in range(10):
30    for a in results.classifiers[i][1].atts():
31        if a.name in attsUsed.keys():
32            attsUsed[a.name] += 1
33        else:
34            attsUsed[a.name] = 1
35for k in attsUsed.keys():
36    print "%2d x %s" % (attsUsed[k], k)
Note: See TracBrowser for help on using the repository browser.