Ignore:
Timestamp:
02/07/12 19:26:14 (2 years ago)
Author:
markotoplak
Branch:
default
rebase_source:
42219203d685928c8bb777610c858b7cc732a968
Message:

Modernized some example scripts.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/reference/rst/code/statExamples.py

    r9372 r9945  
    44# Referenced:  orngStat.htm 
    55 
    6 import orange, orngTest, orngTree 
     6import Orange 
    77 
    8 learners = [orange.BayesLearner(name = "bayes"), 
    9             orngTree.TreeLearner(name="tree"), 
    10             orange.MajorityLearner(name="majrty")] 
     8learners = [ Orange.classification.bayes.NaiveLearner(name = "bayes"), 
     9             Orange.classification.tree.TreeLearner(name="tree"), 
     10             Orange.classification.majority.MajorityLearner(name="majrty")] 
    1111 
    12 voting = orange.ExampleTable("voting") 
    13 res = orngTest.crossValidation(learners, voting) 
     12voting = Orange.data.Table("voting") 
     13res = Orange.evaluation.testing.cross_validation(learners, voting) 
    1414 
    15 vehicle = orange.ExampleTable("vehicle") 
    16 resVeh = orngTest.crossValidation(learners, vehicle) 
     15vehicle = Orange.data.Table("vehicle") 
     16resVeh = Orange.evaluation.testing.cross_validation(learners, vehicle) 
    1717 
    18 import orngStat 
     18import Orange.evaluation.scoring 
    1919 
    20 CAs = orngStat.CA(res) 
    21 APs = orngStat.AP(res) 
    22 Briers = orngStat.BrierScore(res) 
    23 ISs = orngStat.IS(res) 
     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) 
    2424 
    2525print 
     
    2929 
    3030 
    31 CAs = orngStat.CA(res, reportSE=True) 
    32 APs = orngStat.AP(res, reportSE=True) 
    33 Briers = orngStat.BrierScore(res, reportSE=True) 
    34 ISs = orngStat.IS(res, reportSE=True) 
     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) 
    3535 
    3636print 
     
    4141 
    4242print 
    43 cm = orngStat.confusionMatrices(res)[0] 
     43cm = Orange.evaluation.scoring.confusion_matrices(res)[0] 
    4444print "Confusion matrix for naive Bayes:" 
    4545print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN) 
    4646 
    4747print 
    48 cm = orngStat.confusionMatrices(res, cutoff=0.2)[0] 
     48cm = Orange.evaluation.scoring.confusion_matrices(res, cutoff=0.2)[0] 
    4949print "Confusion matrix for naive Bayes:" 
    5050print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN) 
    5151 
    5252print 
    53 cm = orngStat.confusionMatrices(resVeh, vehicle.domain.classVar.values.index("van"))[0] 
     53cm = Orange.evaluation.scoring.confusion_matrices(resVeh, vehicle.domain.class_var.values.index("van"))[0] 
    5454print "Confusion matrix for naive Bayes for 'van':" 
    5555print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN) 
    5656 
    5757print 
    58 cm = orngStat.confusionMatrices(resVeh, vehicle.domain.classVar.values.index("opel"))[0] 
     58cm = Orange.evaluation.scoring.confusion_matrices(resVeh, vehicle.domain.class_var.values.index("opel"))[0] 
    5959print "Confusion matrix for naive Bayes for 'opel':" 
    6060print "TP: %i, FP: %i, FN: %s, TN: %i" % (cm.TP, cm.FP, cm.FN, cm.TN) 
    6161 
    6262print 
    63 cm = orngStat.confusionMatrices(resVeh)[0] 
    64 classes = vehicle.domain.classVar.values 
     63cm = Orange.evaluation.scoring.confusion_matrices(resVeh)[0] 
     64classes = vehicle.domain.class_var.values 
    6565print "\t"+"\t".join(classes) 
    6666for className, classConfusions in zip(classes, cm): 
    6767    print ("%s" + ("\t%i" * len(classes))) % ((className, ) + tuple(classConfusions)) 
    6868 
    69 cm = orngStat.confusionMatrices(res) 
     69cm = Orange.evaluation.scoring.confusion_matrices(res) 
    7070print 
    7171print "Sensitivity and specificity for 'voting'" 
    7272print "method\tsens\tspec" 
    7373for 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])) 
     74    print "%s\t%5.3f\t%5.3f" % (learners[l].name, Orange.evaluation.scoring.sens(cm[l]), Orange.evaluation.scoring.spec(cm[l])) 
    7575 
    76 cm = orngStat.confusionMatrices(resVeh, vehicle.domain.classVar.values.index("van")) 
     76cm = Orange.evaluation.scoring.confusion_matrices(resVeh, vehicle.domain.class_var.values.index("van")) 
    7777print 
    7878print "Sensitivity and specificity for 'vehicle=van'" 
    7979print "method\tsens\tspec" 
    8080for 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])) 
     81    print "%s\t%5.3f\t%5.3f" % (learners[l].name, Orange.evaluation.scoring.sens(cm[l]), Orange.evaluation.scoring.spec(cm[l])) 
    8282 
    8383print 
    8484print "AUC (voting)" 
    8585 
    86 AUCs = orngStat.AUC(res) 
     86AUCs = Orange.evaluation.scoring.AUC(res) 
    8787for l in range(len(learners)): 
    8888    print "%10s: %5.3f" % (learners[l].name, AUCs[l]) 
     
    9292print "AUC for vehicle using weighted single-out method" 
    9393print "bayes\ttree\tmajority" 
    94 AUCs = orngStat.AUC(resVeh, orngStat.AUC.WeightedOneAgainstAll) 
     94AUCs = Orange.evaluation.scoring.AUC(resVeh, Orange.evaluation.scoring.AUC.WeightedOneAgainstAll) 
    9595print "%5.3f\t%5.3f\t%5.3f" % tuple(AUCs) 
    9696 
     
    100100print " " *25 + "  \tbayes\ttree\tmajority" 
    101101for i in range(4): 
    102     AUCs = orngStat.AUC(resVeh, i) 
     102    AUCs = Orange.evaluation.scoring.AUC(resVeh, i) 
    103103    print "%25s: \t%5.3f\t%5.3f\t%5.3f" % ((methods[i], ) + tuple(AUCs)) 
    104104 
    105105 
    106 classes = vehicle.domain.classVar.values 
    107 classDist = orange.Distribution(vehicle.domain.classVar, vehicle) 
     106classes = vehicle.domain.class_var.values 
     107classDist = Orange.statistics.distribution.Distribution(vehicle.domain.class_var, vehicle) 
    108108 
    109109print 
    110110print "AUC for detecting class 'van' in 'vehicle'" 
    111 AUCs = orngStat.AUC_single(resVeh, classIndex = vehicle.domain.classVar.values.index("van")) 
     111AUCs = Orange.evaluation.scoring.AUC_single(resVeh, classIndex = vehicle.domain.class_var.values.index("van")) 
    112112print "%5.3f\t%5.3f\t%5.3f" % tuple(AUCs) 
    113113 
     
    115115print "AUCs for detecting various classes in 'vehicle'" 
    116116for 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))) 
     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))) 
    118118 
    119119print 
    120 classes = vehicle.domain.classVar.values 
    121 AUCmatrix = orngStat.AUC_matrix(resVeh)[0] 
     120classes = vehicle.domain.class_var.values 
     121AUCmatrix = Orange.evaluation.scoring.AUC_matrix(resVeh)[0] 
    122122print "\t"+"\t".join(classes[:-1]) 
    123123for className, AUCrow in zip(classes[1:], AUCmatrix[1:]): 
     
    128128for c1, s1 in enumerate(classes): 
    129129    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))) 
     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))) 
    131131 
    132132 
    133 ri2 = orange.MakeRandomIndices2(voting, 0.6) 
     133ri2 = Orange.data.sample.SubsetIndices2(voting, 0.6) 
    134134train = voting.selectref(ri2, 0) 
    135135test = voting.selectref(ri2, 1) 
    136 res1 = orngTest.learnAndTestOnTestData(learners, train, test) 
     136res1 = Orange.evaluation.testing.learn_and_test_on_test_data(learners, train, test) 
    137137 
    138138print 
    139139print "AUC and SE for voting" 
    140 AUCs = orngStat.AUCWilcoxon(res1) 
     140AUCs = Orange.evaluation.scoring.AUCWilcoxon(res1) 
    141141for li, lrn in enumerate(learners): 
    142142    print "%s: %5.3f+-%5.3f" % (lrn.name, AUCs[li][0], AUCs[li][1]) 
    143143 
    144144print 
    145 print "Difference between naive Bayes and tree: %5.3f+-%5.3f" % tuple(orngStat.compare2AUCs(res1, 0, 1)[2]) 
     145print "Difference between naive Bayes and tree: %5.3f+-%5.3f" % tuple(Orange.evaluation.scoring.compare_2_AUCs(res1, 0, 1)[2]) 
    146146 
    147147print 
    148148print "ROC (first 20 points) for bayes on 'voting'" 
    149 ROC_bayes = orngStat.computeROC(res1)[0] 
     149ROC_bayes = Orange.evaluation.scoring.compute_ROC(res1)[0] 
    150150for t in ROC_bayes[:20]: 
    151151    print "%5.3f\t%5.3f" % t 
Note: See TracChangeset for help on using the changeset viewer.