Machine Learning Credit Card Fraud Detection Project

Machine Learning project 12

Machine Learning Credit Card Fraud Detection Project

Build and evaluate a complete credit card fraud detection workflow with reproducible Python code, explicit metrics, and honest limitations.

Explore Machine Learning training in VizagView all project ideas

Dataset and objective

A 12,000-row synthetic classification dataset with only about 0.5% positive fraud labels.

Skills practised

  • Severe class imbalance
  • Average precision
  • Class weighting
  • False-positive tradeoffs

Requirements

  • Python 3.10 or later
  • A terminal or command prompt
  • python -m pip install scikit-learn
  • About 45-60 minutes to build and review

Machine Learning workflow

  1. Data: A 12,000-row synthetic classification dataset with only about 0.5% positive fraud labels.
  2. Preprocessing: A stratified split preserves the rare class and StandardScaler is fitted only on training data.
  3. Model: Class-weighted LogisticRegression counteracts the majority-class dominance.
  4. Evaluation: Confusion matrix, average precision, and a classification report; accuracy is intentionally not the headline metric.

Complete Python code

Save the code as ml_credit_card_fraud_detection.py. The random state and data handling are included so the result can be reproduced and reviewed.

"""Evaluate fraud detection on an intentionally imbalanced synthetic dataset."""

from __future__ import annotations

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import average_precision_score, classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler


def train_model(random_state: int = 42):
    x, y = make_classification(
        n_samples=12000,
        n_features=24,
        n_informative=10,
        n_redundant=6,
        weights=[0.995, 0.005],
        class_sep=1.2,
        flip_y=0.001,
        random_state=random_state,
    )
    x_train, x_test, y_train, y_test = train_test_split(
        x, y, test_size=0.3, random_state=random_state, stratify=y
    )
    model = make_pipeline(
        StandardScaler(),
        LogisticRegression(max_iter=3000, class_weight="balanced", random_state=random_state),
    )
    model.fit(x_train, y_train)
    probabilities = model.predict_proba(x_test)[:, 1]
    predictions = (probabilities >= 0.5).astype(int)
    return model, y_test, predictions, probabilities


def main() -> None:
    _, y_test, predictions, probabilities = train_model()
    print("Synthetic Credit Card Fraud Detection")
    print("Confusion matrix:")
    print(confusion_matrix(y_test, predictions))
    print(f"Average precision: {average_precision_score(y_test, probabilities):.3f}")
    print(classification_report(y_test, predictions, zero_division=0))
    print("Accuracy alone is misleading for rare fraud; production decisions require investigation and controls.")


if __name__ == "__main__":
    main()

How the pipeline works

A stratified split preserves the rare class and StandardScaler is fitted only on training data.

Class-weighted LogisticRegression counteracts the majority-class dominance. Confusion matrix, average precision, and a classification report; accuracy is intentionally not the headline metric.

Run the project

  1. Create and activate a virtual environment.
  2. Install the dependencies with python -m pip install scikit-learn.
  3. Run python ml_credit_card_fraud_detection.py.
  4. Review every printed metric together with the limitation below; a single score never proves deployment readiness.

How to interpret the evaluation

Classification projects report class-aware metrics so majority classes do not hide weak performance. Regression projects report errors in target units and include R-squared or a simple baseline where appropriate. Always verify the split strategy matches how new data will arrive.

Accuracy, ethics, and safety limits

Synthetic education only. A production fraud score requires secure transaction data, investigation workflows, threshold economics, adversarial monitoring, privacy controls, and human review.

Ways to extend the project

Plot a precision-recall curve, tune the threshold by cost, test temporal validation, compare anomaly detection, and add analyst-review outcomes as feedback.

Continue learning Machine Learning

Try the next project, return to the Softenant project library, or explore the Machine Learning course in Vizag for guided data preparation, model evaluation, and portfolio feedback.