source: orange/Orange/doc/reference/cb-measureattribute.py @ 9671:a7b056375472

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

Moved orange to Orange (part 2)

Line 
1# Description: Shows how to derive a Python class from orange.MeasureAttribute
2# Category:    attribute quality, callbacks to Python
3# Classes:     MeasureAttribute, TreeSplitConstructor
4# Uses:        lenses
5# Referenced:  callbacks.htm
6
7import orange, orngTree, orngMisc
8tab = orange.ExampleTable(r"lenses.tab")
9
10class MeasureAttribute_Cardinality(orange.MeasureAttribute):
11    def __init__(self, moreIsBetter = 1):
12        self.moreIsBetter = moreIsBetter
13       
14    def __call__(self, a1, a2, a3):
15        if type(a1) == int:
16            attrNo, domainContingency, apriorClass = a1, a2, a3
17            q = len(domainContingency[attrNo])
18            name = domainContingency[attrNo].outerVariable.name
19        else:
20            contingency, classDistribution, apriorClass = a1, a2, a3
21            q = len(contingency)
22            name = contingency.outerVariable.name
23#        print "Asked about %s; will answer %i" % (name, q)
24
25        if self.moreIsBetter:
26            return q
27        else:
28            return -q
29   
30def measure_cardinality(a1, a2, a3):
31    if type(a1) == int:
32        attrNo, domainContingency, apriorClass = a1, a2, a3
33        q = len(domainContingency[attrNo])
34        name = domainContingency[attrNo].outerVariable.name
35    else:
36        contingency, classDistribution, apriorClass = a1, a2, a3
37        q = len(contingency)
38        name = contingency.outerVariable.name
39#    print "Asked about %s; will answer %i" % (name, q)
40    return q
41
42
43def measure_cardinality(a1, a2, a3):
44  if type(a1) == int:
45    return len(a2[a1])
46  else:
47    return len(a1)
48
49
50print "LESS IS BETTER"
51treeLearner = orange.TreeLearner()
52treeLearner.split = orange.TreeSplitConstructor_Attribute(measure = MeasureAttribute_Cardinality(0))
53tree = treeLearner(tab)
54orngTree.printTxt(tree)
55
56
57print "\n\nMORE IS BETTER"
58treeLearner = orange.TreeLearner()
59treeLearner.split = orange.TreeSplitConstructor_Attribute(measure = MeasureAttribute_Cardinality(1))
60tree = treeLearner(tab)
61orngTree.printTxt(tree)
62
63print "\n\nFUNCTION (more is better)"
64treeLearner = orange.TreeLearner()
65treeLearner.split = orange.TreeSplitConstructor_Attribute(measure = measure_cardinality)
66tree = treeLearner(tab)
67orngTree.printTxt(tree)
68
69print "\n\nRandom test based on the number of examples"
70class MeasureAttribute_Generator(orange.MeasureAttribute):
71     def __init__(self):
72         self.needs = orange.MeasureAttribute.Generator
73         
74     def __call__(self, attr, generator, priorDist, weigthID):
75         import random
76         r = random.Random()
77         r.seed(len(generator))
78         return r.random()
79     
80treeLearner = orange.TreeLearner()
81treeLearner.split = orange.TreeSplitConstructor_Attribute(measure = MeasureAttribute_Generator())
82tree = treeLearner(tab)
83orngTree.printTxt(tree)
84 
Note: See TracBrowser for help on using the repository browser.