[英]np.vectorize for TypeError: only size-1 arrays can be converted to Python scalars
[英]np.vectorize only size-1 arrays can be converted to scalars
我正在嘗試打印評估我的貝葉斯 model 的 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
但我在我的fpr
和 tpr 上嘗試了tpr
astype(int)
:
x = fpr.astype(int)
y = tpr.astype(int)
它仍然沒有幫助
這里有什么問題? 這是 fpr,tpr 在調用astype
之前的樣子
使用您顯示的fpr
和plot
tpr
得很好:
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,並在 plot 而不是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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.