【问题标题】:Confusion Matrix only considering predictions > threshold仅考虑预测>阈值的混淆矩阵
【发布时间】:2020-09-17 23:06:05
【问题描述】:

我的网络的输出层是

model.add(Dense(2, activation=activations.softmax))

输出一个热编码类别预测。

model.predict 因此返回 n 个预测,如

[9.9584144e-01, 4.1585001e-03],
[7.5779420e-01, 2.4220583e-01],
...

我现在不仅想要通过 sklearn 提供的完整混淆矩阵

metrics.confusion_matrix(y_TEST.argmax(axis=1), y_pred.argmax(axis=1), normalize='pred')

但我想看看混淆矩阵仅适用于最大预测大于给定阈值的情况。

有点像

metrics.confusion_matrix(y_TEST.argmax(axis=1), y_pred.argmax(axis=1), normalize='pred', 
                         min_confidence_threshold='0.9')

sklearn 或此处的任何标准工具是否提供类似的功能?

如果不是,我将如何根据其中一个数组的条件过滤两个数组(y_TEST,y_pred)?

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    这是一个可以做到这一点的函数:

    def threshold_matrix(labels,probs,threshold=0):
        assert 0 <= threshold <= 1
        t_map = lambda x : 1 if tf.math.reduce_max(x) > threshold else 0
        thres = tf.map_fn(t_map, probs)
    
        pred  = tf.boolean_mask(tf.argmax(probs,1),thres)
        labs  = tf.boolean_mask(tf.cast(labels, tf.int64),thres)
    
        return tf.math.confusion_matrix(labs,pred)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-04
      • 2020-07-31
      • 2020-07-16
      • 2019-05-29
      • 2022-06-15
      • 2015-12-14
      • 2014-10-14
      • 1970-01-01
      相关资源
      最近更新 更多