Confusion_matrix 根据两个指标

Confusion_matrix according two indicators

y_true表示正确的目标值;

Y_pred表示分类器返回的概率值估计目标

请根据这两个指标计算混淆矩阵

y_true = [True,False,False,True]

y_pred = [0.15,0.97,0.24,0.88]

def func(y_true,y_pred,thresh):

我还没有解决方案,有人有想法吗?

您可以使用 sklearn.metrics 中的 confusion_matrix。 您所要做的就是将 y_true 和 y_pred 转换为二进制值。

from sklearn.metrics import confusion_matrix
def conf_m(y_true, y_pred, thresh = 0.5):
    y_true = [int(i) for i in y_true]
    y_pred = [1 if x>=thresh else 0 for x in y_pred]
    cm = confusion_matrix(y_true, y_pred)
    return cm

没有sklearn:

import numpy as np
def conf_m(y_true, y_pred, thresh = 0.5):
    y_true = [int(i) for i in y_true]
    y_pred = [1 if x>=thresh else 0 for x in y_pred]
    K = len(np.unique(y_true))
    cm = np.zeros((K, K))

    for i in range(len(y_true)):
        cm[y_true[i]][y_pred[i]] += 1

    return cm