KeyError:尝试在字典中查找最小值时出现411

时间:2020-03-09 09:39:59

标签: python-3.x

我需要在以下代码中找到A的最小值为i的值。但这给了KeyError:411,我听不懂这个错误,有人可以帮我理解这个错误吗?

下面是我的代码:

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

1 个答案:

答案 0 :(得分:1)

检查此

y is a value and not a list or dictionary. 

您正在尝试访问值y [k]以获得整数k。这是不允许的,因为值不可散列。 我建议您再次在代码中进行更正。 希望对您有帮助。