Machine Learning Breast Cancer Prediction Project

Machine Learning project 05

Machine Learning Breast Cancer Prediction Project

Build and evaluate a complete breast cancer prediction workflow with reproducible Python code, explicit metrics, and honest limitations.

Explore Machine Learning training in VizagView all project ideas

Dataset and objective

Scikit-learn’s Wisconsin Diagnostic Breast Cancer teaching dataset with 569 samples and 30 numeric features.

Skills practised

  • Medical dataset handling
  • Class-weighted models
  • ROC AUC
  • Clinical-safety boundaries

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: Scikit-learn’s Wisconsin Diagnostic Breast Cancer teaching dataset with 569 samples and 30 numeric features.
  2. Preprocessing: A stratified holdout split and StandardScaler inside a pipeline prevent test-data leakage.
  3. Model: Class-weighted LogisticRegression provides a transparent classification baseline.
  4. Evaluation: Confusion matrix, class-specific precision/recall/F1, and ROC AUC from holdout probabilities.

Complete Python code

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

"""Evaluate a classifier on scikit-learn's breast-cancer teaching dataset."""

from __future__ import annotations

from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score
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):
    dataset = load_breast_cancer()
    x_train, x_test, y_train, y_test = train_test_split(
        dataset.data,
        dataset.target,
        test_size=0.25,
        random_state=random_state,
        stratify=dataset.target,
    )
    model = make_pipeline(
        StandardScaler(),
        LogisticRegression(max_iter=5000, class_weight="balanced", random_state=random_state),
    )
    model.fit(x_train, y_train)
    predictions = model.predict(x_test)
    probabilities = model.predict_proba(x_test)[:, 1]
    return model, dataset, y_test, predictions, probabilities


def main() -> None:
    _, dataset, y_test, predictions, probabilities = train_model()
    print("Breast Cancer Dataset Classification")
    print("Classes:", dict(enumerate(dataset.target_names)))
    print("Confusion matrix:")
    print(confusion_matrix(y_test, predictions))
    print(f"ROC AUC: {roc_auc_score(y_test, probabilities):.3f}\n")
    print(classification_report(y_test, predictions, target_names=dataset.target_names, zero_division=0))
    print("Education only: this model is not clinically validated and must not diagnose patients.")


if __name__ == "__main__":
    main()

How the pipeline works

A stratified holdout split and StandardScaler inside a pipeline prevent test-data leakage.

Class-weighted LogisticRegression provides a transparent classification baseline. Confusion matrix, class-specific precision/recall/F1, and ROC AUC from holdout probabilities.

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_breast_cancer_prediction.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

Education only. This public teaching dataset and model are not clinically validated and must never diagnose, reassure, triage, or guide treatment for a patient.

Ways to extend the project

Add repeated cross-validation, calibration, feature-coefficient review, uncertainty analysis, and a documented clinician-led validation plan for research contexts.

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.