source: orange/docs/reference/rst/code/statExamples.py @ 9372:aef193695ea9

Revision 9372:aef193695ea9, 4.9 KB checked in by mitar, 2 years ago (diff)

Moved documentation to the separate directory.

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
83print
84print "AUC (voting)"
85
86AUCs = orngStat.AUC(res)
87for l in range(len(learners)):
88    print "%10s: %5.3f" % (learners[l].name, AUCs[l])
89
90
91print
92print "AUC for vehicle using weighted single-out method"
93print "bayes\ttree\tmajority"
94AUCs = orngStat.AUC(resVeh, orngStat.AUC.WeightedOneAgainstAll)
95print "%5.3f\t%5.3f\t%5.3f" % tuple(AUCs)
96
97print
98print "AUC for vehicle, using different methods"
99methods = ["by pairs, weighted", "by pairs", "one vs. all, weighted", "one vs. all"]
100print " " *25 + \tbayes\ttree\tmajority"
101for i in range(4):
102    AUCs = orngStat.AUC(resVeh, i)
103    print "%25s: \t%5.3f\t%5.3f\t%5.3f" % ((methods[i], ) + tuple(AUCs))
104
105
106classes = vehicle.domain.classVar.values
107classDist = orange.Distribution(vehicle.domain.classVar, vehicle)
108
109print
110print "AUC for detecting class 'van' in 'vehicle'"
111AUCs = orngStat.AUC_single(resVeh, classIndex = vehicle.domain.classVar.values.index("van"))
112print "%5.3f\t%5.3f\t%5.3f" % tuple(AUCs)
113
114print
115print "AUCs for detecting various classes in 'vehicle'"
116for c,s in enumerate(classes):
117    print "%s (%5.3f) vs others: \t%5.3f\t%5.3f\t%5.3f" % ((s, classDist[c] ) + tuple(orngStat.AUC_single(resVeh, c)))
118
119print
120classes = vehicle.domain.classVar.values
121AUCmatrix = orngStat.AUC_matrix(resVeh)[0]
122print "\t"+"\t".join(classes[:-1])
123for className, AUCrow in zip(classes[1:], AUCmatrix[1:]):
124    print ("%s" + ("\t%5.3f" * len(AUCrow))) % ((className, ) + tuple(AUCrow))
125
126print
127print "AUCs for detecting various pairs of classes in 'vehicle'"
128for c1, s1 in enumerate(classes):
129    for c2 in range(c1):
130        print "%s vs %s: \t%5.3f\t%5.3f\t%5.3f" % ((s1, classes[c2]) + tuple(orngStat.AUC_pair(resVeh, c1, c2)))
131
132
133ri2 = orange.MakeRandomIndices2(voting, 0.6)
134train = voting.selectref(ri2, 0)
135test = voting.selectref(ri2, 1)
136res1 = orngTest.learnAndTestOnTestData(learners, train, test)
137
138print
139print "AUC and SE for voting"
140AUCs = orngStat.AUCWilcoxon(res1)
141for li, lrn in enumerate(learners):
142    print "%s: %5.3f+-%5.3f" % (lrn.name, AUCs[li][0], AUCs[li][1])
143
144print
145print "Difference between naive Bayes and tree: %5.3f+-%5.3f" % tuple(orngStat.compare2AUCs(res1, 0, 1)[2])
146
147print
148print "ROC (first 20 points) for bayes on 'voting'"
149ROC_bayes = orngStat.computeROC(res1)[0]
150for t in ROC_bayes[:20]:
151    print "%5.3f\t%5.3f" % t
Note: See TracBrowser for help on using the repository browser.