计算测试集precision、recall、f1-score

问题描述

模型训练完成后,调用已保存的模型,读取训练数据集数据进行预测,并计算训练集的precision、recall、f1-score

实现代码

from keras.utils import image_utils
import numpy as np
import os
import cv2
import tensorflow as tf
from keras.utils.image_utils import img_to_array
from sklearn.metrics import recall_score, f1_score, precision_score, accuracy_score
from sklearn.metrics import classification_report
import tensorflow._api.v2.compat.v1 as tf
tf.disable_v2_behavior()
tf.compat.v1.enable_eager_execution()
model = tf.keras.models.load_model('model.h5')
path="D:/test/Data/test"
Image_width=150
Image_height=150
Image_channels=3
Image_size=(Image_width,Image_height)
Image_shape=(Image_width,Image_height,Image_channels)

#读取真实标签(此处标签有两类,用labels1、labels2表示)
def prepare(path):
    fileList = os.listdir(path)
    data=[]
    labels1=[]
    labels2=[]
    for fileName in fileList:
        image=cv2.imread(os.path.join(path,fileName))
        if image is not None:
            std_image=tf.image.per_image_standardization(image)
            image2=cv2.resize(std_image.numpy(),Image_size)
            image2=img_to_array(image2)
            data.append(image2)
            label1=str(fileName).split("_")[0:1]
            label2=str(fileName).split("_")[1:2]
            labels1.extend(label1)
            labels2.extend(label2)
    data=np.array(data,dtype="float32")
    return data,labels1,labels2

data,labels1,labels2=prepare(path)
labels1=list(map(int, labels1)) #真实类别10123)
labels2=list(map(int, labels2)) #真实类别2  
# print(labels1)
# print(labels2)


#预测
fileList = os.listdir(path)
pre_labels1=[]
pre_labels2=[]
for fileName in fileList:
    image = cv2.imread(os.path.join(path, fileName))
    if image is not None:
        img = image_utils.load_img(os.path.join(path, fileName), target_size=(150, 150))
        img_tensor = image_utils.img_to_array(img)
        img_tensor=np.expand_dims(img_tensor,axis=0)
        img_tensor/=255.
        #模型预测
        prediction=model.predict(img_tensor)
        # print('每一类别的概率值:',prediction)
        pre_y1 = np.argmax(prediction[0])
        pre_y2 = np.argmax(prediction[1])
        # print("预测类别是:",pre_y1,pre_y2)
        pre_labels1.extend([pre_y1])
        pre_labels2.extend([pre_y2])

pre_labels1=list(map(int, pre_labels1)) #预测类别1
pre_labels2=list(map(int, pre_labels2)) #预测类别2
# print(pre_labels1)
# print(pre_labels2)


#计算accurary,recall,f1score
y_true1 = labels1
y_pred1 = pre_labels1
measure_result1 = classification_report(y_true1, y_pred1)
print('measure_result1 = \n', measure_result1)

print("class1_accuracy:%.2f" % accuracy_score(y_true1, y_pred1))
print("class1_precision:%.2f" % precision_score(y_true1, y_pred1, labels=[0, 1, 2, 3], average='macro'))
print("class1_recall:%.2f" % recall_score(y_true1, y_pred1, labels=[0, 1, 2, 3], average='macro'))
print("class1_f1-score:%.2f" % f1_score(y_true1, y_pred1, labels=[0, 1, 2, 3], average='macro'))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: precisionrecallf1-score和support是机器学习常用的评估指标。 其precision(精确率)指的是模型预测为正例的样本,真正为正例的比例;recall(召回率)指的是真正为正例的样本,被模型预测为正例的比例;f1-scoreF1值)是precisionrecall的调和平均数,用于综合评估模型的性能;support(支持数)指的是每个类别在数据集出现的次数。 在分类问题precisionrecallf1-score都是用来评估模型的分类准确性的指标,而support则是用来衡量每个类别的样本数量。 ### 回答2: precisionrecallf1-score和support是机器学习对分类模型性能评价的重要指标。 Precision(精确率)是指预测为正样本的样本有多少是真正的正样本。它的计算公式为:Precision = 真正的正样本数 / 预测为正样本的样本数。Precision越高,说明模型预测的正样本越准确。 Recall(召回率)是指所有真正的正样本,模型预测出了多少个正样本。它的计算公式为:Recall = 真正的正样本数 / 所有真正的正样本数。Recall越高,说明模型能够识别到更多的正样本。 F1-scoreF1指标)是精确率和召回率的调和平均值。它的计算公式为:F1-score = 2 * Precision * Recall / (Precision + Recall)。F1-score综合考虑了模型的精确率和召回率,是一个更全面的评价模型分类性能的指标。 Support是指每个类别在测试集出现的频率(数量)。这个指标主要是为了在多分类任务,评价每个类别的影响力大小。 在实际应用,需要综合考虑PrecisionRecallF1-score来评价一个分类模型的准确性和召回能力。比如,在医学领域,如果一个肿瘤预测模型的Recall很高,说明模型能够预测出更多的真实患者,但如果Precision很低,就会出现很多误诊的情况。此时,我们可以将F1-score作为综合评价指标,考虑模型的精确率和召回率的平衡。 ### 回答3: 这四个指标是评估分类模型性能的重要指标,通常会和混淆矩阵一起使用来评价模型的表现。 Precision(精确率):是指模型在预测为正例有多少是真正的正例,也可以说是真正例占全部预测为正例的比例。该指标越高,表示模型判断为正例的数据越有可能是真正的正例。 Recall(召回率):是指模型在所有真正实际为正例的样本,能够被模型正确预测为正例的比例。该指标越高,表示模型能够更好地找到真正的正例。 F1-score:是指精确率和召回率的综合指标,是两者的调和平均数。该指标可以更全面地反映模型的准确率和遗漏率,适用于数据不平衡的情况。 Support(支持度):是指数据集属于某个类别的样本数量,与其他指标不同的是,该指标没有考虑模型的预测结果,只是对数据集的分布做出了描述。 在实际应用,选择哪个指标作为评价标准取决于具体任务的需求以及数据分布的特点。例如,在银行反欺诈领域,由于正例较少,需要更关注召回率以避免错过异常交易,而将精确率作为优化目标可能会导致将正常交易误判。因此,在不同场景下需要合理选择评价指标,并综合考虑多个指标综合评估模型的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aaaq_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值