File size: 1,341 Bytes
141f1e0
bc714de
141f1e0
 
bc714de
141f1e0
 
 
 
bc714de
 
141f1e0
 
bc714de
 
141f1e0
 
 
 
 
 
 
 
bc714de
 
 
 
 
 
141f1e0
 
 
 
bc714de
 
 
 
141f1e0
 
 
 
bc714de
 
 
 
 
 
141f1e0
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
import pandas as pd
from sklearn.metrics import accuracy_score, confusion_matrix, f1_score, precision_score, recall_score


def compute_metrics(predictions_df: pd.DataFrame, true_labels: dict[str, float]) -> dict[str, float]:
    y_true = []
    y_pred = []

    for _, row in predictions_df.iterrows():
        id_val = str(row["id"]).strip()
        if id_val not in true_labels:
            continue

        true_label = int(true_labels[id_val])
        pred_label = int(row["label"])

        y_true.append(true_label)
        y_pred.append(pred_label)

    if len(y_true) == 0:
        return {
            "accuracy": 0.0,
            "f1": 0.0,
            "precision": 0.0,
            "recall": 0.0,
            "tp": 0,
            "fp": 0,
            "fn": 0,
            "tn": 0,
        }

    accuracy = accuracy_score(y_true, y_pred)
    f1 = f1_score(y_true, y_pred, zero_division=0.0)
    precision = precision_score(y_true, y_pred, zero_division=0.0)
    recall = recall_score(y_true, y_pred, zero_division=0.0)

    tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()

    return {
        "accuracy": float(accuracy),
        "f1": float(f1),
        "precision": float(precision),
        "recall": float(recall),
        "tp": int(tp),
        "fp": int(fp),
        "fn": int(fn),
        "tn": int(tn),
    }