-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoft_ensemble.py
54 lines (43 loc) · 1.44 KB
/
soft_ensemble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import yaml
import numpy as np
import pandas as pd
from sklearn.metrics import classification_report, f1_score
with open("./config.yaml") as file:
config = yaml.safe_load(file)
model_dir = os.path.join(
config["model"]["model_loc"],
config["dataset"]["file_name"],
)
_mode = config["ensemble"]["mode"]
notoff_probs = {}
for model_name in config["ensemble"]["models"]:
csv_file = os.path.join(model_dir, model_name, f"_{_mode}.csv")
df = pd.read_csv(csv_file)
for _, row in df.iterrows():
id = row["ids"]
notoff_prob = row["probability"] if row["pred"] == 0 else 1 - row["probability"]
if id in notoff_probs:
notoff_probs[id]["prob"] += notoff_prob
else:
notoff_probs[id] = {"prob": notoff_prob, "true": row["true"]}
_ids, _probs = [], []
_labels, _true = [], []
for id, stats in notoff_probs.items():
prob = stats["prob"] / 3
true = stats["true"]
label = 0 if prob > 0.5 else 1
_ids.append(id)
_true.append(true)
_labels.append(label)
_probs.append(prob if label == 0 else 1 - prob)
print(
classification_report(
np.array(_true),
np.array(_labels),
target_names=["OFF", "NOT"],
)
)
print("wted-f1: {:.3f}".format(f1_score(_true, _labels, average="weighted")))
df = pd.DataFrame({"ids": _ids, "probability": _probs, "pred": _labels, "true": _true})
df.to_csv(f"{model_dir}/softensemble_{_mode}.csv", index=False)