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

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

Moved orange to Orange (part 2)

Line 
1# Category:    evaluation
2# Description: Estimation of accuracy by cross validation. Demonstration of use for different learners.
3# Uses:        voting.tab
4# Classes:     MakeRandomIndicesCV
5# Referenced:  c_performance.htm
6
7import orange, orngTree
8reload(orngTree)
9
10def accuracy(test_data, classifiers):
11    correct = [0.0]*len(classifiers)
12    for ex in test_data:
13        for i in range(len(classifiers)):
14            if classifiers[i](ex) == ex.getclass():
15                correct[i] += 1
16    for i in range(len(correct)):
17        correct[i] = correct[i] / len(test_data)
18    return correct
19
20def cross_validation(data, learners, k=10):
21    acc = [0.0]*len(learners)
22    selection= orange.MakeRandomIndicesCV(data, folds=k)
23    for test_fold in range(k):
24        train_data = data.select(selection, test_fold, negate=1)
25        test_data = data.select(selection, test_fold)
26        classifiers = []
27        for l in learners:
28            classifiers.append(l(train_data))
29        acc1 = accuracy(test_data, classifiers)
30        print "%d: %s" % (test_fold+1, acc1)
31        for j in range(len(learners)):
32            acc[j] += acc1[j]
33    for j in range(len(learners)):
34        acc[j] = acc[j]/k
35    return acc
36
37orange.setrandseed(0)
38# set up the learners
39bayes = orange.BayesLearner()
40tree = orngTree.TreeLearner(mForPruning=2)
41
42bayes.name = "bayes"
43tree.name = "tree"
44learners = [bayes, tree]
45
46# compute accuracies on data
47data = orange.ExampleTable("voting")
48acc = cross_validation(data, learners, k=10)
49print "Classification accuracies:"
50for i in range(len(learners)):
51    print learners[i].name, acc[i]
Note: See TracBrowser for help on using the repository browser.