我正在嘗試零鏡頭分類。我得到如下輸出[{'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)
添加回答
舉報
0/150
提交
取消