Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从分类器输出中提取字典值_Python_Pandas_Dataframe_Dictionary_Text Classification - Fatal编程技术网

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

Python 从分类器输出中提取字典值,python,pandas,dataframe,dictionary,text-classification,Python,Pandas,Dataframe,Dictionary,Text Classification,我正在尝试零射击分类。我得到如下输出 [{'labels': ['rep_appreciation', 'cx_service_appreciation', 'issue_resolved', 'recommend_product', 'callback_realted', 'billing_payment_related', 'disppointed_product'], 'scores': [0.9198898673057556, 0.86722

我正在尝试零射击分类。我得到如下输出

[{'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

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


非常感谢为解决此问题提供的任何提示/帮助。

定义一个函数,该函数为每行返回一个键:值字典,键为标签,值为基于阈值的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
现在,如果您有一个包含行的列表,每一行的格式如上图所示,那么您可以使用map函数为每一行获取上述字典。一旦你得到这个,把它转换成一个数据帧

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)

太棒了,谢谢!。按预期工作。谢谢你的帮助