绘制 Roc 曲线的自动编码器分类

Posted

技术标签:

【中文标题】绘制 Roc 曲线的自动编码器分类【英文标题】:autoencoder classification plotting the Roc curve 【发布时间】:2019-07-06 15:47:21 【问题描述】:

我对机器学习和 python 还是很陌生。任何帮助将不胜感激。

首先很抱歉,这可能是一个重复的问题,或者对机器学习开发人员来说很容易,通常在 matlab 中很容易绘制 roc 曲线。 我想在我的代码上绘制它,但我不知道如何绘制它的分数..

# Describe the number of classes:
num_class = 2

from keras import backend as K


# Custom classifier function:
def classifier_func(x):
    return x-x+K.one_hot(K.argmax(x, axis=1), num_classes=num_class)

# Deep Learning Model:
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, Dense, Activation, Lambda, Flatten, concatenate, Reshape
from keras.models import Model

input_img = Input(shape=(64, 64, 3))

layer_1 = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
...   

layer_7 = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(layer_6)

autoencoder = Model(input_img, layer_7)
autoencoder.compile(optimizer='rmsprop', loss='mse')

autoencoder.summary()

# Creates live data:
# For better yield. The duration of the training is extended.

from keras.preprocessing.image import ImageDataGenerator
generated_data = ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, rotation_range=0,  width_shift_range=0.1, height_shift_range=0.1, horizontal_flip = True, vertical_flip = False)
generated_data.fit(X_train)
epochs = 2
batch_size = 5
autoencoder.fit_generator(generated_data.flow(X_train, X_train, batch_size=batch_size), steps_per_epoch=X_train.shape[0], epochs=epochs, validation_data=(X_test, X_test), callbacks=checkpoints)

# Training Model:

autoencoder.fit(X_train, X_train, batch_size=batch_size, epochs=epochs, validation_data=(X_test, X_test), shuffle=True, callbacks=checkpoints)

decoded_imgs = autoencoder.predict(X_test)

encode = encoder.predict(X_train)

class_dict = np.zeros((num_class, num_class))
for i, sample in enumerate(Y_train):
    class_dict[np.argmax(encode[i], axis=0)][np.argmax(sample)] += 1

print(class_dict)

neuron_class = np.zeros((num_class))
for i in range(num_class):
    neuron_class[i] = np.argmax(class_dict[i], axis=0)

print(neuron_class)

# Getting class as string:
def cat_dog(model_output):
    if model_output == 0:
        return "Cat"
    else:
        return "Dog"

encode = encoder.predict(X_test)

predicted = np.argmax(encode, axis=1)
for i, sample in enumerate(predicted):
    predicted[i] = neuron_class[predicted[i]]

comparison = predicted == np.argmax(Y_test, axis=1)
loss = 1 - np.sum(comparison.astype(int))/Y_test.shape[0]

【问题讨论】:

【参考方案1】:

取自Receiver Operating Characteristic (ROC)的一个简单例子。 当您只有两个类时,此示例有效。

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc

encode = np.array([[0.7, 0.3], [0.4, 0.6], [0.8, 0.2]])
Y_test = np.array([[1, 0], [0, 1], [1, 0]])

# the confidence score of positive class(assuming class 1 be positive class, and 0 be negative)
y_score = encode[np.arange(3), 1]
# true binary label
y_true = np.argmax(Y_test, axis=1)

fpr, tpr, thresholds = roc_curve(y_true, y_score)
roc_auc = auc(fpr, tpr)

plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
         lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()

【讨论】:

感谢您的回答,所以我的 y_score 和 y_true 应该是 y_score =predicted y_true = np.argmax(encode, axis=1)

以上是关于绘制 Roc 曲线的自动编码器分类的主要内容,如果未能解决你的问题,请参考以下文章

在单个 ROC 图上绘制线性判别分析、分类树和朴素贝叶斯曲线

测量系统 Roc 的性能

多分类问题的ROC曲线绘制

如何从分类树概率中绘制 ROC 曲线

构建手动 bagging 分类器后绘制 ROC 曲线

如何从具有 2 个输出神经元的 softmax 二元分类器绘制 ROC 曲线?