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

Revision 526:fe2d65da2b2a, 1.2 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 classifier from orange.Classifier
2# Category:    classification, callbacks to Python
3# Classes:     Classifier
4# Uses:        lenses
5# Referenced:  callbacks.htm
6
7import orange, orngTree, orngMisc
8tab = orange.ExampleTable(r"lenses.tab")
9
10class CartesianClassifier(orange.Classifier):
11    def __init__(self, var1, var2):
12        self.var1 = var1
13        self.var2 = var2
14        self.noValues2 = len(var2.values)
15        self.classVar = orange.EnumVariable("%sx%s" % (var1.name, var2.name))
16        self.classVar.values = ["%s-%s" % (v1, v2) for v1 in var1.values for v2 in var2.values]
17
18    def __call__(self, ex, what = orange.Classifier.GetValue):
19        val = ex[self.var1] * self.noValues2 + ex[self.var2]
20        if what == orange.Classifier.GetValue:
21            return orange.Value(self.classVar, val)
22        probs = orange.DiscDistribution(self.classVar)
23        probs[val] = 1.0
24        if what == orange.Classifier.GetProbabilities:
25            return probs
26        else:
27            return (orange.Value(self.classVar, val), probs)
28
29tt =CartesianClassifier(tab.domain[0], tab.domain[1])
30
31for ex in tab:
32  print "%s ---> %s" % (ex, tt(ex))
Note: See TracBrowser for help on using the repository browser.