#!/usr/bin/env python3 """⑨ 품목별 협상 시나리오 분류 — 독립 실행 채점기 (표준 라이브러리만). 입력: data/predictions.csv — 헤더 y_true,y_pred (분류기 test set 예측 결과) 사용: python run.py [--input data/predictions.csv] [--target 96] 산식: macro-F1 (클래스별 F1 단순 평균) — micro-F1(=accuracy) 병기 """ import argparse import csv import json from collections import defaultdict from pathlib import Path HERE = Path(__file__).parent def main(): ap = argparse.ArgumentParser() ap.add_argument("--input", default=str(HERE / "data" / "predictions.csv")) ap.add_argument("--target", type=float, default=96.0) args = ap.parse_args() with open(args.input, encoding="utf-8-sig") as f: rows = list(csv.DictReader(f)) y_true = [r["y_true"].strip() for r in rows] y_pred = [r["y_pred"].strip() for r in rows] tp, fp, fn = defaultdict(int), defaultdict(int), defaultdict(int) with open(HERE / "detail.jsonl", "w", encoding="utf-8") as df: for i, (t, p) in enumerate(zip(y_true, y_pred)): if t == p: tp[t] += 1 else: fp[p] += 1 fn[t] += 1 df.write(json.dumps({"i": i, "y_true": t, "y_pred": p, "정답": t == p}, ensure_ascii=False) + "\n") per_class, f1s = {}, [] for c in sorted(set(y_true) | set(y_pred)): prec = tp[c] / (tp[c] + fp[c]) if tp[c] + fp[c] else 0.0 rec = tp[c] / (tp[c] + fn[c]) if tp[c] + fn[c] else 0.0 f1 = 2 * prec * rec / (prec + rec) if prec + rec else 0.0 per_class[c] = {"precision": round(prec, 4), "recall": round(rec, 4), "f1": round(f1, 4), "support": y_true.count(c)} f1s.append(f1) macro_f1 = round(100 * sum(f1s) / len(f1s), 2) if f1s else 0.0 micro_f1 = round(100 * sum(1 for t, p in zip(y_true, y_pred) if t == p) / len(y_true), 2) \ if y_true else 0.0 out = { "n": len(y_true), "산식": "macro-F1 (클래스별 F1 단순 평균) — 불균형 대비 보수적 채점", "macro_f1_pct": macro_f1, "micro_f1_pct(accuracy)": micro_f1, "클래스별": per_class, "target_pct": args.target, "verdict": "PASS" if macro_f1 >= args.target else "FAIL", } (HERE / "result.json").write_text(json.dumps(out, ensure_ascii=False, indent=2), encoding="utf-8") print(json.dumps(out, ensure_ascii=False, indent=1)) if __name__ == "__main__": main()