source: orange/docs/reference/rst/code/statExamples.py @ 9945:ba7e22d4ebc5

Revision 9945:ba7e22d4ebc5, 5.6 KB checked in by markotoplak, 2 years ago (diff)

Modernized some example scripts.

Line 
1# Description: Demostrates the use of classification scores
2# Category:    evaluation
3# Uses:        voting.tab
4# Referenced:  orngStat.htm
5
6import Orange
7
8learners = [ Orange.classification.bayes.NaiveLearner(name = "bayes"),
9             Orange.classification.tree.TreeLearner(name="tree"),
10             Orange.classification.majority.MajorityLearner(name="majrty")]
11
12voting = Orange.data.Table("voting")
13res = Orange.evaluation.testing.cross_validation(learners, voting)
14
15vehicle = Orange.data.Table("vehicle")
16resVeh = Orange.evaluation.testing.cross_validation(learners, vehicle)
17
18import Orange.evaluation.scoring
19
20CAs = Orange.evaluation.scoring.CA(res)
21APs = Orange.evaluation.scoring.AP(res)
22Briers = Orange.evaluation.scoring.Brier_score(res)
23ISs = Orange.evaluation.scoring.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 = Orange.evaluation.scoring.CA(res, reportSE=True)
32APs = Orange.evaluation.scoring.AP(res, reportSE=True)
33Briers = Orange.evaluation.scoring.Brier_score(res, reportSE=True)
34ISs = Orange.evaluation.scoring.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 = Orange.evaluation.scoring.confusion_matrices(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 = Orange.evaluation.scoring.confusion_matrices(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 = Orange.evaluation.scoring.confusion_matrices(resVeh, vehicle.domain.class_var.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 = Orange.evaluation.scoring.confusion_matrices(resVeh, vehicle.domain.class_var.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 = Orange.evaluation.scoring.confusion_matrices(resVeh)[0]
64classes = vehicle.domain.class_var.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 = Orange.evaluation.scoring.confusion_matrices(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, Orange.evaluation.scoring.sens(cm[l]), Orange.evaluation.scoring.spec(cm[l]))
75
76cm = Orange.evaluation.scoring.confusion_matrices(resVeh, vehicle.domain.class_var.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, Orange.evaluation.scoring.sens(cm[l]), Orange.evaluation.scoring.spec(cm[l]))
82
83print
84print "AUC (voting)"
85
86AUCs = Orange.evaluation.scoring.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 = Orange.evaluation.scoring.AUC(resVeh, Orange.evaluation.scoring.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 = Orange.evaluation.scoring.AUC(resVeh, i)
103    print "%25s: \t%5.3f\t%5.3f\t%5.3f" % ((methods[i], ) + tuple(AUCs))
104
105
106classes = vehicle.domain.class_var.values
107classDist = Orange.statistics.distribution.Distribution(vehicle.domain.class_var, vehicle)
108
109print
110print "AUC for detecting class 'van' in 'vehicle'"
111AUCs = Orange.evaluation.scoring.AUC_single(resVeh, classIndex = vehicle.domain.class_var.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(Orange.evaluation.scoring.AUC_single(resVeh, c)))
118
119print
120classes = vehicle.domain.class_var.values
121AUCmatrix = Orange.evaluation.scoring.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(Orange.evaluation.scoring.AUC_pair(resVeh, c1, c2)))
131
132
133ri2 = Orange.data.sample.SubsetIndices2(voting, 0.6)
134train = voting.selectref(ri2, 0)
135test = voting.selectref(ri2, 1)
136res1 = Orange.evaluation.testing.learn_and_test_on_test_data(learners, train, test)
137
138print
139print "AUC and SE for voting"
140AUCs = Orange.evaluation.scoring.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(Orange.evaluation.scoring.compare_2_AUCs(res1, 0, 1)[2])
146
147print
148print "ROC (first 20 points) for bayes on 'voting'"
149ROC_bayes = Orange.evaluation.scoring.compute_ROC(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.