Changeset 10633:fb05a6f3a235 in orange for Orange/evaluation/scoring.py
 Timestamp:
 03/23/12 20:20:57 (2 years ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/evaluation/scoring.py
r10580 r10633 50 50 if res.number_of_iterations < 2: 51 51 return [res] 52 52 53 53 ress = [Orange.evaluation.testing.ExperimentResults( 54 54 1, res.classifier_names, res.class_values, … … 68 68 r = Orange.evaluation.testing.ExperimentResults(res.numberOfIterations, 69 69 [res.classifierNames[i]], res.classValues, 70 weights=res.weights, base Class=res.baseClass,70 weights=res.weights, base_class=res.base_class, 71 71 classifiers=[res.classifiers[i]] if res.classifiers else [], 72 72 test_type=res.test_type, labels=res.labels) … … 118 118 if not stats[0]: 119 119 raise ValueError, "Cannot compute the score: no examples or sum of weights is 0." 120 120 121 121 if report_se: 122 122 return [(statc.mean(x), statc.sterr(x)) for x in stats] 123 123 else: 124 124 return [statc.mean(x) for x in stats] 125 125 126 126 def ME(res, **argkw): 127 127 MEs = [0.] * res.number_of_learners … … 129 129 if argkw.get("unweighted", 0) or not res.weights: 130 130 for tex in res.results: 131 MEs = map(lambda res, cls, ac =float(tex.actual_class):131 MEs = map(lambda res, cls, ac=float(tex.actual_class): 132 132 res + abs(float(cls)  ac), MEs, tex.classes) 133 133 totweight = gettotsize(res) … … 216 216 norm[tex.iteration_number] += abs(ai  a[tex.iteration_number]) 217 217 elif argkw.get("normsqr", 0): 218 norm[tex.iteration_number] += (ai  a[tex.iteration_number]) **2218 norm[tex.iteration_number] += (ai  a[tex.iteration_number]) ** 2 219 219 220 220 # iterate accross results of different regressors … … 223 223 scores[i][tex.iteration_number] += abs(float(cls)  ai) 224 224 else: 225 scores[i][tex.iteration_number] += (float(cls)  ai) **2225 scores[i][tex.iteration_number] += (float(cls)  ai) ** 2 226 226 else: # unweighted != 0 227 227 raise NotImplementedError, "weighted error scores with SE not implemented yet" … … 239 239 240 240 return [(statc.mean(x), statc.std(x)) for x in scores] 241 241 242 242 else: # single iteration (testing on a single test set) 243 243 scores = [0.] * res.number_of_learners … … 253 253 else: 254 254 scores = map(lambda res, cls, ac=float(tex.actual_class): 255 res + (float(cls)  ac) **2, scores, tex.classes)255 res + (float(cls)  ac) ** 2, scores, tex.classes) 256 256 257 257 if argkw.get("normabs", 0): 258 258 norm += abs(tex.actual_class  a) 259 259 elif argkw.get("normsqr", 0): 260 norm += (tex.actual_class  a) **2260 norm += (tex.actual_class  a) ** 2 261 261 totweight = gettotsize(res) 262 262 else: … … 266 266 MSEs = map(lambda res, cls, ac=float(tex.actual_class), 267 267 tw=tex.weight: 268 res + tw * (float(cls)  ac) **2, MSEs, tex.classes)268 res + tw * (float(cls)  ac) ** 2, MSEs, tex.classes) 269 269 totweight = gettotweight(res) 270 270 … … 285 285 """Compute meansquared error.""" 286 286 return regression_error(res, **argkw) 287 287 288 288 def RMSE(res, **argkw): 289 289 """Compute root meansquared error.""" … … 330 330 nIter[tex.iteration_number] += 1 331 331 for i, cls in enumerate(tex.classes): 332 MSEs[i][tex.iteration_number] += (float(cls)  ac) **2332 MSEs[i][tex.iteration_number] += (float(cls)  ac) ** 2 333 333 else: 334 334 raise ValueError, "weighted RMSE with SE not implemented yet" … … 337 337 MSEs = [[math.sqrt(x) for x in y] for y in MSEs] 338 338 return [(statc.mean(x), statc.std(x)) for x in MSEs] 339 339 340 340 else: 341 341 MSEs = [0.] * res.number_of_learners … … 343 343 for tex in res.results: 344 344 MSEs = map(lambda res, cls, ac=float(tex.actual_class): 345 res + (float(cls)  ac) **2, MSEs, tex.classes)345 res + (float(cls)  ac) ** 2, MSEs, tex.classes) 346 346 totweight = gettotsize(res) 347 347 else: 348 348 for tex in res.results: 349 349 MSEs = map(lambda res, cls, ac=float(tex.actual_class), 350 tw=tex.weight: res + tw * (float(cls)  ac) **2,350 tw=tex.weight: res + tw * (float(cls)  ac) ** 2, 351 351 MSEs, tex.classes) 352 352 totweight = gettotweight(res) … … 386 386 @deprecated_keywords({"reportSE": "report_se", 387 387 "unweighted": "ignore_weights"}) 388 def __init__(self, test_results, report_se =False, ignore_weights=False):388 def __init__(self, test_results, report_se=False, ignore_weights=False): 389 389 super(CA, self).__init__() 390 390 self.report_se = report_se … … 449 449 w = 1. if self.ignore_weights else tex.weight 450 450 for lrn in range(test_results.number_of_learners): 451 CAsByFold[lrn][tex.iteration_number] += (tex.classes[lrn] == 451 CAsByFold[lrn][tex.iteration_number] += (tex.classes[lrn] == 452 452 tex.actual_class) and w 453 453 foldN[tex.iteration_number] += w … … 472 472 """Compute the average probability assigned to the correct class.""" 473 473 if res.number_of_iterations == 1: 474 APs =[0.] * res.number_of_learners474 APs = [0.] * res.number_of_learners 475 475 if ignore_weights or not res.weights: 476 476 for tex in res.results: … … 525 525 526 526 if res.number_of_iterations == 1: 527 MSEs =[0.] * res.number_of_learners527 MSEs = [0.] * res.number_of_learners 528 528 if ignore_weights or not res.weights: 529 529 totweight = 0. 530 530 for tex in res.results: 531 531 MSEs = map(lambda res, probs: res + reduce( 532 lambda s, pi: s + pi **2, probs, 0) 532 lambda s, pi: s + pi ** 2, probs, 0)  533 533 2 * probs[tex.actual_class], MSEs, tex.probabilities) 534 534 totweight += tex.weight … … 536 536 for tex in res.results: 537 537 MSEs = map(lambda res, probs: res + tex.weight * reduce( 538 lambda s, pi: s + pi **2, probs, 0) 538 lambda s, pi: s + pi ** 2, probs, 0)  539 539 2 * probs[tex.actual_class], MSEs, tex.probabilities) 540 540 totweight = gettotweight(res) … … 553 553 for tex in res.results: 554 554 BSs[tex.iteration_number] = map(lambda rr, probs: rr + reduce( 555 lambda s, pi: s + pi **2, probs, 0) 555 lambda s, pi: s + pi ** 2, probs, 0)  556 556 2 * probs[tex.actual_class], BSs[tex.iteration_number], 557 557 tex.probabilities) … … 560 560 for tex in res.results: 561 561 BSs[tex.iteration_number] = map(lambda res, probs: 562 res + tex.weight * reduce(lambda s, pi: s + pi **2, probs, 0) 562 res + tex.weight * reduce(lambda s, pi: s + pi ** 2, probs, 0)  563 563 2 * probs[tex. actual_class], BSs[tex.iteration_number], 564 564 tex.probabilities) … … 572 572 573 573 def BSS(res, **argkw): 574 return [1  x / 2 for x in apply(Brier_score, (res, 574 return [1  x / 2 for x in apply(Brier_score, (res,), argkw)] 575 575 576 576 def IS_ex(Pc, P): … … 614 614 return [IS / totweight for IS in ISs] 615 615 616 616 617 617 ISs = [[0.] * res.number_of_iterations 618 618 for _ in range(res.number_of_learners)] … … 636 636 637 637 return statistics_by_folds(ISs, foldN, report_se, False) 638 638 639 639 640 640 def Wilcoxon(res, statistics, **argkw): … … 664 664 "classIndex": "class_index", 665 665 "unweighted": "ignore_weights"}) 666 def confusion_matrices(test_results, class_index= 1,666 def confusion_matrices(test_results, class_index= 1, 667 667 ignore_weights=False, cutoff=.5): 668 668 """ … … 678 678 """ 679 679 tfpns = [ConfusionMatrix() for _ in range(test_results.number_of_learners)] 680 680 681 681 if class_index < 0: 682 682 numberOfClasses = len(test_results.class_values) … … 699 699 cm[li][trueClass][predClass] += tex.weight 700 700 return cm 701 702 elif test_results.base Class >= 0:703 class_index = test_results.base Class701 702 elif test_results.base_class >= 0: 703 class_index = test_results.base_class 704 704 else: 705 705 class_index = 1 … … 708 708 if ignore_weights or not test_results.weights: 709 709 for lr in test_results.results: 710 isPositive =(lr.actual_class==class_index)710 isPositive = (lr.actual_class == class_index) 711 711 for i in range(test_results.number_of_learners): 712 712 tfpns[i].addTFPosNeg(lr.probabilities[i][class_index] > … … 714 714 else: 715 715 for lr in test_results.results: 716 isPositive =(lr.actual_class == class_index)716 isPositive = (lr.actual_class == class_index) 717 717 for i in range(test_results.number_of_learners): 718 tfpns[i].addTFPosNeg(lr.probabilities[i][class_index] > 718 tfpns[i].addTFPosNeg(lr.probabilities[i][class_index] > 719 719 cutoff, isPositive, lr.weight) 720 720 else: … … 766 766 if not e: 767 767 return 1, 1, 1 768 ss += (o  e) **2 / e769 df = (dim  1) **2768 ss += (o  e) ** 2 / e 769 df = (dim  1) ** 2 770 770 return ss, df, statc.chisqprob(ss, df) 771 771 … … 807 807 @classmethod 808 808 def compute(self, confusion_matrix): 809 tot = confusion_matrix.TP +confusion_matrix.FN809 tot = confusion_matrix.TP + confusion_matrix.FN 810 810 if tot < 1e6: 811 811 import warnings … … 831 831 @classmethod 832 832 def compute(self, confusion_matrix): 833 tot = confusion_matrix.FP +confusion_matrix.TN833 tot = confusion_matrix.FP + confusion_matrix.TN 834 834 if tot < 1e6: 835 835 import warnings … … 851 851 warnings.warn("Can't compute PPV: one or both classes have no instances") 852 852 return None 853 return confusion_matrix.TP /tot853 return confusion_matrix.TP / tot 854 854 855 855 … … 885 885 p = Precision.compute(confusion_matrix) 886 886 r = Recall.compute(confusion_matrix) 887 if p is not None and r is not None and (p +r) != 0:887 if p is not None and r is not None and (p + r) != 0: 888 888 return 2. * p * r / (p + r) 889 889 else: … … 918 918 # code by Boris Gorelik 919 919 TP, TN, FP, FN = cm.TP, cm.TN, cm.FP, cm.FN 920 920 921 921 try: 922 return (TP *TN  FP*FN) /\923 math.sqrt((TP +FP) * (TP+FN) * (TN+ FP) * (TN+FN))922 return (TP * TN  FP * FN) / \ 923 math.sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN)) 924 924 except ZeroDivisionError: 925 925 # Zero division occurs when there is either no true positives … … 991 991 @deprecated_keywords({"classIndex": "class_index", 992 992 "unweighted": "ignore_weights"}) 993 def AUCWilcoxon(res, class_index= 1, ignore_weights=False, **argkw):993 def AUCWilcoxon(res, class_index= 1, ignore_weights=False, **argkw): 994 994 """Compute the area under ROC (AUC) and its standard error using 995 995 Wilcoxon's approach proposed by Hanley and McNeal (1982). If … … 1015 1015 highPos = thisPos 1016 1016 W += thisNeg * (highPos + thisPos / 2.) 1017 Q2 += thisPos * (lowNeg **2 + lowNeg*thisNeg + thisNeg**2 / 3.)1018 Q1 += thisNeg * (highPos **2 + highPos*thisPos + thisPos**2 / 3.)1017 Q2 += thisPos * (lowNeg ** 2 + lowNeg * thisNeg + thisNeg ** 2 / 3.) 1018 Q1 += thisNeg * (highPos ** 2 + highPos * thisPos + thisPos ** 2 / 3.) 1019 1019 1020 1020 lowNeg += thisNeg 1021 1021 1022 W /= (totPos*totNeg)1023 Q1 /= (totNeg *totPos**2)1024 Q2 /= (totPos *totNeg**2)1025 1026 SE = math.sqrt((W * (1  W) + (totPos  1) * (Q1  W **2) +1027 (totNeg  1) * (Q2  W **2)) / (totPos * totNeg))1022 W /= (totPos * totNeg) 1023 Q1 /= (totNeg * totPos ** 2) 1024 Q2 /= (totPos * totNeg ** 2) 1025 1026 SE = math.sqrt((W * (1  W) + (totPos  1) * (Q1  W ** 2) + 1027 (totNeg  1) * (Q2  W ** 2)) / (totPos * totNeg)) 1028 1028 results.append((W, SE)) 1029 1029 return results … … 1034 1034 @deprecated_keywords({"classIndex": "class_index", 1035 1035 "unweighted": "ignore_weights"}) 1036 def compare_2_AUCs(res, lrn1, lrn2, class_index= 1,1036 def compare_2_AUCs(res, lrn1, lrn2, class_index= 1, 1037 1037 ignore_weights=False, **argkw): 1038 1038 return corn.compare2ROCs(res, lrn1, lrn2, class_index, … … 1040 1040 1041 1041 # for backward compatibility, compare_2_AROCs is obsolete 1042 compare_2_AROCs = compare_2_AUCs 1042 compare_2_AROCs = compare_2_AUCs 1043 1043 1044 1044 1045 1045 @deprecated_keywords({"classIndex": "class_index"}) 1046 def compute_ROC(res, class_index= 1):1046 def compute_ROC(res, class_index= 1): 1047 1047 """Compute a ROC curve as a list of (x, y) tuples, where x is 1048 1048 1specificity and y is sensitivity. … … 1054 1054 1055 1055 for plist in problists: 1056 curve =[(1., 1.)]1056 curve = [(1., 1.)] 1057 1057 TP, TN = totPos, 0. 1058 1058 FN, FP = 0., totNeg … … 1071 1071 results.append(curve) 1072 1072 1073 return results 1073 return results 1074 1074 1075 1075 ## TC's implementation of algorithms, taken from: … … 1103 1103 @deprecated_keywords({"classIndex": "class_index", 1104 1104 "keepConcavities": "keep_concavities"}) 1105 def TC_compute_ROC(res, class_index= 1, keep_concavities=1):1105 def TC_compute_ROC(res, class_index= 1, keep_concavities=1): 1106 1106 problists, tots = corn.computeROCCumulative(res, class_index) 1107 1107 … … 1114 1114 TP = 0. 1115 1115 FP = 0. 1116 curve =[]1116 curve = [] 1117 1117 fPrev = 10e300 # "infinity" score at 0., 0. 1118 1118 for prob in plist: … … 1170 1170 else: 1171 1171 if abs(d  mind) <= 0.0001: ## close enough 1172 closestPoints.append( (x, y, fscore))1173 return closestPoints 1172 closestPoints.append((x, y, fscore)) 1173 return closestPoints 1174 1174 1175 1175 def frange(start, end=None, inc=None): … … 1193 1193 break 1194 1194 L.append(next) 1195 1195 1196 1196 return L 1197 1197 … … 1203 1203 ## returns the average ROC curve and an array of (vertical) standard deviations 1204 1204 @deprecated_keywords({"ROCcurves": "roc_curves"}) 1205 def TC_vertical_average_ROC(roc_curves, samples =10):1205 def TC_vertical_average_ROC(roc_curves, samples=10): 1206 1206 def INTERPOLATE((P1x, P1y, P1fscore), (P2x, P2y, P2fscore), X): 1207 1207 if (P1x == P2x) or P1x < X > P2x or P1x > X < P2x: … … 1310 1310 else: 1311 1311 stdv = 0. 1312 TPstdV.append( stdv)1312 TPstdV.append(stdv) 1313 1313 ## horizontal standard deviation 1314 1314 if len(FPsum) > 1: … … 1316 1316 else: 1317 1317 stdh = 0. 1318 TPstdH.append( stdh)1318 TPstdH.append(stdh) 1319 1319 1320 1320 average.append(TPavg) … … 1331 1331 ##  noClassRugPoints is an array of (x, 0) points 1332 1332 @deprecated_keywords({"classIndex": "class_index"}) 1333 def compute_calibration_curve(res, class_index= 1):1333 def compute_calibration_curve(res, class_index= 1): 1334 1334 ## merge multiple iterations into one 1335 1335 mres = Orange.evaluation.testing.ExperimentResults(1, res.classifier_names, … … 1346 1346 1347 1347 for plist in problists: 1348 yesClassRugPoints = [] 1348 yesClassRugPoints = [] 1349 1349 noClassRugPoints = [] 1350 1350 … … 1355 1355 noClassRugPoints.append((f, thisNeg)) # 1. 1356 1356 1357 index = int(f * bins 1357 index = int(f * bins) 1358 1358 index = min(index, bins  1) ## just in case for value 1. 1359 1359 yesBinsVals[index] += thisPos … … 1368 1368 if allVal == 0.: continue 1369 1369 y = float(yesVal) / float(allVal) 1370 curve.append((f, 1370 curve.append((f, y)) 1371 1371 1372 1372 ## smooth the curve … … 1397 1397 ## on the Lift Curve 1398 1398 @deprecated_keywords({"classIndex": "class_index"}) 1399 def compute_lift_curve(res, class_index= 1):1399 def compute_lift_curve(res, class_index= 1): 1400 1400 ## merge multiple iterations into one 1401 1401 mres = Orange.evaluation.testing.ExperimentResults(1, res.classifier_names, … … 1428 1428 def __init__(self, C=0., D=0., T=0.): 1429 1429 self.C, self.D, self.T = C, D, T 1430 1430 1431 1431 def is_CDT_empty(cdt): 1432 1432 return cdt.C + cdt.D + cdt.T < 1e20 … … 1435 1435 @deprecated_keywords({"classIndex": "class_index", 1436 1436 "unweighted": "ignore_weights"}) 1437 def compute_CDT(res, class_index= 1, ignore_weights=False, **argkw):1437 def compute_CDT(res, class_index= 1, ignore_weights=False, **argkw): 1438 1438 """Obsolete, don't use.""" 1439 1439 if class_index < 0: 1440 if res.base Class >= 0:1441 class_index = res.base Class1440 if res.base_class >= 0: 1441 class_index = res.base_class 1442 1442 else: 1443 1443 class_index = 1 1444 1444 1445 1445 useweights = res.weights and not ignore_weights 1446 1446 weightByClasses = argkw.get("weightByClasses", True) 1447 1447 1448 if res.number_of_iterations >1:1448 if res.number_of_iterations > 1: 1449 1449 CDTs = [CDT() for _ in range(res.number_of_learners)] 1450 1450 iterationExperiments = split_by_iterations(res) … … 1458 1458 if is_CDT_empty(CDTs[0]): 1459 1459 return corn.computeCDT(res, class_index, useweights) 1460 1460 1461 1461 return CDTs 1462 1462 else: … … 1526 1526 super(AUC, self).__init__() 1527 1527 1528 self.ignore_weights =ignore_weights1529 self.method =multiclass1528 self.ignore_weights = ignore_weights 1529 self.method = multiclass 1530 1530 1531 1531 if test_results is not None: … … 1600 1600 1601 1601 if usefulClassPairs > 0: 1602 sum_aucs = [x /usefulClassPairs for x in sum_aucs]1602 sum_aucs = [x / usefulClassPairs for x in sum_aucs] 1603 1603 1604 1604 return sum_aucs … … 1614 1614 subsum_aucs = [0.] * iterations[0].number_of_learners 1615 1615 for ite in iterations: 1616 aucs, foldsUsed = auc_computer(*(ite, 1616 aucs, foldsUsed = auc_computer(*(ite,) + computer_args) 1617 1617 if not aucs: 1618 1618 import warnings … … 1686 1686 Compute AUC using a :obj:`cdt_computer`. 1687 1687 """ 1688 cdts = cdt_computer(*(ite, 1688 cdts = cdt_computer(*(ite,) + computer_args) 1689 1689 if not is_CDT_empty(cdts[0]): 1690 1690 return [(cdt.C + cdt.T / 2) / (cdt.C + cdt.D + cdt.T) / … … 1692 1692 1693 1693 if all_ite: 1694 cdts = cdt_computer(*(all_ite, 1694 cdts = cdt_computer(*(all_ite,) + computer_args) 1695 1695 if not is_CDT_empty(cdts[0]): 1696 1696 return [(cdt.C + cdt.T / 2) / (cdt.C + cdt.D + cdt.T) … … 1704 1704 out and all other classes are treated as a single class. 1705 1705 """ 1706 def __init__(self, test_results=None, class_index= 1, ignore_weights=False):1706 def __init__(self, test_results=None, class_index= 1, ignore_weights=False): 1707 1707 if class_index < 0: 1708 1708 if test_results and test_results.base_class >= 0: … … 1858 1858 su = mcm[l1][l2] + mcm[l2][l1] 1859 1859 if su: 1860 mcm[l2][l1] = (abs(mcm[l1][l2]  mcm[l2][l1])  1) **2 / su1860 mcm[l2][l1] = (abs(mcm[l1][l2]  mcm[l2][l1])  1) ** 2 / su 1861 1861 else: 1862 1862 mcm[l2][l1] = 0 1863 1863 1864 1864 for l1 in range(nLearners): 1865 mcm[l1] =mcm[l1][:l1]1865 mcm[l1] = mcm[l1][:l1] 1866 1866 1867 1867 return mcm … … 1893 1893 su = tf + ft 1894 1894 if su: 1895 return (abs(tf  ft)  1) **2 / su1895 return (abs(tf  ft)  1) ** 2 / su 1896 1896 else: 1897 1897 return 0 … … 1905 1905 res_split = split_by_iterations(res) 1906 1906 res = [stat(r) for r in res_split] 1907 1907 1908 1908 N = len(res) 1909 1909 k = len(res[0]) … … 1918 1918 sums = [x / N for x in sums] 1919 1919 1920 F = 12. / (N * k * (k + 1)) * T 1920 F = 12. / (N * k * (k + 1)) * T  3 * N * (k + 1) 1921 1921 1922 1922 return F, statc.chisqprob(F, k  1), sums … … 1941 1941 for m2 in range(m1 + 1, k): 1942 1942 t, p = statc.wilcoxont([r[m1] for r in res], [r[m2] for r in res]) 1943 if avgranks[m1] <avgranks[m2]:1943 if avgranks[m1] < avgranks[m2]: 1944 1944 nl.append(p) 1945 elif avgranks[m2] <avgranks[m1]:1945 elif avgranks[m2] < avgranks[m1]: 1946 1946 nl.append(p) 1947 1947 else: … … 1969 1969 file = open(file, "wt") 1970 1970 fopened = 1 1971 1971 1972 1972 file.write("set yrange [0:1]\n") 1973 1973 file.write("set xrange [%f:%f]\n" % (proportions[0], proportions[1])) … … 1978 1978 for i in range(len(legend)  1): 1979 1979 if not no_confidence: 1980 file.write("'' title '' with yerrorbars pointtype %i,\\\n" % (i +1))1981 file.write("'' title '%s' with linespoints pointtype %i,\\\n" % (legend[i], i +1))1980 file.write("'' title '' with yerrorbars pointtype %i,\\\n" % (i + 1)) 1981 file.write("'' title '%s' with linespoints pointtype %i,\\\n" % (legend[i], i + 1)) 1982 1982 if not no_confidence: 1983 1983 file.write("'' title '' with yerrorbars pointtype %i,\\\n" % (len(legend))) … … 1987 1987 if not no_confidence: 1988 1988 for p in range(len(proportions)): 1989 file.write("%f\t%f\t%f\n" % (proportions[p], CAs[p][i][0], 1.96 *CAs[p][i][1]))1989 file.write("%f\t%f\t%f\n" % (proportions[p], CAs[p][i][0], 1.96 * CAs[p][i][1])) 1990 1990 file.write("e\n\n") 1991 1991 … … 2000 2000 def print_single_ROC_curve_coordinates(file, curve): 2001 2001 import types 2002 fopened =02003 if type(file) ==types.StringType:2004 file =open(file, "wt")2005 fopened =12002 fopened = 0 2003 if type(file) == types.StringType: 2004 file = open(file, "wt") 2005 fopened = 1 2006 2006 2007 2007 for coord in curve: … … 2014 2014 def plot_ROC_learners(file, curves, learners): 2015 2015 plot_ROC(file, curves, [Orange.misc.getobjectname(learners[i], "Learner %i" % i) for i in range(len(learners))]) 2016 2016 2017 2017 def plot_ROC(file, curves, legend): 2018 2018 import types 2019 fopened =02020 if type(file) ==types.StringType:2021 file =open(file, "wt")2022 fopened =12019 fopened = 0 2020 if type(file) == types.StringType: 2021 file = open(file, "wt") 2022 fopened = 1 2023 2023 2024 2024 file.write("set yrange [0:1]\n") … … 2036 2036 file.write("e\n\n") 2037 2037 2038 file.write("1.0\t1.0\n0.0\t0.0e\n\n") 2038 file.write("1.0\t1.0\n0.0\t0.0e\n\n") 2039 2039 2040 2040 if fopened: … … 2043 2043 2044 2044 @deprecated_keywords({"allResults": "all_results"}) 2045 def plot_McNemar_curve_learners(file, all_results, proportions, learners, reference= 1):2045 def plot_McNemar_curve_learners(file, all_results, proportions, learners, reference= 1): 2046 2046 plot_McNemar_curve(file, all_results, proportions, [Orange.misc.getobjectname(learners[i], "Learner %i" % i) for i in range(len(learners))], reference) 2047 2047 2048 2048 2049 2049 @deprecated_keywords({"allResults": "all_results"}) 2050 def plot_McNemar_curve(file, all_results, proportions, legend, reference= 1):2051 if reference <0:2052 reference =len(legend)12053 2050 def plot_McNemar_curve(file, all_results, proportions, legend, reference= 1): 2051 if reference < 0: 2052 reference = len(legend)  1 2053 2054 2054 import types 2055 fopened =02056 if type(file) ==types.StringType:2057 file =open(file, "wt")2058 fopened =12059 2055 fopened = 0 2056 if type(file) == types.StringType: 2057 file = open(file, "wt") 2058 fopened = 1 2059 2060 2060 #file.write("set yrange [0:1]\n") 2061 2061 #file.write("set xrange [%f:%f]\n" % (proportions[0], proportions[1])) 2062 2062 file.write("set multiplot\n\n") 2063 2063 file.write("plot \\\n") 2064 tmap =range(reference)+range(reference+1, len(legend))2064 tmap = range(reference) + range(reference + 1, len(legend)) 2065 2065 for i in tmap[:1]: 2066 file.write("'' title '%s' with linespoints pointtype %i,\\\n" % (legend[i], i +1))2066 file.write("'' title '%s' with linespoints pointtype %i,\\\n" % (legend[i], i + 1)) 2067 2067 file.write("'' title '%s' with linespoints pointtype %i\n" % (legend[tmap[1]], tmap[1])) 2068 2068 file.write("\n") … … 2076 2076 file.close() 2077 2077 2078 default_point_types =("{$\\circ$}", "{$\\diamond$}", "{$+$}", "{$\\times$}", "{$$}")+tuple([chr(x) for x in range(97, 122)])2079 default_line_types =("\\setsolid", "\\setdashpattern <4pt, 2pt>", "\\setdashpattern <8pt, 2pt>", "\\setdashes", "\\setdots")2078 default_point_types = ("{$\\circ$}", "{$\\diamond$}", "{$+$}", "{$\\times$}", "{$$}") + tuple([chr(x) for x in range(97, 122)]) 2079 default_line_types = ("\\setsolid", "\\setdashpattern <4pt, 2pt>", "\\setdashpattern <8pt, 2pt>", "\\setdashes", "\\setdots") 2080 2080 2081 2081 @deprecated_keywords({"allResults": "all_results"}) … … 2087 2087 def learning_curve_to_PiCTeX(file, all_results, proportions, **options): 2088 2088 import types 2089 fopened =02090 if type(file) ==types.StringType:2091 file =open(file, "wt")2092 fopened =12093 2094 nexamples =len(all_results[0].results)2089 fopened = 0 2090 if type(file) == types.StringType: 2091 file = open(file, "wt") 2092 fopened = 1 2093 2094 nexamples = len(all_results[0].results) 2095 2095 CAs = [CA(x, report_se=True) for x in all_results] 2096 2096 2097 graphsize =float(options.get("graphsize", 10.0)) #cm2098 difprop =proportions[1]proportions[0]2099 ntestexamples =nexamples*proportions[1]2100 xunit =graphsize/ntestexamples2101 2102 yshift =float(options.get("yshift", ntestexamples/20.))2103 2104 pointtypes =options.get("pointtypes", default_point_types)2105 linetypes =options.get("linetypes", default_line_types)2097 graphsize = float(options.get("graphsize", 10.0)) #cm 2098 difprop = proportions[1]  proportions[0] 2099 ntestexamples = nexamples * proportions[1] 2100 xunit = graphsize / ntestexamples 2101 2102 yshift = float(options.get("yshift", ntestexamples / 20.)) 2103 2104 pointtypes = options.get("pointtypes", default_point_types) 2105 linetypes = options.get("linetypes", default_line_types) 2106 2106 2107 2107 if options.has_key("numberedx"): 2108 numberedx =options["numberedx"]2109 if type(numberedx) ==types.IntType:2110 if numberedx >0:2111 numberedx =[nexamples*proportions[int(i/float(numberedx)*len(proportions))] for i in range(numberedx)]+[proportions[1]*nexamples]2112 elif numberedx <0:2108 numberedx = options["numberedx"] 2109 if type(numberedx) == types.IntType: 2110 if numberedx > 0: 2111 numberedx = [nexamples * proportions[int(i / float(numberedx) * len(proportions))] for i in range(numberedx)] + [proportions[1] * nexamples] 2112 elif numberedx < 0: 2113 2113 numberedx = numberedx 2114 newn =[]2115 for i in range(numberedx +1):2116 wanted =proportions[0]+float(i)/numberedx*difprop2117 best =(10, 0)2114 newn = [] 2115 for i in range(numberedx + 1): 2116 wanted = proportions[0] + float(i) / numberedx * difprop 2117 best = (10, 0) 2118 2118 for t in proportions: 2119 td =abs(wantedt)2120 if td <best[0]:2121 best =(td, t)2119 td = abs(wanted  t) 2120 if td < best[0]: 2121 best = (td, t) 2122 2122 if not best[1] in newn: 2123 2123 newn.append(best[1]) 2124 2124 newn.sort() 2125 numberedx =[nexamples*x for x in newn]2126 elif type(numberedx[0]) ==types.FloatType:2127 numberedx =[nexamples*x for x in numberedx]2125 numberedx = [nexamples * x for x in newn] 2126 elif type(numberedx[0]) == types.FloatType: 2127 numberedx = [nexamples * x for x in numberedx] 2128 2128 else: 2129 numberedx =[nexamples*x for x in proportions]2129 numberedx = [nexamples * x for x in proportions] 2130 2130 2131 2131 file.write("\\mbox{\n") 2132 2132 file.write(" \\beginpicture\n") 2133 file.write(" \\setcoordinatesystem units <%10.8fcm, %5.3fcm>\n\n" % (xunit, graphsize)) 2134 file.write(" \\setplotarea x from %5.3f to %5.3f, y from 0 to 1\n" % (0, ntestexamples)) 2133 file.write(" \\setcoordinatesystem units <%10.8fcm, %5.3fcm>\n\n" % (xunit, graphsize)) 2134 file.write(" \\setplotarea x from %5.3f to %5.3f, y from 0 to 1\n" % (0, ntestexamples)) 2135 2135 file.write(" \\axis bottom invisible\n")# label {#examples}\n") 2136 file.write(" ticks short at %s /\n" % reduce(lambda x, y:x+" "+y, ["%i"%(x*nexamples+0.5) for x in proportions]))2136 file.write(" ticks short at %s /\n" % reduce(lambda x, y:x + " " + y, ["%i" % (x * nexamples + 0.5) for x in proportions])) 2137 2137 if numberedx: 2138 file.write(" long numbered at %s /\n" % reduce(lambda x, y:x+y, ["%i " % int(x+0.5) for x in numberedx]))2138 file.write(" long numbered at %s /\n" % reduce(lambda x, y:x + y, ["%i " % int(x + 0.5) for x in numberedx])) 2139 2139 file.write(" /\n") 2140 2140 file.write(" \\axis left invisible\n")# label {classification accuracy}\n") … … 2146 2146 file.write(" \\setdashpattern<1pt, 1pt>\n") 2147 2147 file.write(" \\plot %5.3f %5.3f %5.3f %5.3f /\n" % (0., options["default"], ntestexamples, options["default"])) 2148 2148 2149 2149 for i in range(len(CAs[0])): 2150 coordinates =reduce(lambda x,y:x+" "+y, ["%i %5.3f" % (proportions[p]*nexamples, CAs[p][i][0]) for p in range(len(proportions))])2150 coordinates = reduce(lambda x, y:x + " " + y, ["%i %5.3f" % (proportions[p] * nexamples, CAs[p][i][0]) for p in range(len(proportions))]) 2151 2151 if linetypes: 2152 2152 file.write(" %s\n" % linetypes[i]) … … 2164 2164 def legend_learners_to_PiCTeX(file, learners, **options): 2165 2165 return apply(legend_to_PiCTeX, (file, [Orange.misc.getobjectname(learners[i], "Learner %i" % i) for i in range(len(learners))]), options) 2166 2166 2167 2167 def legend_to_PiCTeX(file, legend, **options): 2168 2168 import types 2169 fopened =02170 if type(file) ==types.StringType:2171 file =open(file, "wt")2172 fopened =12173 2174 pointtypes =options.get("pointtypes", default_point_types)2175 linetypes =options.get("linetypes", default_line_types)2169 fopened = 0 2170 if type(file) == types.StringType: 2171 file = open(file, "wt") 2172 fopened = 1 2173 2174 pointtypes = options.get("pointtypes", default_point_types) 2175 linetypes = options.get("linetypes", default_line_types) 2176 2176 2177 2177 file.write("\\mbox{\n") … … 2183 2183 if linetypes: 2184 2184 file.write(" %s\n" % linetypes[i]) 2185 file.write(" \\plot %5.3f 6 %5.3f 6 /\n" % (i, i +0.2))2185 file.write(" \\plot %5.3f 6 %5.3f 6 /\n" % (i, i + 0.2)) 2186 2186 if pointtypes: 2187 file.write(" \\put {%s} at %5.3f 6\n" % (pointtypes[i], i +0.1))2188 file.write(" \\put {%s} [lb] at %5.3f 0\n" % (legend[i], i +0.25))2187 file.write(" \\put {%s} at %5.3f 6\n" % (pointtypes[i], i + 0.1)) 2188 file.write(" \\put {%s} [lb] at %5.3f 0\n" % (legend[i], i + 0.25)) 2189 2189 2190 2190 file.write(" \\endpicture\n") … … 2205 2205 2206 2206 def friedman(N, k, ranks): 2207 return 12 *N*(sum([rank**2.0 for rank in ranks])  (k*(k+1)*(k+1)/4.0) )/(k*(k+1))2207 return 12 * N * (sum([rank ** 2.0 for rank in ranks])  (k * (k + 1) * (k + 1) / 4.0)) / (k * (k + 1)) 2208 2208 2209 2209 def iman(fried, N, k): 2210 return (N 1)*fried/(N*(k1)  fried)2210 return (N  1) * fried / (N * (k  1)  fried) 2211 2211 2212 2212 f = friedman(N, k, avranks) 2213 2213 im = iman(f, N, k) 2214 fdistdof = (k 1, (k1)*(N1))2215 2216 return (f, k 1), (im, fdistdof)2214 fdistdof = (k  1, (k  1) * (N  1)) 2215 2216 return (f, k  1), (im, fdistdof) 2217 2217 2218 2218 def compute_CD(avranks, N, alpha="0.05", type="nemenyi"): … … 2224 2224 2225 2225 k = len(avranks) 2226 2226 2227 2227 d = {("nemenyi", "0.05"): [0, 0, 1.959964, 2.343701, 2.569032, 2.727774, 2228 2228 2.849705, 2.94832, 3.030879, 3.101730, 3.163684, … … 2243 2243 q = d[(type, alpha)] 2244 2244 2245 cd = q[k] *(k*(k+1)/(6.0*N))**0.52245 cd = q[k] * (k * (k + 1) / (6.0 * N)) ** 0.5 2246 2246 2247 2247 return cd 2248 2248 2249 2249 2250 2250 def graph_ranks(filename, avranks, names, cd=None, cdmethod=None, lowv=None, highv=None, width=6, textspace=1, reverse=False, **kwargs): … … 2282 2282 textspace = float(textspace) 2283 2283 2284 def nth(l, n):2284 def nth(l, n): 2285 2285 """ 2286 2286 Returns only nth elemnt in a list. 2287 2287 """ 2288 n = lloc(l, n)2288 n = lloc(l, n) 2289 2289 return [ a[n] for a in l ] 2290 2290 2291 def lloc(l, n):2291 def lloc(l, n): 2292 2292 """ 2293 2293 List location in list of list structure. … … 2296 2296 """ 2297 2297 if n < 0: 2298 return len(l[0]) +n2299 else: 2300 return n 2298 return len(l[0]) + n 2299 else: 2300 return n 2301 2301 2302 2302 def mxrange(lr): … … 2334 2334 sums = avranks 2335 2335 2336 tempsort = sorted([ (a,i) for i,a in enumerate(sums) ], reverse=reverse)2336 tempsort = sorted([ (a, i) for i, a in enumerate(sums) ], reverse=reverse) 2337 2337 ssums = nth(tempsort, 0) 2338 2338 sortidx = nth(tempsort, 1) 2339 2339 nnames = [ names[x] for x in sortidx ] 2340 2340 2341 2341 if lowv is None: 2342 2342 lowv = min(1, int(math.floor(min(ssums)))) … … 2351 2351 2352 2352 linesblank = 0 2353 scalewidth = width  2 *textspace2353 scalewidth = width  2 * textspace 2354 2354 2355 2355 def rankpos(rank): … … 2358 2358 else: 2359 2359 a = highv  rank 2360 return textspace +scalewidth/(highvlowv)*a2360 return textspace + scalewidth / (highv  lowv) * a 2361 2361 2362 2362 distanceh = 0.25 2363 2363 2364 2364 if cd and cdmethod is None: 2365 2365 2366 2366 #get pairs of non significant methods 2367 2367 … … 2370 2370 #get all pairs 2371 2371 lsums = len(sums) 2372 allpairs = [ (i, j) for i,j in mxrange([[lsums], [lsums]]) if j > i ]2372 allpairs = [ (i, j) for i, j in mxrange([[lsums], [lsums]]) if j > i ] 2373 2373 #remove not significant 2374 notSig = [ (i, j) for i,j in allpairs if abs(sums[i]sums[j]) <= hsd ]2374 notSig = [ (i, j) for i, j in allpairs if abs(sums[i]  sums[j]) <= hsd ] 2375 2375 #keep only longest 2376 2377 def no_longer((i, j), notSig):2378 for i1, j1 in notSig:2376 2377 def no_longer((i, j), notSig): 2378 for i1, j1 in notSig: 2379 2379 if (i1 <= i and j1 > j) or (i1 < i and j1 >= j): 2380 2380 return False 2381 2381 return True 2382 2382 2383 longest = [ (i, j) for i,j in notSig if no_longer((i,j),notSig) ]2384 2383 longest = [ (i, j) for i, j in notSig if no_longer((i, j), notSig) ] 2384 2385 2385 return longest 2386 2386 2387 2387 lines = get_lines(ssums, cd) 2388 linesblank = 0.2 + 0.2 + (len(lines) 1)*0.12388 linesblank = 0.2 + 0.2 + (len(lines)  1) * 0.1 2389 2389 2390 2390 #add scale … … 2393 2393 2394 2394 #calculate height needed height of an image 2395 minnotsignificant = max(2 *0.2, linesblank)2396 height = cline + ((k +1)/2)*0.2 + minnotsignificant2395 minnotsignificant = max(2 * 0.2, linesblank) 2396 height = cline + ((k + 1) / 2) * 0.2 + minnotsignificant 2397 2397 2398 2398 fig = Figure(figsize=(width, height)) 2399 ax = fig.add_axes([0, 0,1,1]) #reverse y axis2399 ax = fig.add_axes([0, 0, 1, 1]) #reverse y axis 2400 2400 ax.set_axis_off() 2401 2401 2402 hf = 1. /height # height factor2403 wf = 1. /width2404 2405 def hfl(l): 2406 return [ a *hf for a in l ]2407 2408 def wfl(l): 2409 return [ a *wf for a in l ]2402 hf = 1. / height # height factor 2403 wf = 1. / width 2404 2405 def hfl(l): 2406 return [ a * hf for a in l ] 2407 2408 def wfl(l): 2409 return [ a * wf for a in l ] 2410 2410 2411 2411 2412 2412 # Upper left corner is (0,0). 2413 2413 2414 ax.plot([0, 1], [0,1], c="w")2414 ax.plot([0, 1], [0, 1], c="w") 2415 2415 ax.set_xlim(0, 1) 2416 2416 ax.set_ylim(1, 0) … … 2420 2420 Input is a list of pairs of points. 2421 2421 """ 2422 ax.plot(wfl(nth(l, 0)), hfl(nth(l,1)), color=color, **kwargs)2422 ax.plot(wfl(nth(l, 0)), hfl(nth(l, 1)), color=color, **kwargs) 2423 2423 2424 2424 def text(x, y, s, *args, **kwargs): 2425 ax.text(wf *x, hf*y, s, *args, **kwargs)2426 2427 line([(textspace, cline), (width textspace, cline)], linewidth=0.7)2428 2425 ax.text(wf * x, hf * y, s, *args, **kwargs) 2426 2427 line([(textspace, cline), (width  textspace, cline)], linewidth=0.7) 2428 2429 2429 bigtick = 0.1 2430 2430 smalltick = 0.05 … … 2436 2436 tick = smalltick 2437 2437 if a == int(a): tick = bigtick 2438 line([(rankpos(a), cline tick/2),(rankpos(a), cline)], linewidth=0.7)2439 2440 for a in range(lowv, highv +1):2441 text(rankpos(a), cline tick/20.05, str(a), ha="center", va="bottom")2438 line([(rankpos(a), cline  tick / 2), (rankpos(a), cline)], linewidth=0.7) 2439 2440 for a in range(lowv, highv + 1): 2441 text(rankpos(a), cline  tick / 2  0.05, str(a), ha="center", va="bottom") 2442 2442 2443 2443 k = len(ssums) 2444 2444 2445 for i in range((k +1)/2):2446 chei = cline + minnotsignificant + i *0.22447 line([(rankpos(ssums[i]), cline), (rankpos(ssums[i]), chei), (textspace 0.1, chei)], linewidth=0.7)2448 text(textspace 0.2, chei, nnames[i], ha="right", va="center")2449 2450 for i in range((k +1)/2, k):2451 chei = cline + minnotsignificant + (k i1)*0.22452 line([(rankpos(ssums[i]), cline), (rankpos(ssums[i]), chei), (textspace +scalewidth+0.1, chei)], linewidth=0.7)2453 text(textspace +scalewidth+0.2, chei, nnames[i], ha="left", va="center")2445 for i in range((k + 1) / 2): 2446 chei = cline + minnotsignificant + i * 0.2 2447 line([(rankpos(ssums[i]), cline), (rankpos(ssums[i]), chei), (textspace  0.1, chei)], linewidth=0.7) 2448 text(textspace  0.2, chei, nnames[i], ha="right", va="center") 2449 2450 for i in range((k + 1) / 2, k): 2451 chei = cline + minnotsignificant + (k  i  1) * 0.2 2452 line([(rankpos(ssums[i]), cline), (rankpos(ssums[i]), chei), (textspace + scalewidth + 0.1, chei)], linewidth=0.7) 2453 text(textspace + scalewidth + 0.2, chei, nnames[i], ha="left", va="center") 2454 2454 2455 2455 if cd and cdmethod is None: … … 2457 2457 #upper scale 2458 2458 if not reverse: 2459 begin, end = rankpos(lowv), rankpos(lowv +cd)2459 begin, end = rankpos(lowv), rankpos(lowv + cd) 2460 2460 else: 2461 2461 begin, end = rankpos(highv), rankpos(highv  cd) 2462 2462 2463 2463 line([(begin, distanceh), (end, distanceh)], linewidth=0.7) 2464 line([(begin, distanceh + bigtick /2), (begin, distanceh  bigtick/2)], linewidth=0.7)2465 line([(end, distanceh + bigtick /2), (end, distanceh  bigtick/2)], linewidth=0.7)2466 text((begin +end)/2, distanceh  0.05, "CD", ha="center", va="bottom")2464 line([(begin, distanceh + bigtick / 2), (begin, distanceh  bigtick / 2)], linewidth=0.7) 2465 line([(end, distanceh + bigtick / 2), (end, distanceh  bigtick / 2)], linewidth=0.7) 2466 text((begin + end) / 2, distanceh  0.05, "CD", ha="center", va="bottom") 2467 2467 2468 2468 #non significance lines 2469 2469 def draw_lines(lines, side=0.05, height=0.1): 2470 2470 start = cline + 0.2 2471 for l, r in lines:2472 line([(rankpos(ssums[l]) side, start), (rankpos(ssums[r])+side, start)], linewidth=2.5)2471 for l, r in lines: 2472 line([(rankpos(ssums[l])  side, start), (rankpos(ssums[r]) + side, start)], linewidth=2.5) 2473 2473 start += height 2474 2474 … … 2476 2476 2477 2477 elif cd: 2478 begin = rankpos(avranks[cdmethod] cd)2479 end = rankpos(avranks[cdmethod] +cd)2480 line([(begin, cline), (end, cline)], linewidth=2.5) 2481 line([(begin, cline + bigtick /2), (begin, cline  bigtick/2)], linewidth=2.5)2482 line([(end, cline + bigtick /2), (end, cline  bigtick/2)], linewidth=2.5)2483 2478 begin = rankpos(avranks[cdmethod]  cd) 2479 end = rankpos(avranks[cdmethod] + cd) 2480 line([(begin, cline), (end, cline)], linewidth=2.5) 2481 line([(begin, cline + bigtick / 2), (begin, cline  bigtick / 2)], linewidth=2.5) 2482 line([(end, cline + bigtick / 2), (end, cline  bigtick / 2)], linewidth=2.5) 2483 2484 2484 print_figure(fig, filename, **kwargs) 2485 2485 … … 2490 2490 :math:`HammingLoss(H,D)=\\frac{1}{D} \\sum_{i=1}^{D} \\frac{Y_i \\vartriangle Z_i}{L}` 2491 2491 """ 2492 losses = [0] *res.number_of_learners2492 losses = [0] * res.number_of_learners 2493 2493 label_num = len(res.labels) 2494 2494 example_num = gettotsize(res) … … 2503 2503 if labels[j] != aclass[j]: 2504 2504 losses[i] += 1 2505 2506 return [float(x) /(label_num*example_num) for x in losses]2507 2508 def mlc_accuracy(res, forgiveness_rate =1.0):2505 2506 return [float(x) / (label_num * example_num) for x in losses] 2507 2508 def mlc_accuracy(res, forgiveness_rate=1.0): 2509 2509 """ 2510 2510 Godbole & Sarawagi, 2004 uses the metrics accuracy, precision, recall as follows: … … 2517 2517 :math:`Accuracy(H,D)=\\frac{1}{D} \\sum_{i=1}^{D} (\\frac{Y_i \\cap Z_i}{Y_i \\cup Z_i})^{\\alpha}` 2518 2518 """ 2519 accuracies = [0.0] *res.number_of_learners2519 accuracies = [0.0] * res.number_of_learners 2520 2520 example_num = gettotsize(res) 2521 2521 2522 2522 for e in res.results: 2523 2523 aclass = e.actual_class … … 2526 2526 if len(labels) <> len(aclass): 2527 2527 raise ValueError, "The dimensions of the classified output and the actual class array do not match." 2528 2528 2529 2529 intersection = 0.0 2530 2530 union = 0.0 … … 2537 2537 if union: 2538 2538 accuracies[i] += intersection / union 2539 2540 return [math.pow(x /example_num,forgiveness_rate) for x in accuracies]2539 2540 return [math.pow(x / example_num, forgiveness_rate) for x in accuracies] 2541 2541 2542 2542 def mlc_precision(res): … … 2544 2544 :math:`Precision(H,D)=\\frac{1}{D} \\sum_{i=1}^{D} \\frac{Y_i \\cap Z_i}{Z_i}` 2545 2545 """ 2546 precisions = [0.0] *res.number_of_learners2546 precisions = [0.0] * res.number_of_learners 2547 2547 example_num = gettotsize(res) 2548 2548 2549 2549 for e in res.results: 2550 2550 aclass = e.actual_class … … 2553 2553 if len(labels) <> len(aclass): 2554 2554 raise ValueError, "The dimensions of the classified output and the actual class array do not match." 2555 2555 2556 2556 intersection = 0.0 2557 2557 predicted = 0.0 … … 2563 2563 if predicted: 2564 2564 precisions[i] += intersection / predicted 2565 2566 return [x /example_num for x in precisions]2565 2566 return [x / example_num for x in precisions] 2567 2567 2568 2568 def mlc_recall(res): … … 2570 2570 :math:`Recall(H,D)=\\frac{1}{D} \\sum_{i=1}^{D} \\frac{Y_i \\cap Z_i}{Y_i}` 2571 2571 """ 2572 recalls = [0.0] *res.number_of_learners2572 recalls = [0.0] * res.number_of_learners 2573 2573 example_num = gettotsize(res) 2574 2574 2575 2575 for e in res.results: 2576 2576 aclass = e.actual_class … … 2579 2579 if len(labels) <> len(aclass): 2580 2580 raise ValueError, "The dimensions of the classified output and the actual class array do not match." 2581 2581 2582 2582 intersection = 0.0 2583 2583 actual = 0.0 … … 2589 2589 if actual: 2590 2590 recalls[i] += intersection / actual 2591 2592 return [x /example_num for x in recalls]2591 2592 return [x / example_num for x in recalls] 2593 2593 2594 2594 #def mlc_ranking_loss(res): … … 2671 2671 ################################################################################ 2672 2672 if __name__ == "__main__": 2673 avranks = 2673 avranks = [3.143, 2.000, 2.893, 1.964] 2674 2674 names = ["prva", "druga", "tretja", "cetrta" ] 2675 2675 cd = compute_CD(avranks, 14)
Note: See TracChangeset
for help on using the changeset viewer.