首页 > 解决方案 > 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

标签: python-3.x

解决方案


检查这个,

y is a value and not a list or dictionary. 

您正在尝试访问整数 k 的值 y[k]。这是不允许的,因为一个值是不可散列的。我建议您再次检查您的代码并更正它。希望这会帮助你。


推荐阅读