File size: 3,008 Bytes
681b79a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Build the definitive unified-budget k-shot ladder.

Every method receives the same pilot batch (k% of the target study, same
draw, same seeds) and is evaluated on the unlabelled remainder. Sources:

* ``fewshot_ens_refit.csv``      - our refit / refit+ensemble arms, k=5-20%
* ``fewshot_kscale.csv``         - RF arms (refit / ens4 / ens6 / fp), k=30-80%
* ``fewshot_kscale_xgb.csv``     - XGBoost arms, k=30-80%
* ``fewshot_final.csv``          - adapted-tree mixes, k=10/50%
* ``fewshot_baselines*.csv``     - same-budget CatBoost / XGBoost baselines

    PYTHONPATH=src python scripts/unified_ladder.py
"""

from __future__ import annotations

from pathlib import Path

import pandas as pd

R = Path(__file__).resolve().parents[1] / "results"


def read(name: str) -> pd.DataFrame | None:
    p = R / name
    return pd.read_csv(p) if p.exists() else None


def main() -> None:
    ours = []
    er = read("fewshot_ens_refit.csv")
    if er is not None:
        d = er[(er["use"] == "refit+ens") & (er["select"] == "random")]
        ours.append(d.assign(arm="refit+ens"))
    ks = read("fewshot_kscale.csv")
    if ks is not None:
        ours.append(ks[ks["arm"].isin(["ens4", "fp"])].assign(arm=ks["arm"]))
    kx = read("fewshot_kscale_xgb.csv")
    if kx is not None:
        ours.append(kx[kx["arm"].isin(["xgb_ens", "xgb_fp"])].assign(arm=kx["arm"]))
    ff = read("fewshot_final.csv")
    if ff is not None:
        ours.append(ff[ff["arm"].isin(["trees3_fp", "trees4"])].assign(arm=ff["arm"]))
    ours = pd.concat(ours)

    base = []
    for name, model_col in [("fewshot_baselines.csv", "model"),
                            ("fewshot_baselines_ext.csv", "model"),
                            ("fewshot_baselines_k3.csv", "model")]:
        df = read(name)
        if df is not None:
            base.append(df)
    base = pd.concat(base)

    rows = []
    for k in sorted(ours["k_shot"].unique()):
        o = ours[ours["k_shot"] == k].groupby("arm")["spearman"].agg(["mean", "size"])
        b = base[base["k_shot"] == k].groupby("model")["spearman"].agg(["mean", "size"])
        if not len(o) or not len(b):
            continue
        best_base = b["mean"].max()
        best_base_name = b["mean"].idxmax()
        best_ours = o["mean"].max()
        best_ours_name = o["mean"].idxmax()
        rows.append({"k_shot": k,
                     "best_baseline": best_base, "best_baseline_name": best_base_name,
                     "best_ours": best_ours, "best_ours_name": best_ours_name,
                     "delta": best_ours - best_base,
                     "rel_improve": best_ours / best_base - 1,
                     **{f"ours_{a}": round(v, 4) for a, v in o["mean"].items()},
                     **{f"base_{m}": round(v, 4) for m, v in b["mean"].items()}})
    out = pd.DataFrame(rows)
    out.to_csv(R / "unified_ladder.csv", index=False)
    print(out.round(4).to_string(index=False))
    print(f"\nwrote {R/'unified_ladder.csv'}")


if __name__ == "__main__":
    main()