一、打印分类报告(使用scikit-learn库中的函数)

from sklearn.metrics import classification_report# y_test为测试集真实标签, y_pred为测试集预测的标签
print(classification_report(y_test, y_pred))

例子:

# Standard scientific Python imports
import matplotlib.pyplot as plt# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
from sklearn.model_selection import train_test_split# The digit dataset
digits = datasets.load_digits()"""The data that we are interested in is made of 8x8 images of digits, Let'shave a look at the first 4 images, stored in the 'images' attributes of the dataset. If we were working from image files, we could load them using'matplotlib.pyplot.imread'. Note that each image must have the same size. For these images, we know which digit they represent: it is given in the 'target' of the dataset.
"""_, axes = plt.subplots(2, 4)
images_and_labels = list(zip(digits.images, digits.target))
for ax, (image, label) in zip(axes[0, :], images_and_labels[:4]):ax.set_axis_off()ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')ax.set_title('Training: %d' % label)# To apply a classifier on this data, we need to flatten the image,
# to turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))classifier = svm.SVC(gamma=0.001)# Split data into train and test subsets
X_train, X_test, y_train, y_test = train_test_split(data, digits.target, test_size=0.5, shuffle=False)# We learn the digits on the first half of the digits
classifier.fit(X_train, y_train)# Now predict the value of the digit on the second half:
predicted = classifier.predict(X_test)images_and_predictions = list(zip(digits.images[n_samples // 2:], predicted))
for ax, (image, prediction) in zip(axes[1, ], images_and_predictions[:4]):ax.set_axis_off()ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')ax.set_title('Prediction: %i' % prediction)print('Classification report for classifier %s:\n%s\n' % (classifier, metrics.classification_report(y_test, predicted)))
cm = confusion_matrix(y_test, predicted, labels=digits.target_names)
print(cm)
disp = ConfusionMatrixDisplay(cm, display_labels=digits.target_names)
disp.plot()
plt.show()

output:

Classification report for classifier SVC(gamma=0.001):precision    recall  f1-score   support0       1.00      0.99      0.99        881       0.99      0.97      0.98        912       0.99      0.99      0.99        863       0.98      0.87      0.92        914       0.99      0.96      0.97        925       0.95      0.97      0.96        916       0.99      0.99      0.99        917       0.96      0.99      0.97        898       0.94      1.00      0.97        889       0.93      0.98      0.95        92accuracy                           0.97       899macro avg       0.97      0.97      0.97       899
weighted avg       0.97      0.97      0.97       899

参考链接:
https://scikit-learn.org.cn/view/45.html


二、求准确率,精确率,召回率、F1值等。

from sklearn.metrics import accuracy_score, recall_score, f1_score, precision_score
y_test = [0, 3, 4, 12, 5, 5, 4, 1, 2]
y_pred = [0, 1, 2, 2, 3, 3, 3, 2, 2]print('Accuracy score:', accuracy_score(y_test, y_pred))
print('Recall:', recall_score(y_test, y_pred, average='weighted'))
print('F1-score:', f1_score(y_test, y_pred, average='weighted'))
print('Precision score:', precision_score(y_test, y_pred, average='weighted'))

out:

Accuracy score:0.2222222222222222
Recall: 0.2222222222222222
F1-score: 0.15555555555555556
Precision score: 0.1388888888888889

也可以用precision_recall_fscore_support(y_true, y_pred, average='weighted'),跟上面是一样的效果。
参考链接:

  1. https://scikit-learn.org/stable/modules/model_evaluation.html#
  2. https://github.com/wmn7/Traffic-Classification/blob/master/TrafficFlowClassification/utils/evaluate_tools.py

scikit-learn:打印分类报告,求准确率、精确率、召回率、F1值等指标相关推荐

  1. 分类、推荐系统评测指标—准确率(Precision)、召回率(Recall)、F值(F-Measure) /(F-score)

    分类.推荐系统评测指标-准确率(Precision).召回率(Recall).F值(F-Measure) /(F-score) 1.准确率与召回率(Precision & Recall) 准确 ...

  2. 【机器学习入门】(13) 实战:心脏病预测,补充: ROC曲线、精确率--召回率曲线,附python完整代码和数据集

    各位同学好,经过前几章python机器学习的探索,想必大家对各种预测方法也有了一定的认识.今天我们来进行一次实战,心脏病病例预测,本文对一些基础方法就不进行详细解释,有疑问的同学可以看我前几篇机器学习 ...

  3. 精度,精确率,召回率_了解并记住精度和召回率

    精度,精确率,召回率 Hello folks, greetings. So, maybe you are thinking what's so hard in precision and recall ...

  4. 准确率 召回率_机器学习中F值(F-Measure)、准确率(Precision)、召回率(Recall)

    在机器学习.数据挖掘.推荐系统完成建模之后,需要对模型的效果做评价. 业内目前常常采用的评价指标有准确率(Precision).召回率(Recall).F值(F-Measure)等,下图是不同机器学习 ...

  5. 机器学习算法中的准确率(Precision)、召回率(Recall)、F值(F-Measure)

    转载自:https://www.cnblogs.com/Zhi-Z/p/8728168.html 摘要: 数据挖掘.机器学习和推荐系统中的评测指标-准确率(Precision).召回率(Recall) ...

  6. 准确率(Precision)、召回率(Recall)以及F值(F-Measure)

    在信息检索.分类体系中,有一系列的指标,搞清楚这些指标对于评价检索和分类性能非常重要,因此最近根据网友的博客做了一个汇总. 准确率.召回率.F1 信息检索.分类.识别.翻译等领域两个最基本指标是召回率 ...

  7. 机器学习:准确率(Precision)、召回率(Recall)、F值(F-Measure)、ROC曲线、PR曲线

    增注:虽然当时看这篇文章的时候感觉很不错,但是还是写在前面,想要了解关于机器学习度量的几个尺度,建议大家直接看周志华老师的西瓜书的第2章:模型评估与选择,写的是真的很好!! 以下第一部分内容转载自:机 ...

  8. 推荐系统评测指标—准确率(Precision)、召回率(Recall)、F值(F-Measure)

     下面简单列举几种常用的推荐系统评测指标: 1.准确率与召回率(Precision & Recall) 准确率和召回率是广泛用于信息检索和统计学分类领域的两个度量值,用来评价结果的质量.其 ...

  9. 自然语言处理:分词评测指标——准确率(Precision)、召回率(Recall)、F值(F-Measure)

    下面简单列举几种常用的推荐系统评测指标: 1.准确率与召回率(Precision & Recall) 准确率和召回率是广泛用于信息检索和统计学分类领域的两个度量值,用来评价结果的质量.其中精度 ...

最新文章

  1. ZOJ 3879(大模拟)
  2. python selenium 获取同一元素的多个属性_python+selenium如何获取元素中并列的属性值?...
  3. 我的MVVM框架 v0.1发布
  4. hadoop中mapreduce参数优化
  5. editplus3编辑器颜色修改
  6. vue中使用echarts
  7. C++ 正则表达式教程:C++ 中的正则表达式与示例
  8. Photoshop提高照片对比度的几种实用方法
  9. java word 添加图片_java – 在word文档中插入图片
  10. 惊鸿一现的永恒经典2007-05-07 09:40:18
  11. 率土之滨鸿蒙团,【率土之滨】无需“垒实”也能鏖战全场!群吕布混编弓解析...
  12. 各类识别、深度学习-开源代码文献梳理
  13. 大整数运算之 大整数加法、减法、乘法
  14. 11、Nepxion Discovery 之全链路界面操作蓝绿灰度发布
  15. pom 文件的project标签报错Failed to read artifact descriptor for xxx:jar
  16. 51单片机初学1-51单片机介绍
  17. IntelliJ IDEA像Eclipse一样打开工作空间,管理多个项目
  18. C++高级搜索算法迭代加深—————骑士精神
  19. C语言最-佳存款方案程序(代码原创)
  20. MATLAB代码:考虑阶梯式碳交易机制与电制氢的综合能源系统热电优化

热门文章

  1. 程序员需要常用到的几大工具,省事高效
  2. Spring Boot问题之JSP无法显示Could not resolve view with name ‘xxxx‘ in servlet with name ‘dispatcherServlet
  3. vm安装xp系统提示system not found,一直网卡形式启动
  4. 飞鱼星VE982W试用
  5. Revit二次开发之使用外部程序集DT_Mgd读取CAD图遇到的问题
  6. html5相册瀑布背景,HTML5使用canvas实现代码流瀑布的方法
  7. CFD Post中创建圆柱面
  8. 病毒性感冒与细菌性感冒
  9. AX3600+openwrt 旁路由设置
  10. Spring Boot 多模块开发与排坑指南