混淆矩阵是一个矩阵,类别个数可以有多个,a[i][j]表示将类别i的样本误判为类别j的个数。

classification_report用来分析不同类别的准确率,召回率,F1值等,从而便于按照类别查看准确率、召回率。

总体的正确率跟classification_report中的正确率是不一样。


import numpy as np
import sklearn.metrics as metrics def report(mine, real):
if len(mine) != len(real):
print("mine和real长度不一样")
exit(0)
all_classes = set(list(mine) + list(real))
precision = dict()
recall = dict()
f1 = dict()
support = dict()
for c in all_classes:
if np.count_nonzero(mine == c):
precision[c] = np.count_nonzero(np.logical_and(mine == real, real == c)) / np.count_nonzero(mine == c)
else:
precision[c] = 0
if np.count_nonzero(real == c):
recall[c] = np.count_nonzero(np.logical_and(mine == real, real == c)) / np.count_nonzero(real == c)
else:
recall[c] = 0
if precision[c] and recall[c]:
f1[c] = 2 / (1 / precision[c] + 1 / recall[c])
else:
f1[c] = 0
support[c] = np.count_nonzero(real_ans == c)
s = ''
s += "%10s%10s%10s%10s%10s\n" % ("class", "precision", "recall", "f1", "support")
fmtstr2 = "%10s%10.2f%10.2f%10.2f%10d\n"
for c in all_classes:
s += (fmtstr2 % (c, precision[c], recall[c], f1[c], support[c]))
s += fmtstr2 % ("avg",
np.sum([precision[c] * support[c] for c in all_classes]) / len(mine),
np.sum([recall[c] * support[c] for c in all_classes]) / len(mine),
np.sum([f1[c] * support[c] for c in all_classes]) / len(mine),
len(mine)
)
return s my_ans = np.random.randint(0, 2, 10)
real_ans = np.random.randint(0, 2, 10)
print(my_ans)
print(real_ans)
print("分类报告是按照类别分开的")
print('=' * 10)
print(metrics.classification_report(real_ans, my_ans))
print('=' * 10)
print(report(my_ans, real_ans))
print("准确率跟上面的正确率不一样")
print(metrics.accuracy_score(real_ans, my_ans))
print(np.count_nonzero(my_ans == real_ans) / len(my_ans))

理解metrics.classification_report的更多相关文章

  1. 机器学习笔记,使用metrics.classification_report显示精确率,召回率,f1指数

    sklearn中的classification_report函数用于显示主要分类指标的文本报告.在报告中显示每个类的精确度,召回率,F1值等信息. 主要参数: y_true:1维数组,或标签指示器数组 ...

  2. 量化预测质量之分类报告 sklearn.metrics.classification_report

    classification_report的调用为:classification_report(y_true, y_pred, labels=None, target_names=None, samp ...

  3. scikit-learn - 分类模型的评估 (classification_report)

    使用说明 参数 sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample ...

  4. np2016课程总结

    林牧 SA16222166 课程目标 课程安排 A1a A2 A3 项目集成 环境搭建 其他方面的收获 本课心得 课程目标 通过实现一个医学辅助诊断的专家系统原型,具体为实现对血常规检测报告OCR识别 ...

  5. scikit_learn入门

    原文:http://www.cnblogs.com/taceywong/p/4568806.html 原文地址:http://scikit-learn.org/stable/tutorial/basi ...

  6. Kaggle新手入门之路

    学完了Coursera上Andrew Ng的Machine Learning后,迫不及待地想去参加一场Kaggle的比赛,却发现从理论到实践的转变实在是太困难了,在此记录学习过程. 一:安装Anaco ...

  7. Scikit-learn:模型评估Model evaluation

    http://blog.csdn.net/pipisorry/article/details/52250760 模型评估Model evaluation: quantifying the qualit ...

  8. Examples of Scikit-learn Usages

    Examples of Scikit-learn Usages KFold K-折交叉验证 >>> import numpy as np >>> from skle ...

  9. Python机器学习实践与Kaggle实战(转)

    https://mlnote.wordpress.com/2015/12/16/python%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%AE%9E%E8%B7%B5 ...

随机推荐

  1. 【Javascript设计模式1】-单例模式

    <parctical common lisp>的作者曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通用的解决方案. 不管是弱类型 ...

  2. GPS精度因子(GDOP,PDOP,HDOP,VDOP,TDOP)

    PDOP:位置精度因子(Position Dilution of Precision),直译为“精度强弱度”,通常翻译为“相对误差”.具体含义是:由于观测成果的好坏与被测量的人造卫星和接收仪间的几何形 ...

  3. php5.6 的interactive模式

    1. 发现运行php 的interactive shell 的时候,不能输入一行执行一行,而要 输入完一整段内容,再按  ctrl + d才能执行这段内容. 原因是,没安装 readline这个模块, ...

  4. ubuntu16.04与mysql的运维注意事项

    1:环境 ubuntu16.04 虚拟机,需要搭建一个MySQL的生产或者测试环境 2:操作步骤 2.1:更新系统源 首次给root用户指定密码  ,先用安装用户登录 sudo  apt-get up ...

  5. 以sb7code为基础创建一个基本的OpenGL项目

      以sb7code为基础创建一个基本的OpenGL项目   从github上面下载sb7code代码: https://github.com/openglsuperbible/sb7code 打开H ...

  6. [React] Validate React Forms with Formik and Yup

    Validating forms in React can take several lines of code to build. However, Formik's ErrorMessage co ...

  7. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  8. ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法

    一.ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法 二.找到ux目录下的TabCloseMenu.js文件,将内容替换成下面代码. 三.代码: /** * Plugin f ...

  9. [Practical.Vim(2012.9)].Drew.Neil.Tip21学习摘要

    Vim has three kinds of Visual mode. In character-wise Visual mode, we can select anything from a sin ...

  10. css改变hr颜色

    html中用css改变颜色,<hr style="border:0;height:1px;">如果不加border:0;的话,虽然颜色改变了,但是会显示一条黑色的边框. ...