24 lines
1.0 KiB
Python
24 lines
1.0 KiB
Python
"""Metriche di valutazione del classificatore."""
|
|||
|
|
from __future__ import annotations
|
||
|
|
import numpy as np
|
||
|
|
from sklearn.metrics import roc_auc_score, accuracy_score, brier_score_loss
|
||
|
|
|
||
|
|
|
||
|
|
def metrics(y_true, p_pred) -> dict:
|
||
|
|
y_true = np.asarray(y_true)
|
||
|
|
p = np.asarray(p_pred)
|
||
|
|
out = {"n": int(len(y_true)), "base_rate": float(y_true.mean())}
|
||
|
|
if len(np.unique(y_true)) < 2:
|
||
|
|
out.update({"auc": np.nan, "acc": np.nan, "brier": np.nan,
|
||
|
|
"prec_top_decile": np.nan, "lift_top_decile": np.nan})
|
||
|
|
return out
|
||
|
|
out["auc"] = float(roc_auc_score(y_true, p))
|
||
|
|
out["acc"] = float(accuracy_score(y_true, (p >= 0.5).astype(int)))
|
||
|
|
out["brier"] = float(brier_score_loss(y_true, p))
|
||
|
|
# precisione sul decile a piu' alta probabilita' (uso pratico: prendo solo i segnali piu' forti)
|
||
|
|
k = max(1, int(0.1 * len(p)))
|
||
|
|
top = np.argsort(p)[-k:]
|
||
|
|
out["prec_top_decile"] = float(y_true[top].mean())
|
||
|
|
out["lift_top_decile"] = float(y_true[top].mean() / max(y_true.mean(), 1e-9))
|
||
|
|
return out
|