Changeset 10280:44d2bfa6236e in orange
 Timestamp:
 02/16/12 14:44:46 (2 years ago)
 Branch:
 default
 rebase_source:
 9ac845321789a6ea7d6bfb5605ff5204e9574b6c
 Location:
 Orange
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

Orange/evaluation/scoring.py
r10258 r10280 356 356 # Scores for evaluation of classifiers 357 357 358 class CA Class(object):358 class CA(list): 359 359 """Computation of CA from different types of test_results""" 360 360 CONFUSION_MATRIX = 0 … … 365 365 @deprecated_keywords({"reportSE": "report_se", 366 366 "unweighted": "ignore_weights"}) 367 def __ call__(self, test_results, report_se = False, ignore_weights=False):367 def __init__(self, test_results, report_se = False, ignore_weights=False): 368 368 """Return percentage of matches between predicted and actual class. 369 369 … … 378 378 the assumption of normal distribution otherwise. 379 379 """ 380 super(CA, self).__init__() 381 self.report_se = report_se 382 self.ignore_weights = ignore_weights 383 380 384 input_type = self.get_input_type(test_results) 381 385 if input_type == self.CONFUSION_MATRIX: 382 return self.from_confusion_matrix(test_results, report_se)386 self[:] = [self.from_confusion_matrix(test_results)] 383 387 elif input_type == self.CONFUSION_MATRIX_LIST: 384 return self.from_confusion_matrix_list(test_results, report_se)388 self[:] = self.from_confusion_matrix_list(test_results) 385 389 elif input_type == self.CLASSIFICATION: 386 return self.from_classification_results( 387 test_results, report_se, ignore_weights) 390 self[:] = self.from_classification_results(test_results) 388 391 elif input_type == self.CROSS_VALIDATION: 389 return self.from_crossvalidation_results( 390 test_results, report_se, ignore_weights) 391 392 def from_confusion_matrix(self, cm, report_se): 393 all_predictions = cm.TP+cm.FN+cm.FP+cm.TN 392 self[:] = self.from_crossvalidation_results(test_results) 393 394 def from_confusion_matrix(self, cm): 395 all_predictions = 0. 396 correct_predictions = 0. 397 if isinstance(cm, ConfusionMatrix): 398 all_predictions += cm.TP+cm.FN+cm.FP+cm.TN 399 correct_predictions += cm.TP+cm.TN 400 else: 401 for r, row in enumerate(cm): 402 for c, column in enumerate(row): 403 if r == c: 404 correct_predictions += column 405 all_predictions += column 406 394 407 check_non_zero(all_predictions) 395 ca = (cm.TP+cm.TN)/all_predictions396 397 if report_se:408 ca = correct_predictions/all_predictions 409 410 if self.report_se: 398 411 return ca, ca*(1ca)/math.sqrt(all_predictions) 399 412 else: 400 413 return ca 401 414 402 def from_confusion_matrix_list(self, confusion_matrices, report_se): 403 return [self.from_confusion_matrix(cm, report_se=report_se) 404 for cm in confusion_matrices] 405 406 def from_classification_results(self, test_results, report_se, ignore_results): 415 def from_confusion_matrix_list(self, confusion_matrices): 416 return [self.from_confusion_matrix(cm) for cm in confusion_matrices] 417 418 def from_classification_results(self, test_results): 407 419 CAs = [0.0]*test_results.number_of_learners 408 420 totweight = 0. 409 421 for tex in test_results.results: 410 w = 1. if ignore_results else tex.weight422 w = 1. if self.ignore_weights else tex.weight 411 423 CAs = map(lambda res, cls: res+(cls==tex.actual_class and w), CAs, tex.classes) 412 424 totweight += w … … 414 426 ca = [x/totweight for x in CAs] 415 427 416 if report_se:428 if self.report_se: 417 429 return [(x, x*(1x)/math.sqrt(totweight)) for x in ca] 418 430 else: 419 431 return ca 420 432 421 def from_crossvalidation_results(self, test_results , report_se, ignore_weights):433 def from_crossvalidation_results(self, test_results): 422 434 CAsByFold = [[0.0]*test_results.number_of_iterations for _ in range(test_results.number_of_learners)] 423 435 foldN = [0.0]*test_results.number_of_iterations 424 436 425 437 for tex in test_results.results: 426 w = 1. if ignore_weights else tex.weight438 w = 1. if self.ignore_weights else tex.weight 427 439 for lrn in range(test_results.number_of_learners): 428 440 CAsByFold[lrn][tex.iteration_number] += (tex.classes[lrn]==tex.actual_class) and w 429 441 foldN[tex.iteration_number] += w 430 442 431 return statistics_by_folds(CAsByFold, foldN, report_se, False)443 return statistics_by_folds(CAsByFold, foldN, self.report_se, False) 432 444 433 445 def get_input_type(self, test_results): … … 442 454 return self.CONFUSION_MATRIX_LIST 443 455 444 445 CA = CAClass()446 456 447 457 @deprecated_keywords({"reportSE": "report_se", 
Orange/testing/unit/tests/test_evaluation_scoring.py
r10279 r10280 117 117 cm = scoring.confusion_matrices(cv, class_index=1) 118 118 ca = scoring.CA(cm[0]) 119 self.assert True(isinstance(ca, float))119 self.assertEqual(len(ca), 1) 120 120 121 121 def test_ca_from_confusion_matrix_for_classification_on_iris(self): … … 139 139 cm = scoring.confusion_matrices(cv, class_index=1) 140 140 ca = scoring.CA(cm[0], report_se=True) 141 self.assert True(isinstance(ca, tuple))141 self.assertEqual(len(ca), 1) 142 142 143 143 def test_ca_on_iris(self):
Note: See TracChangeset
for help on using the changeset viewer.