Changeset 10367:058e9b1cc258 in orange


Ignore:
Timestamp:
02/24/12 19:03:05 (2 years ago)
Author:
Lan Zagar <lan.zagar@…>
Branch:
default
rebase_source:
f33a19bc2bfc8f52ac2e2928e53df9fb37f5b9ab
Message:

Some more code style and doc improvements.

Location:
Orange
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Orange/evaluation/scoring.py

    r10362 r10367  
    3030 
    3131def gettotweight(res): 
    32     """Sum all the weights""" 
     32    """Sum all the weights.""" 
    3333    totweight = reduce(lambda x, y: x + y.weight, res.results, 0) 
    3434    if totweight == 0.: 
     
    3737 
    3838def gettotsize(res): 
    39     """ Get number of result instances """ 
     39    """Get number of result instances.""" 
    4040    if len(res.results): 
    4141        return len(res.results) 
     
    4545 
    4646def split_by_iterations(res): 
    47     """ Splits ExperimentResults of multiple iteratation test into a list 
     47    """Split ExperimentResults of a multiple iteratation test into a list 
    4848    of ExperimentResults, one for each iteration. 
    4949    """ 
     
    6161 
    6262def split_by_classifiers(res): 
    63     """ Splites an instance of :obj:`ExperimentResults` into a list of 
     63    """Split an instance of :obj:`ExperimentResults` into a list of 
    6464    :obj:`ExperimentResults`, one for each classifier.  
    6565    """ 
     
    7373        r.results = [] 
    7474        for te in res.results: 
    75             r.results.append(Orange.evaluation.testing.TestedExample(te.iterationNumber, 
    76                                 te.actualClass, n=1, weight=te.weight)) 
     75            r.results.append(Orange.evaluation.testing.TestedExample( 
     76                te.iterationNumber, te.actualClass, n=1, weight=te.weight)) 
    7777            r.results[-1].classes = [te.classes[i]] 
    7878            r.results[-1].probabilities = [te.probabilities[i]] 
     
    8282 
    8383def class_probabilities_from_res(res, **argkw): 
    84     """Calculate class probabilities""" 
     84    """Calculate class probabilities.""" 
    8585    probs = [0.] * len(res.class_values) 
    8686    if argkw.get("unweighted", 0) or not res.weights: 
     
    9494            totweight += tex.weight 
    9595        check_non_zero(totweight) 
    96     return [prob/totweight for prob in probs] 
     96    return [prob / totweight for prob in probs] 
    9797 
    9898 
     
    145145class ConfusionMatrix: 
    146146    """ 
    147     Classification result summary 
     147    Classification result summary. 
    148148    """ 
    149149    #: True Positive predictions 
     
    160160    def addTFPosNeg(self, predicted_positive, is_positive, weight=1.0): 
    161161        """ 
    162         Update confusion matrix with result of a single classification 
     162        Update confusion matrix with result of a single classification. 
    163163 
    164164        :param predicted_positive: positive class value was predicted 
     
    184184 
    185185def check_argkw(dct, lst): 
    186     """check_argkw(dct, lst) -> returns true if any items have non-zero value in dct""" 
     186    """Return True if any item from lst has a non-zero value in dct.""" 
    187187    return reduce(lambda x, y: x or y, [dct.get(k, 0) for k in lst]) 
    188188 
    189189def regression_error(res, **argkw): 
    190     """regression_error(res) -> regression error (default: MSE)""" 
     190    """Return the regression error (default: MSE).""" 
    191191    if argkw.get("SE", 0) and res.number_of_iterations > 1: 
    192192        # computes the scores for each iteration, then averages 
     
    283283 
    284284def MSE(res, **argkw): 
    285     """ Computes mean-squared error. """ 
     285    """Compute mean-squared error.""" 
    286286    return regression_error(res, **argkw) 
    287287     
    288288def RMSE(res, **argkw): 
    289     """ Computes root mean-squared error. """ 
     289    """Compute root mean-squared error.""" 
    290290    argkw.setdefault("sqrt", True) 
    291291    return regression_error(res, **argkw) 
    292292 
    293293def MAE(res, **argkw): 
    294     """ Computes mean absolute error. """ 
     294    """Compute mean absolute error.""" 
    295295    argkw.setdefault("abs", True) 
    296296    return regression_error(res, **argkw) 
    297297 
    298298def RSE(res, **argkw): 
    299     """ Computes relative squared error. """ 
     299    """Compute relative squared error.""" 
    300300    argkw.setdefault("norm-sqr", True) 
    301301    return regression_error(res, **argkw) 
    302302 
    303303def RRSE(res, **argkw): 
    304     """ Computes relative squared error. """ 
     304    """Compute relative squared error.""" 
    305305    argkw.setdefault("norm-sqr", True) 
    306306    argkw.setdefault("sqrt", True) 
     
    308308 
    309309def RAE(res, **argkw): 
    310     """ Computes relative absolute error. """ 
     310    """Compute relative absolute error.""" 
    311311    argkw.setdefault("abs", True) 
    312312    argkw.setdefault("norm-abs", True) 
     
    314314 
    315315def R2(res, **argkw): 
    316     """ Computes the coefficient of determination, R-squared. """ 
     316    """Compute the coefficient of determination, R-squared.""" 
    317317    argkw.setdefault("norm-sqr", True) 
    318318    argkw.setdefault("R2", True) 
     
    320320 
    321321def MSE_old(res, **argkw): 
    322     """MSE(res) -> mean-squared error""" 
     322    """Compute mean-squared error.""" 
    323323    if argkw.get("SE", 0) and res.number_of_iterations > 1: 
    324324        MSEs = [[0.] * res.number_of_iterations 
     
    357357 
    358358def RMSE_old(res, **argkw): 
    359     """RMSE(res) -> root mean-squared error""" 
     359    """Compute root mean-squared error.""" 
    360360    argkw.setdefault("sqrt", 1) 
    361361    return MSE_old(res, **argkw) 
     
    470470                      "unweighted": "ignore_weights"}) 
    471471def AP(res, report_se=False, ignore_weights=False, **argkw): 
    472     """ Computes the average probability assigned to the correct class. """ 
     472    """Compute the average probability assigned to the correct class.""" 
    473473    if res.number_of_iterations == 1: 
    474474        APs=[0.] * res.number_of_learners 
     
    509509                      "unweighted": "ignore_weights"}) 
    510510def Brier_score(res, report_se=False, ignore_weights=False, **argkw): 
    511     """ Computes the Brier's score, defined as the average (over test examples) 
    512     of sumx(t(x)-p(x))2, where x is a class, t(x) is 1 for the correct class 
    513     and 0 for the others, and p(x) is the probability that the classifier 
    514     assigned to the class x 
     511    """Compute the Brier score, defined as the average (over test instances) 
     512    of :math:`\sum_x(t(x) - p(x))^2`, where x is a class value, t(x) is 1 for 
     513    the actual class value and 0 otherwise, and p(x) is the predicted 
     514    probability of x. 
    515515    """ 
    516516    # Computes an average (over examples) of sum_x(t(x) - p(x))^2, where 
     
    584584@deprecated_keywords({"reportSE": "report_se"}) 
    585585def IS(res, apriori=None, report_se=False, **argkw): 
    586     """ Computes the information score as defined by  
     586    """Compute the information score as defined by  
    587587    `Kononenko and Bratko (1991) \ 
    588588    <http://www.springerlink.com/content/g5p7473160476612/>`_. 
     
    773773def sens(confusion_matrix): 
    774774    """ 
    775     Return `sensitivity <http://en.wikipedia.org/wiki/Sensitivity_and_specificity>`_ 
    776     (proportion of actual positives which are correctly identified as such). 
     775    Return `sensitivity 
     776    <http://en.wikipedia.org/wiki/Sensitivity_and_specificity>`_ (proportion 
     777    of actual positives which are correctly identified as such). 
    777778    """ 
    778779    if type(confusion_matrix) == list: 
     
    800801def spec(confusion_matrix): 
    801802    """ 
    802     Return `specificity <http://en.wikipedia.org/wiki/Sensitivity_and_specificity>`_ 
     803    Return `specificity 
     804    <http://en.wikipedia.org/wiki/Sensitivity_and_specificity>`_ 
    803805    (proportion of negatives which are correctly identified). 
    804806    """ 
     
    817819def PPV(confusion_matrix): 
    818820    """ 
    819     Return `positive predictive value <http://en.wikipedia.org/wiki/Positive_predictive_value>`_ 
    820     (proportion of subjects with positive test results who are correctly diagnosed).""" 
     821    Return `positive predictive value 
     822    <http://en.wikipedia.org/wiki/Positive_predictive_value>`_ (proportion of 
     823    subjects with positive test results who are correctly diagnosed).""" 
    821824    if type(confusion_matrix) == list: 
    822825        return [PPV(cm) for cm in confusion_matrix] 
     
    840843@deprecated_keywords({"confm": "confusion_matrix"}) 
    841844def NPV(confusion_matrix): 
    842     """Return `negative predictive value <http://en.wikipedia.org/wiki/Negative_predictive_value>`_ 
    843      (proportion of subjects with a negative test result who are correctly 
    844      diagnosed). 
     845    """ 
     846    Return `negative predictive value 
     847    <http://en.wikipedia.org/wiki/Negative_predictive_value>`_ (proportion of 
     848    subjects with a negative test result who are correctly diagnosed). 
    845849     """ 
    846850    if type(confusion_matrix) == list: 
     
    856860@deprecated_keywords({"confm": "confusion_matrix"}) 
    857861def F1(confusion_matrix): 
    858     """Return `F1 score <http://en.wikipedia.org/wiki/F1_score>`_ 
    859     (harmonic mean of precision and recall).""" 
     862    """ 
     863    Return `F1 score <http://en.wikipedia.org/wiki/F1_score>`_ 
     864    (harmonic mean of precision and recall). 
     865    """ 
    860866    if type(confusion_matrix) == list: 
    861867        return [F1(cm) for cm in confusion_matrix] 
     
    873879@deprecated_keywords({"confm": "confusion_matrix"}) 
    874880def Falpha(confusion_matrix, alpha=1.0): 
    875     """Return the alpha-mean of precision and recall over the given confusion matrix.""" 
     881    """ 
     882    Return the alpha-mean of precision and recall over the given confusion 
     883    matrix. 
     884    """ 
    876885    if type(confusion_matrix) == list: 
    877886        return [Falpha(cm, alpha=alpha) for cm in confusion_matrix] 
     
    885894def MCC(confusion_matrix): 
    886895    """ 
    887     Return `Matthew correlation coefficient <http://en.wikipedia.org/wiki/Matthews_correlation_coefficient>`_ 
    888     (correlation coefficient between the observed and predicted binary classifications) 
     896    Return `Matthew correlation coefficient 
     897    <http://en.wikipedia.org/wiki/Matthews_correlation_coefficient>`_ 
     898    (correlation coefficient between the observed and predicted binary 
     899    classifications). 
    889900    """ 
    890901    # code by Boris Gorelik 
     
    923934   raters. 
    924935 
    925    @param confusion_matrix: confusion matrix, or list of confusion matrices. To obtain 
    926                            non-binary confusion matrix, call 
    927                            Orange.evaluation.scoring.compute_confusion_matrices and set the 
    928                            classIndex parameter to -2. 
     936   @param confusion_matrix: confusion matrix, or list of confusion matrices. 
     937                            To obtain non-binary confusion matrix, call 
     938                            Orange.evaluation.scoring.confusion_matrices 
     939                            and set the class_index parameter to -2. 
    929940   @param b_is_list_of_matrices: specifies whether confm is list of matrices. 
    930                            This function needs to operate on non-binary 
    931                            confusion matrices, which are represented by python 
    932                            lists, therefore one needs a way to distinguish 
    933                            between a single matrix and list of matrices 
     941                            This function needs to operate on non-binary 
     942                            confusion matrices, which are represented by python 
     943                            lists, therefore one needs a way to distinguish 
     944                            between a single matrix and list of matrices 
    934945   """ 
    935946 
     
    944955   else: 
    945956       if isinstance(confusion_matrix, ConfusionMatrix): 
    946            confusion_matrix = numpy.array( [[confusion_matrix.TP, confusion_matrix.FN], 
    947                    [confusion_matrix.FP, confusion_matrix.TN]], dtype=float) 
     957           confusion_matrix = numpy.array([[confusion_matrix.TP, 
     958               confusion_matrix.FN], [confusion_matrix.FP, 
     959               confusion_matrix.TN]], dtype=float) 
    948960       else: 
    949961           confusion_matrix = numpy.array(confusion_matrix, dtype=float) 
     
    951963       marginalSumOfRows = numpy.sum(confusion_matrix, axis=0) 
    952964       marginalSumOfColumns = numpy.sum(confusion_matrix, axis=1) 
    953        jointProportion = (marginalSumOfColumns + marginalSumOfRows)/ \ 
    954                            (2.0 * numpy.sum(confusion_matrix)) 
    955        # In the eq. above, 2.0 is what the Wikipedia page calls 
     965       jointProportion = (marginalSumOfColumns + marginalSumOfRows) / \ 
     966                           (2. * numpy.sum(confusion_matrix)) 
     967       # In the eq. above, 2. is what the Wikipedia page calls 
    956968       # the number of annotators. Here we have two annotators: 
    957969       # the observed (true) labels (annotations) and the predicted by 
     
    959971 
    960972       prExpected = numpy.sum(jointProportion ** 2) 
    961        prActual = numpy.sum(numpy.diag(confusion_matrix)) /numpy.sum(confusion_matrix) 
     973       prActual = numpy.sum(numpy.diag(confusion_matrix)) / \ 
     974                  numpy.sum(confusion_matrix) 
    962975 
    963976       ret = (prActual - prExpected) / (1.0 - prExpected) 
     
    967980                      "unweighted": "ignore_weights"}) 
    968981def AUCWilcoxon(res, class_index=-1, ignore_weights=False, **argkw): 
    969     """ Computes the area under ROC (AUC) and its standard error using 
     982    """Compute the area under ROC (AUC) and its standard error using 
    970983    Wilcoxon's approach proposed by Hanley and McNeal (1982). If  
    971     :obj:`classIndex` is not specified, the first class is used as 
     984    :obj:`class_index` is not specified, the first class is used as 
    972985    "the positive" and others are negative. The result is a list of 
    973986    tuples (aROC, standard error). 
     
    979992    problists, tots = corn.computeROCCumulative(res, class_index, useweights) 
    980993 
    981     results=[] 
     994    results = [] 
    982995 
    983996    totPos, totNeg = tots[1], tots[0] 
    984997    N = totPos + totNeg 
    985998    for plist in problists: 
    986         highPos, lowNeg = totPos, 0.0 
    987         W, Q1, Q2 = 0.0, 0.0, 0.0 
     999        highPos, lowNeg = totPos, 0. 
     1000        W, Q1, Q2 = 0., 0., 0. 
    9881001        for prob in plist: 
    9891002            thisPos, thisNeg = prob[1][1], prob[1][0] 
    9901003            highPos -= thisPos 
    991             W += thisNeg * (highPos + thisPos/2.) 
    992             Q2 += thisPos * (lowNeg**2  + lowNeg*thisNeg  + thisNeg**2 /3.) 
    993             Q1 += thisNeg * (highPos**2 + highPos*thisPos + thisPos**2 /3.) 
     1004            W += thisNeg * (highPos + thisPos / 2.) 
     1005            Q2 += thisPos * (lowNeg**2  + lowNeg*thisNeg  + thisNeg**2 / 3.) 
     1006            Q1 += thisNeg * (highPos**2 + highPos*thisPos + thisPos**2 / 3.) 
    9941007 
    9951008            lowNeg += thisNeg 
     
    9991012        Q2 /= (totPos*totNeg**2) 
    10001013 
    1001         SE = math.sqrt( (W*(1-W) + (totPos-1)*(Q1-W**2) + (totNeg-1)*(Q2-W**2)) / (totPos*totNeg) ) 
     1014        SE = math.sqrt((W * (1 - W) + (totPos - 1) * (Q1 - W**2) + 
     1015                       (totNeg - 1) * (Q2 - W**2)) / (totPos * totNeg)) 
    10021016        results.append((W, SE)) 
    10031017    return results 
    10041018 
    1005 AROC = AUCWilcoxon # for backward compatibility, AROC is obsolote 
     1019AROC = AUCWilcoxon # for backward compatibility, AROC is obsolete 
    10061020 
    10071021 
     
    10101024def compare_2_AUCs(res, lrn1, lrn2, class_index=-1, 
    10111025                   ignore_weights=False, **argkw): 
    1012     return corn.compare2ROCs(res, lrn1, lrn2, class_index, res.weights and not ignore_weights) 
    1013  
    1014 compare_2_AROCs = compare_2_AUCs # for backward compatibility, compare_2_AROCs is obsolote 
     1026    return corn.compare2ROCs(res, lrn1, lrn2, class_index, 
     1027                             res.weights and not ignore_weights) 
     1028 
     1029# for backward compatibility, compare_2_AROCs is obsolete 
     1030compare_2_AROCs = compare_2_AUCs  
    10151031 
    10161032 
    10171033@deprecated_keywords({"classIndex": "class_index"}) 
    10181034def compute_ROC(res, class_index=-1): 
    1019     """ Computes a ROC curve as a list of (x, y) tuples, where x is  
     1035    """Compute a ROC curve as a list of (x, y) tuples, where x is  
    10201036    1-specificity and y is sensitivity. 
    10211037    """ 
     
    10271043    for plist in problists: 
    10281044        curve=[(1., 1.)] 
    1029         TP, TN = totPos, 0.0 
     1045        TP, TN = totPos, 0. 
    10301046        FN, FP = 0., totNeg 
    10311047        for prob in plist: 
     
    10381054            FP -= thisNeg 
    10391055 
    1040             sens = TP/(TP+FN) 
    1041             spec = TN/(FP+TN) 
    1042             curve.append((1-spec, sens)) 
     1056            sens = TP / (TP + FN) 
     1057            spec = TN / (FP + TN) 
     1058            curve.append((1 - spec, sens)) 
    10431059        results.append(curve) 
    10441060 
     
    10461062 
    10471063## TC's implementation of algorithms, taken from: 
    1048 ## T Fawcett: ROC Graphs: Notes and Practical Considerations for Data Mining Researchers, submitted to KDD Journal.  
     1064## T Fawcett: ROC Graphs: Notes and Practical Considerations for 
     1065## Data Mining Researchers, submitted to KDD Journal. 
    10491066def ROC_slope((P1x, P1y, P1fscore), (P2x, P2y, P2fscore)): 
    10501067    if P1x == P2x: 
     
    10811098 
    10821099    for plist in problists: 
    1083         ## corn gives an increasing by scores list, we need a decreasing by scores 
     1100        ## corn gives an increasing by scores list, we need a decreasing 
    10841101        plist.reverse() 
    1085         TP = 0.0 
    1086         FP = 0.0 
     1102        TP = 0. 
     1103        FP = 0. 
    10871104        curve=[] 
    1088         fPrev = 10e300 # "infinity" score at 0.0, 0.0 
     1105        fPrev = 10e300 # "infinity" score at 0., 0. 
    10891106        for prob in plist: 
    10901107            f = prob[0] 
    1091             if f <> fPrev: 
     1108            if f != fPrev: 
    10921109                if P: 
    1093                     tpr = TP/P 
     1110                    tpr = TP / P 
    10941111                else: 
    1095                     tpr = 0.0 
     1112                    tpr = 0. 
    10961113                if N: 
    1097                     fpr = FP/N 
     1114                    fpr = FP / N 
    10981115                else: 
    1099                     fpr = 0.0 
    1100                 curve = ROC_add_point((fpr, tpr, fPrev), curve, keep_concavities) 
     1116                    fpr = 0. 
     1117                curve = ROC_add_point((fpr, tpr, fPrev), curve, 
     1118                                      keep_concavities) 
    11011119                fPrev = f 
    11021120            thisPos, thisNeg = prob[1][1], prob[1][0] 
     
    11041122            FP += thisNeg 
    11051123        if P: 
    1106             tpr = TP/P 
    1107         else: 
    1108             tpr = 0.0 
     1124            tpr = TP / P 
     1125        else: 
     1126            tpr = 0. 
    11091127        if N: 
    1110             fpr = FP/N 
    1111         else: 
    1112             fpr = 0.0 
     1128            fpr = FP / N 
     1129        else: 
     1130            fpr = 0. 
    11131131        curve = ROC_add_point((fpr, tpr, f), curve, keep_concavities) ## ugly 
    11141132        results.append(curve) 
     
    11161134    return results 
    11171135 
    1118 ## returns a list of points at the intersection of the tangential iso-performance line and the given ROC curve 
     1136## returns a list of points at the intersection of the tangential 
     1137## iso-performance line and the given ROC curve 
    11191138## for given values of FPcost, FNcost and pval 
    11201139def TC_best_thresholds_on_ROC_curve(FPcost, FNcost, pval, curve): 
    1121     m = (FPcost*(1.0 - pval)) / (FNcost*pval) 
    1122  
    1123     ## put the iso-performance line in point (0.0, 1.0) 
    1124     x0, y0 = (0.0, 1.0) 
    1125     x1, y1 = (1.0, 1.0 + m) 
    1126     d01 = math.sqrt((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0)) 
     1140    m = (FPcost * (1. - pval)) / (FNcost * pval) 
     1141 
     1142    ## put the iso-performance line in point (0., 1.) 
     1143    x0, y0 = (0., 1.) 
     1144    x1, y1 = (1., 1. + m) 
     1145    d01 = math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) 
    11271146 
    11281147    ## calculate and find the closest point to the line 
    11291148    firstp = 1 
    1130     mind = 0.0 
    1131     a = (x0*y1 - x1*y0) 
     1149    mind = 0. 
     1150    a = x0 * y1 - x1 * y0 
    11321151    closestPoints = [] 
    11331152    for (x, y, fscore) in curve: 
    1134         d = ((y0 - y1)*x + (x1 - x0)*y + a) / d01 
     1153        d = ((y0 - y1) * x + (x1 - x0) * y + a) / d01 
    11351154        d = abs(d) 
    11361155        if firstp or d < mind: 
     
    11461165 
    11471166    if end is None: 
    1148         end = start + 0.0 
    1149         start = 0.0 
     1167        end = start + 0. 
     1168        start = 0. 
    11501169 
    11511170    if inc is None or inc == 0: 
    1152         inc = 1.0 
     1171        inc = 1. 
    11531172 
    11541173    L = [start] 
     
    11781197        dx = float(P2x) - float(P1x) 
    11791198        dy = float(P2y) - float(P1y) 
    1180         m = dy/dx 
    1181         return P1y + m*(X - P1x) 
     1199        m = dy / dx 
     1200        return P1y + m * (X - P1x) 
    11821201 
    11831202    def TP_FOR_FP(FPsample, ROC, npts): 
     
    11931212            return tp 
    11941213        elif fp < FPsample and i + 1 < len(ROC): 
    1195             return INTERPOLATE(ROC[i], ROC[i+1], FPsample) 
     1214            return INTERPOLATE(ROC[i], ROC[i + 1], FPsample) 
    11961215        elif fp < FPsample and i + 1 == len(ROC): # return the last 
    11971216            return ROC[i][1] 
    11981217        raise ValueError, "cannot compute: TP_FOR_FP in TC_vertical_average_ROC" 
    1199         #return 0.0 
     1218        #return 0. 
    12001219 
    12011220    average = [] 
     
    12091228        TPavg = [] 
    12101229        TPstd = [] 
    1211         for FPsample in frange(0.0, 1.0, 1.0/samples): 
     1230        for FPsample in frange(0., 1., 1. / samples): 
    12121231            TPsum = [] 
    12131232            for i in range(nrocs): 
    1214                 TPsum.append( TP_FOR_FP(FPsample, ROCS[i], npts[i]) ) ##TPsum = TPsum + TP_FOR_FP(FPsample, ROCS[i], npts[i]) 
    1215             TPavg.append( (FPsample, statc.mean(TPsum)) ) 
     1233                ##TPsum = TPsum + TP_FOR_FP(FPsample, ROCS[i], npts[i]) 
     1234                TPsum.append(TP_FOR_FP(FPsample, ROCS[i], npts[i])) 
     1235            TPavg.append((FPsample, statc.mean(TPsum))) 
    12161236            if len(TPsum) > 1: 
    12171237                stdv = statc.std(TPsum) 
    12181238            else: 
    1219                 stdv = 0.0 
    1220             TPstd.append( stdv ) 
     1239                stdv = 0. 
     1240            TPstd.append(stdv) 
    12211241 
    12221242        average.append(TPavg) 
     
    12321252## returns the average ROC curve, an array of vertical standard deviations and an array of horizontal standard deviations 
    12331253@deprecated_keywords({"ROCcurves": "roc_curves"}) 
    1234 def TC_threshold_average_ROC(roc_curves, samples = 10): 
     1254def TC_threshold_average_ROC(roc_curves, samples=10): 
    12351255    def POINT_AT_THRESH(ROC, npts, thresh): 
    12361256        i = 0 
     
    12651285        TPstdV = [] 
    12661286        TPstdH = [] 
    1267         for tidx in frange(0, (len(T) - 1.0), float(len(T))/samples): 
     1287        for tidx in frange(0, (len(T) - 1.), float(len(T)) / samples): 
    12681288            FPsum = [] 
    12691289            TPsum = [] 
     
    12721292                FPsum.append(fp) 
    12731293                TPsum.append(tp) 
    1274             TPavg.append( (statc.mean(FPsum), statc.mean(TPsum)) ) 
     1294            TPavg.append((statc.mean(FPsum), statc.mean(TPsum))) 
    12751295            ## vertical standard deviation 
    12761296            if len(TPsum) > 1: 
    12771297                stdv = statc.std(TPsum) 
    12781298            else: 
    1279                 stdv = 0.0 
     1299                stdv = 0. 
    12801300            TPstdV.append( stdv ) 
    12811301            ## horizontal standard deviation 
     
    12831303                stdh = statc.std(FPsum) 
    12841304            else: 
    1285                 stdh = 0.0 
     1305                stdh = 0. 
    12861306            TPstdH.append( stdh ) 
    12871307 
     
    12931313 
    12941314## Calibration Curve 
    1295 ## returns an array of (curve, yesClassPredictions, noClassPredictions) elements, where: 
     1315## returns an array of (curve, yesClassPredictions, noClassPredictions) 
     1316## elements, where: 
    12961317##  - curve is an array of points (x, y) on the calibration curve 
    12971318##  - yesClassRugPoints is an array of (x, 1) points 
     
    13001321def compute_calibration_curve(res, class_index=-1): 
    13011322    ## merge multiple iterations into one 
    1302     mres = Orange.evaluation.testing.ExperimentResults(1, res.classifier_names, res.class_values, res.weights, classifiers=res.classifiers, loaded=res.loaded, test_type=res.test_type, labels=res.labels) 
     1323    mres = Orange.evaluation.testing.ExperimentResults(1, res.classifier_names, 
     1324        res.class_values, res.weights, classifiers=res.classifiers, 
     1325        loaded=res.loaded, test_type=res.test_type, labels=res.labels) 
    13031326    for te in res.results: 
    1304         mres.results.append( te ) 
     1327        mres.results.append(te) 
    13051328 
    13061329    problists, tots = corn.computeROCCumulative(mres, class_index) 
     
    13081331    results = [] 
    13091332 
    1310     bins = 10 ## divide interval between 0.0 and 1.0 into N bins 
     1333    bins = 10 ## divide interval between 0. and 1. into N bins 
    13111334 
    13121335    for plist in problists: 
     
    13171340        noBinsVals = [0] * bins 
    13181341        for (f, (thisNeg, thisPos)) in plist: 
    1319             yesClassRugPoints.append( (f, thisPos) ) #1.0 
    1320             noClassRugPoints.append( (f, thisNeg) ) #1.0 
     1342            yesClassRugPoints.append((f, thisPos)) # 1. 
     1343            noClassRugPoints.append((f, thisNeg)) # 1. 
    13211344 
    13221345            index = int(f * bins ) 
    1323             index = min(index, bins - 1) ## just in case for value 1.0 
     1346            index = min(index, bins - 1) ## just in case for value 1. 
    13241347            yesBinsVals[index] += thisPos 
    13251348            noBinsVals[index] += thisNeg 
     
    13271350        curve = [] 
    13281351        for cn in range(bins): 
    1329             f = float(cn * 1.0 / bins) + (1.0 / 2.0 / bins) 
     1352            f = float(cn * 1. / bins) + (1. / 2. / bins) 
    13301353            yesVal = yesBinsVals[cn] 
    13311354            noVal = noBinsVals[cn] 
    13321355            allVal = yesVal + noVal 
    1333             if allVal == 0.0: continue 
    1334             y = float(yesVal)/float(allVal) 
    1335             curve.append( (f,  y) ) 
     1356            if allVal == 0.: continue 
     1357            y = float(yesVal) / float(allVal) 
     1358            curve.append((f,  y)) 
    13361359 
    13371360        ## smooth the curve 
     
    13491372        else: 
    13501373            curve = loessCurve 
    1351         curve = [c[:2] for c in curve] ## remove the third value (variance of epsilon?) that suddenly appeared in the output of the statc.loess function 
     1374        ## remove the third value (variance of epsilon?) that suddenly 
     1375        ## appeared in the output of the statc.loess function 
     1376        curve = [c[:2] for c in curve] 
    13521377        results.append((curve, yesClassRugPoints, noClassRugPoints)) 
    13531378 
     
    13571382## Lift Curve 
    13581383## returns an array of curve elements, where: 
    1359 ##  - curve is an array of points ((TP+FP)/(P + N), TP/P, (th, FP/N)) on the Lift Curve 
     1384##  - curve is an array of points ((TP + FP) / (P + N), TP / P, (th, FP / N)) 
     1385##    on the Lift Curve 
    13601386@deprecated_keywords({"classIndex": "class_index"}) 
    13611387def compute_lift_curve(res, class_index=-1): 
    13621388    ## merge multiple iterations into one 
    1363     mres = Orange.evaluation.testing.ExperimentResults(1, res.classifier_names, res.class_values, res.weights, classifiers=res.classifiers, loaded=res.loaded, test_type=res.test_type, labels=res.labels) 
     1389    mres = Orange.evaluation.testing.ExperimentResults(1, res.classifier_names, 
     1390        res.class_values, res.weights, classifiers=res.classifiers, 
     1391        loaded=res.loaded, test_type=res.test_type, labels=res.labels) 
    13641392    for te in res.results: 
    1365         mres.results.append( te ) 
     1393        mres.results.append(te) 
    13661394 
    13671395    problists, tots = corn.computeROCCumulative(mres, class_index) 
     
    13701398    P, N = tots[1], tots[0] 
    13711399    for plist in problists: 
    1372         ## corn gives an increasing by scores list, we need a decreasing by scores 
     1400        ## corn gives an increasing by scores list, we need a decreasing 
    13731401        plist.reverse() 
    1374         TP = 0.0 
    1375         FP = 0.0 
    1376         curve = [(0.0, 0.0, (10e300, 0.0))] 
     1402        TP = 0. 
     1403        FP = 0. 
     1404        curve = [(0., 0., (10e300, 0.))] 
    13771405        for (f, (thisNeg, thisPos)) in plist: 
    13781406            TP += thisPos 
    13791407            FP += thisNeg 
    1380             curve.append( ((TP+FP)/(P + N), TP, (f, FP/(N or 1))) ) 
     1408            curve.append(((TP + FP) / (P + N), TP, (f, FP / (N or 1)))) 
    13811409        results.append(curve) 
    13821410 
    13831411    return P, N, results 
    1384 ### 
     1412 
    13851413 
    13861414class CDT: 
    1387   """ Stores number of concordant (C), discordant (D) and tied (T) pairs (used for AUC) """ 
    1388   def __init__(self, C=0.0, D=0.0, T=0.0): 
     1415  """Store the number of concordant (C), discordant (D) and tied (T) pairs.""" 
     1416  def __init__(self, C=0., D=0., T=0.): 
    13891417    self.C, self.D, self.T = C, D, T 
    13901418    
     
    13961424                      "unweighted": "ignore_weights"}) 
    13971425def compute_CDT(res, class_index=-1, ignore_weights=False, **argkw): 
    1398     """Obsolete, don't use""" 
    1399     if class_index<0: 
    1400         if res.baseClass>=0: 
     1426    """Obsolete, don't use.""" 
     1427    if class_index < 0: 
     1428        if res.baseClass >= 0: 
    14011429            class_index = res.baseClass 
    14021430        else: 
     
    14401468    """ 
    14411469    Compute the area under ROC curve given a set of experimental results. 
    1442     For multivalued class problems, return the result of :obj:`by_weighted_pairs`. 
    1443     If testing consisted of multiple folds, each fold is scored and 
    1444     average score is returned. If a fold contains only instances with 
     1470    For multivalued class problems, return the result of 
     1471    :obj:`by_weighted_pairs`. 
     1472    If testing consisted of multiple folds, each fold is scored and the 
     1473    average score is returned. If a fold contains only instances with the 
    14451474    same class value, folds will be merged. 
    14461475 
     
    14731502        """ 
    14741503        Compute AUC for each pair of classes (ignoring instances of all other 
    1475         classes) and averages the results, weighting them by the number of 
     1504        classes) and average the results, weighting them by the number of 
    14761505        pairs of instances from these two classes (e.g. by the product of 
    14771506        probabilities of the two classes). AUC computed in this way still 
    1478         behaves as concordance index, e.g., gives the probability that two 
     1507        behaves as the concordance index, e.g., gives the probability that two 
    14791508        randomly chosen instances from different classes will be correctly 
    14801509        recognized (if the classifier knows from which two classes the 
     
    14881517    def by_pairs(cls, res, ignore_weights=False): 
    14891518        """ 
    1490         Similar as above, except that the average over class pairs is not 
    1491         weighted. This AUC is, like the binary, independent of class 
    1492         distributions, but it is not related to concordance index any more. 
     1519        Similar to by_weighted_pairs, except that the average over class pairs 
     1520        is not weighted. This AUC is, like the binary version, independent of 
     1521        class distributions, but it is not related to the concordance index 
     1522        any more. 
    14931523        """ 
    14941524        auc = AUC(ignore_weights=ignore_weights) 
     
    15011531        For each class, it computes AUC for this class against all others (that 
    15021532        is, treating other classes as one class). The AUCs are then averaged by 
    1503         the class probabilities. This is related to concordance index in which 
    1504         we test the classifier's (average) capability for distinguishing the 
    1505         instances from a specified class from those that come from other classes. 
     1533        the class probabilities. This is related to the concordance index in 
     1534        which we test the classifier's (average) capability of distinguishing 
     1535        the instances from a specified class from those that come from other 
     1536        classes. 
    15061537        Unlike the binary AUC, the measure is not independent of class 
    15071538        distributions. 
     
    15141545    @classmethod 
    15151546    def one_against_all(cls, res, ignore_weights=False): 
    1516         """As above, except that the average is not weighted.""" 
     1547        """ 
     1548        Similar to weighted_one_against_all, except that the average 
     1549        is not weighted. 
     1550        """ 
    15171551        auc = AUC(ignore_weights=ignore_weights) 
    15181552        auc._compute_for_multi_value_class(res, method=cls.OneAgainstAll) 
     
    15251559        out and all other classes are treated as a single class. 
    15261560        """ 
    1527         if class_index<0: 
    1528             if res.base_class>=0: 
     1561        if class_index < 0: 
     1562            if res.base_class >= 0: 
    15291563                class_index = res.base_class 
    15301564            else: 
     
    15911625                for classIndex2 in range(classIndex1): 
    15921626                    subsum_aucs = self._compute_for_multiple_folds( 
    1593                                              self._compute_one_class_against_another, 
    1594                                              iterations, 
    1595                                              (classIndex1, classIndex2, 
    1596                                               all_ite, 
    1597                                               res.number_of_iterations)) 
     1627                        self._compute_one_class_against_another, iterations, 
     1628                        (classIndex1, classIndex2, all_ite, 
     1629                        res.number_of_iterations)) 
    15981630                    if subsum_aucs: 
    15991631                        if method == self.ByWeightedPairs: 
     
    16251657        return self 
    16261658 
    1627     # computes the average AUC over folds using a "AUCcomputer" (AUC_i or AUC_ij) 
    1628     # it returns the sum of what is returned by the computer, unless at a certain 
    1629     # fold the computer has to resort to computing over all folds or even this failed; 
     1659    # computes the average AUC over folds using "AUCcomputer" (AUC_i or AUC_ij) 
     1660    # it returns the sum of what is returned by the computer, 
     1661    # unless at a certain fold the computer has to resort to computing 
     1662    # over all folds or even this failed; 
    16301663    # in these cases the result is returned immediately 
    16311664    @deprecated_keywords({"AUCcomputer": "auc_computer", 
     
    16451678        return self 
    16461679 
    1647     # Computes AUC; in multivalued class problem, AUC is computed as one against all 
    1648     # Results over folds are averages; if some folds examples from one class only, the folds are merged 
     1680    # Computes AUC 
     1681    # in multivalued class problem, AUC is computed as one against all 
     1682    # results over folds are averages 
     1683    # if some folds examples from one class only, the folds are merged 
    16491684    def _compute_for_single_class(self, res, class_index): 
    16501685        if res.number_of_iterations > 1: 
     
    16561691 
    16571692    # Computes AUC for a pair of classes (as if there were no other classes) 
    1658     # Results over folds are averages; if some folds have examples from one class only, the folds are merged 
     1693    # results over folds are averages 
     1694    # if some folds have examples from one class only, the folds are merged 
    16591695    def _compute_for_pair_of_classes(self, res, class_index1, class_index2): 
    16601696        if res.number_of_iterations > 1: 
    1661             self._compute_for_multiple_folds(self._compute_one_class_against_another, 
     1697            self._compute_for_multiple_folds( 
     1698                self._compute_one_class_against_another, 
    16621699                split_by_iterations(res), 
    16631700                (class_index1, class_index2, res, res.number_of_iterations)) 
    16641701        else: 
    1665             self._compute_one_class_against_another(res, class_index1, class_index2) 
    1666  
    1667     # computes AUC between class i and the other classes (treating them as the same class) 
     1702            self._compute_one_class_against_another(res, class_index1, 
     1703                                                    class_index2) 
     1704 
     1705    # computes AUC between class i and the other classes 
     1706    # (treating them as the same class) 
    16681707    @deprecated_keywords({"classIndex": "class_index", 
    16691708                          "divideByIfIte": "divide_by_if_ite"}) 
     
    16711710                                      divide_by_if_ite=1.0): 
    16721711        """Compute AUC between class i and all the other classes)""" 
    1673         return self._compute_auc(corn.computeCDT, ite, all_ite, divide_by_if_ite, 
    1674             (class_index, not self.ignore_weights)) 
     1712        return self._compute_auc(corn.computeCDT, ite, all_ite, 
     1713            divide_by_if_ite, (class_index, not self.ignore_weights)) 
    16751714 
    16761715 
    16771716    # computes AUC between classes i and j as if there are no other classes 
    1678     def _compute_one_class_against_another(self, ite, class_index1, 
    1679             class_index2, all_ite=None, 
    1680             divide_by_if_ite=1.0): 
     1717    def _compute_one_class_against_another( 
     1718        self, ite, class_index1, class_index2, 
     1719        all_ite=None, divide_by_if_ite=1.): 
    16811720        """ 
    16821721        Compute AUC between classes i and j as if there are no other classes. 
    16831722        """ 
    1684         return self._compute_auc(corn.computeCDTPair, ite, all_ite, divide_by_if_ite, 
     1723        return self._compute_auc(corn.computeCDTPair, ite, 
     1724            all_ite, divide_by_if_ite, 
    16851725            (class_index1, class_index2, not self.ignore_weights)) 
    16861726 
    16871727    # computes AUC using a specified 'cdtComputer' function 
    1688     # It tries to compute AUCs from 'ite' (examples from a single iteration) and, 
    1689     # if C+D+T=0, from 'all_ite' (entire test set). In the former case, the AUCs 
    1690     # are divided by 'divideByIfIte'. Additional flag is returned which is True in 
    1691     # the former case, or False in the latter. 
     1728    # It tries to compute AUCs from 'ite' (examples from a single iteration) 
     1729    # and, if C+D+T=0, from 'all_ite' (entire test set). In the former case, 
     1730    # the AUCs are divided by 'divideByIfIte'. 
     1731    # Additional flag is returned which is True in the former case, 
     1732    # or False in the latter. 
    16921733    @deprecated_keywords({"cdt_computer": "cdtComputer", 
    16931734                          "divideByIfIte": "divide_by_if_ite", 
    16941735                          "computerArgs": "computer_args"}) 
    16951736    def _compute_auc(self, cdt_computer, ite, all_ite, divide_by_if_ite, 
    1696               computer_args): 
     1737                     computer_args): 
    16971738        """ 
    16981739        Compute AUC using a :obj:`cdt_computer`. 
     
    17001741        cdts = cdt_computer(*(ite, ) + computer_args) 
    17011742        if not is_CDT_empty(cdts[0]): 
    1702             return [(cdt.C+cdt.T/2)/(cdt.C+cdt.D+cdt.T)/divide_by_if_ite for cdt in cdts], True 
     1743            return [(cdt.C + cdt.T / 2) / (cdt.C + cdt.D + cdt.T) / 
     1744                    divide_by_if_ite for cdt in cdts], True 
    17031745 
    17041746        if all_ite: 
    17051747            cdts = cdt_computer(*(all_ite, ) + computer_args) 
    17061748            if not is_CDT_empty(cdts[0]): 
    1707                 return [(cdt.C+cdt.T/2)/(cdt.C+cdt.D+cdt.T) for cdt in cdts], False 
     1749                return [(cdt.C + cdt.T / 2) / (cdt.C + cdt.D + cdt.T) 
     1750                        for cdt in cdts], False 
    17081751 
    17091752        return False, False 
     
    17161759        else: 
    17171760            iterations, all_ite = [res], None 
    1718         aucs = [[[] for _ in range(numberOfClasses)] for _ in 
    1719                                                     range(number_of_learners)] 
     1761        aucs = [[[] for _ in range(numberOfClasses)] 
     1762                for _ in range(number_of_learners)] 
    17201763        for classIndex1 in range(numberOfClasses): 
    17211764            for classIndex2 in range(classIndex1): 
    17221765                pair_aucs = self._compute_for_multiple_folds( 
    1723                     self._compute_one_class_against_another, 
    1724                     iterations, (classIndex1, 
    1725                                                   classIndex2, 
    1726                                                   all_ite, 
    1727                                                   res.number_of_iterations)) 
     1766                    self._compute_one_class_against_another, iterations, 
     1767                    (classIndex1, classIndex2, all_ite, 
     1768                     res.number_of_iterations)) 
    17281769                if pair_aucs: 
    17291770                    for lrn in range(number_of_learners): 
     
    17481789 
    17491790@replace_use_weights 
    1750 def AUC_multi(res, ignore_weights=False, method = 0): 
     1791def AUC_multi(res, ignore_weights=False, method=0): 
    17511792    auc = deprecated_function_name(AUC)(ignore_weights=ignore_weights, 
    17521793        method=method) 
     
    17621803    auc = deprecated_function_name(AUC)() 
    17631804    result = auc._compute_auc(cdtComputer, ite, all_ite, divide_by_if_ite, 
    1764         computer_args) 
     1805                              computer_args) 
    17651806    return result 
    17661807 
    17671808@replace_use_weights 
    17681809def AUC_i(ite, class_index, ignore_weights=False, all_ite=None, 
    1769           divide_by_if_ite=1.0): 
     1810          divide_by_if_ite=1.): 
    17701811    auc = deprecated_function_name(AUC)() 
    17711812    result = auc._compute_one_class_against_another(ite, class_index, 
    1772         all_ite=None, divide_by_if_ite=1.0) 
     1813        all_ite=None, divide_by_if_ite=1.) 
    17731814    return result 
    17741815 
     
    17761817@replace_use_weights 
    17771818def AUC_ij(ite, class_index1, class_index2, ignore_weights=False, 
    1778            all_ite = None, divide_by_if_ite = 1.0): 
     1819           all_ite=None, divide_by_if_ite=1.): 
    17791820    auc = deprecated_function_name(AUC)(ignore_weights=ignore_weights) 
    17801821    result = auc._compute_one_class_against_another( 
    1781         ite, class_index1, class_index2, all_ite = None, divide_by_if_ite = 1.0) 
     1822        ite, class_index1, class_index2, all_ite=None, divide_by_if_ite=1.) 
    17821823    return result 
    17831824 
     
    18041845@deprecated_keywords({"unweighted": "ignore_weights"}) 
    18051846def McNemar(res, ignore_weights=False, **argkw): 
    1806     """ Computes a triangular matrix with McNemar statistics for each pair of 
     1847    """ 
     1848    Compute a triangular matrix with McNemar statistics for each pair of 
    18071849    classifiers. The statistics is distributed by chi-square distribution with 
    18081850    one degree of freedom; critical value for 5% significance is around 3.84. 
     
    18111853    mcm = [] 
    18121854    for i in range(nLearners): 
    1813        mcm.append([0.0]*res.number_of_learners) 
     1855       mcm.append([0.] * res.number_of_learners) 
    18141856 
    18151857    if not res.weights or ignore_weights: 
     
    18191861            for l1 in range(nLearners): 
    18201862                for l2 in range(l1, nLearners): 
    1821                     if classes[l1]==actual: 
    1822                         if classes[l2]!=actual: 
     1863                    if classes[l1] == actual: 
     1864                        if classes[l2] != actual: 
    18231865                            mcm[l1][l2] += 1 
    1824                     elif classes[l2]==actual: 
     1866                    elif classes[l2] == actual: 
    18251867                        mcm[l2][l1] += 1 
    18261868    else: 
     
    18301872            for l1 in range(nLearners): 
    18311873                for l2 in range(l1, nLearners): 
    1832                     if classes[l1]==actual: 
    1833                         if classes[l2]!=actual: 
     1874                    if classes[l1] == actual: 
     1875                        if classes[l2] != actual: 
    18341876                            mcm[l1][l2] += i.weight 
    1835                     elif classes[l2]==actual: 
     1877                    elif classes[l2] == actual: 
    18361878                        mcm[l2][l1] += i.weight 
    18371879 
    18381880    for l1 in range(nLearners): 
    18391881        for l2 in range(l1, nLearners): 
    1840             su=mcm[l1][l2] + mcm[l2][l1] 
     1882            su = mcm[l1][l2] + mcm[l2][l1] 
    18411883            if su: 
    1842                 mcm[l2][l1] = (abs(mcm[l1][l2]-mcm[l2][l1])-1)**2 / su 
     1884                mcm[l2][l1] = (abs(mcm[l1][l2] - mcm[l2][l1]) - 1)**2 / su 
    18431885            else: 
    18441886                mcm[l2][l1] = 0 
     
    18511893 
    18521894def McNemar_of_two(res, lrn1, lrn2, ignore_weights=False): 
    1853     """ McNemar_of_two computes a McNemar statistics for a pair of classifier, 
     1895    """ 
     1896    McNemar_of_two computes a McNemar statistics for a pair of classifier, 
    18541897    specified by indices learner1 and learner2. 
    18551898    """ 
    1856     tf = ft = 0.0 
     1899    tf = ft = 0. 
    18571900    if not res.weights or ignore_weights: 
    18581901        for i in res.results: 
    1859             actual=i.actual_class 
    1860             if i.classes[lrn1]==actual: 
    1861                 if i.classes[lrn2]!=actual: 
     1902            actual = i.actual_class 
     1903            if i.classes[lrn1] == actual: 
     1904                if i.classes[lrn2] != actual: 
    18621905                    tf += i.weight 
    1863             elif i.classes[lrn2]==actual: 
     1906            elif i.classes[lrn2] == actual: 
    18641907                    ft += i.weight 
    18651908    else: 
    18661909        for i in res.results: 
    1867             actual=i.actual_class 
    1868             if i.classes[lrn1]==actual: 
    1869                 if i.classes[lrn2]!=actual: 
    1870                     tf += 1.0 
    1871             elif i.classes[lrn2]==actual: 
    1872                     ft += 1.0 
     1910            actual = i.actual_class 
     1911            if i.classes[lrn1] == actual: 
     1912                if i.classes[lrn2] != actual: 
     1913                    tf += 1. 
     1914            elif i.classes[lrn2] == actual: 
     1915                    ft += 1. 
    18731916 
    18741917    su = tf + ft 
    18751918    if su: 
    1876         return (abs(tf-ft)-1)**2 / su 
     1919        return (abs(tf - ft) - 1)**2 / su 
    18771920    else: 
    18781921        return 0 
     
    18801923 
    18811924def Friedman(res, stat=CA): 
    1882     """ Compares classifiers by Friedman test, treating folds as different examles. 
    1883         Returns F, p and average ranks 
     1925    """ 
     1926    Compare classifiers with Friedman test, treating folds as different examles. 
     1927    Returns F, p and average ranks. 
    18841928    """ 
    18851929    res_split = split_by_iterations(res) 
     
    18881932    N = len(res) 
    18891933    k = len(res[0]) 
    1890     sums = [0.0]*k 
     1934    sums = [0.] * k 
    18911935    for r in res: 
    1892         ranks = [k-x+1 for x in statc.rankdata(r)] 
    1893         if stat==Brier_score: # reverse ranks for Brier_score (lower better) 
    1894             ranks = [k+1-x for x in ranks] 
     1936        ranks = [k - x + 1 for x in statc.rankdata(r)] 
     1937        if stat == Brier_score: # reverse ranks for Brier_score (lower better) 
     1938            ranks = [k + 1 - x for x in ranks] 
    18951939        sums = map(add, ranks, sums) 
    18961940 
    1897     T = sum(x*x for x in sums) 
    1898     sums = [x/N for x in sums] 
    1899  
    1900     F = 12.0 / (N*k*(k+1)) * T  - 3 * N * (k+1) 
    1901  
    1902     return F, statc.chisqprob(F, k-1), sums 
     1941    T = sum(x * x for x in sums) 
     1942    sums = [x / N for x in sums] 
     1943 
     1944    F = 12. / (N * k * (k + 1)) * T  - 3 * N * (k + 1) 
     1945 
     1946    return F, statc.chisqprob(F, k - 1), sums 
    19031947 
    19041948 
    19051949def Wilcoxon_pairs(res, avgranks, stat=CA): 
    1906     """ Returns a triangular matrix, where element[i][j] stores significance of difference 
    1907         between i-th and j-th classifier, as computed by Wilcoxon test. The element is positive 
    1908         if i-th is better than j-th, negative if it is worse, and 1 if they are equal. 
    1909         Arguments to function are ExperimentResults, average ranks (as returned by Friedman) 
    1910         and, optionally, a statistics; greater values should mean better results.append 
     1950    """ 
     1951    Return a triangular matrix, where element[i][j] stores significance of 
     1952    difference between the i-th and the j-th classifier, as computed by the 
     1953    Wilcoxon test. The element is positive if the i-th is better than the j-th, 
     1954    negative if it is worse, and 1 if they are equal. 
     1955    Arguments are ExperimentResults, average ranks (as returned by Friedman) 
     1956    and, optionally, a statistics; greater values should mean better results. 
    19111957    """ 
    19121958    res_split = split_by_iterations(res) 
     
    19171963    for m1 in range(k): 
    19181964        nl = [] 
    1919         for m2 in range(m1+1, k): 
     1965        for m2 in range(m1 + 1, k): 
    19201966            t, p = statc.wilcoxont([r[m1] for r in res], [r[m2] for r in res]) 
    19211967            if avgranks[m1]<avgranks[m2]: 
     
    19311977@deprecated_keywords({"allResults": "all_results", 
    19321978                      "noConfidence": "no_confidence"}) 
    1933 def plot_learning_curve_learners(file, all_results, proportions, learners, no_confidence=0): 
    1934     plot_learning_curve(file, all_results, proportions, [Orange.misc.getobjectname(learners[i], "Learner %i" % i) for i in range(len(learners))], no_confidence) 
     1979def plot_learning_curve_learners(file, all_results, proportions, learners, 
     1980                                 no_confidence=0): 
     1981    plot_learning_curve(file, all_results, proportions, 
     1982        [Orange.misc.getobjectname(learners[i], "Learner %i" % i) 
     1983        for i in range(len(learners))], no_confidence) 
    19351984 
    19361985 
    19371986@deprecated_keywords({"allResults": "all_results", 
    19381987                      "noConfidence": "no_confidence"}) 
    1939 def plot_learning_curve(file, all_results, proportions, legend, no_confidence=0): 
     1988def plot_learning_curve(file, all_results, proportions, legend, 
     1989                        no_confidence=0): 
    19401990    import types 
    1941     fopened=0 
    1942     if type(file)==types.StringType: 
    1943         file=open(file, "wt") 
    1944         fopened=1 
     1991    fopened = 0 
     1992    if type(file) == types.StringType: 
     1993        file = open(file, "wt") 
     1994        fopened = 1 
    19451995         
    19461996    file.write("set yrange [0:1]\n") 
     
    19502000 
    19512001    file.write("plot \\\n") 
    1952     for i in range(len(legend)-1): 
     2002    for i in range(len(legend) - 1): 
    19532003        if not no_confidence: 
    19542004            file.write("'-' title '' with yerrorbars pointtype %i,\\\n" % (i+1)) 
     
    26432693 
    26442694 
    2645 ######################################################################################### 
     2695################################################################################ 
    26462696if __name__ == "__main__": 
    26472697    avranks =  [3.143, 2.000, 2.893, 1.964] 
  • Orange/regression/pls.py

    r10330 r10367  
    4141 
    4242To predict values for the first two data instances 
    43 use the followin code  
     43use the following code:  
    4444 
    4545.. literalinclude:: code/pls-example.py 
    4646    :lines: 16-20 
    4747 
    48 Output 
    49  
    50 :: 
     48Output:: 
    5149 
    5250    Actual     [<orange.Value 'Y1'='0.490'>, <orange.Value 'Y2'='1.237'>, <orange.Value 'Y3'='1.808'>, <orange.Value 'Y4'='0.422'>] 
     
    5755 
    5856To see the coefficient of the model (in this case they are stored in a matrix) 
    59 print the model 
     57print the model: 
    6058 
    6159.. literalinclude:: code/pls-example.py 
    6260    :lines: 22 
    6361 
    64 The ouptut looks like 
    65  
    66 :: 
     62The ouptut looks like this:: 
    6763 
    6864    Regression coefficients: 
Note: See TracChangeset for help on using the changeset viewer.