如何根据这些数据绘制ROC曲线?

时间:2019-01-06 00:31:59

标签: python tensorflow keras evaluation roc

我已经使用Keras训练了卷积神经网络(CNN),并执行以下操作以找到测试数据集的准确性:

for root, dirs, files in os.walk(test_directory):
    for file in files:
        img = cv2.imread(root + '/' + file)
        img = cv2.resize(img,(512,512),interpolation=cv2.INTER_AREA)
        img = np.expand_dims(img, axis=0)
        img = img/255.0
        if os.path.basename(root) == 'nevus':
            label = 1
        elif os.path.basename(root) == 'melanoma':
            label = 0
        img_class = model.predict_classes(img)
        prediction = img_class[0]
        if prediction == label:
            correct_classification = correct_classification + 1
        print 'This is the prediction: '
        print prediction
        number_of_test_images = number_of_test_images + 1

print 'correct results:'
print correct_classification

print 'number of test images'
print number_of_test_images

print 'Accuray:'
print number_of_test_images/correct_classification * 100

是否可以通过在测试数据集上测试模型来找到ROC曲线?

谢谢。

1 个答案:

答案 0 :(得分:0)

ROC曲线只是在不同的概率阈值下绘制TP(真正)与FP(假正)的曲线。因此,如果这是一个二进制分类问题,那么您只需更改测试数据集预测的概率阈值即可获得TP-FP率。本质上,创建一个具有三列的表:[Prob Threshold, TP, FP]并将其绘制出来。您需要使用model.predict_proba(...)来按类别获取概率,然后使用它来创建ROC曲线。

对于多类别,它会有些棘手。您虽然有一些选择。您可以绘制每个类别的ROC曲线(一个案例与多个案例),实质上是将主要类别与所有其他类别进行二值化。另外,对于多类别,您可以执行attempts的操作,并创建微平均ROC曲线或宏观平均ROC曲线。

相关问题