#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()
相关推荐
Pniubi6 小时前
力扣73 矩阵置零
算法·leetcode·矩阵
kingx31 天前
Bugku 逆向 MatrixHunter:黑盒 oracle + GF(2) 消元出唯一解
网络安全·矩阵·逆向·ctf
莱茶荼菜5 天前
【slam公式】
人工智能·线性代数·机器学习
吴名氏.5 天前
Java转AI Agent教程(第九周)错误矩阵
java·人工智能·矩阵·ai agent
伯奥克客服6 天前
2026年视频矩阵厂家技术评估:从EDID到控制协议开放度的六维筛选法
线性代数·矩阵·音视频
释厄6237 天前
三角基底律——矩阵3x6 是所有三角形的基底·3D中高就是边
线性代数·3d·矩阵
桑年8 天前
四、Pandas数据处理
人工智能·python·线性代数·pandas·数据可视化·数据处理
Kobebryant-Manba9 天前
IEKT向量矩阵流程&评估
人工智能·算法·矩阵
学习论之费曼学习法11 天前
无人值守AI内容矩阵实战(10):Windows 上让 Python 进程 7×24 活着
人工智能·windows·python·矩阵·powershell·进程守护
zhongben_keji11 天前
家政小程序开发全解:经营模式、品类矩阵、派单约束与信用核验
线性代数·矩阵·apache