多分类的 准确率 召回率代码

from sklearn.metrics import classification_report,confusion_matrix

# 准确率 召回率 F1 每个类的数据量
precision_recall_report = classification_report(
              y_true=all_groundtruth_list,
              y_pred=all_predict_list,
              labels=list(range(0,len(all_label_list))),
              target_names=all_label_list)
print(precision_recall_report)

# 混淆矩阵
matrix = confusion_matrix(
         y_true=all_groundtruth_list,
         y_pred=all_predict_list,
         labels=list(range(0,len(all_label_list))))  
print(matrix)

输出多分类中每一类的准确率(该类正确的数/该类总数),也可以直接调用包

from sklearn.metrics import confusion_matrix, f1_score, classification_report
其中多分类的pre和该类的准确率等价?
def getACC(Y_test,Y_pred,n):
    acc =[]
    con_mat = confusion_matrix(Y_test,Y_pred)
    for i in range(n):
        number = np.sum(con_mat[:,:])
        tp = con_mat[i][i]
        fn = np.sum(con_mat[i,:])- tp
        fp = np.sum(con_mat[:,i])- tp
        tn = number - tp - fn - fp
        acc1 =(tp+tn)/(number)
        acc.append(acc1)
    return acc