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

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

Moved orange to Orange (part 2)

Line
1# Description: Leave-one-out method for estimation of classification accuracy. Demonstration of use for different learners
2# Category:    evaluation
3# Uses:        voting.tab
4# Referenced:  c_performance.htm
5
6import orange, orngTree
7
8def leave_one_out(data, learners):
9    acc = [0.0]*len(learners)
10    selection = [1] * len(data)
11    last = 0
12    for i in range(len(data)):
13        print 'leave-one-out: %d of %d' % (i, len(data))
14        selection[last] = 1
15        selection[i] = 0
16        train_data = data.select(selection, 1)
17        for j in range(len(learners)):
18            classifier = learners[j](train_data)
19            if classifier(data[i]) == data[i].getclass():
20                acc[j] += 1
21        last = i
22
23    for j in range(len(learners)):
24        acc[j] = acc[j]/len(data)
25    return acc
26
27orange.setrandseed(0)
28# set up the learners
29bayes = orange.BayesLearner()
30tree = orngTree.TreeLearner(minExamples=10, mForPruning=2)
31bayes.name = "bayes"
32tree.name = "tree"
33learners = [bayes, tree]
34
35# compute accuracies on data
36data = orange.ExampleTable("voting")
37acc = leave_one_out(data, learners)
38print "Classification accuracies:"
39for i in range(len(learners)):
40    print learners[i].name, acc[i]
Note: See TracBrowser for help on using the repository browser.