首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >始终获得AUC = 0.5

始终获得AUC = 0.5
EN

Stack Overflow用户
提问于 2019-01-06 12:45:28
回答 1查看 1.6K关注 0票数 1

我正在尝试使用卷积神经网络(CNN)来预测测试图像的类别,如下所示:

代码语言:javascript
运行
复制
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
        labels.append(label)
        img_class = model.predict_classes(img)
        img_class_probability = model.predict(img)
        prediction_probability = img_class_probability[0]
        prediction_probabilities.append(prediction_probability)
        prediction = img_class[0]
        if prediction == label:
            correct_classification = correct_classification + 1
        number_of_test_images = number_of_test_images + 1

fpr, tpr, thresholds = roc_curve(labels, prediction_probabilities)
auc_value = auc(fpr, tpr)

我想问一下为什么我的AUC总是= 0.5?也许我做错了什么?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-06-11 16:08:10

在这种情况下,可能是AUC的阈值设置不正确,这导致所有模型输出值(得分/概率)在所有阈值选择下始终是同一类别,因此AUC将始终为0.5

以下示例说明了阈值对AUC的影响

代码语言:javascript
运行
复制
import tensorflow as tf # tf 2.0+

y_true = [0, 0, 1, 1, 1, 1]
y_pred = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006]

#--------------------------------------------------------------
m1 = tf.keras.metrics.AUC(thresholds=[0.0, 0.5, 1.0])
m1.update_state(y_true, y_pred)

# when threshold=0.0 , y_class always be 1
# when threshold=0.5 , y_class always be 0
# when threshold=1.0 , y_class always be 0 

print('AUC={}'.format(m1.result().numpy())) 
# output: AUC=0.5


#--------------------------------------------------------------
m2 = tf.keras.metrics.AUC(thresholds=[0.0, 0.0045, 1.0])
m2.update_state(y_true, y_pred)

# when threshold=0.0    , y_class always be 1 
# when threshold=0.0045 , y_class will   be [0, 0, 0, 0, 1, 1]
# when threshold=1.0    , y_class always be 0 

print('AUC={}'.format(m2.result().numpy())) 
# output: AUC=0.75


#--------------------------------------------------------------
m3 = tf.keras.metrics.AUC(num_thresholds=300) 
# print(m3.thresholds)
m3.update_state(y_true, y_pred)

print('AUC={}'.format(m3.result().numpy())) 
# output: AUC=0.875
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54058703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档