source: orange/Orange/doc/ofb/ensemble2.py @ 9671:a7b056375472

Revision 9671:a7b056375472, 2.2 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

Line 
1# Description: Example of how to build ensamble learners in Orange. Takes a list of learners, and for prediction uses the highest predicted class probability.
2# Category:    modelling
3# Uses:        promoters.tab
4# Classes:     orngTest.crossValidation
5# Referenced:  o_ensemble.htm
6
7import orange, orngTree
8import orngTest, orngStat
9
10# define the learner and classifier such that
11# it can be used as similar standard orange classes
12
13def WinnerLearner(examples=None, **kwds):
14  learner = apply(WinnerLearner_Class, (), kwds)
15  if examples:
16    return learner(examples)
17  else:
18    return learner
19
20class WinnerLearner_Class:
21  def __init__(self, name='winner classifier', learners=None):
22    self.name = name
23    self.learners = learners
24
25  def __call__(self, data, learners=None, weight=None):
26    if learners:
27      self.learners = learners
28    classifiers = []
29    for l in self.learners:
30      classifiers.append(l(data))
31    return WinnerClassifier(classifiers = classifiers)
32
33class WinnerClassifier:
34  def __init__(self, **kwds):
35    self.__dict__.update(kwds)
36
37  def __call__(self, example, resultType = orange.GetValue):
38    pmatrix = []
39    for c in self.classifiers:
40      pmatrix.append(c(example, orange.GetProbabilities))
41
42    maxp = []  # stores max class probabilities for each classifiers
43    for pv in pmatrix:
44      maxp.append(max(pv))
45
46    p = max(maxp)  # max class probability
47    classifier_index = maxp.index(p)
48    c = pmatrix[classifier_index].modus()
49   
50    if resultType == orange.GetValue:
51      return c
52    elif resultType == orange.getClassDistribution:
53      return pmatrix[classifier_index]
54    else:
55      return (c, pmatrix[classifier_index])
56
57
58tree = orngTree.TreeLearner(mForPruning=5.0)
59tree.name = 'class. tree'
60bayes = orange.BayesLearner()
61bayes.name = 'naive bayes'
62winner = WinnerLearner(learners=[tree, bayes])
63winner.name = 'winner'
64
65majority = orange.MajorityLearner()
66majority.name = 'default'
67learners = [majority, tree, bayes, winner]
68
69data = orange.ExampleTable("promoters")
70
71results = orngTest.crossValidation(learners, data)
72print "Classification Accuracy:"
73for i in range(len(learners)):
74    print ("%15s: %5.3f") % (learners[i].name, orngStat.CA(results)[i])
Note: See TracBrowser for help on using the repository browser.