# source:orange/docs/tutorial/rst/code/py-small.py@11052:c22077a09e63

Revision 11052:c22077a09e63, 1.7 KB checked in by blaz <blaz.zupan@…>, 16 months ago (diff)

new tutorial

Line
1import Orange
2
3class SmallLearner(Orange.classification.PyLearner):
4    def __init__(self, base_learner=Orange.classification.bayes.NaiveLearner,
5                 name='small', m=5):
6        self.name = name
7        self.m   = m
8        self.base_learner = base_learner
9
10    def __call__(self, data, weight=None):
11        gain = Orange.feature.scoring.InfoGain()
12        m = min(self.m, len(data.domain.features))
13        best = [f for _, f in sorted((gain(x, data), x) for x in data.domain.features)[-m:]]
14        domain = Orange.data.Domain(best + [data.domain.class_var])
15
16        model = self.base_learner(Orange.data.Table(domain, data), weight)
17        return Orange.classification.PyClassifier(classifier=model, name=self.name)
18
19class OptimizedSmallLearner(Orange.classification.PyLearner):
20    def __init__(self, name="opt_small", ms=range(1,30,3)):
21        self.ms = ms
22        self.name = name
23
24    def __call__(self, data, weight=None):
25        scores = []
26        for m in self.ms:
27            res = Orange.evaluation.testing.cross_validation([SmallLearner(m=m)], data, folds=5)
28            scores.append((Orange.evaluation.scoring.AUC(res)[0], m))
29        _, best_m = max(scores)
30
31        return SmallLearner(data, m=best_m)
32
33data = Orange.data.Table("promoters")
34s_learner = SmallLearner(m=3)
35classifier = s_learner(data)
36print classifier(data[20])
37print classifier(data[20], Orange.classification.Classifier.GetProbabilities)
38
39nbc = Orange.classification.bayes.NaiveLearner(name="nbc")
40s_learner = SmallLearner(m=3)
41o_learner = OptimizedSmallLearner()
42
43learners = [o_learner, s_learner, nbc]
44res = Orange.evaluation.testing.cross_validation(learners, data, folds=10)
45print ", ".join("%s: %.3f" % (l.name, s) for l, s in zip(learners, Orange.evaluation.scoring.AUC(res)))
46
Note: See TracBrowser for help on using the repository browser.