python-3.x - KeyError:尝试在字典中查找最小值时出现 411
问题描述
我需要在下面的代码中找到 A 的值最小的 i 的值。但它给 KeyError: 411, I'm not able to understand this error 有人可以帮助我理解这个错误吗?
下面是我的代码:
my_dict = {}
val = data['prob']
y = data['y']
for i in val:
y_pred = []
FP = 0
FN = 0
for j in val:
if (j >= i):
y_pred.append(1)
else:
y_pred.append(0)
for k in range(len(y_pred)):
if (y[k] == 1 and y_pred[k] == 0):
FN += 1
elif (y[k] == 0 and y_pred[k] == 1):
FP += 1
A = (500 * FN)+(100 * FP)
my_dict[i] = A
min(my_dict)
错误:
KeyError Traceback (most recent call last)
<ipython-input-29-3676d59d07c2> in <module>
12 y_pred.append(0)
13 for k in range(len(y_pred)):
---> 14 if (y[k] == 1 and y_pred[k] == 0):
15 FN += 1
16 elif (y[k] == 0 and y_pred[k] == 1):
~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
866 key = com.apply_if_callable(key, self)
867 try:
--> 868 result = self.index.get_value(self, key)
869
870 if not is_scalar(result):
~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
4373 try:
4374 return self._engine.get_value(s, k,
-> 4375 tz=getattr(series.dtype, 'tz', None))
4376 except KeyError as e1:
4377 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 411
解决方案
检查这个,
y is a value and not a list or dictionary.
您正在尝试访问整数 k 的值 y[k]。这是不允许的,因为一个值是不可散列的。我建议您再次检查您的代码并更正它。希望这会帮助你。
推荐阅读
- cordova - 科尔多瓦框架工作7 youtube iframe在phonegap构建后未加载到ios中
- sql - 存储过程中 for 循环后的 DB2 Select Statement 错误
- azure - 使用 api 的 azure key Vault 防火墙设置
- javascript - 线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:对于输入字符串:“10”
- javascript - 'Observable 类型的参数
' 不可分配给“LoginRequest”类型的参数 - linux - 我想在 linux 上删除多行文本
- java - IntellijJ 可以通过 apache taglib 和 EL 在 JSP 中找到用法吗
- laravel-5 - 虚拟主机 Laravel - 我需要带有我的应用程序名称的 URL 地址
- angular - 404 错误(SystemJS)XHR 错误加载为未找到融合图表
- javascript - 如何检查目录是否为空?