从分类器输出中提取字典值

Extract Dictionary Values from Classifier Output

我正在尝试零样本分类。我得到如下输出

[{'labels': ['rep_appreciation',
   'cx_service_appreciation',
   'issue_resolved',
   'recommend_product',
   'callback_realted',
   'billing_payment_related',
   'disppointed_product'],
  'scores': [0.9198898673057556,
   0.8672246932983398,
   0.79215407371521,
   0.6239275336265564,
   0.4782547056674957,
   0.39024001359939575,
   0.010263209231197834],
  'sequence': 'Alan Edwards provided me with nothing less the excellent assistance'}

以上是数据框中一行的输出

我希望最终构建一个数据框列和输出值映射如下。如果分数高于特定阈值,标签为 1s

非常感谢任何 nudge/help 解决此问题的方法。

定义一个函数,其中 returns 一个键:每行的值字典,键是标签,值是基于阈值的 1/0

def get_label_score_dict(row, threshold):
    result_dict = dict()
    for _label, _score in zip(row['labels'], row['scores']):
        if _score > threshold:
            result_dict.update({_label: 1})
        else:
            result_dict.update({_label: 0})
    return result_dict

现在,如果您有一个 list_of_rows 并且每一行都采用如上所示的形式,那么您可以使用 map 函数为每一行获取上述字典。一旦你得到这个,把它转换成一个DataFrame。

th = 0.5    #whatever threshold value you want
result = list(map(lambda x: get_label_score_dict(x, th), list_of_rows))
result_df = pd.DataFrame(result)