我编写了一个函数,给定一个数据帧,为每一列生成一个分数。我想将此分数添加到一个表中,该表的结构为嵌套字典。问题是,当我将分数添加到嵌套字典中的一个特定键时,它将使用相同的键将该分数添加到整个字典中。特别是以下代码:
kernel = input("Please enter the kernel you want to use:\n")
score = Kernel_evaluation(Estimators, x, Targets, threshold)
print (f'The score is {score}')
for dict_1 in Scoring:
if dict_1 == dataframe.columns[x]:
for KEY in Scoring[dict_1]:
if KEY == kernel:
Scoring[dict_1][KEY] = score
break
else:
continue
break
else:
continue
评分是嵌套字典,包含4个键(我在数据框中有4列),每个键都包含一个包含6个键的字典(我有6个内核要处理)。当我将值分数添加到对应的键时,它会将我添加到所有4个字典(在我的示例中,内核是高斯的,并且4个字典中的每个“高斯”键每次都用分数值更新。我不知道为什么会发生。你能帮我吗
示例数据是iris数据集,我希望字典字典如下:
Scoring = {'sepal_length': {'gaussian': 0.0, 'tophat': 0.0, 'epanechnikov': 0.0, 'exponential': 0.0, 'linear': 0.0, 'cosine': 0.0},
'sepal_width': {'gaussian': 0.0, 'tophat': 0.0, 'epanechnikov': 0.0, 'exponential': 0.0, 'linear': 0.0, 'cosine': 0.0},
'petal_length': {'gaussian': 0.0, 'tophat': 0.0, 'epanechnikov': 0.0, 'exponential': 0.0, 'linear': 0.0, 'cosine': 0.0},
'petal_width': {'gaussian': 0.0, 'tophat': 0.0, 'epanechnikov': 0.0, 'exponential': 0.0, 'linear': 0.0, 'cosine': 0.0}}
但是对于四个字典中的每个键,我想要一个分数(我通过另一个函数获得,但它是一个简单的数字)。当我将这个分数添加到Scoring[KEY][key]
时,它将添加到所有四个字典中
试试这个:
相关问题 更多 >
编程相关推荐