Spaces:
Running on Zero
Running on Zero
Download scripts/calibrer_libelles.py from Myz0/CV-Matching: direct link, hf CLI and curl.
- Browser
- Download file 4.32 kB
-
https://huggingface.co/spaces/Myz0/CV-Matching/resolve/main/scripts/calibrer_libelles.py
- Command line
-
hf download hf://spaces/Myz0/CV-Matching/scripts/calibrer_libelles.py
-
curl -L -o calibrer_libelles.py https://huggingface.co/spaces/Myz0/CV-Matching/resolve/main/scripts/calibrer_libelles.py
4.32 kB
| """Calibre les seuils des libellés sur le jeu d'évaluation n° 2 (notes humaines 0..3). | |
| Décision attendue par couple (CV, poste type) : note 3 -> « à convoquer », 2 -> « à étudier », | |
| 0-1 -> « non ». Décision du service : son pourcentage (meilleur AVP du poste) comparé à deux seuils : | |
| >= t_haut « Très bonne / Excellente » (à convoquer) | |
| >= t_bas « Bonne / Partielle » (à étudier) | |
| sinon « Faible » (non) | |
| On cherche (t_bas, t_haut) qui maximisent le kappa pondéré (quadratique) entre les deux décisions. | |
| Validation : poste mis de côté (on règle sur 19 postes types, on applique au 20e, pour chacun). | |
| Lancer depuis la racine (après scripts.evaluer_jeu, qui met les similarités en cache) : | |
| python -m scripts.calibrer_libelles | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import numpy as np | |
| from app import config, scoring | |
| from scripts.evaluer_classement import matrice | |
| from scripts.evaluer_jeu import JEU, notes, similarites | |
| NOMS = ["non", "à étudier", "à convoquer"] | |
| def kappa(a: np.ndarray, b: np.ndarray, k: int = 3) -> float: | |
| """Kappa de Cohen à pondération quadratique.""" | |
| o = np.zeros((k, k)) | |
| for x, y in zip(a, b): | |
| o[x, y] += 1 | |
| w = np.array([[(i - j) ** 2 for j in range(k)] for i in range(k)]) / (k - 1) ** 2 | |
| e = np.outer(o.sum(1), o.sum(0)) / o.sum() | |
| return 1 - (w * o).sum() / (w * e).sum() | |
| def decider(pct: np.ndarray, bas: float, haut: float) -> np.ndarray: | |
| return np.where(pct >= haut, 2, np.where(pct >= bas, 1, 0)) | |
| def regler(pct, verite, grille): | |
| return max(grille, key=lambda s: (kappa(verite, decider(pct, *s)), -s[1])) | |
| def matrice_confusion(verite, dec) -> str: | |
| lignes = [f" {'note humaine':14s} " + " ".join(f"{n:>12s}" for n in NOMS)] | |
| for v in (2, 1, 0): | |
| lignes.append(f" {NOMS[v]:14s} " + " ".join(f"{int(np.sum((verite == v) & (dec == d))):12d}" for d in range(3))) | |
| return "\n".join(lignes) | |
| def main(): | |
| jeu = json.loads(JEU.read_text(encoding="utf-8")) | |
| ids, poids, poids_sf, sims = similarites(jeu["cvs"]) | |
| R, poste_avp = notes(jeu, ids) | |
| m_pct = 100 * matrice(sims, poids_sf, scoring.SIM_BAS, scoring.SIM_HAUT) | |
| postes = sorted({p for p in poste_avp if p}) | |
| couples = [] # (poste, pourcentage, décision attendue) | |
| for c in range(len(R)): | |
| for k in postes: | |
| cols = [a for a, q in enumerate(poste_avp) if q == k] | |
| g = int(R[c, cols[0]]) | |
| couples.append((k, max(m_pct[c, a] for a in cols), {3: 2, 2: 1}.get(g, 0))) | |
| poste = np.array([k for k, _, _ in couples]) | |
| pct = np.array([x for _, x, _ in couples]) | |
| verite = np.array([v for _, _, v in couples]) | |
| seuils = sorted(config.SEUILS_LIBELLE) # [20, 33, 50] | |
| actuel = (seuils[0], seuils[-1]) # partielle dès 20, très bonne dès 50 | |
| grille = [(b, h) for b in np.arange(5, 40, 1.0) for h in np.arange(b + 2, 60, 1.0)] | |
| print(f"{len(couples)} couples (CV, poste type) : {np.sum(verite == 2)} à convoquer, " | |
| f"{np.sum(verite == 1)} à étudier, {np.sum(verite == 0)} non\n") | |
| d0 = decider(pct, *actuel) | |
| print(f"Seuils actuels (étudier >= {actuel[0]:.0f} %, convoquer >= {actuel[1]:.0f} %) : kappa {kappa(verite, d0):.3f}") | |
| print(matrice_confusion(verite, d0)) | |
| # Validation : poste mis de côté | |
| dec_cv = np.zeros_like(verite) | |
| choisis = [] | |
| for k in postes: | |
| test = poste == k | |
| s = regler(pct[~test], verite[~test], grille) | |
| choisis.append(s) | |
| dec_cv[test] = decider(pct[test], *s) | |
| print(f"\nSeuils réglés, poste mis de côté (validation) : kappa {kappa(verite, dec_cv):.3f}") | |
| print(matrice_confusion(verite, dec_cv)) | |
| b, h = np.array(choisis).T | |
| print(f" seuils choisis selon le pli : étudier {b.min():.0f}-{b.max():.0f} %, convoquer {h.min():.0f}-{h.max():.0f} %") | |
| final = regler(pct, verite, grille) | |
| print(f"\nSeuils réglés sur tous les postes (à reporter) : étudier >= {final[0]:.0f} %, convoquer >= {final[1]:.0f} %" | |
| f" (kappa {kappa(verite, decider(pct, *final)):.3f}, optimiste)") | |
| if __name__ == "__main__": | |
| main() | |