Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

Credit Card Fraud Detection

Analyze imbalanced fraud data, visualize separability, and build a baseline approach.

Goal

Fraud datasets are usually highly imbalanced.

You will:

  • Explore class imbalance
  • Check feature distributions
  • Build a baseline model evaluation plan

Analysis pipeline

diagram Fraud detection analysis pipeline mermaid
From raw transactions to a metric that actually matters for imbalanced data.

Step 1: Load

Python
import pandas as pd
 
df = pd.read_csv("data/fraud.csv")
print(df.shape)
print(df.head())

Step 2: Class imbalance

Python
print(df["fraud"].value_counts())
print(df["fraud"].value_counts(normalize=True))

Step 3: Visualize distributions

Python
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

sketch Class imbalance: legit vs fraud p5.js
The fraud bar is intentionally tiny - that is the whole challenge of this dataset.

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

quizCheck yourself
  1. Fraud is 0.2% of the rows. A model that always predicts not fraud gets what accuracy and why is it useless?

    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.

  2. Which call reveals the class imbalance quickly?

    Show answer

    A — df["fraud"].value_counts(normalize=True) — A normalized frequency table shows the share of legit versus fraud rows.

  3. Why does the KDE comparison use common_norm=False?

    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.

  4. Which set of metrics does the project prefer to accuracy?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Compare a feature by class

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Why accuracy misleads

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did