【问题标题】:np.vectorize only size-1 arrays can be converted to scalarsnp.vectorize 只有大小为 1 的数组可以转换为标量
【发布时间】:2021-06-20 13:00:50
【问题描述】:

我正在尝试打印评估我的贝叶斯模型的 ROC 曲线

fpr, tpr, _ =roc_curve(y_test, y_pred)

功能:

plt.plot(fpr, tpr, label='Naive Bayes (AUROC = %0.3f)' % y_pred)

plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend()  
plt.show()

我在绘制时收到错误。以前的帖子建议使用np.vectorize 然而,我在fprtpr 上尝试了astype(int)

x = fpr.astype(int)
y = tpr.astype(int)

还是没用

这里有什么问题?这是调用 astype 之前 fpr,tpr 的样子

【问题讨论】:

  • 请发布完整的错误跟踪,以及fprtpr 的结果(值)作为文本不是图像(你知道你可以从 Jupyter 输出单元中复制粘贴它们,对吧?)。

标签: python matplotlib scikit-learn


【解决方案1】:

使用您显示的 fprtpr 的值,plot 可以正常工作:

import numpy as np
from matplotlib import pyplot as plt

fpr = np.array([0, 0.136, 1.])
tpr = np.array([0, 0.5, 1.])

plt.plot(fpr, tpr)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()

问题源于 y_pred 令人费解地出现在它不属于它的地方,即在 label 参数中:

# dummy y_pred - exact values do not matter
y_pred = np.array([0.1, 0.34, 0.43, 0.89])

plt.plot(fpr, tpr, label='Naive Bayes (AUROC = %0.3f)' % y_pred)

结果(不足为奇):

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-8-ccbf0542501c> in <module>()
----> 1 plt.plot(fpr, tpr, label='Naive Bayes (AUROC = %0.3f)' % y_pred)

TypeError: only size-1 arrays can be converted to Python scalars

令人费解的是,为什么您在明确暗示您确实想要 AUC 分数的地方尝试使用预测 y_pred

您应该单独计算 AUC,并在绘图的适当位置使用它,而不是 y_pred

from sklearn.metrics import roc_auc_score
AUC = roc_auc_score(y_test, y_pred)
plt.plot(fpr, tpr, label='Naive Bayes (AUROC = %0.3f)' % AUC)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2018-07-22
    • 2021-07-08
    • 2023-01-14
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多