Card Fraud Detection in Python: Imbalanced Classes, Precision-Recall and a Cost-Based Threshold
<p>Tamarind Bank Ltd's rules engine stops every card transaction scoring 60 or more. Most of its alerts turn out to be genuine customers, and most fraud still gets through. Your job is to decide which transactions to stop, and to prove the decision in rupees.</p><p>You get your own copy of the bank's data: three weeks of card transactions with the outcome, and the next week's without it. You build the analysis and the model yourself, lesson by lesson. Then you give the same job to your AI assistant (ChatGPT, Claude, Gemini or Copilot) and check what it gives back. Bartman runs your script in a sealed sandbox and scores your predictions and decisions against what next week's transactions actually turned out to be. Neither you, your AI nor the sandbox ever sees those outcomes.</p><ul><li>Clean a real-world export: repeated rows, a hand-typed label, disputes that were not fraud, rupee amounts in Indian grouping, and why 97% accuracy means nothing on <strong>imbalanced classes</strong>.</li><li>Score a rules engine with <strong>precision, recall, ROC AUC and average precision</strong>, and read the PR curve against the fraud rate.</li><li>Price every cutoff (every alert costs, every missed fraud costs its amount) and find the <strong>cost-minimising threshold</strong>, not the F1 one.</li><li>Build features that exist at swipe time and catch <strong>leakage</strong>: a column recorded after the fraud.</li><li>Cross-validate logistic regression and gradient boosting, keep probabilities true to the base rate, and turn them into stop/let-through decisions by <strong>expected cost</strong>.</li></ul><p>Every learner gets different data and a different alert cost, so answers can't be copied. Each check explains what went wrong without giving the answer away, and the classic mistakes (precision and recall swapped, the trapezoid AP, charging only false alarms, a leaky feature, flagging everyone) are recognised and named.</p>
Tools: Python, pandas, scikit-learn, Imbalanced Classification, Precision-Recall, Average Precision, Cost-Sensitive Thresholds, Logistic Regression, Gradient Boosting, Data Leakage, matplotlib, AI Assistants for Python