source: orange/orange/doc/Orange/rst/code/testing-test.py @ 7609:bf9836042ee1

Revision 7609:bf9836042ee1, 2.5 KB checked in by jzbontar <jure.zbontar@…>, 3 years ago (diff)

testing module

Line 
1import Orange
2import random
3
4table = Orange.data.Table("voting")
5
6bayes = Orange.classification.bayes.NaiveLearner(name="bayes")
7tree = Orange.classification.tree.TreeLearnerBase(name="tree")
8majority = Orange.classification.majority.MajorityLearner(name="default")
9learners = [bayes, tree, majority]
10
11def printResults(res):
12    CAs = Orange.evaluation.scoring.CA(res, reportSE=1)
13    for name, ca in zip(res.classifierNames, CAs):
14        print "%s: %5.3f+-%5.3f" % (name, ca[0], 1.96 * ca[1]),
15    print
16
17print "\nproportionsTest that will always give the same results"
18for i in range(3):
19    res = Orange.evaluation.testing.proportionTest(learners, table, 0.7)
20    printResults(res)
21
22print "\nproportionsTest that will give different results, \
23but the same each time the script is run"
24myRandom = Orange.core.RandomGenerator()
25for i in range(3):
26    res = Orange.evaluation.testing.proportionTest(learners, table, 0.7,
27        randomGenerator=myRandom)
28    printResults(res)
29# End
30
31if "NO_RANDOMNESS" not in vars():
32    print "\nproportionsTest that will give different results each time it is run"
33    for i in range(3):
34        res = Orange.evaluation.testing.proportionTest(learners, table, 0.7,
35            randseed=random.randint(0, 100))
36        printResults(res)
37# End
38
39print "\nproportionsTest + storing classifiers"
40res = Orange.evaluation.testing.proportionTest(learners, table, 0.7, 100,
41    storeClassifiers=1)
42print "#iter %i, #classifiers %i" % \
43    (len(res.classifiers), len(res.classifiers[0]))
44
45print "\nGood old 10-fold cross validation"
46res = Orange.evaluation.testing.crossValidation(learners, table)
47printResults(res)
48
49print "\nLearning curve"
50prop = Orange.core.frange(0.2, 1.0, 0.2)
51res = Orange.evaluation.testing.learningCurveN(learners, table, folds=5,
52    proportions=prop)
53for i in range(len(prop)):
54    print "%5.3f:" % prop[i],
55    printResults(res[i])
56# End
57
58print "\nLearning curve with pre-separated data"
59indices = Orange.core.MakeRandomIndices2(table, p0=0.7)
60train = table.select(indices, 0)
61test = table.select(indices, 1)
62res = Orange.evaluation.testing.learningCurveWithTestData(learners, train,
63    test, times=5, proportions=prop)
64for i in range(len(prop)):
65    print "%5.3f:" % prop[i],
66    printResults(res[i])
67# End
68
69print "\nLearning and testing on pre-separated data"
70res = Orange.evaluation.testing.learnAndTestOnTestData(learners, train, test)
71printResults(res)
72
73print "\nLearning and testing on the same data"
74res = Orange.evaluation.testing.learnAndTestOnLearnData(learners, table)
75printResults(res)
Note: See TracBrowser for help on using the repository browser.