Changeset 4602:fc7e929087be in orange
 Timestamp:
 04/21/08 12:54:53 (6 years ago)
 Branch:
 default
 Convert:
 592a83f6d329888a9497eeb8c8fd9ca7aaad0628
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/orngStat.py
r4451 r4602 272 272 273 273 def CA(res, reportSE = False, **argkw): 274 """Return classification accuracy.""" 274 275 if res.numberOfIterations==1: 275 276 if type(res)==ConfusionMatrix: 276 div = nm.TP +nm.FN+nm.FP+nm.TN277 div = nm.TP + nm.FN + nm.FP + nm.TN 277 278 checkNonZero(div) 278 ca = [(nm.TP +nm.TN)/div]279 ca = [(nm.TP + nm.TN) / div] 279 280 else: 280 281 CAs = [0.0]*res.numberOfLearners … … 314 315 315 316 317 def 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*(1x)/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 316 356 # Obsolete, but kept for compatibility 317 357 def CA_se(res, **argkw): 358 """Return standard deviation of classification accuracy.""" 318 359 return CA(res, True, **argkw) 319 360 320 361 321 362 def AP(res, reportSE = False, **argkw): 363 """Return probability assigned to the actual class.""" 322 364 if res.numberOfIterations == 1: 323 365 APs=[0.0]*res.numberOfLearners … … 349 391 350 392 def BrierScore(res, reportSE = False, **argkw): 351 """ Computes Brier score"""352 #Computes an average (over examples) of sum_x(t(x)  p(x))^2, where353 #x is class,354 #t(x) is 0 for 'wrong' and 1 for 'correct' class355 #p(x) is predicted probabilty.356 #There's a trick: since t(x) is zero for all classes but the357 #correct one (c), we compute the sum as sum_x(p(x)^2)  2*p(c) + 1358 #Since +1 is there for each example, it adds 1 to the average359 #We skip the +1 inside the sum and add it just at the end of the function360 # We take max(result, 0) to avoid 0.0000x due to rounding errors393 """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.""" 361 403 362 404 if res.numberOfIterations == 1: … … 456 498 457 499 def 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 459 502 if (Pc>=P): 460 503 return log2(P)+log2(Pc) … … 463 506 464 507 def IS(res, apriori=None, reportSE = False, **argkw): 508 """Return information score (Bratko & Kononenko, 1991).""" 465 509 if not apriori: 466 510 apriori = classProbabilitiesFromRes(res) … … 650 694 651 695 def recall(confm): 696 """Return recall (from confusion matrix).""" 652 697 return sens(confm) 653 698 654 699 655 700 def spec(confm): 701 """Return specificity (from confusion matrix).""" 656 702 if type(confm) == list: 657 703 return [spec(cm) for cm in confm] … … 666 712 667 713 def PPV(confm): 714 """Return positive preditive value (from confusion matrix).""" 668 715 if type(confm) == list: 669 716 return [PPV(cm) for cm in confm] … … 678 725 679 726 def precision(confm): 727 """Return precision (from confusion matrix).""" 680 728 return PPV(confm) 681 729 682 730 683 731 def NPV(confm): 732 """Return negative predictive value (from confusion matrix).""" 684 733 if type(confm) == list: 685 734 return [NPV(cm) for cm in confm] … … 693 742 694 743 def F1(confm): 744 """Return F1 measure(harmonic mean of precision and recall, from confusion matrix).""" 695 745 if type(confm) == list: 696 746 return [F1(cm) for cm in confm]
Note: See TracChangeset
for help on using the changeset viewer.