AUC,准确率,敏感性,特异性计算

被刻印的时光 ゝ 提交于 2020-01-11 06:44:31
import numpy as np
from sklearn.metrics import confusion_matrix,accuracy_score,f1_score,roc_auc_score,recall_score,precision_score

def calculate_metric(gt, pred): 
    pred[pred>0.5]=1
    pred[pred<1]=0
    confusion = confusion_matrix(gt,pred)
    TP = confusion[1, 1]
    TN = confusion[0, 0]
    FP = confusion[0, 1]
    FN = confusion[1, 0]
    print('Accuracy:',(TP+TN)/float(TP+TN+FP+FN))
    print('Sensitivity:',TP / float(TP+FN))
    print('Specificity:',TN / float(TN+FP))  

path = "OS3_val_result.csv"
with open(path, 'r') as f:
    file_list = f.read()
    file_list = file_list.split('\n')[1:-1]
    file_list = [file.split(',') for file in file_list]                     

y = np.array([float(y[1]) for y in file_list])
preds2 = np.array([float(preds[2]) for preds in file_list])
preds3 = np.array([float(preds[3]) for preds in file_list])
preds4 = np.array([float(preds[4]) for preds in file_list])

auc = roc_auc_score(y,preds2)
print('AUC:',auc)
calculate_metric(y,preds2)

auc = roc_auc_score(y,preds3)
print('AUC:',auc)
calculate_metric(y,preds3)

auc = roc_auc_score(y,preds4)
print('AUC:',auc)
calculate_metric(y,preds4)

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!