Changeset 10022:61f650b5c37e in orange


Ignore:
Timestamp:
02/07/12 23:57:49 (2 years ago)
Author:
Lan Zagar <lan.zagar@…>
Branch:
default
rebase_source:
b67b8c0a3b86cdb310e0c779cfd9c118044f4e0b
Message:

Added two multitarget helpers for using singletarget scoring methods.

Location:
Orange/evaluation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Orange/evaluation/scoring.py

    r10005 r10022  
    24002400#    pass 
    24012401 
     2402 
     2403def mt_average_scores(res, score, weights=None): 
     2404    """ 
     2405    Average the scores of individual targets. 
     2406 
     2407    :param score: Single-target scoring method. 
     2408    :param weights: List of real weights, one for each target, 
     2409                    for a weighted average. 
     2410 
     2411    """ 
     2412    if not len(res.results): 
     2413        raise ValueError, "Cannot compute the score: no examples." 
     2414    if res.number_of_learners < 1: 
     2415        return [] 
     2416    if weights is None: 
     2417        weights = [1. for _ in res.results[0].actual_class] 
     2418    # save original classes 
     2419    clsss = [te.classes for te in res.results] 
     2420    aclsss = [te.actual_class for te in res.results] 
     2421    # compute single target scores 
     2422    single_scores = [] 
     2423    for i in range(len(clsss[0][0])): 
     2424        for te, clss, aclss in zip(res.results, clsss, aclsss): 
     2425            te.classes = [cls[i] for cls in clss] 
     2426            te.actual_class = aclss[i] 
     2427        single_scores.append(score(res)) 
     2428    # restore original classes 
     2429    for te, clss, aclss in zip(res.results, clsss, aclsss): 
     2430        te.classes = clss 
     2431        te.actual_class = aclss 
     2432    return [sum(w * s for w, s in zip(weights, scores)) / sum(weights) 
     2433        for scores in zip(*single_scores)] 
     2434 
     2435def mt_flattened_score(res, score): 
     2436    """ 
     2437    Flatten the predictions of multiple targets 
     2438    and compute a single-target score. 
     2439     
     2440    :param score: Single-target scoring method. 
     2441    """ 
     2442    res2 = Orange.evaluation.testing.ExperimentResults(res.number_of_iterations, 
     2443        res.classifier_names, class_values=res.class_values, 
     2444        weights=res.weights, classifiers=res.classifiers, loaded=res.loaded, 
     2445        test_type=Orange.evaluation.testing.TEST_TYPE_SINGLE, labels=res.labels) 
     2446    for te in res.results: 
     2447        for i, ac in enumerate(te.actual_class): 
     2448            te2 = Orange.evaluation.testing.TestedExample( 
     2449                iteration_number=te.iteration_number, actual_class=ac) 
     2450            for c, p in zip(te.classes, te.probabilities): 
     2451                te2.add_result(c[i], p[i]) 
     2452            res2.results.append(te2) 
     2453    return score(res2) 
     2454 
     2455 
    24022456######################################################################################### 
    24032457if __name__ == "__main__": 
  • Orange/evaluation/testing.py

    r10021 r10022  
    2525    def __init__(self, iteration_number=None, actual_class=None, n=0, weight=1.0): 
    2626        """ 
    27         :param iteration_number: 
    28         :param actual_class: 
    29         :param n: 
    30         :param weight: 
     27        :param iteration_number: The iteration number of TestedExample. 
     28        :param actual_class: The actual class of TestedExample. 
     29        :param n: The number of learners. 
     30        :param weight: The weight of the TestedExample. 
    3131        """ 
    3232        self.classes = [None]*n 
Note: See TracChangeset for help on using the changeset viewer.