二进制分类器Keras回调是否具有敏感性和特异性?

时间:2018-07-12 06:50:59

标签: python numpy tensorflow scikit-learn keras

class_mode='binary'时,如何获得敏感性和特异性? -我当前的解决方案适用于class_mode='categorical'

from keras.callbacks import Callback
import numpy as np
from sklearn.metrics import confusion_matrix


class SensitivitySpecificityCallback(Callback):
    def on_epoch_end(self, epoch, logs=None):
        if epoch:
            x_test, y_test = self.validation_data[0], self.validation_data[1]
            predictions = self.model.predict(x_test)
            output_sensitivity_specificity(epoch, predictions, y_test)


def output_sensitivity_specificity(epoch, predictions, y_test):
    y_test = np.argmax(y_test, axis=-1)
    predictions = np.argmax(predictions, axis=-1)
    c = confusion_matrix(y_test, predictions)
    print('Confusion matrix:\n', c)
    print('[{:03d}] sensitivity'.format(epoch), c[0, 0] / (c[0, 1] + c[0, 0]))
    print('[{:03d}] specificity'.format(epoch), c[1, 1] / (c[1, 1] + c[1, 0]))

82 source lines full code example(兼容Python 2和3)

所有输出错误:

Confusion matrix:
 [[40]]
Traceback (most recent call last):
  File "network.py", line 118, in <module>
    callbacks=[SensitivitySpecificityCallback()], verbose=1)
  File "lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "lib/python2.7/site-packages/keras/engine/training.py", line 1426, in fit_generator
    initial_epoch=initial_epoch)
  File "lib/python2.7/site-packages/keras/engine/training_generator.py", line 229, in fit_generator
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "lib/python2.7/site-packages/keras/callbacks.py", line 77, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "network.py", line 56, in on_epoch_end
    output_sensitivity_specificity(epoch, predictions, y_test)
  File "network.py", line 64, in output_sensitivity_specificity
    print('[{:03d}] sensitivity'.format(epoch), c[0, 0] / (c[0, 1] + c[0, 0]))
IndexError: index 1 is out of bounds for axis 1 with size 1

1 个答案:

答案 0 :(得分:1)

由于在二进制模式下,您实际上是在预测一个值,该值指示肯定的类别(即二进制分类)的概率,对预测使用.argmax()总是返回0。因此,在这种情况下,您需要修改output_sensitivity_specificity函数:

def output_sensitivity_specificity(epoch, predictions, y_test, mode='binary'):
    if mode == 'binary':
        # determine positive class predictions
        idx = predictions >= 0.5
        predictions = np.zeros(predictions.shape)
        predictions[idx] = 1
        # no need to modify y_test since it consists of zeros and ones already
    else:
        y_test = np.argmax(y_test, axis=-1)
        predictions = np.argmax(predictions, axis=-1)

    c = confusion_matrix(y_test, predictions)
    print('Confusion matrix:\n', c)
    print('[{:03d}] sensitivity'.format(epoch), c[0, 0] / (c[0, 1] + c[0, 0]))
    print('[{:03d}] specificity'.format(epoch), c[1, 1] / (c[1, 1] + c[1, 0]))

在回调中调用mode=class_mode时只需传递output_sensitivity_specificity即可,它对二进制和分类模式均有效。