我从一个简单的Python机器学习副本中得到“keyrerror:1”

2024-09-30 18:31:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么会出现这个错误,我正在使用Mnist btw,相同的数据集和函数用于不同的模型(即LDA),这与SVM作为模型的实现是相同的

natural_targets_dict_woae = {}
non_natural_targets_dict_woae = {}
one_number_targets_dict_woae = {}
for ix, series in attacktargets_woae.iterrows():
    natural_targets_dict_woae[ix] = series.argmax()
    non_natural_targets_dict_woae[ix] = series.drop(ix).argmin()
    one_number_targets_dict_woae[ix] = y_test_woae[0]

natural_foolingtargets_woae = np.zeros((y_test_woae.shape[0]))
non_natural_foolingtargets_woae = np.zeros((y_test_woae.shape[0]))
one_number_foolingtargets_woae = np.zeros((y_test_woae.shape[0]))
for n in range(len(natural_foolingtargets_woae)):
    target_woae = y_test_woae[n]
    natural_foolingtargets_woae[n] = natural_targets_dict_woae[target_woae]
    non_natural_foolingtargets_woae[n] = non_natural_targets_dict_woae[target_woae]
    one_number_foolingtargets_woae[n] = one_number_targets_dict_woae[target_woae]

attack_woae.create_one_hot_targets(natural_foolingtargets_woae.astype(np.int))
attack_woae.attack_to_max_epsilon(targeted_gradient, epsilon_number)
natural_scores_woae = attack_woae.scores

attack_woae.create_one_hot_targets(non_natural_foolingtargets_woae.astype(np.int))
attack_woae.attack_to_max_epsilon(targeted_gradient, epsilon_number)
non_natural_scores_woae = attack_woae.scores

attack_woae.create_one_hot_targets(one_number_foolingtargets_woae.astype(np.int))
attack_woae.attack_to_max_epsilon(targeted_gradient, epsilon_number)
one_target_scores_woae = attack_woae.scores

错误:

KeyError Traceback (most recent call last) <ipython-input-83-e52f71235839> in <module>
     12 for n in range(len(natural_foolingtargets_woae)):
     13     target_woae = y_test_woae[n]
---> 14     natural_foolingtargets_woae[n] = natural_targets_dict_woae[target_woae]
     15     non_natural_foolingtargets_woae[n] = non_natural_targets_dict_woae[target_woae]
     16     one_number_foolingtargets_woae[n] = one_number_targets_dict_woae[target_woae]

KeyError: 1

Tags: testnumbertargetnpnaturalonedictix