source: orange/docs/reference/rst/code/testing-test.py @ 9696:26fc745b27f6

Revision 9696:26fc745b27f6, 2.6 KB checked in by Jure Zbontar <jure.zbontar@…>, 2 years ago (diff)

Renamed RandomGenerator to Random in docs.

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