【问题标题】:Plot ROC curve for CNN model绘制 CNN 模型的 ROC 曲线
【发布时间】:2020-05-05 02:57:27
【问题描述】:

如何在 Python 中为多个患者的 CNN 模型绘制 ROC 曲线?

当我运行我的代码时,我得到了一个空图。我该如何解决这个错误?

    acc=0
    fp=0
    tp=0
    fn=0
    lastTenResult=list()
    for el in interPrediction:
        if(el[1]>0.5):
            acc=acc+1
            lastTenResult.append(1)
        else:
            lastTenResult.append(0)
        if(len(lastTenResult)>10):
            acc=acc-lastTenResult.pop(0)
        if(acc>=8):
          fp=fp+1
          lastTenResult=list()
          acc=0
    lastTenResult=list()
    for el in preictPrediction:
        if(el[1]>0.5):
            acc=acc+1
            lastTenResult.append(1)
        else:
            lastTenResult.append(0)
        if(len(lastTenResult)>10):
            acc=acc-lastTenResult.pop(0)
        if(acc>=8):
          tp=tp+1 
        else:
            if(len(lastTenResult)==10):
               fn=fn+1 
                sensitivity=tp/(tp+fn)
    FPR=fp/(secondsInterictalInTest/(60*60))
    TPR=tp/(tp + fn)
  result=result+str(i+1)+','+str(tp)+','+str(fp)+','+str(fn)+','+str(secondsInterictalInTest)+','
    result=result+str(sensitivity)+','+str(FPR)+'\n'
    print('True Positive, False Positive, False negative, Second of Inter in Test, Sensitivity, FPR')
print(str(tp)+','+str(fp)+','+str(fn)+','+str(secondsInterictalInTest)+','+str(sensitivity)+','+str(FPR))
with open(OutputPath, "a+") as myfile:
    myfile.write(result)
    x =FPR  # false_positive_rate
    y =TPR # true_positive_rate 
    # This is the ROC curve
    plt.plot(x,y)
    plt.show()

【问题讨论】:

标签: python machine-learning plot deep-learning roc


【解决方案1】:

首先,您介意验证x 和y 都是带有NA 的数值列表吗?如果是这样,您可以尝试启动这样的数字吗?

import matplotlib.pyplot as plt
plt.figure() #             <---------------------------
plt.plot(x,y, label="XXX")
plt.plot([0,1], [0,1], 'r--')
pltt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.show()

sklearn 中有一些功能可以让您非常轻松地做到这一点。

from sklearn.metrics import roc_aus_score
from sklearn.metrics import roc_curve

FPR, TPR, thresholds = roc_curve(Y)

希望它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-29
    • 2016-02-04
    • 2019-02-27
    • 2018-07-06
    • 2021-03-03
    • 2018-04-01
    • 2021-03-12
    • 2013-08-10
    相关资源
    最近更新 更多