pytorch中计算准确率,召回率和F1值的方法
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要讲解了"pytorch中计算准确率,召回率和F1值的方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"pytorch中计算准确率,召回率和
千家信息网最后更新 2025年11月07日pytorch中计算准确率,召回率和F1值的方法
这篇文章主要讲解了"pytorch中计算准确率,召回率和F1值的方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"pytorch中计算准确率,召回率和F1值的方法"吧!
看代码吧~
predict = output.argmax(dim = 1)confusion_matrix =torch.zeros(2,2)for t, p in zip(predict.view(-1), target.view(-1)): confusion_matrix[t.long(), p.long()] += 1a_p =(confusion_matrix.diag() / confusion_matrix.sum(1))[0]b_p = (confusion_matrix.diag() / confusion_matrix.sum(1))[1]a_r =(confusion_matrix.diag() / confusion_matrix.sum(0))[0]b_r = (confusion_matrix.diag() / confusion_matrix.sum(0))[1]补充:pytorch 查全率 recall 查准率 precision F1调和平均 准确率 accuracy
看代码吧~
def eval(): net.eval() test_loss = 0 correct = 0 total = 0 classnum = 9 target_num = torch.zeros((1,classnum)) predict_num = torch.zeros((1,classnum)) acc_num = torch.zeros((1,classnum)) for batch_idx, (inputs, targets) in enumerate(testloader): if use_cuda: inputs, targets = inputs.cuda(), targets.cuda() inputs, targets = Variable(inputs, volatile=True), Variable(targets) outputs = net(inputs) loss = criterion(outputs, targets) # loss is variable , if add it(+=loss) directly, there will be a bigger ang bigger graph. test_loss += loss.data[0] _, predicted = torch.max(outputs.data, 1) total += targets.size(0) correct += predicted.eq(targets.data).cpu().sum() pre_mask = torch.zeros(outputs.size()).scatter_(1, predicted.cpu().view(-1, 1), 1.) predict_num += pre_mask.sum(0) tar_mask = torch.zeros(outputs.size()).scatter_(1, targets.data.cpu().view(-1, 1), 1.) target_num += tar_mask.sum(0) acc_mask = pre_mask*tar_mask acc_num += acc_mask.sum(0) recall = acc_num/target_num precision = acc_num/predict_num F1 = 2*recall*precision/(recall+precision) accuracy = acc_num.sum(1)/target_num.sum(1)#精度调整 recall = (recall.numpy()[0]*100).round(3) precision = (precision.numpy()[0]*100).round(3) F1 = (F1.numpy()[0]*100).round(3) accuracy = (accuracy.numpy()[0]*100).round(3)# 打印格式方便复制 print('recall'," ".join('%s' % id for id in recall)) print('precision'," ".join('%s' % id for id in precision)) print('F1'," ".join('%s' % id for id in F1)) print('accuracy',accuracy)补充:Python scikit-learn,分类模型的评估,精确率和召回率,classification_report
分类模型的评估标准一般最常见使用的是准确率(estimator.score()),即预测结果正确的百分比。
混淆矩阵:
准确率是相对所有分类结果;精确率、召回率、F1-score是相对于某一个分类的预测评估标准。
精确率(Precision):预测结果为正例样本中真实为正例的比例(查的准)( )
召回率(Recall):真实为正例的样本中预测结果为正例的比例(查的全)( )
分类的其他评估标准:F1-score,反映了模型的稳健型
demo.py(分类评估,精确率、召回率、F1-score,classification_report):
from sklearn.datasets import fetch_20newsgroupsfrom sklearn.model_selection import train_test_splitfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.metrics import classification_report # 加载数据集 从scikit-learn官网下载新闻数据集(共20个类别)news = fetch_20newsgroups(subset='all') # all表示下载训练集和测试集 # 进行数据分割 (划分训练集和测试集)x_train, x_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25) # 对数据集进行特征抽取 (进行特征提取,将新闻文档转化成特征词重要性的数字矩阵)tf = TfidfVectorizer() # tf-idf表示特征词的重要性# 以训练集数据统计特征词的重要性 (从训练集数据中提取特征词)x_train = tf.fit_transform(x_train) print(tf.get_feature_names()) # ["condensed", "condescend", ...] x_test = tf.transform(x_test) # 不需要重新fit()数据,直接按照训练集提取的特征词进行重要性统计。 # 进行朴素贝叶斯算法的预测mlt = MultinomialNB(alpha=1.0) # alpha表示拉普拉斯平滑系数,默认1print(x_train.toarray()) # toarray() 将稀疏矩阵以稠密矩阵的形式显示。'''[[ 0. 0. 0. ..., 0.04234873 0. 0. ] [ 0. 0. 0. ..., 0. 0. 0. ] ..., [ 0. 0.03934786 0. ..., 0. 0. 0. ]'''mlt.fit(x_train, y_train) # 填充训练集数据 # 预测类别y_predict = mlt.predict(x_test)print("预测的文章类别为:", y_predict) # [4 18 8 ..., 15 15 4] # 准确率print("准确率为:", mlt.score(x_test, y_test)) # 0.853565365025 print("每个类别的精确率和召回率:", classification_report(y_test, y_predict, target_names=news.target_names))''' precision recall f1-score support alt.atheism 0.86 0.66 0.75 207 comp.graphics 0.85 0.75 0.80 238 sport.baseball 0.96 0.94 0.95 253 ...,'''召回率的意义(应用场景):产品的不合格率(不想漏掉任何一个不合格的产品,查全);癌症预测(不想漏掉任何一个癌症患者)
感谢各位的阅读,以上就是"pytorch中计算准确率,召回率和F1值的方法"的内容了,经过本文的学习后,相信大家对pytorch中计算准确率,召回率和F1值的方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
准确率
数据
特征
分类
训练
精确
特征词
评估
方法
重要
矩阵
类别
结果
重要性
标准
模型
学习
产品
代码
内容
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器进系统出现ctrl
福州网络安全教育平台登录
服务器sf720中标
数据库架构是什么
pg数据库查看视图
出入境开展网络安全教育
为什么服务器没有收到订单通知
网络安全设计心得体会
白话网络技术
永康平台软件开发
浅析网络安全问题管理制度
网吧网络安全审核流程
仿制药汤姆森数据库
淘宝网络技术服务中心
七日杀服务器查询
数据库建立用户自定义类型
计算机网络技术需要考取的证书
在mfc中数据库怎么建立
应用分布服务器
数据库影响行数怎么获取
融资租赁 软件开发公司
计算机考试 数据库技术
数理统计数据库
如何自己搭建一个邮箱服务器
腾讯qq数据库表的设计
泰拉瑞亚多人mod 联机服务器地址
手机app的服务器配置
物联网数据库开源
服务器怎么造的
域服务器管理ip