为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

天涯尽头无女友 2023-06-20 10:46:15
我正在尝试零镜头分类。我得到如下输出[{'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非常感谢任何解决此问题的推动/帮助。
查看完整描述

1 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

定义一个函数,它返回一个键:每行的值字典,键是标签,值是基于阈值的 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)


查看完整回答
反对 回复 2023-06-20
  • 1 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信