Project goal
Detect fraudulent credit card transactions.
Why this project matters
- High financial risk
- Strong interview signal
- Shows imbalanced data handling
- Focus on recall over accuracy
Business problem
Fraud cases are rare. Missing fraud costs money. False alarms hurt customers. You balance both.
Dataset
Credit card transactions dataset. Target Class 0 genuine 1 fraud
Data reality
- Fraud less than 1 percent
- Accuracy becomes misleading
Tech stack
- Python
- Pandas
- NumPy
- Matplotlib
- Seaborn
- Scikit-learn
Step 1. Import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, classification_report, roc_auc_score
Step 2. Load data
df = pd.read_csv("creditcard.csv")
df.head()
Step 3. Basic checks
df.shape
df['Class'].value_counts()
Output example:
• Genuine 284315
• Fraud 492
Step 4. Data understanding
Check class imbalance:
sns.countplot(x='Class', data=df)
plt.show()
Insight Highly imbalanced dataset.
Step 5. Feature scaling
Scale Amount column:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df['Amount'] = scaler.fit_transform(df[['Amount']])
Drop Time.python
df.drop('Time', axis=1, inplace=True)
Step 6. Split features and target
X = df.drop('Class', axis=1)
y = df['Class']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
Step 7. Baseline model
Logistic Regression with class weight:
model = LogisticRegression(
max_iter=1000, class_weight='balanced'
)
model.fit(X_train, y_train)
Why class_weight
• Penalizes fraud mistakes more
• Improves recall
Step 8. Predictions
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:,1]
Step 9. Evaluation
Confusion matrix:
confusion_matrix(y_test, y_pred)
Classification report:
print(classification_report(y_test, y_pred))
ROC AUC:
roc_auc_score(y_test, y_prob)
Typical results
• Accuracy looks high but ignored
• Fraud recall improves sharply
• ROC AUC around 0.97
Step 10. Threshold tuning
Increase fraud recall:
y_pred_custom = (y_prob > 0.3).astype(int)
confusion_matrix(y_test, y_pred_custom)
Business logic Lower threshold catches more fraud. More false alerts accepted.
Step 11. Advanced approach
Random Forest:
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(
n_estimators=100, class_weight='balanced', random_state=42
)
rf.fit(X_train, y_train)
rf_prob = rf.predict_proba(X_test)[:,1]
roc_auc_score(y_test, rf_prob)
Resume bullet example
- Built fraud detection model on highly imbalanced data
- Improved fraud recall using class weighting and threshold tuning
- Evaluated model using ROC AUC instead of accuracy
Interview explanation flow
- Explain imbalance problem
- Why accuracy fails
- Why recall matters
- How threshold changes business impact
Mini task for you
- Apply SMOTE
- Compare with Isolation Forest
- Plot Precision Recall curve
Double Tap ♥️ For More