无法设置xticks以选择适当的截止阈值

问题描述 投票:0回答:1

在模型评估阶段,我观察到我的模型预测的值在30.309-59.556范围内。

我想设置一个阈值,以便可以说大于该阈值的值是正确的,否则是不正确的。

我将其他阈值设置为:

start, stop, step = min(y_pred), max(y_pred)+1, (max(y_pred) - min(y_pred))/10

for i in arange(start, stop, step):
    k.append(round(i, 2))

这为我提供了10个不同的等距阈值,如下所示:

Thresholds: [30.31, 33.23, 36.16, 39.08, 42.01, 44.93, 47.86, 50.78, 53.71, 56.63, 59.56]

我对这些阈值进行了模型评估,结果如下:

Sensitivity: [1.0, 0.999, 0.983, 0.866, 0.497, 0.197, 0.093, 0.036, 0.007, 0.0, 0.0]
Specificity: [0.0, 0.0, 0.003, 0.027, 0.172, 0.576, 0.845, 0.915, 0.971, 0.997, 1.0]

[灵敏度从1到0,特异性从0到1。

我绘制它们,结果如下:

fig = plt.figure(facecolor='white', figsize=(18, 4))
plt.title("Sensitivity vs Specificiy")
plt.plot(sensitivity, c='red')
plt.plot(specificity, c='Green')
plt.legend(['Sensitivity','Specificity'])
plt.grid(True)
plt.show()

enter image description here

但是我希望x轴显示阈值,而不是0到10之间的数字。

我尝试设置xticks,但结果如下:

fig = plt.figure(facecolor='white', figsize=(18, 4))
plt.title("Sensitivity vs Specificiy")
plt.plot(sensitivity, c='red')
plt.plot(specificity, c='Green')
plt.legend(['Sensitivity','Specificity'])
**plt.xticks(k)**
plt.grid(True)
plt.show()

enter image description here我在哪里犯错?

python-3.x matplotlib plot threshold linegraph
1个回答
0
投票

而不是将sensitivityspecificity绘制为一维图,而可以将它们相对于thresholds绘制为2D图。只需将其作为x轴添加到绘图中即可。

fig = plt.figure(facecolor='white', figsize=(18, 4))
plt.title("Sensitivity vs Specificiy")
plt.plot(thresholds, sensitivity, c='red')
plt.plot(thresholds, specificity, c='Green')
plt.legend(['Sensitivity','Specificity'])
plt.grid(True)
plt.show()

结果如下。enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.