Changeset 10362:539f49e3586d in orange


Ignore:
Timestamp:
02/24/12 13:12:28 (2 years ago)
Author:
Lan Zagar <lan.zagar@…>
Branch:
default
rebase_source:
b107b292c78ab9b716058336d460ba28782be01a
Message:

Small (but numerous) code style improvements in the first third of scoring.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/evaluation/scoring.py

    r10343 r10362  
    1 import  math, functools 
     1import math 
     2import functools 
    23from operator import add 
     4 
    35import numpy 
    46 
     
    2022def log2(x): 
    2123    """Calculate logarithm in base 2.""" 
    22     return math.log(x)/math.log(2) 
     24    return math.log(x) / math.log(2) 
    2325 
    2426def check_non_zero(x): 
    25     """Throw Value Error when x = 0.0.""" 
    26     if x==0.0: 
    27         raise ValueError, "Cannot compute the score: no examples or sum of weights is 0.0." 
     27    """Throw Value Error when x = 0.""" 
     28    if x == 0.: 
     29        raise ValueError, "Cannot compute the score: no examples or sum of weights is 0." 
    2830 
    2931def gettotweight(res): 
    3032    """Sum all the weights""" 
    31     totweight = reduce(lambda x, y: x+y.weight, res.results, 0) 
    32     if totweight==0.0: 
    33         raise ValueError, "Cannot compute the score: sum of weights is 0.0." 
     33    totweight = reduce(lambda x, y: x + y.weight, res.results, 0) 
     34    if totweight == 0.: 
     35        raise ValueError, "Cannot compute the score: sum of weights is 0." 
    3436    return totweight 
    3537 
     
    6870                    weights=res.weights, baseClass=res.baseClass, 
    6971                    classifiers=[res.classifiers[i]] if res.classifiers else [], 
    70                     test_type = res.test_type, labels = res.labels) 
     72                    test_type=res.test_type, labels=res.labels) 
    7173        r.results = [] 
    7274        for te in res.results: 
     
    8183def class_probabilities_from_res(res, **argkw): 
    8284    """Calculate class probabilities""" 
    83     probs = [0.0] * len(res.class_values) 
     85    probs = [0.] * len(res.class_values) 
    8486    if argkw.get("unweighted", 0) or not res.weights: 
    8587        for tex in res.results: 
    86             probs[int(tex.actual_class)] += 1.0 
     88            probs[int(tex.actual_class)] += 1. 
    8789        totweight = gettotsize(res) 
    8890    else: 
    89         totweight = 0.0 
     91        totweight = 0. 
    9092        for tex in res.results: 
    9193            probs[tex.actual_class] += tex.weight 
     
    103105    if iteration_is_outer: 
    104106        if not stats: 
    105             raise ValueError, "Cannot compute the score: no examples or sum of weights is 0.0." 
     107            raise ValueError, "Cannot compute the score: no examples or sum of weights is 0." 
    106108        number_of_learners = len(stats[0]) 
    107         stats = filter(lambda (x, fN): fN>0.0, zip(stats,fold_n)) 
    108         stats = [ [x[lrn]/fN for x, fN in stats] for lrn in range(number_of_learners)] 
     109        stats = filter(lambda (x, fN): fN > 0, zip(stats, fold_n)) 
     110        stats = [[x[lrn] / fN for x, fN in stats] 
     111                 for lrn in range(number_of_learners)] 
    109112    else: 
    110         stats = [ [x/Fn for x, Fn in filter(lambda (x, Fn): Fn > 0.0, zip(lrnD, fold_n))] for lrnD in stats] 
     113        stats = [[x / Fn for x, Fn in filter(lambda (x, Fn): Fn > 0, 
     114                 zip(lrnD, fold_n))] for lrnD in stats] 
    111115 
    112116    if not stats: 
    113117        raise ValueError, "Cannot compute the score: no classifiers" 
    114118    if not stats[0]: 
    115         raise ValueError, "Cannot compute the score: no examples or sum of weights is 0.0." 
     119        raise ValueError, "Cannot compute the score: no examples or sum of weights is 0." 
    116120     
    117121    if report_se: 
     
    121125     
    122126def ME(res, **argkw): 
    123     MEs = [0.0]*res.number_of_learners 
     127    MEs = [0.] * res.number_of_learners 
    124128 
    125129    if argkw.get("unweighted", 0) or not res.weights: 
     
    130134    else: 
    131135        for tex in res.results: 
    132             MEs = map(lambda res, cls, ac = float(tex.actual_class), tw = tex.weight: 
    133                        res + tw*abs(float(cls) - ac), MEs, tex.classes) 
     136            MEs = map(lambda res, cls, ac=float(tex.actual_class), tw=tex.weight: 
     137                       res + tw * abs(float(cls) - ac), MEs, tex.classes) 
    134138        totweight = gettotweight(res) 
    135139 
    136     return [x/totweight for x in MEs] 
     140    return [x / totweight for x in MEs] 
    137141 
    138142MAE = ME 
     
    154158    @deprecated_keywords({"predictedPositive": "predicted_positive", 
    155159                          "isPositive": "is_positive"}) 
    156     def addTFPosNeg(self, predicted_positive, is_positive, weight = 1.0): 
     160    def addTFPosNeg(self, predicted_positive, is_positive, weight=1.0): 
    157161        """ 
    158162        Update confusion matrix with result of a single classification 
     
    181185def check_argkw(dct, lst): 
    182186    """check_argkw(dct, lst) -> returns true if any items have non-zero value in dct""" 
    183     return reduce(lambda x,y: x or y, [dct.get(k, 0) for k in lst]) 
     187    return reduce(lambda x, y: x or y, [dct.get(k, 0) for k in lst]) 
    184188 
    185189def regression_error(res, **argkw): 
     
    187191    if argkw.get("SE", 0) and res.number_of_iterations > 1: 
    188192        # computes the scores for each iteration, then averages 
    189         scores = [[0.0] * res.number_of_iterations for _ in range(res.number_of_learners)] 
    190         norm=None 
     193        scores = [[0.] * res.number_of_iterations 
     194                  for _ in range(res.number_of_learners)] 
     195        norm = None 
    191196        if argkw.get("norm-abs", 0) or argkw.get("norm-sqr", 0): 
    192             norm = [0.0] * res.number_of_iterations 
    193  
    194         nIter = [0]*res.number_of_iterations       # counts examples in each iteration 
    195         a = [0]*res.number_of_iterations           # average class in each iteration 
     197            norm = [0.] * res.number_of_iterations 
     198 
     199        # counts examples in each iteration 
     200        nIter = [0] * res.number_of_iterations 
     201        # average class in each iteration 
     202        a = [0] * res.number_of_iterations 
    196203        for tex in res.results: 
    197204            nIter[tex.iteration_number] += 1 
    198205            a[tex.iteration_number] += float(tex.actual_class) 
    199         a = [a[i]/nIter[i] for i in range(res.number_of_iterations)] 
     206        a = [a[i] / nIter[i] for i in range(res.number_of_iterations)] 
    200207 
    201208        if argkw.get("unweighted", 0) or not res.weights: 
     
    217224                    else: 
    218225                        scores[i][tex.iteration_number] += (float(cls) - ai)**2 
    219         else: # unweighted<>0 
     226        else: # unweighted != 0 
    220227            raise NotImplementedError, "weighted error scores with SE not implemented yet" 
    221228 
    222229        if argkw.get("norm-abs") or argkw.get("norm-sqr"): 
    223             scores = [[x/n for x, n in zip(y, norm)] for y in scores] 
    224         else: 
    225             scores = [[x/ni for x, ni in zip(y, nIter)] for y in scores] 
     230            scores = [[x / n for x, n in zip(y, norm)] for y in scores] 
     231        else: 
     232            scores = [[x / ni for x, ni in zip(y, nIter)] for y in scores] 
    226233 
    227234        if argkw.get("R2"): 
     
    234241         
    235242    else: # single iteration (testing on a single test set) 
    236         scores = [0.0] * res.number_of_learners 
    237         norm = 0.0 
     243        scores = [0.] * res.number_of_learners 
     244        norm = 0. 
    238245 
    239246        if argkw.get("unweighted", 0) or not res.weights: 
     
    242249            for tex in res.results: 
    243250                if argkw.get("abs", 0): 
    244                     scores = map(lambda res, cls, ac = float(tex.actual_class): 
     251                    scores = map(lambda res, cls, ac=float(tex.actual_class): 
    245252                                 res + abs(float(cls) - ac), scores, tex.classes) 
    246253                else: 
    247                     scores = map(lambda res, cls, ac = float(tex.actual_class): 
     254                    scores = map(lambda res, cls, ac=float(tex.actual_class): 
    248255                                 res + (float(cls) - ac)**2, scores, tex.classes) 
    249256 
     
    255262        else: 
    256263            # UNFINISHED 
    257             MSEs = [0.]*res.number_of_learners 
     264            MSEs = [0.] * res.number_of_learners 
    258265            for tex in res.results: 
    259                 MSEs = map(lambda res, cls, ac = float(tex.actual_class), 
    260                            tw = tex.weight: 
     266                MSEs = map(lambda res, cls, ac=float(tex.actual_class), 
     267                           tw=tex.weight: 
    261268                           res + tw * (float(cls) - ac)**2, MSEs, tex.classes) 
    262269            totweight = gettotweight(res) 
    263270 
    264271        if argkw.get("norm-abs", 0) or argkw.get("norm-sqr", 0): 
    265             scores = [s/norm for s in scores] 
     272            scores = [s / norm for s in scores] 
    266273        else: # normalize by number of instances (or sum of weights) 
    267             scores = [s/totweight for s in scores] 
     274            scores = [s / totweight for s in scores] 
    268275 
    269276        if argkw.get("R2"): 
    270             scores = [1.0 - s for s in scores] 
     277            scores = [1. - s for s in scores] 
    271278 
    272279        if argkw.get("sqrt", 0): 
     
    315322    """MSE(res) -> mean-squared error""" 
    316323    if argkw.get("SE", 0) and res.number_of_iterations > 1: 
    317         MSEs = [[0.0] * res.number_of_iterations for _ in range(res.number_of_learners)] 
    318         nIter = [0]*res.number_of_iterations 
     324        MSEs = [[0.] * res.number_of_iterations 
     325                for _ in range(res.number_of_learners)] 
     326        nIter = [0] * res.number_of_iterations 
    319327        if argkw.get("unweighted", 0) or not res.weights: 
    320328            for tex in res.results: 
     
    325333        else: 
    326334            raise ValueError, "weighted RMSE with SE not implemented yet" 
    327         MSEs = [[x/ni for x, ni in zip(y, nIter)] for y in MSEs] 
     335        MSEs = [[x / ni for x, ni in zip(y, nIter)] for y in MSEs] 
    328336        if argkw.get("sqrt", 0): 
    329337            MSEs = [[math.sqrt(x) for x in y] for y in MSEs] 
     
    331339         
    332340    else: 
    333         MSEs = [0.0]*res.number_of_learners 
     341        MSEs = [0.] * res.number_of_learners 
    334342        if argkw.get("unweighted", 0) or not res.weights: 
    335343            for tex in res.results: 
    336                 MSEs = map(lambda res, cls, ac = float(tex.actual_class): 
     344                MSEs = map(lambda res, cls, ac=float(tex.actual_class): 
    337345                           res + (float(cls) - ac)**2, MSEs, tex.classes) 
    338346            totweight = gettotsize(res) 
    339347        else: 
    340348            for tex in res.results: 
    341                 MSEs = map(lambda res, cls, ac = float(tex.actual_class), tw = tex.weight: 
    342                            res + tw * (float(cls) - ac)**2, MSEs, tex.classes) 
     349                MSEs = map(lambda res, cls, ac=float(tex.actual_class), 
     350                           tw=tex.weight: res + tw * (float(cls) - ac)**2, 
     351                           MSEs, tex.classes) 
    343352            totweight = gettotweight(res) 
    344353 
    345354        if argkw.get("sqrt", 0): 
    346355            MSEs = [math.sqrt(x) for x in MSEs] 
    347         return [x/totweight for x in MSEs] 
     356        return [x / totweight for x in MSEs] 
    348357 
    349358def RMSE_old(res, **argkw): 
     
    384393        input_type = self.get_input_type(test_results) 
    385394        if input_type == self.CONFUSION_MATRIX: 
    386             self[:] =  [self.from_confusion_matrix(test_results)] 
     395            self[:] = [self.from_confusion_matrix(test_results)] 
    387396        elif input_type == self.CONFUSION_MATRIX_LIST: 
    388397            self[:] = self.from_confusion_matrix_list(test_results) 
     
    390399            self[:] = self.from_classification_results(test_results) 
    391400        elif input_type == self.CROSS_VALIDATION: 
    392             self[:] =  self.from_crossvalidation_results(test_results) 
     401            self[:] = self.from_crossvalidation_results(test_results) 
    393402 
    394403    def from_confusion_matrix(self, cm): 
     
    396405        correct_predictions = 0. 
    397406        if isinstance(cm, ConfusionMatrix): 
    398             all_predictions += cm.TP+cm.FN+cm.FP+cm.TN 
    399             correct_predictions += cm.TP+cm.TN 
     407            all_predictions += cm.TP + cm.FN + cm.FP + cm.TN 
     408            correct_predictions += cm.TP + cm.TN 
    400409        else: 
    401410            for r, row in enumerate(cm): 
     
    406415 
    407416        check_non_zero(all_predictions) 
    408         ca = correct_predictions/all_predictions 
     417        ca = correct_predictions / all_predictions 
    409418 
    410419        if self.report_se: 
    411             return ca, ca*(1-ca)/math.sqrt(all_predictions) 
     420            return ca, ca * (1 - ca) / math.sqrt(all_predictions) 
    412421        else: 
    413422            return ca 
     
    417426 
    418427    def from_classification_results(self, test_results): 
    419         CAs = [0.0]*test_results.number_of_learners 
     428        CAs = [0.] * test_results.number_of_learners 
    420429        totweight = 0. 
    421430        for tex in test_results.results: 
    422431            w = 1. if self.ignore_weights else tex.weight 
    423             CAs = map(lambda res, cls: res+(cls==tex.actual_class and w), CAs, tex.classes) 
     432            CAs = map(lambda res, cls: res + (cls == tex.actual_class and w), 
     433                      CAs, tex.classes) 
    424434            totweight += w 
    425435        check_non_zero(totweight) 
    426         ca = [x/totweight for x in CAs] 
     436        ca = [x / totweight for x in CAs] 
    427437 
    428438        if self.report_se: 
    429             return [(x, x*(1-x)/math.sqrt(totweight)) for x in ca] 
     439            return [(x, x * (1 - x) / math.sqrt(totweight)) for x in ca] 
    430440        else: 
    431441            return ca 
    432442 
    433443    def from_crossvalidation_results(self, test_results): 
    434         CAsByFold = [[0.0]*test_results.number_of_iterations for _ in range(test_results.number_of_learners)] 
    435         foldN = [0.0]*test_results.number_of_iterations 
     444        CAsByFold = [[0.] * test_results.number_of_iterations 
     445                     for _ in range(test_results.number_of_learners)] 
     446        foldN = [0.] * test_results.number_of_iterations 
    436447 
    437448        for tex in test_results.results: 
    438449            w = 1. if self.ignore_weights else tex.weight 
    439450            for lrn in range(test_results.number_of_learners): 
    440                 CAsByFold[lrn][tex.iteration_number] += (tex.classes[lrn]==tex.actual_class) and w 
     451                CAsByFold[lrn][tex.iteration_number] += (tex.classes[lrn] ==  
     452                    tex.actual_class) and w 
    441453            foldN[tex.iteration_number] += w 
    442454 
     
    457469@deprecated_keywords({"reportSE": "report_se", 
    458470                      "unweighted": "ignore_weights"}) 
    459 def AP(res, report_se = False, ignore_weights=False, **argkw): 
     471def AP(res, report_se=False, ignore_weights=False, **argkw): 
    460472    """ Computes the average probability assigned to the correct class. """ 
    461473    if res.number_of_iterations == 1: 
    462         APs=[0.0]*res.number_of_learners 
     474        APs=[0.] * res.number_of_learners 
    463475        if ignore_weights or not res.weights: 
    464476            for tex in res.results: 
    465                 APs = map(lambda res, probs: res + probs[tex.actual_class], APs, tex.probabilities) 
     477                APs = map(lambda res, probs: res + probs[tex.actual_class], 
     478                          APs, tex.probabilities) 
    466479            totweight = gettotsize(res) 
    467480        else: 
    468481            totweight = 0. 
    469482            for tex in res.results: 
    470                 APs = map(lambda res, probs: res + probs[tex.actual_class]*tex.weight, APs, tex.probabilities) 
     483                APs = map(lambda res, probs: res + probs[tex.actual_class] * 
     484                          tex.weight, APs, tex.probabilities) 
    471485                totweight += tex.weight 
    472486        check_non_zero(totweight) 
    473         return [AP/totweight for AP in APs] 
    474  
    475     APsByFold = [[0.0]*res.number_of_learners for _ in range(res.number_of_iterations)] 
    476     foldN = [0.0] * res.number_of_iterations 
     487        return [AP / totweight for AP in APs] 
     488 
     489    APsByFold = [[0.] * res.number_of_learners 
     490                 for _ in range(res.number_of_iterations)] 
     491    foldN = [0.] * res.number_of_iterations 
    477492    if ignore_weights or not res.weights: 
    478493        for tex in res.results: 
    479             APsByFold[tex.iteration_number] = map(lambda res, probs: res + probs[tex.actual_class], APsByFold[tex.iteration_number], tex.probabilities) 
     494            APsByFold[tex.iteration_number] = map(lambda res, probs: 
     495                res + probs[tex.actual_class], 
     496                APsByFold[tex.iteration_number], tex.probabilities) 
    480497            foldN[tex.iteration_number] += 1 
    481498    else: 
    482499        for tex in res.results: 
    483             APsByFold[tex.iteration_number] = map(lambda res, probs: res + probs[tex.actual_class] * tex.weight, APsByFold[tex.iteration_number], tex.probabilities) 
     500            APsByFold[tex.iteration_number] = map(lambda res, probs: 
     501                res + probs[tex.actual_class] * tex.weight, 
     502                APsByFold[tex.iteration_number], tex.probabilities) 
    484503            foldN[tex.iteration_number] += tex.weight 
    485504 
     
    489508@deprecated_keywords({"reportSE": "report_se", 
    490509                      "unweighted": "ignore_weights"}) 
    491 def Brier_score(res, report_se = False, ignore_weights=False, **argkw): 
     510def Brier_score(res, report_se=False, ignore_weights=False, **argkw): 
    492511    """ Computes the Brier's score, defined as the average (over test examples) 
    493512    of sumx(t(x)-p(x))2, where x is a class, t(x) is 1 for the correct class 
     
    506525 
    507526    if res.number_of_iterations == 1: 
    508         MSEs=[0.0]*res.number_of_learners 
     527        MSEs=[0.] * res.number_of_learners 
    509528        if ignore_weights or not res.weights: 
    510             totweight = 0.0 
     529            totweight = 0. 
    511530            for tex in res.results: 
    512                 MSEs = map(lambda res, probs: 
    513                            res + reduce(lambda s, pi: s+pi**2, probs, 0) - 2*probs[tex.actual_class], MSEs, tex.probabilities) 
     531                MSEs = map(lambda res, probs: res + reduce( 
     532                    lambda s, pi: s + pi**2, probs, 0) -  
     533                    2 * probs[tex.actual_class], MSEs, tex.probabilities) 
    514534                totweight += tex.weight 
    515535        else: 
    516536            for tex in res.results: 
    517                 MSEs = map(lambda res, probs: 
    518                            res + tex.weight*reduce(lambda s, pi: s+pi**2, probs, 0) - 2*probs[tex.actual_class], MSEs, tex.probabilities) 
     537                MSEs = map(lambda res, probs: res + tex.weight * reduce( 
     538                    lambda s, pi: s + pi**2, probs, 0) -  
     539                    2 * probs[tex.actual_class], MSEs, tex.probabilities) 
    519540            totweight = gettotweight(res) 
    520541        check_non_zero(totweight) 
    521542        if report_se: 
    522             return [(max(x/totweight+1.0, 0), 0) for x in MSEs]  ## change this, not zero!!! 
    523         else: 
    524             return [max(x/totweight+1.0, 0) for x in MSEs] 
    525  
    526     BSs = [[0.0]*res.number_of_learners for _ in range(res.number_of_iterations)] 
     543            ## change this, not zero!!! 
     544            return [(max(x / totweight + 1., 0), 0) for x in MSEs] 
     545        else: 
     546            return [max(x / totweight + 1., 0) for x in MSEs] 
     547 
     548    BSs = [[0.] * res.number_of_learners 
     549           for _ in range(res.number_of_iterations)] 
    527550    foldN = [0.] * res.number_of_iterations 
    528551 
    529552    if ignore_weights or not res.weights: 
    530553        for tex in res.results: 
    531             BSs[tex.iteration_number] = map(lambda rr, probs: 
    532                        rr + reduce(lambda s, pi: s+pi**2, probs, 0) - 2*probs[tex.actual_class], BSs[tex.iteration_number], tex.probabilities) 
     554            BSs[tex.iteration_number] = map(lambda rr, probs: rr + reduce( 
     555                lambda s, pi: s + pi**2, probs, 0) - 
     556                2 * probs[tex.actual_class], BSs[tex.iteration_number], 
     557                tex.probabilities) 
    533558            foldN[tex.iteration_number] += 1 
    534559    else: 
    535560        for tex in res.results: 
    536561            BSs[tex.iteration_number] = map(lambda res, probs: 
    537                        res + tex.weight*reduce(lambda s, pi: s+pi**2, probs, 0) - 2*probs[tex.actual_class], BSs[tex.iteration_number], tex.probabilities) 
     562                res + tex.weight * reduce(lambda s, pi: s + pi**2, probs, 0) - 
     563                2 * probs[tex. actual_class], BSs[tex.iteration_number], 
     564                tex.probabilities) 
    538565            foldN[tex.iteration_number] += tex.weight 
    539566 
    540567    stats = statistics_by_folds(BSs, foldN, report_se, True) 
    541568    if report_se: 
    542         return [(x+1.0, y) for x, y in stats] 
     569        return [(x + 1., y) for x, y in stats] 
    543570    else: 
    544         return [x+1.0 for x in stats] 
     571        return [x + 1. for x in stats] 
    545572 
    546573def BSS(res, **argkw): 
    547     return [1-x/2 for x in apply(Brier_score, (res, ), argkw)] 
     574    return [1 - x / 2 for x in apply(Brier_score, (res, ), argkw)] 
    548575 
    549576def IS_ex(Pc, P): 
    550577    """Pc aposterior probability, P aprior""" 
    551     if Pc>=P: 
    552         return -log2(P)+log2(Pc) 
     578    if Pc >= P: 
     579        return -log2(P) + log2(Pc) 
    553580    else: 
    554         return -(-log2(1-P)+log2(1-Pc)) 
     581        return -(-log2(1 - P) + log2(1 - Pc)) 
    555582 
    556583 
    557584@deprecated_keywords({"reportSE": "report_se"}) 
    558 def IS(res, apriori=None, report_se = False, **argkw): 
     585def IS(res, apriori=None, report_se=False, **argkw): 
    559586    """ Computes the information score as defined by  
    560587    `Kononenko and Bratko (1991) \ 
     
    567594        apriori = class_probabilities_from_res(res) 
    568595 
    569     if res.number_of_iterations==1: 
    570         ISs = [0.0]*res.number_of_learners 
     596    if res.number_of_iterations == 1: 
     597        ISs = [0.] * res.number_of_learners 
    571598        if argkw.get("unweighted", 0) or not res.weights: 
    572599            for tex in res.results: 
     
    579606              for i in range(len(tex.probabilities)): 
    580607                    cls = tex.actual_class 
    581                     ISs[i] += IS_ex(tex.probabilities[i][cls], apriori[cls]) * tex.weight 
     608                    ISs[i] += (IS_ex(tex.probabilities[i][cls], apriori[cls]) * 
     609                               tex.weight) 
    582610            totweight = gettotweight(res) 
    583611        if report_se: 
    584             return [(IS/totweight,0) for IS in ISs] 
    585         else: 
    586             return [IS/totweight for IS in ISs] 
     612            return [(IS / totweight, 0) for IS in ISs] 
     613        else: 
     614            return [IS / totweight for IS in ISs] 
    587615 
    588616         
    589     ISs = [[0.0]*res.number_of_iterations for _ in range(res.number_of_learners)] 
     617    ISs = [[0.] * res.number_of_iterations 
     618           for _ in range(res.number_of_learners)] 
    590619    foldN = [0.] * res.number_of_iterations 
    591620 
     
    595624            for i in range(len(tex.probabilities)): 
    596625                cls = tex.actual_class 
    597                 ISs[i][tex.iteration_number] += IS_ex(tex.probabilities[i][cls], apriori[cls]) 
     626                ISs[i][tex.iteration_number] += IS_ex(tex.probabilities[i][cls], 
     627                                                apriori[cls]) 
    598628            foldN[tex.iteration_number] += 1 
    599629    else: 
     
    601631            for i in range(len(tex.probabilities)): 
    602632                cls = tex.actual_class 
    603                 ISs[i][tex.iteration_number] += IS_ex(tex.probabilities[i][cls], apriori[cls]) * tex.weight 
     633                ISs[i][tex.iteration_number] += IS_ex(tex.probabilities[i][cls], 
     634                                                apriori[cls]) * tex.weight 
    604635            foldN[tex.iteration_number] += tex.weight 
    605636 
     
    622653 
    623654    k = len(res.results[0].classes) 
    624     if k<2: 
     655    if k < 2: 
    625656        raise TypeError, "nothing to compare (less than two classifiers given)" 
    626     if k==2: 
     657    if k == 2: 
    627658        return apply(Wilcoxon, (res, statistics), argkw) 
    628659    else: 
     
    648679    tfpns = [ConfusionMatrix() for _ in range(test_results.number_of_learners)] 
    649680     
    650     if class_index<0: 
     681    if class_index < 0: 
    651682        numberOfClasses = len(test_results.class_values) 
    652683        if class_index < -1 or numberOfClasses > 2: 
    653             cm = [[[0.0] * numberOfClasses for _ in range(numberOfClasses)] for _ in range(test_results.number_of_learners)] 
     684            cm = [[[0.] * numberOfClasses for _ in range(numberOfClasses)] 
     685                  for _ in range(test_results.number_of_learners)] 
    654686            if ignore_weights or not test_results.weights: 
    655687                for tex in test_results.results: 
     
    668700            return cm 
    669701             
    670         elif test_results.baseClass>=0: 
     702        elif test_results.baseClass >= 0: 
    671703            class_index = test_results.baseClass 
    672704        else: 
     
    678710                isPositive=(lr.actual_class==class_index) 
    679711                for i in range(test_results.number_of_learners): 
    680                     tfpns[i].addTFPosNeg(lr.probabilities[i][class_index]>cutoff, isPositive) 
     712                    tfpns[i].addTFPosNeg(lr.probabilities[i][class_index] > 
     713                                         cutoff, isPositive) 
    681714        else: 
    682715            for lr in test_results.results: 
    683                 isPositive=(lr.actual_class==class_index) 
     716                isPositive=(lr.actual_class == class_index) 
    684717                for i in range(test_results.number_of_learners): 
    685                     tfpns[i].addTFPosNeg(lr.probabilities[i][class_index]>cutoff, isPositive, lr.weight) 
     718                    tfpns[i].addTFPosNeg(lr.probabilities[i][class_index] >  
     719                                         cutoff, isPositive, lr.weight) 
    686720    else: 
    687721        if ignore_weights or not test_results.weights: 
    688722            for lr in test_results.results: 
    689                 isPositive=(lr.actual_class==class_index) 
     723                isPositive = (lr.actual_class == class_index) 
    690724                for i in range(test_results.number_of_learners): 
    691                     tfpns[i].addTFPosNeg(lr.classes[i]==class_index, isPositive) 
     725                    tfpns[i].addTFPosNeg(lr.classes[i] == class_index, 
     726                                         isPositive) 
    692727        else: 
    693728            for lr in test_results.results: 
    694                 isPositive=(lr.actual_class==class_index) 
     729                isPositive = (lr.actual_class == class_index) 
    695730                for i in range(test_results.number_of_learners): 
    696                     tfpns[i].addTFPosNeg(lr.classes[i]==class_index, isPositive, lr.weight) 
     731                    tfpns[i].addTFPosNeg(lr.classes[i] == class_index, 
     732                                         isPositive, lr.weight) 
    697733    return tfpns 
    698734 
     
    722758    colPriors = [sum(r[i] for r in confusion_matrix) for i in range(dim)] 
    723759    total = sum(rowPriors) 
    724     rowPriors = [r/total for r in rowPriors] 
    725     colPriors = [r/total for r in colPriors] 
     760    rowPriors = [r / total for r in rowPriors] 
     761    colPriors = [r / total for r in colPriors] 
    726762    ss = 0 
    727763    for ri, row in enumerate(confusion_matrix): 
     
    730766            if not e: 
    731767                return -1, -1, -1 
    732             ss += (o-e)**2 / e 
    733     df = (dim-1)**2 
     768            ss += (o - e)**2 / e 
     769    df = (dim - 1)**2 
    734770    return ss, df, statc.chisqprob(ss, df) 
    735771 
     
    749785            return -1 
    750786 
    751         return confusion_matrix.TP/tot 
     787        return confusion_matrix.TP / tot 
    752788 
    753789 
     
    775811            warnings.warn("Can't compute specificity: one or both classes have no instances") 
    776812            return -1 
    777         return confusion_matrix.TN/tot 
     813        return confusion_matrix.TN / tot 
    778814 
    779815 
     
    786822        return [PPV(cm) for cm in confusion_matrix] 
    787823    else: 
    788         tot = confusion_matrix.TP+confusion_matrix.FP 
     824        tot = confusion_matrix.TP + confusion_matrix.FP 
    789825        if tot < 1e-6: 
    790826            import warnings 
     
    811847        return [NPV(cm) for cm in confusion_matrix] 
    812848    else: 
    813         tot = confusion_matrix.FN+confusion_matrix.TN 
     849        tot = confusion_matrix.FN + confusion_matrix.TN 
    814850        if tot < 1e-6: 
    815851            import warnings 
    816852            warnings.warn("Can't compute NPV: one or both classes have no instances") 
    817853            return -1 
    818         return confusion_matrix.TN/tot 
     854        return confusion_matrix.TN / tot 
    819855 
    820856@deprecated_keywords({"confm": "confusion_matrix"}) 
     
    862898           
    863899        try:    
    864             r = (((truePositive * trueNegative) - (falsePositive * falseNegative))/  
    865                 math.sqrt(  (truePositive + falsePositive)  *  
    866                 ( truePositive + falseNegative ) *  
    867                 ( trueNegative + falsePositive ) *  
    868                 ( trueNegative + falseNegative ) ) 
    869                 ) 
     900            r = (((truePositive * trueNegative) - 
     901                  (falsePositive * falseNegative)) / 
     902                 math.sqrt((truePositive + falsePositive) * 
     903                           (truePositive + falseNegative) *  
     904                           (trueNegative + falsePositive) *  
     905                           (trueNegative + falseNegative))) 
    870906        except ZeroDivisionError: 
    871907            # Zero difision occurs when there is either no true positives  
     
    900936   if b_is_list_of_matrices: 
    901937       try: 
    902            return [scotts_pi(cm, b_is_list_of_matrices=False) for cm in confusion_matrix] 
     938           return [scotts_pi(cm, b_is_list_of_matrices=False) 
     939                   for cm in confusion_matrix] 
    903940       except TypeError: 
    904941           # Nevermind the parameter, maybe this is a "conventional" binary 
Note: See TracChangeset for help on using the changeset viewer.