plot Roc curve using keras

左心房为你撑大大i 提交于 2021-02-10 20:51:57

问题


I have a neural network model and I am using KerasClassifier and then using KFold for cross-validation. Now I am having issues in plotting the ROC curve. I have tried few codes but most of them is giving me an error of multi-labeled is not interpreted. I have the following code till my neural network produces the accuracy. I will be thankful if anyone can help me with the later part of the code.

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)

Here is the link to the dataset. https://drive.google.com/open?id=15emI90-sPZMkHLuwRbNfTBli0h_S-PpM


回答1:


For your case since your target is multiclass, you cannot use ROC for the evaluation of the classifier. In a case that there was a binary classifier this link shows how to draw an ROC curve.



来源:https://stackoverflow.com/questions/52101004/plot-roc-curve-using-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!