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), }