使用keras绘制Roc曲线

时间:2018-08-30 15:48:28

标签: neural-network keras roc auc

我有一个神经网络模型,我正在使用KerasClassifier,然后使用KFold进行交叉验证。现在我在绘制ROC曲线时遇到问题。我尝试了很少的代码,但大多数代码都给了我一个多标签未解释的错误。在神经网络产生精度之前,我拥有以下代码。如果有人可以在代码的后半部分为我提供帮助,我将不胜感激。

import numpy as np
import pandas as pd
from keras.layers import Dense, Input
from keras.models import Model, Sequential
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score, KFold
from sklearn.preprocessing import LabelEncoder, MinMaxScaler,StandardScaler
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
seed = 7
np.random.seed(seed)
dataset = pd.read_csv('lukemia_2003.csv')
X_train = dataset.values[:,0:12600]
Y_train = dataset.values[:,12600]
scalar = MinMaxScaler()
scaled_data = scalar.fit_transform(X_train)
pca = PCA(n_components=10)
X_train_pca = pca.fit_transform(scaled_data)
encoder = LabelEncoder()
encoder.fit(Y_train)
encoded_Y = encoder.transform(Y_train)
dummy_Y = np_utils.to_categorical(encoded_Y)
hid_layer1 = 4
hid_layer2 = 4
output_layer = 4
def my_model():
    encoded = Sequential()
    encoded.add(Dense(hid_layer1, input_dim = 10, activation='tanh'))
    encoded.add(Dense(hid_layer2, activation='tanh'))
    encoded.add(Dense(output_layer, activation='softmax'))
    encoded.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return encoded

result_mean_list = []
std_list = []
for i in range(30):
    estimator = KerasClassifier(build_fn=my_model, epochs=1500, batch_size=5, verbose=2)
    kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
    results = cross_val_score(estimator, X_train_pca, dummy_Y, cv=kfold)
    result_mean_list.append(round(results.mean()*100,2))
    std_list.append(round(results.std()*100,2))


print ("Result mean: ", result_mean_list)
print ("Standard Deviation List: ", std_list)

这是数据集的链接。 https://drive.google.com/open?id=15emI90-sPZMkHLuwRbNfTBli0h_S-PpM

1 个答案:

答案 0 :(得分:0)

对于您的情况,由于目标是多类的,因此不能使用ROC进行分类器评估。在存在二进制分类器的情况下,此link显示了如何绘制ROC曲线。