简体   繁体   English

使用 for 循环制作具有多个键的字典

[英]Making a dictionary having multiple keys using for loop

I have a dictionary of metric names.我有一个指标名称字典。

metrics = {
    'accuracy': accuracy,
    'loss': loss
}

I want to create another dictionary that stores the values of the metrics for a set of hierarchical labels.我想创建另一个字典来存储一组分层标签的度量值。 The dictionary is supposed to be something like this:字典应该是这样的:

{'c1_accuracy': 0.84, 'c1_loss': 5.67, 'c2_loss': 8.78, 'c2_accuracy': 0.73}

I am trying to use something like this我正在尝试使用这样的东西

summary_batch = {'c1_{}'.format(metric): metrics['metric'](c1_output, c1_label) for metric in metrics}

How can I do it for all the labels (c1, c2, ...) at once?如何一次为所有标签(c1,c2,...)执行此操作?

Edit: I am trying to solve a hierarchical classification problem where I have two coarse classes (let's say c1 and c2) and one fine class (f).编辑:我正在尝试解决一个层次分类问题,其中我有两个粗略的类(比如说 c1 和 c2)和一个精细的 class(f)。 metrics dictionary is just used to store the metrics which are used in the training and evaluation loops. metrics字典仅用于存储在训练和评估循环中使用的指标。 I have separate functions for calculating the metric values.我有单独的函数来计算度量值。 In the above example, metrics['metric'](c1_output, c1_label) for metric in metrics will call the accuracy and loss functions for the true labels ( c1_label ) and model output ( c1_output ).在上面的示例中, metrics['metric'](c1_output, c1_label) for metric in metrics将调用真实标签 ( c1_label ) 和 model output ( c1_output ) 的accuracyloss函数。

I want to store these metrics for both coarse classes and the fine class.我想为粗略类和精细 class 存储这些指标。 Is there a way I can do that without using add or writing separate loops for each class?有没有一种方法可以做到这一点,而无需为每个 class 使用添加或编写单独的循环?

It would be interesting to know how you obtain the values to fill the dictionary in order to do it while creating it, however, you can create it empty with this loop知道如何获取值以填充字典以便在创建字典时执行它会很有趣,但是,您可以使用此循环将其创建为空

metrics = {}

for n in range(10):
    key_constructor = 'c{}_{}'
    key_acc = key_constructor.format(n,'accuracy')
    key_loss = key_constructor.format(n,'loss')
    metrics[key_acc] = 0
    metrics[key_loss] = 0

If you want to have a hierarchy why not using a nested dict?如果你想有一个层次结构,为什么不使用嵌套字典?

summuary_batch = {
    "c1": {
        "accuracy": xxx,
        "loss": yyy,
    },
    "c2": {
        "accuracy": zzz,
        "loss": ttt,
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 pandas Dataframe 中的列中的值作为键并将另一列中的所有值作为值制作字典 - Making a dictionary using values in a column in pandas Dataframe as keys and having all values in another column as values 如何在for循环中访问多个字典键? - How to access multiple dictionary keys within for loop? 使用多个键从文本文件制作python字典 - Making python dictionary from a text file with multiple keys 使用for循环在字典中创建内部键 - Creating inside keys in a dictionary using a for loop 使用 for 循环将键和值添加到空字典 - Adding keys and values to an empty dictionary using for loop 我可以在单个for循环上指定嵌套字典的多个键,而不是使用嵌套的for循环吗? - Can I specify multiple keys to nested dictionary on single for loop rather than using nested for loops? 使用for循环使用其他词典中的键创建新词典 - Use a for loop to create a new dictionary using keys from another dictionary 具有多个键的单个for循环的值的多个字典列表 - Multiple dictionary list of values assignment with a single for loop for multiple keys 在Python字典中为一个值使用多个键 - Using multiple keys for one value in a Python dictionary 如何使用多个键将 map 值添加到字典中? - How to map values to a dictionary using multiple keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM