python - np.vectorize 只有大小为 1 的数组可以转换为标量
问题描述
我正在尝试打印评估我的贝叶斯模型的 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
尝试过:astype(int)
fpr
tpr
x = fpr.astype(int)
y = tpr.astype(int)
它仍然没有帮助
这里有什么问题?这是 fpr,tpr 在调用之前的样子astype
解决方案
使用fpr
andtpr
你展示的值,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
令人费解的是,为什么您尝试 y_pred
在明确暗示您确实想要 AUC 分数的地方使用预测。
您应该单独计算 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)
推荐阅读
- python - 错误:“ImageDataGenerator”对象没有属性“形状”
- node.js - AWS Managed Hyper ledger Fabric 区块链 client.getUserContext 返回 null
- sql - 为什么我的代码没有到达第二个执行方法?
- python - 尝试使用 autopep8 时出现 EOF 错误
- python - 无法从 Ubuntu 18.04.1 LTS 中的 OpenPLI 存储库创建映像
- flutter - DateTimePickerFormField 在 VS Code 中不起作用
- vba - 如何更新从 VBA Shell 或 WScript.Shell 调用的 cmd.exe?
- python - Tensorflow获取原始框架而不是边界框绘制框架
- c# - DropDownListFor 不设置值
- azure - 通过 Python SDK 获取规模集中 VM 的私有 IP 地址(规模集中没有公共 IP 地址)