使用 pandas 构建数据框

Construct a data frame with pandas

我想根据我在这里展示的数据构建一个数据框。谁能帮助我朝着正确的方向前进?提前致谢!!

test_comment = "We’re exceptionally proud of the 62,000 employees who work in our restaurants, along with the hundreds of Russian suppliers who support our business, and our local franchisees. "

# tokenizing comment ^
encoding = tokenizer.encode_plus(
  test_comment,
  add_special_tokens=True,
  max_length=512,
  return_token_type_ids=False,
  padding="max_length",
  return_attention_mask=True,
  return_tensors='pt',
)

# returning probability values for each label
_, test_prediction = trained_model(encoding["input_ids"], encoding["attention_mask"])
test_prediction = test_prediction.flatten().numpy()

for label, prediction in zip(LABEL_COLUMNS, test_prediction):
  print(f"{label}: {prediction}",)

[0 if x <= 0.5 else 1 for x in test_prediction]

结果:

morality_binary: 0.433603435754776
emotion_binary: 0.5506623983383179
positive_binary: 0.6030590534210205
negative_binary: 0.022853979840874672
care_binary: 0.1553395688533783
fairness_binary: 0.2245887666940689
authority_binary: 0.11432072520256042
sanctity_binary: 0.0428963303565979
harm_binary: 0.032407380640506744
injustice_binary: 0.029283544048666954
betrayal_binary: 0.013294332660734653
subversion_binary: 0.02164781652390957
degradation_binary: 0.019699573516845703

首选结果: ...如果结果高于 0.5,则在数据框中插入 1,否则插入 0。

test_comment                   morality_binary     emotion_binary   positive_binary  (... rest of labels)
We’re exceptionally proud...   0                   1                1

通过构造函数创建 DataFrame,将比较大的值转换为 0.51,否则 0 通过转换为整数:

df = pd.DataFrame([(test_prediction > 0.5).astype(int)], columns=LABEL_COLUMNS)

你可以事先有一个空的字典而不是 print(f"{label}: {prediction}",) 你可以做类似 result['label'] = prediction 的事情,然后形成你可以做的数据框 pd.DataFrame(result, columns=['label', 'prediction'])