source: orange/Orange/testing/regression/tests_20/modules_statExamples.py @ 9952:986e9cd806f4

Revision 9952:986e9cd806f4, 4.9 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

Tests moved and renamed from orange 20.

Line 
1# Description: Demostrates the use of classification scores
2# Category:    evaluation
3# Uses:        voting.tab
4# Referenced:  orngStat.htm
5
6import orange, orngTest, orngTree
7
8learners = [orange.BayesLearner(name = "bayes"),
9            orngTree.TreeLearner(name="tree"),
10            orange.MajorityLearner(name="majrty")]
11
12voting = orange.ExampleTable("voting")
13res = orngTest.crossValidation(learners, voting)
14
15vehicle = orange.ExampleTable("vehicle")
16resVeh = orngTest.crossValidation(learners, vehicle)
17
18import orngStat
19
20CAs = orngStat.CA(res)
21APs = orngStat.AP(res)
22Briers = orngStat.BrierScore(res)
23ISs = orngStat.IS(res)
24
25print
26print "method\tCA\tAP\tBrier\tIS"
27for l in range(len(learners)):
28    print "%s\t%5.3f\t%5.3f\t%5.3f\t%6.3f" % (learners[l].name, CAs[l], APs[l], Briers[l], ISs[l])
29
30
31CAs = orngStat.CA(res, reportSE=True)
32APs = orngStat.AP(res, reportSE=True)
33Briers = orngStat.BrierScore(res, reportSE=True)
34ISs = orngStat.IS(res, reportSE=True)
35
36print
37print "method\tCA\tAP\tBrier\tIS"
38for l in range(len(learners)):
39    print "%s\t%5.3f+-%5.3f\t%5.3f+-%5.3f\t%5.3f+-%5.3f\t%6.3f+-%5.3f" % ((learners[l].name, ) + CAs[l] + APs[l] + Briers[l] + ISs[l])
40
41
42print
43cm = orngStat.confusionMatrices(res)[0]
44print "Confusion matrix for naive Bayes:"
45print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN)
46
47print
48cm = orngStat.confusionMatrices(res, cutoff=0.2)[0]
49print "Confusion matrix for naive Bayes:"
50print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN)
51
52print
53cm = orngStat.confusionMatrices(resVeh, vehicle.domain.classVar.values.index("van"))[0]
54print "Confusion matrix for naive Bayes for 'van':"
55print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN)
56
57print
58cm = orngStat.confusionMatrices(resVeh, vehicle.domain.classVar.values.index("opel"))[0]
59print "Confusion matrix for naive Bayes for 'opel':"
60print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN)
61
62print
63cm = orngStat.confusionMatrices(resVeh)[0]
64classes = vehicle.domain.classVar.values
65print "\t"+"\t".join(classes)
66for className, classConfusions in zip(classes, cm):
67    print ("%s" + ("\t%i" * len(classes))) % ((className, ) + tuple(classConfusions))
68
69cm = orngStat.confusionMatrices(res)
70print
71print "Sensitivity and specificity for 'voting'"
72print "method\tsens\tspec"
73for l in range(len(learners)):
74    print "%s\t%5.3f\t%5.3f" % (learners[l].name, orngStat.sens(cm[l]), orngStat.spec(cm[l]))
75
76cm = orngStat.confusionMatrices(resVeh, vehicle.domain.classVar.values.index("van"))
77print
78print "Sensitivity and specificity for 'vehicle=van'"
79print "method\tsens\tspec"
80for l in range(len(learners)):
81    print "%s\t%5.3f\t%5.3f" % (learners[l].name, orngStat.sens(cm[l]), orngStat.spec(cm[l]))
82
83
84
85print
86print "AUC (voting)"
87
88AUCs = orngStat.AUC(res)
89for l in range(len(learners)):
90    print "%10s: %5.3f" % (learners[l].name, AUCs[l])
91   
92reload(orngStat)
93
94print
95print "AUC for vehicle using weighted single-out method"
96print "bayes\ttree\tmajority"
97AUCs = orngStat.AUC(resVeh, orngStat.AUC.WeightedOneAgainstAll)
98print "%5.3f\t%5.3f\t%5.3f" % tuple(AUCs)
99
100print
101print "AUC for vehicle, using different methods"
102methods = ["by pairs, weighted", "by pairs", "one vs. all, weighted", "one vs. all"]
103print " " *25 + \tbayes\ttree\tmajority"
104for i in range(4):
105    AUCs = orngStat.AUC(resVeh, i)
106    print "%25s: \t%5.3f\t%5.3f\t%5.3f" % ((methods[i], ) + tuple(AUCs))
107
108
109classes = vehicle.domain.classVar.values
110classDist = orange.Distribution(vehicle.domain.classVar, vehicle)
111
112print
113print "AUC for detecting class 'van' in 'vehicle'"
114AUCs = orngStat.AUC_single(resVeh, classIndex = vehicle.domain.classVar.values.index("van"))
115print "%5.3f\t%5.3f\t%5.3f" % tuple(AUCs)
116
117print
118print "AUCs for detecting various classes in 'vehicle'"
119for c,s in enumerate(classes):
120    print "%s (%5.3f) vs others: \t%5.3f\t%5.3f\t%5.3f" % ((s, classDist[c] ) + tuple(orngStat.AUC_single(resVeh, c)))
121
122print
123classes = vehicle.domain.classVar.values
124AUCmatrix = orngStat.AUC_matrix(resVeh)[0]
125print "\t"+"\t".join(classes[:-1])
126for className, AUCrow in zip(classes[1:], AUCmatrix[1:]):
127    print ("%s" + ("\t%5.3f" * len(AUCrow))) % ((className, ) + tuple(AUCrow))
128
129print
130print "AUCs for detecting various pairs of classes in 'vehicle'"
131for c1, s1 in enumerate(classes):
132    for c2 in range(c1):
133        print "%s vs %s: \t%5.3f\t%5.3f\t%5.3f" % ((s1, classes[c2]) + tuple(orngStat.AUC_pair(resVeh, c1, c2)))
134
135
136ri2 = orange.MakeRandomIndices2(voting, 0.6)
137train = voting.selectref(ri2, 0)
138test = voting.selectref(ri2, 1)
139res1 = orngTest.learnAndTestOnTestData(learners, train, test)
140
141print
142print "AUC and SE for voting"
143AUCs = orngStat.AUCWilcoxon(res1)
144for li, lrn in enumerate(learners):
145    print "%s: %5.3f+-%5.3f" % (lrn.name, AUCs[li][0], AUCs[li][1])
146
147print
148print "Difference between naive Bayes and tree: %5.3f+-%5.3f" % tuple(orngStat.compare2AUCs(res1, 0, 1)[2])
149
150print
151print "ROC (first 20 points) for bayes on 'voting'"
152ROC_bayes = orngStat.computeROC(res1)[0]
153for t in ROC_bayes[:20]:
154    print "%5.3f\t%5.3f" % t
Note: See TracBrowser for help on using the repository browser.