source: orange/orange/doc/reference/cb-learner.py @ 526:fe2d65da2b2a

Revision 526:fe2d65da2b2a, 1.5 KB checked in by janezd <janez.demsar@…>, 10 years ago (diff)
  • moved documentation from a separate module to this one
Line 
1# Description: Shows how to derive a Python class form orange.Learner
2# Category:    classification, learning, callbacks to Python
3# Classes:     Learner, ContingencyAttrClass, orngMisc.BestOnTheFly
4# Uses:        lenses
5# Referenced:  callbacks.htm
6
7import orange, orngTree, orngMisc
8tab = orange.ExampleTable(r"lenses.tab")
9
10class OneAttributeLearner(orange.Learner):
11    def __init__(self, measure):
12        self.measure = measure
13
14    def __call__(self, gen, weightID=0):
15        selectBest = orngMisc.BestOnTheFly()
16        for attr in gen.domain.attributes:
17            selectBest.candidate(self.measure(attr, gen, None, weightID))
18        bestAttr = gen.domain.attributes[selectBest.winnerIndex()]
19        classifier = orange.ClassifierByLookupTable(gen.domain.classVar, bestAttr)
20
21        contingency = orange.ContingencyAttrClass(bestAttr, gen, weightID)
22        for i in range(len(contingency)):
23            classifier.lookupTable[i] = contingency[i].modus()
24            classifier.distributions[i] = contingency[i]
25        classifier.lookupTable[-1] = contingency.innerDistribution.modus()
26        classifier.distributions[-1] = contingency.innerDistribution
27        for d in classifier.distributions:
28            d.normalize()
29       
30        return classifier
31
32
33oal = OneAttributeLearner(orange.MeasureAttribute_gainRatio())
34c = oal(tab)
35
36print c.variable
37print c.variable.values
38print c.lookupTable
39print c.distributions
40
41for ex in tab:
42    print "%s ---> %s" % (ex, c(ex))
Note: See TracBrowser for help on using the repository browser.