source: orange/docs/tutorial/rst/code/accuracy5.py @ 10304:a7e4c281c527

Revision 10304:a7e4c281c527, 1.6 KB checked in by mstajdohar, 2 years ago (diff)

Trimmed some test results.

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
8
9def accuracy(test_data, classifiers):
10    correct = [0.0] * len(classifiers)
11    for ex in test_data:
12        for i in range(len(classifiers)):
13            if classifiers[i](ex) == ex.getclass():
14                correct[i] += 1
15    for i in range(len(correct)):
16        correct[i] = correct[i] / len(test_data)
17    return correct
18
19def cross_validation(data, learners, k=10):
20    acc = [0.0] * len(learners)
21    selection = orange.MakeRandomIndicesCV(data, folds=k)
22    for test_fold in range(k):
23        train_data = data.select(selection, test_fold, negate=1)
24        test_data = data.select(selection, test_fold)
25        classifiers = []
26        for l in learners:
27            classifiers.append(l(train_data))
28        acc1 = accuracy(test_data, classifiers)
29        print "%d: %s" % (test_fold + 1, ["%.6f" % a for a in acc1])
30        for j in range(len(learners)):
31            acc[j] += acc1[j]
32    for j in range(len(learners)):
33        acc[j] = acc[j] / k
34    return acc
35
36orange.setrandseed(0)
37# set up the learners
38bayes = orange.BayesLearner()
39tree = orngTree.TreeLearner(mForPruning=2)
40
41bayes.name = "bayes"
42tree.name = "tree"
43learners = [bayes, tree]
44
45# compute accuracies on data
46data = orange.ExampleTable("voting")
47acc = cross_validation(data, learners, k=10)
48print "Classification accuracies:"
49for i in range(len(learners)):
50    print learners[i].name, acc[i]
Note: See TracBrowser for help on using the repository browser.