The Story: Aisha's Three Fraud Models
Her manager asks: "Which model should we deploy? And what threshold should we use to flag a transaction as fraud?"
Aisha knows accuracy won't answer this — only 0.5% of transactions are fraudulent, so a model that always says "Not Fraud" gets 99.5% accuracy while being completely useless. She needs a metric that evaluates model quality across all possible thresholds at once. That metric is the ROC Curve and its summary statistic, the AUC.
Look for the interactive boxes: the Threshold Explorer (Section 02), the ROC Step Builder (03), AUC Two Ways (05), the FPR Budget Comparison (06), the Separation Lab (07), the Imbalance Lab (08), the Operating Point Picker (09), the One-vs-Rest Explorer (10) and the Bootstrap Lab (13). Move sliders, click labels and step through the maths. Every number is computed live.
What Is the ROC Curve?
The ROC Curve (Receiver Operating Characteristic Curve) is a graph that shows how a classifier performs at every possible classification threshold — from 0.0 (classify everything as fraud) to 1.0 (classify nothing as fraud).
At each threshold, the model produces a different confusion matrix. The ROC curve plots two numbers extracted from that matrix:
As you lower the fraud threshold (e.g. from 0.7 → 0.3), the model flags more transactions. This catches more real fraud (TPR rises) but also incorrectly flags more legitimate transactions (FPR also rises). The ROC curve traces this exact trade-off as the threshold sweeps from 1 down to 0. A perfect model reaches TPR=1 while keeping FPR=0.
Try It — Threshold Explorer
Here are 30 transactions: 10 fraud (top row) and 20 legit (bottom row), placed by their fraud probability. Everything right of the purple line is flagged as fraud. Drag the slider (or click the strip) and watch the confusion matrix, TPR and FPR change. Each threshold gives one dot on the ROC plot.
कहानी: आयशा के तीन Fraud Models
उसका manager पूछता है: "हमें कौन-सा model deploy करना चाहिए? और किसी transaction को fraud मानने के लिए कौन-सा threshold रखें?"
आयशा जानती है कि accuracy से इसका जवाब नहीं मिलेगा — सिर्फ़ 0.5% transactions fraud हैं, इसलिए जो model हमेशा "Not Fraud" बोले, उसे 99.5% accuracy मिल जाती है, फिर भी वह बिल्कुल बेकार है। उसे ऐसा metric चाहिए जो model की quality को सभी संभव thresholds पर एक साथ जाँचे। वह metric है ROC Curve और उसका सार बताने वाला नंबर, AUC।
ये interactive boxes देखिए: Threshold Explorer (सेक्शन 02), ROC Step Builder (03), AUC Two Ways (05), FPR Budget Comparison (06), Separation Lab (07), Imbalance Lab (08), Operating Point Picker (09), One-vs-Rest Explorer (10) और Bootstrap Lab (13)। Sliders खिसकाइए, labels पर click कीजिए और गणित को एक-एक step में देखिए। हर नंबर live calculate होता है।
ROC Curve क्या है?
ROC Curve (Receiver Operating Characteristic Curve) एक graph है जो दिखाता है कि एक classifier हर संभव classification threshold पर कैसा काम करता है — 0.0 (सब कुछ fraud मानो) से लेकर 1.0 (कुछ भी fraud मत मानो) तक।
हर threshold पर model एक अलग confusion matrix बनाता है। ROC curve उस matrix से निकले दो नंबर plot करता है:
जब आप fraud threshold को नीचे करते हैं (जैसे 0.7 → 0.3), तो model ज़्यादा transactions को flag करता है। इससे ज़्यादा असली fraud पकड़ा जाता है (TPR बढ़ता है), लेकिन ज़्यादा legit transactions भी गलती से flag होते हैं (FPR भी बढ़ता है)। ROC curve ठीक यही trade-off दिखाता है, जब threshold 1 से नीचे 0 तक जाता है। एक perfect model FPR=0 रखते हुए TPR=1 तक पहुँच जाता है।
खुद करके देखिए — Threshold Explorer
यहाँ 30 transactions हैं: 10 fraud (ऊपर की row) और 20 legit (नीचे की row), जो अपनी fraud probability के हिसाब से रखे गए हैं। बैंगनी line के दाईं ओर सब कुछ fraud के रूप में flag होता है। Slider खींचिए (या strip पर click कीजिए) और देखिए कैसे confusion matrix, TPR और FPR बदलते हैं। हर threshold ROC plot पर एक बिंदु देता है।
故事:Aisha 的三个欺诈检测 model
她的经理问:“我们应该上线哪个 model?要用哪个 threshold 来把一笔交易标记为欺诈?”
Aisha 知道 accuracy 回答不了这个问题。只有 0.5% 的交易是欺诈, 所以一个总是说“不是欺诈”的 model 也能得到 99.5% 的 accuracy,但它完全 没用。她需要一个指标,能一次性在所有可能的 threshold 上 评价 model 的好坏。 这个指标就是 ROC Curve,以及它的汇总数值 AUC。
请留意这些互动框:Threshold Explorer(第 02 节)、ROC Step Builder(03)、 AUC Two Ways(05)、FPR Budget Comparison(06)、Separation Lab(07)、 Imbalance Lab(08)、Operating Point Picker(09)、One-vs-Rest Explorer(10) 和 Bootstrap Lab(13)。拖动滑块,点击 label,一步一步看计算过程。每个数字都是实时算出来的。
什么是 ROC Curve?
ROC Curve(Receiver Operating Characteristic Curve,受试者工作特征曲线)是一张图, 它显示 classifier 在每一个可能的分类 threshold 下的表现: 从 0.0(把所有交易都判为欺诈)到 1.0(一笔也不判为欺诈)。
在每个 threshold 下,model 都会得到一个不同的 confusion matrix。ROC curve 画出的是从这个矩阵里取出的两个数字:
当你降低欺诈 threshold(例如从 0.7 → 0.3),model 会标记更多交易。这样能抓到更多真正的欺诈(TPR 上升),但也会 错误地标记更多正常交易(FPR 也上升)。threshold 从 1 一路降到 0 时, ROC curve 正好画出了这种取舍。 完美的 model 能达到 TPR=1,同时保持 FPR=0。
动手试试:Threshold Explorer
这里有 30 笔交易:10 笔欺诈(上面一行)和 20 笔正常交易(下面一行),按欺诈 probability 摆放。 紫色线右边的交易都会被标记为欺诈。拖动滑块(或点击色条),看看 confusion matrix、TPR 和 FPR 怎么变化。每个 threshold 都会在 ROC 图上得到一个点。
Building the ROC Curve Step by Step
Let's use a tiny dataset — 10 transactions, 5 fraudulent (F) and 5 legitimate (L). A model has assigned each a fraud probability. We sort by probability descending and sweep the threshold:
| Rank | Transaction | Actual Label | Fraud Probability (p̂) | Action at this threshold |
|---|---|---|---|---|
| 1 | T-09 | Fraud (F) | 0.95 | ← Lower threshold to here → predict T-09 as Fraud |
| 2 | T-03 | Legit (L) | 0.88 | ← Predict T-09, T-03 as Fraud |
| 3 | T-07 | Fraud (F) | 0.82 | ← Predict top-3 as Fraud |
| 4 | T-01 | Fraud (F) | 0.71 | ← Predict top-4 as Fraud |
| 5 | T-06 | Legit (L) | 0.60 | ← Predict top-5 as Fraud |
| 6 | T-10 | Legit (L) | 0.45 | ← Predict top-6 as Fraud |
| 7 | T-04 | Fraud (F) | 0.38 | ← Predict top-7 as Fraud |
| 8 | T-02 | Legit (L) | 0.25 | ← Predict top-8 as Fraud |
| 9 | T-05 | Fraud (F) | 0.15 | ← Predict top-9 as Fraud |
| 10 | T-08 | Legit (L) | 0.05 | ← Predict all as Fraud |
Total Positives (Fraud): P = 5 |
Total Negatives (Legit): N = 5
At each rank boundary we record the cumulative TP, FP, and compute TPR = TP/5, FPR = FP/5:
| Threshold ≥ | Predicted as Fraud | TP | FP | FPR = FP/5 | TPR = TP/5 | ROC Point |
|---|---|---|---|---|---|---|
| 1.00 (nothing) | — | 0 | 0 | 0.00 | 0.00 | (0.00, 0.00) ← Start |
| 0.95 | T-09 F | 1 | 0 | 0.00 | 0.20 | (0.00, 0.20) |
| 0.88 | + T-03 L | 1 | 1 | 0.20 | 0.20 | (0.20, 0.20) |
| 0.82 | + T-07 F | 2 | 1 | 0.20 | 0.40 | (0.20, 0.40) |
| 0.71 | + T-01 F | 3 | 1 | 0.20 | 0.60 | (0.20, 0.60) |
| 0.60 | + T-06 L | 3 | 2 | 0.40 | 0.60 | (0.40, 0.60) |
| 0.45 | + T-10 L | 3 | 3 | 0.60 | 0.60 | (0.60, 0.60) |
| 0.38 | + T-04 F | 4 | 3 | 0.60 | 0.80 | (0.60, 0.80) |
| 0.25 | + T-02 L | 4 | 4 | 0.80 | 0.80 | (0.80, 0.80) |
| 0.15 | + T-05 F | 5 | 4 | 0.80 | 1.00 | (0.80, 1.00) |
| 0.00 (all) | + T-08 L | 5 | 5 | 1.00 | 1.00 | (1.00, 1.00) ← End |
When a Fraud case is added, the point moves straight up (TPR increases, FPR stays the same — good news). When a Legit case is added, the point moves straight right (FPR increases, TPR stays the same — bad news). A model that perfectly ranks all frauds above all legitimate transactions would reach (0, 1.0) before moving right at all — an AUC of 1.0.
Try It — ROC Step Builder
Rebuild the table above one row at a time. Press Next to lower the threshold past the next transaction. A fraud moves the point up; a legit moves it right. Click any Fraud/Legit label to change it, or try a perfect or a worst-possible ranking.
ROC Curve को Step by Step बनाना
चलिए एक बहुत छोटा dataset लेते हैं — 10 transactions, 5 fraud (F) और 5 legit (L)। एक model ने हर एक को fraud probability दी है। हम probability के हिसाब से बड़े से छोटे क्रम में sort करते हैं और threshold को नीचे खिसकाते हैं:
| Rank | Transaction | असली Label | Fraud Probability (p̂) | इस threshold पर क्या होता है |
|---|---|---|---|---|
| 1 | T-09 | Fraud (F) | 0.95 | ← Threshold यहाँ तक नीचे करो → T-09 को Fraud बताओ |
| 2 | T-03 | Legit (L) | 0.88 | ← T-09, T-03 को Fraud बताओ |
| 3 | T-07 | Fraud (F) | 0.82 | ← ऊपर के 3 को Fraud बताओ |
| 4 | T-01 | Fraud (F) | 0.71 | ← ऊपर के 4 को Fraud बताओ |
| 5 | T-06 | Legit (L) | 0.60 | ← ऊपर के 5 को Fraud बताओ |
| 6 | T-10 | Legit (L) | 0.45 | ← ऊपर के 6 को Fraud बताओ |
| 7 | T-04 | Fraud (F) | 0.38 | ← ऊपर के 7 को Fraud बताओ |
| 8 | T-02 | Legit (L) | 0.25 | ← ऊपर के 8 को Fraud बताओ |
| 9 | T-05 | Fraud (F) | 0.15 | ← ऊपर के 9 को Fraud बताओ |
| 10 | T-08 | Legit (L) | 0.05 | ← सबको Fraud बताओ |
कुल Positives (Fraud): P = 5 |
कुल Negatives (Legit): N = 5
हर rank की सीमा पर हम कुल TP, FP लिखते हैं, और TPR = TP/5, FPR = FP/5 निकालते हैं:
| Threshold ≥ | Fraud बताए गए | TP | FP | FPR = FP/5 | TPR = TP/5 | ROC बिंदु |
|---|---|---|---|---|---|---|
| 1.00 (कुछ नहीं) | — | 0 | 0 | 0.00 | 0.00 | (0.00, 0.00) ← शुरुआत |
| 0.95 | T-09 F | 1 | 0 | 0.00 | 0.20 | (0.00, 0.20) |
| 0.88 | + T-03 L | 1 | 1 | 0.20 | 0.20 | (0.20, 0.20) |
| 0.82 | + T-07 F | 2 | 1 | 0.20 | 0.40 | (0.20, 0.40) |
| 0.71 | + T-01 F | 3 | 1 | 0.20 | 0.60 | (0.20, 0.60) |
| 0.60 | + T-06 L | 3 | 2 | 0.40 | 0.60 | (0.40, 0.60) |
| 0.45 | + T-10 L | 3 | 3 | 0.60 | 0.60 | (0.60, 0.60) |
| 0.38 | + T-04 F | 4 | 3 | 0.60 | 0.80 | (0.60, 0.80) |
| 0.25 | + T-02 L | 4 | 4 | 0.80 | 0.80 | (0.80, 0.80) |
| 0.15 | + T-05 F | 5 | 4 | 0.80 | 1.00 | (0.80, 1.00) |
| 0.00 (सभी) | + T-08 L | 5 | 5 | 1.00 | 1.00 | (1.00, 1.00) ← अंत |
जब एक Fraud case जुड़ता है, तो बिंदु सीधा ऊपर जाता है (TPR बढ़ता है, FPR वही रहता है — अच्छी खबर)। जब एक Legit case जुड़ता है, तो बिंदु सीधा दाईं ओर जाता है (FPR बढ़ता है, TPR वही रहता है — बुरी खबर)। जो model सभी fraud को सभी legit transactions से ऊपर perfect तरीके से rank करे, वह दाईं ओर ज़रा भी जाने से पहले (0, 1.0) पर पहुँच जाएगा — यानी AUC 1.0।
खुद करके देखिए — ROC Step Builder
ऊपर वाली table को एक-एक row करके दोबारा बनाइए। आगे दबाइए ताकि threshold अगले transaction से नीचे चला जाए। एक fraud बिंदु को ऊपर ले जाता है; एक legit उसे दाईं ओर ले जाता है। किसी भी Fraud/Legit label पर click करके उसे बदलिए, या एक perfect या सबसे खराब ranking आज़माइए।
一步一步画出 ROC Curve
我们用一个很小的 dataset:10 笔交易,5 笔欺诈(F), 5 笔正常(L)。model 已经给每笔交易一个欺诈 probability。 我们按 probability 从高到低排序,然后逐步移动 threshold:
| 排名 | 交易 | 真实 label | 欺诈 probability (p̂) | 在这个 threshold 下的动作 |
|---|---|---|---|---|
| 1 | T-09 | 欺诈 (F) | 0.95 | ← 把 threshold 降到这里 → 把 T-09 预测为欺诈 |
| 2 | T-03 | 正常 (L) | 0.88 | ← 把 T-09、T-03 预测为欺诈 |
| 3 | T-07 | 欺诈 (F) | 0.82 | ← 把前 3 笔预测为欺诈 |
| 4 | T-01 | 欺诈 (F) | 0.71 | ← 把前 4 笔预测为欺诈 |
| 5 | T-06 | 正常 (L) | 0.60 | ← 把前 5 笔预测为欺诈 |
| 6 | T-10 | 正常 (L) | 0.45 | ← 把前 6 笔预测为欺诈 |
| 7 | T-04 | 欺诈 (F) | 0.38 | ← 把前 7 笔预测为欺诈 |
| 8 | T-02 | 正常 (L) | 0.25 | ← 把前 8 笔预测为欺诈 |
| 9 | T-05 | 欺诈 (F) | 0.15 | ← 把前 9 笔预测为欺诈 |
| 10 | T-08 | 正常 (L) | 0.05 | ← 全部预测为欺诈 |
positive 总数(欺诈):P = 5 |
negative 总数(正常):N = 5
在每个排名边界,我们记下累计的 TP、FP,并计算 TPR = TP/5,FPR = FP/5:
| threshold ≥ | 预测为欺诈 | TP | FP | FPR = FP/5 | TPR = TP/5 | ROC 点 |
|---|---|---|---|---|---|---|
| 1.00(一笔也没有) | — | 0 | 0 | 0.00 | 0.00 | (0.00, 0.00) ← 起点 |
| 0.95 | T-09 F | 1 | 0 | 0.00 | 0.20 | (0.00, 0.20) |
| 0.88 | + T-03 L | 1 | 1 | 0.20 | 0.20 | (0.20, 0.20) |
| 0.82 | + T-07 F | 2 | 1 | 0.20 | 0.40 | (0.20, 0.40) |
| 0.71 | + T-01 F | 3 | 1 | 0.20 | 0.60 | (0.20, 0.60) |
| 0.60 | + T-06 L | 3 | 2 | 0.40 | 0.60 | (0.40, 0.60) |
| 0.45 | + T-10 L | 3 | 3 | 0.60 | 0.60 | (0.60, 0.60) |
| 0.38 | + T-04 F | 4 | 3 | 0.60 | 0.80 | (0.60, 0.80) |
| 0.25 | + T-02 L | 4 | 4 | 0.80 | 0.80 | (0.80, 0.80) |
| 0.15 | + T-05 F | 5 | 4 | 0.80 | 1.00 | (0.80, 1.00) |
| 0.00(全部) | + T-08 L | 5 | 5 | 1.00 | 1.00 | (1.00, 1.00) ← 终点 |
每加入一笔欺诈交易,点就直接向上移动 (TPR 增加,FPR 不变,这是好消息)。每加入一笔正常 交易,点就直接向右移动(FPR 增加,TPR 不变,这是坏消息)。如果一个 model 把所有欺诈都完美地排在所有 正常交易前面,它会先到达 (0, 1.0),然后才向右移动,这时 AUC 等于 1.0。
动手试试:ROC Step Builder
一行一行地重建上面的表格。按下一步,把 threshold 降到下一笔交易之下。 欺诈让点向上走;正常交易让点向右走。点击任意一个 Fraud/Legit label 就能修改它, 也可以试试完美的 ranking 或最差的 ranking。
The ROC Curve — Visualised
Each vertical jump corresponds to catching a real fraud. Each horizontal jump is a false alarm on a legitimate transaction. AUC = 0.64 means this model ranks a random fraud above a random legitimate transaction 64% of the time.
Calculating AUC — The Trapezoidal Rule
AUC is the area under the ROC curve, computed using the trapezoidal rule: for each consecutive pair of points (FPR₁, TPR₁) and (FPR₂, TPR₂), the area of the trapezoid beneath them is:
Seg 1: (0.00, 0.20) → (0.20, 0.20): width = 0.20, avg TPR = 0.20
Area = 0.20 × 0.20 = 0.040
Seg 2: (0.20, 0.60) → (0.40, 0.60): width = 0.20, avg TPR = 0.60
Area = 0.20 × 0.60 = 0.120
Seg 3: (0.40, 0.60) → (0.60, 0.60): width = 0.20, avg TPR = 0.60
Area = 0.20 × 0.60 = 0.120
Seg 4: (0.60, 0.80) → (0.80, 0.80): width = 0.20, avg TPR = 0.80
Area = 0.20 × 0.80 = 0.160
Seg 5: (0.80, 1.00) → (1.00, 1.00): width = 0.20, avg TPR = 1.00
Area = 0.20 × 1.00 = 0.200
The model has an AUC of 0.64 — better than random (0.5) but far from a production-ready model (0.90+).
Try It — AUC Two Ways
Left: the trapezoid rule. Press Next segment to add the areas one at a time. Right: the pair view. Every fraud is compared with every legit transaction (5 × 5 = 25 pairs). A ✓ means the fraud got the higher score. Edit any score — both methods always give the same AUC.
ROC Curve — तस्वीर में
हर ऊपर की छलांग एक असली fraud पकड़ने का मतलब है। हर दाईं ओर की छलांग एक legit transaction पर झूठा alarm है। AUC = 0.64 का मतलब है कि यह model 64% बार एक random fraud को एक random legit transaction से ऊपर rank करता है।
AUC निकालना — Trapezoidal Rule
AUC, ROC curve के नीचे का area है। इसे trapezoidal rule से निकाला जाता है: लगातार आने वाले हर दो बिंदुओं (FPR₁, TPR₁) और (FPR₂, TPR₂) के लिए, उनके नीचे बने trapezoid का area है:
हिस्सा 1: (0.00, 0.20) → (0.20, 0.20): चौड़ाई = 0.20, औसत TPR = 0.20
Area = 0.20 × 0.20 = 0.040
हिस्सा 2: (0.20, 0.60) → (0.40, 0.60): चौड़ाई = 0.20, औसत TPR = 0.60
Area = 0.20 × 0.60 = 0.120
हिस्सा 3: (0.40, 0.60) → (0.60, 0.60): चौड़ाई = 0.20, औसत TPR = 0.60
Area = 0.20 × 0.60 = 0.120
हिस्सा 4: (0.60, 0.80) → (0.80, 0.80): चौड़ाई = 0.20, औसत TPR = 0.80
Area = 0.20 × 0.80 = 0.160
हिस्सा 5: (0.80, 1.00) → (1.00, 1.00): चौड़ाई = 0.20, औसत TPR = 1.00
Area = 0.20 × 1.00 = 0.200
इस model का AUC 0.64 है — random (0.5) से बेहतर, लेकिन production के लायक model (0.90+) से काफ़ी दूर।
खुद करके देखिए — AUC Two Ways
बाईं ओर: trapezoid rule। अगला टुकड़ा दबाइए और area एक-एक करके जोड़िए। दाईं ओर: जोड़ियों वाला view। हर fraud की तुलना हर legit transaction से होती है (5 × 5 = 25 जोड़ियाँ)। ✓ का मतलब है कि fraud को ज़्यादा score मिला। कोई भी score बदलिए — दोनों तरीके हमेशा एक ही AUC देते हैं।
ROC Curve 图解
每一次竖直跳动,表示抓到一笔真正的欺诈。每一次水平跳动,表示对一笔正常交易的误报。AUC = 0.64 表示:随机拿一笔欺诈和一笔正常交易,这个 model 有 64% 的情况会把欺诈排在前面。
计算 AUC:trapezoidal rule
AUC 是 ROC curve 下面的面积,用 trapezoidal rule(梯形法则)来计算:对每一对相邻的点 (FPR₁, TPR₁) 和 (FPR₂, TPR₂),它们下面的梯形面积是:
线段 1: (0.00, 0.20) → (0.20, 0.20):宽度 = 0.20,平均 TPR = 0.20
面积 = 0.20 × 0.20 = 0.040
线段 2: (0.20, 0.60) → (0.40, 0.60):宽度 = 0.20,平均 TPR = 0.60
面积 = 0.20 × 0.60 = 0.120
线段 3: (0.40, 0.60) → (0.60, 0.60):宽度 = 0.20,平均 TPR = 0.60
面积 = 0.20 × 0.60 = 0.120
线段 4: (0.60, 0.80) → (0.80, 0.80):宽度 = 0.20,平均 TPR = 0.80
面积 = 0.20 × 0.80 = 0.160
线段 5: (0.80, 1.00) → (1.00, 1.00):宽度 = 0.20,平均 TPR = 1.00
面积 = 0.20 × 1.00 = 0.200
这个 model 的 AUC 是 0.64:比随机(0.5)好, 但离可以上线的 model(0.90+)还差很远。
动手试试:AUC Two Ways
左边:trapezoidal rule。按下一段,一次加一块面积。 右边:配对视图。每笔欺诈都和每笔正常交易比较(5 × 5 = 25 对)。 ✓ 表示欺诈的 score 更高。随便修改一个 score,两种方法得到的 AUC 总是一样的。
Multi-Model Comparison — The Real Power of ROC
The true strength of ROC curves is comparing multiple models simultaneously, across all thresholds at once. Aisha plots all three of her models on the same axes:
The Gradient Boosting model dominates at every threshold — its curve is highest everywhere. Aisha should deploy this model. The Random Forest is a solid backup. Logistic Regression lags at higher FPR values, suggesting it misses more fraud at typical operating thresholds.
A model whose ROC curve is higher and to the left at your operating FPR is better. If Aisha's fraud team can investigate 10% of all transactions (FPR ≤ 0.10), she reads the y-value at FPR=0.10: Gradient Boost ≈ 78% recall, Random Forest ≈ 45% recall, Logistic Regression ≈ 20% recall. Clear winner.
Try It — FPR Budget Comparison
These are the three curves from the chart above. Set the false-alarm rate Aisha's team can afford (or click on the plot). The dots show how much fraud each model catches at that budget.
कई Models की तुलना — ROC की असली ताकत
ROC curves की असली ताकत है कई models की एक साथ तुलना, वो भी सभी thresholds पर एक साथ। आयशा अपने तीनों models को एक ही axes पर plot करती है:
Gradient Boosting model हर threshold पर सबसे आगे है — उसका curve हर जगह सबसे ऊपर है। आयशा को यही model deploy करना चाहिए। Random Forest एक अच्छा backup है। Logistic Regression ज़्यादा FPR values पर पीछे रह जाता है, यानी आम operating thresholds पर यह ज़्यादा fraud छोड़ देता है।
जिस model का ROC curve आपके operating FPR पर ज़्यादा ऊपर और बाईं ओर हो, वह बेहतर है। अगर आयशा की fraud team सभी transactions में से 10% की जाँच कर सकती है (FPR ≤ 0.10), तो वह FPR=0.10 पर y-value पढ़ती है: Gradient Boost ≈ 78% recall, Random Forest ≈ 45% recall, Logistic Regression ≈ 20% recall। साफ़ विजेता।
खुद करके देखिए — FPR Budget Comparison
ये ऊपर वाले chart के तीनों curves हैं। आयशा की team जितना false-alarm rate झेल सकती है, वह set कीजिए (या plot पर click कीजिए)। बिंदु दिखाते हैं कि उस budget पर हर model कितना fraud पकड़ता है।
多个 model 比较:ROC 真正的强项
ROC curve 真正的强项,是可以同时比较多个 model, 而且一次覆盖所有 threshold。Aisha 把她的三个 model 画在同一组坐标轴上:
Gradient Boosting model 在每个 threshold 下都领先,它的曲线在每个地方都最高。 Aisha 应该上线这个 model。Random Forest 是可靠的备选。Logistic Regression 在较高的 FPR 处落后,说明在常用的 threshold 下它会漏掉更多欺诈。
在你使用的 FPR 处,ROC curve 更高、更靠左的 model 更好。如果 Aisha 的反欺诈团队能调查所有交易的 10% (FPR ≤ 0.10),她就看 FPR=0.10 处的 y 值:Gradient Boost ≈ 78% recall, Random Forest ≈ 45% recall,Logistic Regression ≈ 20% recall。赢家很明显。
动手试试:FPR Budget Comparison
这是上图中的三条曲线。设置 Aisha 团队能承受的误报率 (或者直接点击图)。圆点显示在这个预算下,每个 model 能抓到多少欺诈。
Interpreting the AUC Score
- Equivalent to random guessing
- The diagonal line on the ROC plot
- Model has learned nothing useful
- Your floor — always beat this
- Poor discrimination
- Better than random but barely
- Revisit features & data quality
- Rarely production-worthy
- Good to very good discrimination
- Acceptable for many real problems
- Tune threshold to fit your domain
- Most production models live here
- Excellent discrimination
- Always check for data leakage
- Future data can always degrade this
- AUC = 1.0 → perfect (too perfect?)
An AUC below 0.5 is not automatically bad — it means the model is consistently wrong. Flip its predictions (predict 1 when it says 0) and you get a model with AUC = 1 − original AUC. This usually means a bug in your label encoding (0 and 1 swapped) or in your feature pipeline.
Try It — Separation Lab
Where does AUC come from? The blue hump is the scores of legit transactions; the red hump is fraud. Slide separation to pull them apart or push them together, and watch the ROC curve and AUC move through the bands above. Try a negative separation, then press Flip the model.
AUC Score का मतलब समझना
- Random अंदाज़ा लगाने के बराबर
- ROC plot पर diagonal line
- Model ने कुछ भी काम का नहीं सीखा
- यह आपकी सबसे निचली सीमा है — इससे हमेशा आगे रहिए
- कमज़ोर फ़र्क करने की क्षमता
- Random से बेहतर, पर बस थोड़ा-सा
- Features और data quality दोबारा जाँचिए
- Production के लायक शायद ही होता है
- अच्छी से बहुत अच्छी फ़र्क करने की क्षमता
- कई असली problems के लिए ठीक है
- अपने काम के हिसाब से threshold सेट कीजिए
- ज़्यादातर production models यहीं होते हैं
- बेहतरीन फ़र्क करने की क्षमता
- हमेशा data leakage की जाँच कीजिए
- आने वाला data इसे कभी भी गिरा सकता है
- AUC = 1.0 → perfect (कहीं ज़रूरत से ज़्यादा perfect तो नहीं?)
0.5 से कम AUC अपने-आप बुरा नहीं है — इसका मतलब है कि model लगातार गलत है। उसकी predictions उलट दीजिए (जब वह 0 कहे तब 1 बताइए) तो आपको AUC = 1 − पुराना AUC वाला model मिलता है। आमतौर पर इसका मतलब है कि आपकी label encoding में (0 और 1 आपस में बदल गए) या feature pipeline में कोई bug है।
खुद करके देखिए — Separation Lab
AUC आता कहाँ से है? नीला उभार legit transactions के scores हैं; लाल उभार fraud है। separation को खिसकाइए ताकि दोनों दूर हों या पास आएँ, और देखिए कैसे ROC curve और AUC ऊपर वाली श्रेणियों में चलते हैं। Negative separation आज़माइए, फिर Model उलटें दबाइए।
怎么解读 AUC score
- 等于随机乱猜
- 就是 ROC 图上的对角线
- model 没学到任何有用的东西
- 这是底线,一定要超过它
- 区分能力差
- 比随机好,但只好一点点
- 重新检查 feature 和数据质量
- 很少能直接上线
- 区分能力良好到很好
- 对很多实际问题来说可以接受
- 调整 threshold,适应你的业务领域
- 大多数上线的 model 都在这个范围
- 区分能力非常好
- 一定要检查有没有数据泄漏
- 未来的数据随时可能让它变差
- AUC = 1.0 → 完美(是不是太完美了?)
AUC 低于 0.5 不一定就是坏事,它说明 model 一直在犯同样的错。把它的 prediction 反过来(它说 0 时就预测 1), 你就得到一个 AUC = 1 − 原来 AUC 的 model。这通常说明你的 label 编码 有 bug(0 和 1 搞反了),或者 feature 处理流程有 bug。
动手试试:Separation Lab
AUC 从哪里来?蓝色的小山是正常交易的 score;红色的小山是欺诈的 score。 拖动 separation 滑块,把两座山拉开或推近,看看 ROC curve 和 AUC 怎么 在上面的各个区间里移动。试试负的 separation,然后按翻转 model。
ROC-AUC vs Precision-Recall AUC — When Each Wins
The ROC curve is not always the right tool. On severely imbalanced datasets — like fraud (0.1% positive rate) — the ROC curve can be misleadingly optimistic. The Precision-Recall (PR) curve is more honest in those cases.
| Axis | Formula | Uses |
|---|---|---|
| X (FPR) | FP / (FP + TN) | TN — the large negatives pool |
| Y (TPR / Recall) | TP / (TP + FN) | TP and FN only |
On imbalanced data, TN is huge. Even many FPs produce a tiny FPR, making the curve look excellent while Precision is terrible.
| Axis | Formula | Uses |
|---|---|---|
| X (Recall) | TP / (TP + FN) | No TN involved |
| Y (Precision) | TP / (TP + FP) | No TN involved |
Because TN never appears, PR-AUC is sensitive to imbalance. A bad model that generates many FPs will show up clearly here even when ROC-AUC looks fine.
| Situation | Use ROC-AUC | Use PR-AUC |
|---|---|---|
| Class ratio ~50:50 | ✓ Best choice | Also works |
| Mild imbalance (10:1) | ✓ Good choice | Also informative |
| Severe imbalance (100:1 or worse) | Can be misleading | ✓ Preferred |
| Comparing models across teams | ✓ Standard choice | Less universal |
| Care more about Precision | Less relevant | ✓ Shows precision clearly |
| Fraud / medical rare-event detection | Use with caution | ✓ Much more informative |
Confusion matrix: TP=40, FP=460, FN=10, TN=99 490
ROC-AUC: Excellent ~0.93 — because FPR = 460/99 950 = 0.0046 (tiny denominator).
Precision: 40/500 = 8% — for every 12 alerts sent to the fraud team, 11 are false alarms.
ROC says "great model." PR curve says "this model is drowning your fraud team in noise." In banking operations, PR-AUC is the decision metric.
Try It — Imbalance Lab
One model, 50 frauds. Now add more and more legit transactions with the slider — all the way to Aisha's bank with 99 950 legit per day. Watch the left plot (ROC) stay still while the right plot (precision-recall) sinks.
ROC-AUC बनाम Precision-Recall AUC — कब कौन जीतता है
ROC curve हमेशा सही tool नहीं होता। बहुत ज़्यादा imbalanced datasets पर — जैसे fraud (0.1% positive rate) — ROC curve ज़रूरत से ज़्यादा अच्छी तस्वीर दिखाकर धोखा दे सकता है। ऐसे मामलों में Precision-Recall (PR) curve ज़्यादा ईमानदार होता है।
| Axis | Formula | किसका इस्तेमाल |
|---|---|---|
| X (FPR) | FP / (FP + TN) | TN — negatives का बड़ा ढेर |
| Y (TPR / Recall) | TP / (TP + FN) | सिर्फ़ TP और FN |
Imbalanced data पर TN बहुत बड़ा होता है। बहुत सारे FP होने पर भी FPR छोटा रहता है, जिससे curve शानदार दिखता है जबकि Precision बहुत खराब होती है।
| Axis | Formula | किसका इस्तेमाल |
|---|---|---|
| X (Recall) | TP / (TP + FN) | TN शामिल नहीं |
| Y (Precision) | TP / (TP + FP) | TN शामिल नहीं |
क्योंकि TN कभी आता ही नहीं, PR-AUC imbalance को महसूस करता है। जो खराब model बहुत सारे FP बनाता है, वह यहाँ साफ़ दिख जाएगा, भले ही ROC-AUC ठीक लगे।
| स्थिति | ROC-AUC इस्तेमाल करें | PR-AUC इस्तेमाल करें |
|---|---|---|
| Class ratio ~50:50 | ✓ सबसे अच्छा विकल्प | यह भी चलता है |
| हल्का imbalance (10:1) | ✓ अच्छा विकल्प | यह भी जानकारी देता है |
| बहुत ज़्यादा imbalance (100:1 या उससे बुरा) | धोखा दे सकता है | ✓ इसे चुनें |
| अलग-अलग teams के models की तुलना | ✓ आम विकल्प | कम प्रचलित |
| Precision की ज़्यादा परवाह | कम काम का | ✓ precision साफ़ दिखाता है |
| Fraud / medical में दुर्लभ घटना पकड़ना | सावधानी से इस्तेमाल करें | ✓ कहीं ज़्यादा जानकारी देता है |
Confusion matrix: TP=40, FP=460, FN=10, TN=99 490
ROC-AUC: शानदार ~0.93 — क्योंकि FPR = 460/99 950 = 0.0046 (नीचे वाली संख्या बहुत बड़ी है)।
Precision: 40/500 = 8% — fraud team को भेजे गए हर 12 alerts में से 11 झूठे alarm हैं।
ROC कहता है "बढ़िया model"। PR curve कहता है "यह model आपकी fraud team को बेकार के alerts में डुबो रहा है।" Banking के काम में PR-AUC ही फ़ैसला करने वाला metric है।
खुद करके देखिए — Imbalance Lab
एक model, 50 fraud। अब slider से और ज़्यादा legit transactions जोड़ते जाइए — आयशा के bank तक, जहाँ हर दिन 99 950 legit होते हैं। देखिए कि बायाँ plot (ROC) वैसा ही रहता है, जबकि दायाँ plot (precision-recall) नीचे गिरता है।
ROC-AUC 与 Precision-Recall AUC:各自什么时候更好
ROC curve 并不总是合适的工具。在严重不平衡的 dataset 上, 比如欺诈(positive 比例只有 0.1%),ROC curve 可能乐观得让人误会。 这时 Precision-Recall (PR) curve 更诚实。
| 坐标轴 | 公式 | 用到 |
|---|---|---|
| X (FPR) | FP / (FP + TN) | TN:数量很大的 negative 群体 |
| Y (TPR / Recall) | TP / (TP + FN) | 只有 TP 和 FN |
在不平衡的数据上,TN 非常大。即使有很多 FP,FPR 也很小,让 曲线看起来很棒,但 Precision 其实很差。
| 坐标轴 | 公式 | 用到 |
|---|---|---|
| X (Recall) | TP / (TP + FN) | 不涉及 TN |
| Y (Precision) | TP / (TP + FP) | 不涉及 TN |
因为 TN 从不出现,所以 PR-AUC 对不平衡很敏感。一个产生很多 FP 的 坏 model,即使 ROC-AUC 看起来不错,在这里也会清楚地暴露出来。
| 情况 | 用 ROC-AUC | 用 PR-AUC |
|---|---|---|
| class 比例约 50:50 | ✓ 最佳选择 | 也可以 |
| 轻度不平衡(10:1) | ✓ 好的选择 | 也有参考价值 |
| 严重不平衡(100:1 或更差) | 可能误导人 | ✓ 首选 |
| 在不同团队之间比较 model | ✓ 标准选择 | 没那么通用 |
| 更关心 Precision | 关系不大 | ✓ 能清楚显示 precision |
| 欺诈 / 医疗罕见事件检测 | 谨慎使用 | ✓ 信息量大得多 |
Confusion matrix:TP=40,FP=460,FN=10,TN=99 490
ROC-AUC:非常好,约 0.93。因为 FPR = 460/99 950 = 0.0046(分母太大,所以结果很小)。
Precision:40/500 = 8%。反欺诈团队每收到 12 条警报,就有 11 条是误报。
ROC 说:“很棒的 model。”PR curve 说:“这个 model 正在用噪音淹没你的反欺诈团队。” 在银行业务中,PR-AUC 才是做决定的指标。
动手试试:Imbalance Lab
一个 model,50 笔欺诈。现在用滑块加入越来越多的正常交易,一直加到 Aisha 的银行那样, 每天 99 950 笔正常交易。你会看到左图(ROC)几乎不动,而右图(precision-recall)一直往下沉。
Choosing the Right Operating Point
The ROC curve shows all possible thresholds. Choosing which point to operate at requires a business decision. Three common strategies:
TPR − FPR. The point farthest from the diagonal.
Balanced — treats FP and FN equally. Good default when costs are unknown.
cost(FP)·FPR + cost(FN)·(1−TPR). Define the cost of
a false alarm vs a missed fraud. In fraud detection, missing a £10 000 fraud
costs far more than one wasted analyst hour.
Try It — Operating Point Picker
300 transactions, one model, one ROC curve. Pick a strategy and set its business numbers. The picker tries every threshold and keeps the best one. The right plot shows the score of each threshold.
सही Operating Point चुनना
ROC curve सभी संभव thresholds दिखाता है। किस बिंदु पर काम करना है, यह चुनने के लिए business का फ़ैसला चाहिए। तीन आम तरीके:
TPR − FPR को सबसे ज़्यादा करता है। Diagonal से सबसे दूर वाला बिंदु।
संतुलित — FP और FN को बराबर मानता है। जब cost पता न हो, तब अच्छा default है।
cost(FP)·FPR + cost(FN)·(1−TPR) को सबसे कम कीजिए। एक झूठे alarm और
एक छूटे हुए fraud की cost तय कीजिए। Fraud detection में, £10 000 का fraud छूट जाना
analyst के एक बर्बाद घंटे से कहीं ज़्यादा महँगा पड़ता है।
खुद करके देखिए — Operating Point Picker
300 transactions, एक model, एक ROC curve। एक तरीका चुनिए और उसके business नंबर set कीजिए। Picker हर threshold आज़माता है और सबसे अच्छा रखता है। दायाँ plot हर threshold का score दिखाता है।
选择合适的 operating point
ROC curve 显示了所有可能的 threshold。要选择在哪个点上运行, 需要做一个业务决定。三种常见的策略:
TPR − FPR 最大。也就是离对角线最远的点。
它很平衡,对 FP 和 FN 一视同仁。不知道成本时,它是很好的默认选择。
cost(FP)·FPR + cost(FN)·(1−TPR) 最小。先定义
一次误报和一次漏掉欺诈各自的成本。在欺诈检测中,漏掉一笔 £10 000 的欺诈,
代价远远大于浪费分析员一个小时。
动手试试:Operating Point Picker
300 笔交易,一个 model,一条 ROC curve。选一个策略,并设置它的业务数字。选择器会尝试 每一个 threshold,并保留最好的那个。右图显示每个 threshold 的 score。
Multi-Class ROC — One-vs-Rest (OvR)
For problems with more than two classes (e.g. classifying a tumour as Benign, Malignant Type A, or Malignant Type B), the standard binary ROC cannot be directly applied. The most common extension is One-vs-Rest (OvR):
Weighted-average AUC: weighted by class support — better for imbalanced multi-class.
scikit-learn:
roc_auc_score(y, proba, multi_class='ovr', average='macro')
Try It — One-vs-Rest Explorer
30 patients, three classes. The model gives every patient three probabilities. Pick a class: it becomes "positive" and the other two become "rest". Now it is a normal binary ROC. Compare the three AUCs and the two averages.
Multi-Class ROC — One-vs-Rest (OvR)
जिन problems में दो से ज़्यादा classes हों (जैसे किसी tumour को Benign, Malignant Type A, या Malignant Type B में बाँटना), वहाँ आम binary ROC सीधे नहीं लग सकता। इसका सबसे आम विस्तार है One-vs-Rest (OvR):
Weighted-average AUC: हर class के samples की गिनती से वज़न — imbalanced multi-class के लिए बेहतर।
scikit-learn:
roc_auc_score(y, proba, multi_class='ovr', average='macro')
खुद करके देखिए — One-vs-Rest Explorer
30 मरीज़, तीन classes। Model हर मरीज़ को तीन probabilities देता है। एक class चुनिए: वह "positive" बन जाती है और बाकी दो "rest" बन जाती हैं। अब यह एक आम binary ROC है। तीनों AUCs और दोनों औसतों की तुलना कीजिए।
multiclass ROC:One-vs-Rest (OvR)
如果问题有两个以上的 class(例如把肿瘤分为 良性、恶性 A 型或恶性 B 型), 标准的二分类 ROC 就不能直接使用。最常见的扩展方法 是 One-vs-Rest (OvR):
Weighted-average AUC:按每个 class 的样本数加权,更适合不平衡的 multiclass 问题。
scikit-learn:
roc_auc_score(y, proba, multi_class='ovr', average='macro')
动手试试:One-vs-Rest Explorer
30 位病人,三个 class。model 给每位病人三个 probability。选一个 class:它就变成“positive”, 另外两个变成“rest”(其余)。现在它就是一个普通的二分类 ROC。比较三个 AUC 和两种平均值。
Python — ROC Curve & AUC from Scratch
Python — ROC Curve & AUC शुरू से खुद बनाना
Python:从零实现 ROC Curve 和 AUC
import numpy as np
# ── Build ROC curve manually ─────────────────────────────────
def roc_curve_manual(y_true, y_scores):
"""Compute TPR and FPR at every unique threshold."""
thresholds = np.sort(np.unique(y_scores))[::-1] # descending
P = np.sum(y_true == 1) # total positives
N = np.sum(y_true == 0) # total negatives
tpr_list = [0.0]
fpr_list = [0.0]
for thresh in thresholds:
y_pred = (y_scores >= thresh).astype(int)
TP = np.sum((y_pred == 1) & (y_true == 1))
FP = np.sum((y_pred == 1) & (y_true == 0))
tpr_list.append(TP / P)
fpr_list.append(FP / N)
tpr_list.append(1.0)
fpr_list.append(1.0)
return np.array(fpr_list), np.array(tpr_list)
# ── AUC via trapezoidal rule ─────────────────────────────────
def auc_trapezoid(fpr, tpr):
"""Area under the ROC curve using np.trapz."""
return np.trapz(tpr, fpr) # integrates TPR over FPR
# ── 10-transaction example ───────────────────────────────────
y_true = np.array([1, 0, 1, 1, 0, 0, 1, 0, 1, 0])
y_scores = np.array([0.95, 0.88, 0.82, 0.71, 0.60,
0.45, 0.38, 0.25, 0.15, 0.05])
fpr, tpr = roc_curve_manual(y_true, y_scores)
auc = auc_trapezoid(fpr, tpr)
print("FPR points:", fpr.round(2))
print("TPR points:", tpr.round(2))
print(f"AUC : {auc:.4f}") # 0.6400
# ── Youden's J — find optimal threshold ──────────────────────
thresholds = np.sort(np.unique(y_scores))[::-1]
j_scores = tpr[1:-1] - fpr[1:-1] # TPR - FPR at each threshold
best_idx = np.argmax(j_scores)
best_thresh = thresholds[best_idx]
print(f"Best threshold (Youden's J): {best_thresh:.2f}")
print(f" TPR at best: {tpr[best_idx+1]:.2f} "
f"FPR at best: {fpr[best_idx+1]:.2f}")
Python — Full Pipeline with scikit-learn
Python — scikit-learn के साथ पूरी Pipeline
Python:用 scikit-learn 搭建完整流程
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.metrics import (
roc_curve, roc_auc_score,
precision_recall_curve, average_precision_score,
RocCurveDisplay
)
# ── 1. Create imbalanced dataset ─────────────────────────────
X, y = make_classification(
n_samples=5000, n_features=20,
weights=[0.95, 0.05], # 95% legit, 5% fraud
random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42, stratify=y
)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# ── 2. Train three models ────────────────────────────────────
models = {
'Logistic Regression' : LogisticRegression(C=1.0, max_iter=500),
'Random Forest' : RandomForestClassifier(n_estimators=100, random_state=42),
'Gradient Boosting' : GradientBoostingClassifier(n_estimators=100, random_state=42),
}
for name, model in models.items():
model.fit(X_train, y_train)
# ── 3. Compute & plot ROC curves ─────────────────────────────
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
colors = ['#6366f1', '#f59e0b', '#34d399']
for (name, model), color in zip(models.items(), colors):
y_proba = model.predict_proba(X_test)[:, 1]
# ROC
fpr, tpr, _ = roc_curve(y_test, y_proba)
auc = roc_auc_score(y_test, y_proba)
axes[0].plot(fpr, tpr, color=color, lw=2,
label=f"{name} (AUC={auc:.3f})")
# Precision-Recall
prec, rec, _ = precision_recall_curve(y_test, y_proba)
ap = average_precision_score(y_test, y_proba)
axes[1].plot(rec, prec, color=color, lw=2,
label=f"{name} (AP={ap:.3f})")
# ROC — diagonal baseline
axes[0].plot([0, 1], [0, 1], 'k--', label='Random (AUC=0.50)')
axes[0].set_xlabel('False Positive Rate')
axes[0].set_ylabel('True Positive Rate')
axes[0].set_title('ROC Curve')
axes[0].legend()
# PR — baseline (always-positive classifier)
baseline = y_test.sum() / len(y_test)
axes[1].axhline(y=baseline, color='k', linestyle='--',
label=f'Random (AP={baseline:.3f})')
axes[1].set_xlabel('Recall')
axes[1].set_ylabel('Precision')
axes[1].set_title('Precision-Recall Curve')
axes[1].legend()
plt.tight_layout()
plt.show()
# ── 4. Optimal threshold via Youden's J ──────────────────────
gb_proba = models['Gradient Boosting'].predict_proba(X_test)[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, gb_proba)
j_scores = tpr - fpr
best_idx = np.argmax(j_scores)
best_thresh = thresholds[best_idx]
print(f"Optimal threshold (Youden): {best_thresh:.4f}")
print(f" TPR = {tpr[best_idx]:.4f} FPR = {fpr[best_idx]:.4f}")
# ── 5. Multi-class AUC (if needed) ───────────────────────────
from sklearn.datasets import load_iris
from sklearn.multiclass import OneVsRestClassifier
X_mc, y_mc = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X_mc, y_mc, test_size=0.3)
ovr_model = OneVsRestClassifier(LogisticRegression(max_iter=500))
ovr_model.fit(X_tr, y_tr)
y_proba_mc = ovr_model.predict_proba(X_te)
macro_auc = roc_auc_score(y_te, y_proba_mc,
multi_class='ovr', average='macro')
print(f"Multi-class Macro AUC (Iris): {macro_auc:.4f}")
Common Mistakes with ROC & AUC
StandardScaler before the train-test split leaks test-set
statistics into training, inflating all metrics including AUC. Always fit on
train only, transform both.
CalibratedClassifierCV for well-calibrated probabilities.
roc_auc_score assumes label=1 is the positive class. If your rare
class is encoded as 0 and the majority as 1, AUC will be inverted (often <0.5).
Always verify your positive class label.
Try It — Bootstrap Lab
Two models tested on the same 200 transactions: AUC 0.872 vs 0.868. Is A really better? Resample the test set with replacement and recompute both AUCs, a thousand times. If the 95% interval of the difference contains 0, the gap is noise. Then switch to 2 000 test cases and run it again.
ROC & AUC में होने वाली आम गलतियाँ
StandardScaler को fit करने से test set के
आँकड़े training में घुस जाते हैं, और AUC समेत सभी metrics बढ़े हुए दिखते हैं। हमेशा सिर्फ़
train पर fit कीजिए, और दोनों को transform कीजिए।
CalibratedClassifierCV इस्तेमाल कीजिए।
roc_auc_score मानता है कि label=1 positive class है। अगर आपकी दुर्लभ
class 0 के रूप में encode है और बड़ी class 1 के रूप में, तो AUC उलट जाएगा (अक्सर <0.5)।
हमेशा अपना positive class label जाँचिए।
खुद करके देखिए — Bootstrap Lab
दो models एक ही 200 transactions पर test हुए: AUC 0.872 बनाम 0.868। क्या A सच में बेहतर है? Test set को replacement के साथ दोबारा sample कीजिए और दोनों AUCs फिर से निकालिए, हज़ार बार। अगर फ़र्क के 95% interval में 0 आता है, तो यह अंतर सिर्फ़ noise है। फिर 2 000 test cases पर switch कीजिए और दोबारा चलाइए।
使用 ROC 和 AUC 的常见错误
StandardScaler,会把 test 集的
统计信息泄漏到 training 中,让包括 AUC 在内的所有指标都虚高。一定要只在
train 上拟合,然后对两者都做转换。
CalibratedClassifierCV 得到校准良好的 probability。
roc_auc_score 默认 label=1 是 positive class。如果你的罕见
class 被编码为 0,多数 class 被编码为 1,AUC 就会反过来(常常 <0.5)。
一定要确认你的 positive class label。
动手试试:Bootstrap Lab
两个 model 在同样的 200 笔交易上测试:AUC 0.872 对 0.868。A 真的更好吗?对 test 集做有放回的 重抽样,重新计算两个 AUC,重复一千次。如果差值的 95% 区间包含 0, 这个差距就只是噪音。然后换成 2 000 个 test 样本,再运行一次。
Golden Rules
cross_val_score(model, X, y, scoring='roc_auc', cv=5).
A model with mean AUC = 0.89 ± 0.02 across folds is far more trustworthy
than one with a single holdout AUC of 0.92 but high variance.
सुनहरे नियम
cross_val_score(model, X, y, scoring='roc_auc', cv=5) इस्तेमाल कीजिए।
जिस model का folds में औसत AUC = 0.89 ± 0.02 हो, वह उस model से कहीं ज़्यादा भरोसेमंद है
जिसका एक अकेला holdout AUC 0.92 हो लेकिन variance ज़्यादा हो।
黄金法则
cross_val_score(model, X, y, scoring='roc_auc', cv=5)。
一个在各折上平均 AUC = 0.89 ± 0.02 的 model,远比
只有一次留出 AUC 为 0.92、但方差很大的 model 更可信。