source: orange/orange/doc/ofb/domain10.py @ 6538:a5f65d7f0b2c

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

Made XPM version of the icon 32x32.

Line 
1# Description: A simple implementation of wrapper feature subset selection
2# Category:    modelling
3# Uses:        voting
4# Classes:     Domain, orngTest.crossValidation
5# Referenced:  domain.htm
6
7import orange, orngTest, orngStat, orngTree
8
9def WrapperFSS(data, learner, verbose=0, folds=10):
10  classVar = data.domain.classVar
11  currentAtt = []
12  freeAttributes = list(data.domain.attributes)
13
14  newDomain = orange.Domain(currentAtt + [classVar])
15  d = data.select(newDomain)
16  results = orngTest.crossValidation([learner], d, folds=folds)
17  maxStat = orngStat.CA(results)[0]
18  if verbose>=2:
19    print "start (%5.3f)" % maxStat
20
21  while 1:
22    stat = []
23    for a in freeAttributes:
24      newDomain = orange.Domain([a] + currentAtt + [classVar])
25      d = data.select(newDomain)
26      results = orngTest.crossValidation([learner], d, folds=folds)
27      stat.append(orngStat.CA(results)[0])
28      if verbose>=2:
29        print %s gained %5.3f" % (a.name, orngStat.CA(results)[0])
30
31    if (max(stat) > maxStat):
32      oldMaxStat = maxStat
33      maxStat = max(stat)
34      bestVarIndx = stat.index(max(stat))
35      if verbose:
36        print "gain: %5.3f, attribute: %s" % (maxStat-oldMaxStat, freeAttributes[bestVarIndx].name)
37      currentAtt = currentAtt + [freeAttributes[bestVarIndx]]
38      del freeAttributes[bestVarIndx]
39    else:
40      if verbose:
41        print "stopped (%5.3f)" % (max(stat) - maxStat)
42      return orange.Domain(currentAtt + [classVar])
43      break
44
45def WrappedFSSLearner(learner, examples=None, verbose=0, folds=10, **kwds):
46  kwds['verbose'] = verbose
47  kwds['folds'] = folds
48  learner = apply(WrappedFSSLearner_Class, (learner,), kwds)
49  if examples:
50    return learner(examples)
51  else:
52    return learner
53
54class WrappedFSSLearner_Class:
55  def __init__(self, learner, verbose=0, folds=10, name='learner w wrapper fss'):
56    self.name = name
57    self.learner = learner
58    self.verbose = verbose
59    self.folds = folds
60
61  def __call__(self, data, weight=None):
62    domain = WrapperFSS(data, self.learner, self.verbose, self.folds)
63    selectedData = data.select(domain)
64    if self.verbose:
65      print 'features:', selectedData.domain
66    model = self.learner(selectedData, weight)
67    return Classifier(classifier = model)
68
69class Classifier:
70  def __init__(self, **kwds):
71    self.__dict__.update(kwds)
72
73  def __call__(self, example, resultType = orange.GetValue):
74    return self.classifier(example, resultType)
75
76
77#base = orngTree.TreeLearner(mForPruning=0.5)
78#base.name = 'tree'
79base = orange.BayesLearner()
80base.name = 'bayes'
81import warnings
82warnings.filterwarnings("ignore", ".*'BayesLearner': .*", orange.KernelWarning)
83
84fssed = WrappedFSSLearner(base, verbose=1, folds=5)
85fssed.name = 'w fss'
86
87# evaluation
88
89learners = [base, fssed]
90data = orange.ExampleTable("voting")
91results = orngTest.crossValidation(learners, data, folds=10)
92
93print "Learner      CA     IS     Brier    AUC"
94for i in range(len(learners)):
95  print "%-12s %5.3f  %5.3f  %5.3f  %5.3f" % (learners[i].name, \
96    orngStat.CA(results)[i], orngStat.IS(results)[i],
97    orngStat.BrierScore(results)[i], orngStat.AUC(results)[i])
Note: See TracBrowser for help on using the repository browser.