Menu
CoddyTech

Card Fraud Watch

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 49 of 51.

A credit union's card team can check only a few payments a day by hand. Last month, the frauds hid at the edges:

Last month's 300 card payments, hour of the day across and amount up on a log scale: gray honest payments fill the daytime middle, red crosses mark 12 reported frauds, tiny ones at night and very large ones at any hour

But nobody knows a fraud until a customer complains, weeks later. So two detectors must find the odd payments with no labels at all.

This week's complaints are in. Which detector would have caught more?

challenge icon

Challenge

Hard

This week's 400 payments are in payments.csv; the program reads how many the team can check, budget.

  1. Scale the four features.
  2. Flag budget payments with an isolation forest (random_state=0) and with a local outlier factor (20 neighbors).
  3. Only now open fraud: print what each list caught, and how many honest payments it had the team check.
  4. Send the team the list that caught more: the isolation forest's list or the local outlier factor's list, else either list, they tie.

For last month and a budget of 10:

Budget: 10 of 300 payments
Isolation forest: caught 6 of 12 frauds, 4 honest payments checked
Local outlier factor: caught 10 of 12 frauds, 0 honest payments checked
Send the team: the local outlier factor's list

Try it yourself

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor

# One row per card payment; fraud is 1 for the ones customers reported weeks later
payments = pd.read_csv("payments.csv")
features = ["amount_eur", "hour", "km_from_home", "minutes_since_last"]

# How many payments the team can check by hand
budget = int(input())

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler