metrics.py 文件源码

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

项目:motion-classification 作者: matthiasplappert 项目源码 文件源码
def multilabel_classification_report(y_true, y_pred, fmt='.3f', target_names=None):
    y_true = check_multilabel_array(y_true)
    y_pred = check_multilabel_array(y_pred)
    if y_true.shape != y_pred.shape:
        raise ValueError('y_true and y_pred must have equal shapes')
    n_labels = y_true.shape[1]
    if target_names is not None and len(target_names) != n_labels:
        raise ValueError('target_names must specify a name for all %d labels' % n_labels)

    # Collect stats
    precision, recall, f1_score, support = precision_recall_fscore_support(y_true, y_pred)
    tp, fp, tn, fn = multilabel_tp_fp_tn_fn_scores(y_true, y_pred)
    accuracy = multilabel_accuracy(y_true, y_pred)

    # Generate data for table, where each row represents a label
    headers = ['', 'precision', 'recall', 'f1-score', 'accuracy', 'support', 'TP', 'TN', 'FP', 'FN']
    data = []
    for label_idx in range(n_labels):
        target_name = str(label_idx) if target_names is None else target_names[label_idx]
        row = [target_name, precision[label_idx], recall[label_idx], f1_score[label_idx], accuracy[label_idx],
               support[label_idx], tp[label_idx], tn[label_idx], fp[label_idx], fn[label_idx]]
        data.append(row)

    # Calculate summaries for all values
    summary = ['avg / total', np.average(precision), np.average(recall), np.average(f1_score), np.average(accuracy),
               np.sum(support), np.sum(tp), np.sum(tn), np.sum(fp), np.sum(fn)]
    data.append(summary)

    return tabulate(data, headers=headers, floatfmt=fmt)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号