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



我对机器倾斜和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]

采用接收器工作特性 (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()

相关内容

  • 没有找到相关文章

最新更新