首页 > 解决方案 > 如何获得序列模型 Sklearn 的给定预测的概率

问题描述

我有一个顺序卷积神经网络模型,它正在 sklearns 手写数字数据集上进行训练,我现在正在评估模型并尝试从头开始创建 Roc 曲线,这需要我为我的测试数据获取 Tpr 和 Fpr,这样我就可以根据给定的阈值绘制它(据我了解)。我试图获得每个预测的预测概率,但我似乎无法弄清楚如何,我尝试过 predict_proba() 但我认为我的模型错误,有没有办法做到这一点,如果不是我可以如何进行绘图我的大鹏;我必须做一个conf吗?每个预测的矩阵,这似乎很啰嗦?

我在下面显示的另一篇文章中发现了一个 Roc 曲线函数,我试图获得“分数”

score = np.array([0.9, 0.8, 0.7, 0.6, 0.55, 0.54, 0.53, 0.52, 0.51, 0.505, 0.4, 0.39, 0.38, 0.37, 0.36, 0.35, 0.34, 0.33, 0.30, 0.1])
y = np.array([1,1,0, 1, 1, 1, 0, 0, 1, 0, 1,0, 1, 0, 0, 0, 1 , 0, 1, 0])

# false positive rate
fpr = []
# true positive rate
tpr = []
# Iterate thresholds from 0.0, 0.01, ... 1.0
thresholds = np.arange(0.0, 1.01, .01)

# get number of positive and negative examples in the dataset
P = sum(y)
N = len(y) - P

# iterate through all thresholds and determine fraction of true positives
# and false positives found at this threshold
for thresh in thresholds:
    FP=0
    TP=0
    for i in range(len(score)):
        if (score[i] > thresh):
            if y[i] == 1:
                TP = TP + 1
            if y[i] == 0:
                FP = FP + 1
    fpr.append(FP/float(N))
    tpr.append(TP/float(P))

plt.scatter(fpr, tpr)
plt.show()

标签: pythonnumpytensorflowscikit-learn

解决方案


推荐阅读