ROC曲线看起来太锐利而不平滑

时间:2019-07-16 12:17:01

标签: python keras conv-neural-network roc auc

我正在学习使用在那儿找到的数据集之一在Kaggle的Keras上编写CNN。

我笔记本的链接是

https://www.kaggle.com/vj6978/brain-tumor-vimal?scriptVersionId=16814133

链接上提供了代码,数据集和ROC曲线。我正在绘制的ROC曲线看起来看起来很锐利而不平滑。

代码如下:

import os
import cv2
import random
import numpy as np
from numpy.lib.stride_tricks import as_strided

from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.metrics import auc

import matplotlib.pyplot as plt

import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, AveragePooling2D, Dropout, Flatten

from PIL import Image

data_set = []
data_label = []

training_data = []

input_path = "../input/brain_tumor_dataset"

CATEGORIES = ["no", "yes"]

"""
    The show function simply takes in a numpy array and displays it as an image.
"""

def show(img_input):
    plt.imshow(img_input)
    plt.show()

def create_training_data():

    for category in CATEGORIES:
        path = os.path.join(input_path, category)
        category_index = CATEGORIES.index(category)
        for image in os.listdir(path):
            try:
                img_array = cv2.imread(os.path.join(path, image), cv2.IMREAD_GRAYSCALE)

                img_array = img_array.astype(np.float32)
                img_array = cv2.resize(img_array, (128, 128))
                training_data.append([img_array, category_index])
            except Exception as e:
                print(e)

create_training_data()

random.shuffle(training_data)

for feature, label in training_data:
    data_set.append(feature)
    data_label.append(label)


x_train, x_test, y_train, y_test = train_test_split(data_set, data_label, test_size = 0.1, 
                                                    random_state = 45)

data_set = np.array(x_train).reshape(-1, 128, 128, 1)

x_test = np.array(x_test).reshape(-1, 128, 128, 1)

data_set = data_set/255.0

model = Sequential()
model.add(Conv2D(128, (3,3), input_shape = data_set.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Conv2D(128, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Flatten())
model.add(Dense(64))

model.add(Dense(1))
model.add(Activation("sigmoid"))

model.summary()

model.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ['accuracy'])

model.fit(data_set, y_train, batch_size = 32, epochs = 15, validation_split = 0.1)

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

y_pred_keras = model.predict(x_test).ravel()
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_test, y_test)

auc_keras = auc(fpr_keras, tpr_keras)

plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_keras, tpr_keras, label='Keras (area = {:.3f})'.format(auc_keras))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.show()


曲线看起来像这样

enter image description here

任何帮助将不胜感激。

感谢Vimal James

1 个答案:

答案 0 :(得分:0)

fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_test, y_test)这行是您的错误所在。我想你的意思是:

fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_test, y_pred_keras)