evaluation.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:AutoML-Challenge 作者: postech-mlg-exbrain 项目源码 文件源码
def _calculate_score(solution, prediction, task_type, metric=None):

    if task_type not in TASK_TYPES:
        raise NotImplementedError(task_type)

    solution = np.array(solution, dtype=np.float32)

    if task_type == MULTICLASS_CLASSIFICATION:
        # This used to crash on travis-ci; special treatment to find out why
        # it crashed!
        solution_binary = np.zeros(prediction.shape)

        for i in range(solution_binary.shape[0]):
            label = int(np.round_(solution[i]))
            solution_binary[i, label] = 1
        solution = solution_binary

    elif task_type == BINARY_CLASSIFICATION:
        solution = solution.reshape(-1, 1)
        prediction = prediction[:, 1].reshape(-1, 1)

    if solution.shape != prediction.shape:
        raise ValueError("Solution shape %s != prediction shape %s" %
                         (solution.shape, prediction.shape))

    if metric is None:
        score = dict()
        if task_type in REGRESSION_TASKS:
            cprediction = sanitize_array(prediction)
            for metric_ in REGRESSION_METRICS:
                score[metric_] = regression_metrics.calculate_score(metric_,
                                                                    solution,
                                                                    cprediction)
        else:
            csolution, cprediction = normalize_array(solution, prediction)
            for metric_ in CLASSIFICATION_METRICS:
                score[metric_] = classification_metrics.calculate_score(
                    metric_, csolution, cprediction, task_type)

        for metric_ in score:
            if np.isnan(score[metric_]):
                score[metric_] = 0


    else:
        if task_type in REGRESSION_TASKS:
            cprediction = sanitize_array(prediction)
            score = regression_metrics.calculate_score(metric,
                                                       solution,
                                                       cprediction)
        else:
            csolution, cprediction = normalize_array(solution, prediction)
            score = classification_metrics.calculate_score(metric,
                                                           csolution,
                                                           cprediction,
                                                           task=task_type)
        if np.isnan(score):
            score = 0

    return score
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号