#P4538.第2题-基于混淆矩阵,推导分类模型的核心评估指标

第2题-基于混淆矩阵,推导分类模型的核心评估指标 - problem_ide - CodeFun2000

python 复制代码
import sys
import numpy as np

def solve():
    pred = list(map(int,input().split()))
    trueY = list(map(int,input().split()))
    weights = list(map(float,input().split()))

    n = len(weights)
    m = len(pred)
    #print(n)
    #print(m)
    precision = [0 for _ in range(n)]
    recall = [0 for _ in range(n)]
    f1 = [0 for _ in range(n)]

    for i in range(n):
        TP = 0
        FP = 0
        FN = 0
        
        for j in range(m):
            if(trueY[j]==i or pred[j]==i):
                if(pred[j] == trueY[j]):
                    TP +=1
                elif(pred[j]==i and trueY[j]!=i):
                    FP+=1
                elif(pred[j]!=i and trueY[j]==i):
                    FN+=1
        
        if((TP+FP)!=0):
            precision[i] = TP/(TP+FP)
        else:
            precision[i] = 0
        
        if((TP+FN)!=0):
            recall[i] = TP/(TP+FN)
        else:
            recall[i] = 0
        

        if((precision[i]!=0) and (recall[i]!=0)):
            f1[i] = 2*precision[i]*recall[i]/(precision[i]+recall[i])
        else:
            f1[i] = 0
    
    precision = np.array(precision)
    recall = np.array(recall)
    f1 = np.array(f1)
    #print(precision)
    #print(recall)
    #print(f1)
    
    precision_avg = np.sum(precision*weights)
    recall_avg = np.sum(recall*weights)
    f1_avg = np.sum(f1*weights)

    print(f"{precision_avg:.2f} {recall_avg:.2f} {f1_avg:.2f}",end='')

if __name__ =='__main__':
    solve()
相关推荐
小白小宋11 小时前
【PUSCH第三期】5G NR QC-LDPC编码深度解析:从协议校验矩阵构造到MATLAB完整实现
5g·matlab·矩阵
啦啦啦_999921 小时前
1. 线性回归之 向量&矩阵
算法·矩阵·线性回归
star learning white1 天前
线性代数3
人工智能·线性代数·机器学习
爱吃巧克力的程序媛1 天前
计算机图形学---如何理解模型矩阵、视图矩阵、投影矩阵
数码相机·线性代数·矩阵
做cv的小昊1 天前
【TJU】研究生应用统计学课程笔记(5)——第二章 参数估计(2.3 C-R不等式)
c语言·笔记·线性代数·机器学习·数学建模·r语言·概率论
借雨醉东风1 天前
程序分享--常见算法/编程面试题:旋转矩阵
c++·线性代数·算法·面试·职场和发展·矩阵
菜鸟丁小真2 天前
LeetCode hot100 -73.矩阵置零
数据结构·leetcode·矩阵·知识点总结
做cv的小昊2 天前
【TJU】应用统计学——第七周作业(4.2 多元线性回归分析、4.3 可化为线性回归的曲线回归、4.4 单因子方差分析)
线性代数·算法·数学建模·矩阵·回归·线性回归·概率论
菜鸟丁小真2 天前
LeetCode hot100 -54.螺旋矩阵
算法·leetcode·矩阵·知识点总结