# source:orange/orange/doc/ofb/accuracy4.py@6538:a5f65d7f0b2c

Revision 6538:a5f65d7f0b2c, 1.6 KB checked in by Mitar <Mitar@…>, 4 years ago (diff)

Made XPM version of the icon 32x32.

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