Changeset 4602:fc7e929087be in orange


Ignore:
Timestamp:
04/21/08 12:54:53 (6 years ago)
Author:
blaz <blaz.zupan@…>
Branch:
default
Convert:
592a83f6d329888a9497eeb8c8fd9ca7aaad0628
Message:

added evaluation with cost matrix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/orngStat.py

    r4451 r4602  
    272272 
    273273def CA(res, reportSE = False, **argkw): 
     274    """Return classification accuracy.""" 
    274275    if res.numberOfIterations==1: 
    275276        if type(res)==ConfusionMatrix: 
    276             div = nm.TP+nm.FN+nm.FP+nm.TN 
     277            div = nm.TP + nm.FN + nm.FP + nm.TN 
    277278            checkNonZero(div) 
    278             ca = [(nm.TP+nm.TN)/div] 
     279            ca = [(nm.TP + nm.TN) / div] 
    279280        else: 
    280281            CAs = [0.0]*res.numberOfLearners 
     
    314315 
    315316 
     317def cost(res, costmatrix, reportSE = False, **argkw): 
     318    """Returns the classification cost (given the cost matrix).""" 
     319    if res.numberOfIterations==1: 
     320        costs = [0.0] * res.numberOfLearners 
     321        if argkw.get("unweighted", 0) or not res.weights: 
     322            totweight = gettotsize(res) 
     323            for tex in res.results: 
     324                costs = map(lambda res, cls: res + costmatrix[cls][tex.actualClass], costs, tex.classes) 
     325        else: 
     326            totweight = 0. 
     327            for tex in res.results: 
     328                costs = map(lambda res, cls: res + costmatrix[cls][tex.actualClass] * tex.weight, costs, tex.classes) 
     329                totweight += tex.weight 
     330        checkNonZero(totweight) 
     331        c = [x / totweight for x in costs] 
     332             
     333        if reportSE: 
     334            return [(x, x*(1-x)/math.sqrt(totweight)) for x in c] 
     335        else: 
     336            return c 
     337         
     338    else: 
     339        costByFold = [[0.0]*res.numberOfIterations for i in range(res.numberOfLearners)] 
     340        foldN = [0.0]*res.numberOfIterations 
     341 
     342        if argkw.get("unweighted", 0) or not res.weights: 
     343            for tex in res.results: 
     344                for lrn in range(res.numberOfLearners): 
     345                    costByFold[lrn][tex.iterationNumber] += costmatrix[tex.classes[lrn]][tex.actualClass] 
     346                foldN[tex.iterationNumber] += 1 
     347        else: 
     348            for tex in res.results: 
     349                for lrn in range(res.numberOfLearners): 
     350                    costByFold[lrn][tex.iterationNumber] += costmatrix[tex.classes[lrn]][tex.actualClass] * tex.weight 
     351                foldN[tex.iterationNumber] += tex.weight 
     352 
     353        return statisticsByFolds(costByFold, foldN, reportSE, False) 
     354 
     355 
    316356# Obsolete, but kept for compatibility 
    317357def CA_se(res, **argkw): 
     358    """Return standard deviation of classification accuracy.""" 
    318359    return CA(res, True, **argkw) 
    319360 
    320361 
    321362def AP(res, reportSE = False, **argkw): 
     363    """Return probability assigned to the actual class.""" 
    322364    if res.numberOfIterations == 1: 
    323365        APs=[0.0]*res.numberOfLearners 
     
    349391 
    350392def BrierScore(res, reportSE = False, **argkw): 
    351     """Computes Brier score""" 
    352     # Computes an average (over examples) of sum_x(t(x) - p(x))^2, where 
    353     #    x is class, 
    354     #    t(x) is 0 for 'wrong' and 1 for 'correct' class 
    355     #    p(x) is predicted probabilty. 
    356     # There's a trick: since t(x) is zero for all classes but the 
    357     # correct one (c), we compute the sum as sum_x(p(x)^2) - 2*p(c) + 1 
    358     # Since +1 is there for each example, it adds 1 to the average 
    359     # We skip the +1 inside the sum and add it just at the end of the function 
    360     # We take max(result, 0) to avoid -0.0000x due to rounding errors 
     393    """Return Brier score. 
     394    Computes an average (over examples) of sum_x(t(x) - p(x))^2, where 
     395       x is class, 
     396       t(x) is 0 for 'wrong' and 1 for 'correct' class 
     397       p(x) is predicted probabilty. 
     398    There's a trick: since t(x) is zero for all classes but the 
     399    correct one (c), we compute the sum as sum_x(p(x)^2) - 2*p(c) + 1 
     400    Since +1 is there for each example, it adds 1 to the average 
     401    We skip the +1 inside the sum and add it just at the end of the function 
     402    We take max(result, 0) to avoid -0.0000x due to rounding errors.""" 
    361403 
    362404    if res.numberOfIterations == 1: 
     
    456498 
    457499def IS_ex(Pc, P): 
    458     "Pc aposterior probability, P aprior" 
     500    """Return information score of an example from posterior and prior class probability.""" 
     501    # Pc posterior probability, P prior probability 
    459502    if (Pc>=P): 
    460503        return -log2(P)+log2(Pc) 
     
    463506     
    464507def IS(res, apriori=None, reportSE = False, **argkw): 
     508    """Return information score (Bratko & Kononenko, 1991).""" 
    465509    if not apriori: 
    466510        apriori = classProbabilitiesFromRes(res) 
     
    650694 
    651695def recall(confm): 
     696    """Return recall (from confusion matrix).""" 
    652697    return sens(confm) 
    653698 
    654699 
    655700def spec(confm): 
     701    """Return specificity (from confusion matrix).""" 
    656702    if type(confm) == list: 
    657703        return [spec(cm) for cm in confm] 
     
    666712 
    667713def PPV(confm): 
     714    """Return positive preditive value (from confusion matrix).""" 
    668715    if type(confm) == list: 
    669716        return [PPV(cm) for cm in confm] 
     
    678725 
    679726def precision(confm): 
     727    """Return precision (from confusion matrix).""" 
    680728    return PPV(confm) 
    681729 
    682730 
    683731def NPV(confm): 
     732    """Return negative predictive value (from confusion matrix).""" 
    684733    if type(confm) == list: 
    685734        return [NPV(cm) for cm in confm] 
     
    693742 
    694743def F1(confm): 
     744    """Return F1 measure(harmonic mean of precision and recall, from confusion matrix).""" 
    695745    if type(confm) == list: 
    696746        return [F1(cm) for cm in confm] 
Note: See TracChangeset for help on using the changeset viewer.