ClarusC64 commited on
Commit
8d038d1
·
verified ·
1 Parent(s): c1fbb68

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +67 -0
scorer.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import json
3
+ import pandas as pd
4
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
5
+
6
+ def load_csv(path):
7
+ try:
8
+ return pd.read_csv(path)
9
+ except Exception as e:
10
+ print(f"Error loading {path}: {e}")
11
+ sys.exit(1)
12
+
13
+ def main():
14
+ if len(sys.argv) != 3:
15
+ print("Usage: python scorer.py predictions.csv data/test.csv")
16
+ sys.exit(1)
17
+
18
+ pred_path = sys.argv[1]
19
+ truth_path = sys.argv[2]
20
+
21
+ pred = load_csv(pred_path)
22
+ truth = load_csv(truth_path)
23
+
24
+ required_pred = {"scenario_id", "prediction"}
25
+ if not required_pred.issubset(pred.columns):
26
+ print("Error: predictions.csv must contain scenario_id,prediction")
27
+ sys.exit(1)
28
+
29
+ if "scenario_id" not in truth.columns or "label" not in truth.columns:
30
+ print("Error: test.csv must contain scenario_id,label")
31
+ sys.exit(1)
32
+
33
+ try:
34
+ pred["prediction"] = pred["prediction"].astype(float).astype(int)
35
+ truth["label"] = truth["label"].astype(float).astype(int)
36
+ except (ValueError, TypeError):
37
+ print("Error: prediction and label columns must contain 0 or 1.")
38
+ sys.exit(1)
39
+
40
+ merged = truth.merge(pred[["scenario_id", "prediction"]], on="scenario_id", how="inner")
41
+
42
+ if len(merged) == 0:
43
+ print("Error: no matching scenario_id values.")
44
+ sys.exit(1)
45
+
46
+ y_true = merged["label"]
47
+ y_pred = merged["prediction"]
48
+
49
+ cm = confusion_matrix(y_true, y_pred, labels=[0, 1])
50
+
51
+ results = {
52
+ "accuracy": accuracy_score(y_true, y_pred),
53
+ "precision": precision_score(y_true, y_pred, zero_division=0),
54
+ "recall": recall_score(y_true, y_pred, zero_division=0),
55
+ "f1": f1_score(y_true, y_pred, zero_division=0),
56
+ "confusion_matrix": {
57
+ "tn": int(cm[0][0]),
58
+ "fp": int(cm[0][1]),
59
+ "fn": int(cm[1][0]),
60
+ "tp": int(cm[1][1])
61
+ }
62
+ }
63
+
64
+ print(json.dumps(results, indent=2))
65
+
66
+ if __name__ == "__main__":
67
+ main()