
v1 정밀도/재현율 은 하나의 고정된 threshold 에 대한 값. 실전에서는 모델이 연속 점수(score) 를 내고, threshold 를 바꾸면 P/R 이 달라짐 → PR curve.
threshold = y_score[i] 에 대해 y_pred = (y_score >= threshold).PR 곡선 아래 면적의 계단 (step) 근사:
(sklearn 의 average_precision_score 정의 — 사다리꼴이 아닌 직사각형 합.)
에서 시작하고, threshold 가 커질수록 precision 높고 recall 낮음 → 곡선은 왼쪽 상단 (0, 1) 에서 오른쪽 하단으로.
완벽 ranker: 양성 점수가 모두 음성보다 크면 AP = 1.0. 랜덤 ranker: AP ≈ 양성 비율 (prevalence).
함수 pr_curve_ap(y_score, y_true) 를 완성하세요.
y_score: 양성 확률 또는 점수, shape (N,).y_true: 0/1 정수 레이블, shape (N,).(precision, recall, thresholds, ap)
precision, recall: shape (K+1,) 배열 — 내림차순 threshold 각 지점 + 마지막에 P=1, R=0 (convention).thresholds: shape (K,) — 내림차순 점수.ap: Python float — .np.argsort(-y_score) 로 내림차순 인덱스 → cumulative TP/FP 로 한 번에 계산.| # | 이름 | 검증 |
|---|---|---|
| 1 | 반환 4-tuple | |
| 2 | precision, recall shape 일치 | |
| 3 | 완벽 ranker → AP = 1.0 | |
| 4 | recall monotonically increasing | |
| 5 | recall 마지막 = 0, precision 마지막 = 1 (convention) | |
| 6 | 랜덤 ranker → AP ≈ 양성 비율 | |
| 7 | sklearn average_precision_score 와 일치 |
코드를 작성하고 Run 을 눌러보세요.