Goal
Fraud datasets are usually highly imbalanced.
You will:
- Explore class imbalance
- Check feature distributions
- Build a baseline model evaluation plan
Analysis pipeline
flowchart LR A["Raw transactions"] --> B["Check imbalance
(value_counts)"] B --> C["Compare
(feature dist. by class)"] C --> D["Visualize
(kde/overlap)"] D --> E["Pick metric
(precision/recall/AUC)"]
Step 1: Load
import pandas as pd
df = pd.read_csv("data/fraud.csv")
print(df.shape)
print(df.head())Step 2: Class imbalance
print(df["fraud"].value_counts())
print(df["fraud"].value_counts(normalize=True))Step 3: Visualize distributions
import seaborn as sns
import matplotlib.pyplot as plt
# Example: compare one feature by class
feature = "amount"
plt.figure(figsize=(7, 4))
sns.kdeplot(data=df, x=feature, hue="fraud", common_norm=False)
plt.title(f"{feature} distribution by class")
plt.tight_layout()
plt.show()Visualize it
Step 4: Baseline modeling note
For fraud, accuracy is misleading.
Prefer:
- Precision / Recall
- F1 score
- ROC-AUC
- PR-AUC
Deliverable
- How imbalanced is the dataset?
- Which features differ between classes?
- What metric will you optimize?
Check your understanding
Fraud is 0.2% of the rows. A model that always predicts not fraud gets what accuracy and why is it useless?
Accuracy is dominated by the majority class, which is why the project says accuracy alone is the wrong metric.
Show answer
A — About 99.8%, but it catches no fraud at all — Accuracy is dominated by the majority class, which is why the project says accuracy alone is the wrong metric.
Which call reveals the class imbalance quickly?
A normalized frequency table shows the share of legit versus fraud rows.
Show answer
A — df["fraud"].value_counts(normalize=True) — A normalized frequency table shows the share of legit versus fraud rows.
Why does the KDE comparison use common_norm=False?
With a shared normalization the tiny class would be flattened and unreadable.
Show answer
A — So each class density is scaled on its own and a tiny fraud class is still visible — With a shared normalization the tiny class would be flattened and unreadable.
Which set of metrics does the project prefer to accuracy?
For rare-event detection, these metrics reflect how well the minority class is found.
Show answer
A — Precision, recall, F1, ROC-AUC and PR-AUC — For rare-event detection, these metrics reflect how well the minority class is found.
🧪 Try It Yourself
Exercise 1 – Measure class imbalance
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Compare a feature by class
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Why accuracy misleads
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.