python - python得到了结果,这里是keyerror:1
问题描述
#compute ROC curve and ROC area for each class
fpr= dict()
tpr= dict()
roc_auc = dict()
y_test= to_categorical(Y_test, num_classes)
y_test=np.array(y_test)
pred= np. array(predictions)
n_classes= num_classes
lw=2 #float values in point
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
#compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), pred.ravel())
roc_auc["micro"]= auc(fpr["micro"], tpr["micro"])
#compute macro-average ROC curve and ROC area
# first aggregate all false posivte rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range (n_classes)]))
# Then interpolate all ROC curves at this points
mean_tpr= np.zeros_like(all_fpr)
for i in range (n_classes):
mean_tpr += interp(all_fpr, fpr[i],tpr[i])
#finally average it and compute auc
mean_tpr /= n_classes
fpr["macro"]= all_fpr
tpr["macro"]=mean_tpr
roc_auc["macro"]= auc(fpr["macro"],tpr["macro"])
#plot all ROC
plt.figure()............
我没能找出这个错误的原因,#PROBLEM File "", line 241, in all_fpr = np.unique(np.concatenate([fpr[i] for i in range (n_classes)])) KeyError : 1 i尝试了很多但未能修复它...有人请帮助我这里是代码
解决方案
该错误表明您的字典fpr
没有带有 key 的条目1
,您可能需要再次查看您的条件以防止这种情况发生。
这是错误的一个小例子
>>> d={0: 'hi'}
>>> print(d[0])
hi
>>> print(d[1])
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print(d[1])
KeyError: 1
>>>
推荐阅读
- go - 如何在golang中使用杜松子酒服务两个静态站点?
- javascript - LitElement 如何监控子 dom 并更改样式
- python - 如何使用 Flask/Jinja2 连续渲染项目?
- php - 从 Android 手机获取 userAgent
- javascript - 多维默认数组
- r - 如何对 qwrap2 summary_table 的输出列进行排序
- javascript - 访问车把模板中的环境变量
- python - 如何在 Django 中创建从 AbstractUser 继承的多个 UserProfile?
- php - 图片上传到 TMP 文件位置不正确
- ruby-on-rails - Rails 6 + 纱线 + 数据表问题