Heart Disease Analysis Data Science Project

Data Science project 09

Heart Disease Analysis Data Science Project

Build a complete heart disease analysis workflow with reproducible Python code, traceable calculations, validation checks, and honest limitations.

Explore Data Science training in VizagView all project ideas

Analysis question

How can group summaries and correlations be computed while clearly separating a teaching exercise from medical evidence?

Dataset

Nine hundred seeded synthetic patient-like rows with age, cholesterol, resting blood pressure, maximum heart rate, smoking flag, and a generated outcome.

Requirements

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

Method and data checks

Calculate the synthetic outcome rate, compare group means, and rank univariate correlations with the generated outcome.

  • Every patient-like row is synthetic
  • Group counts reconcile to the full dataset
  • The outcome is excluded from its own correlation list
  • Correlation is described as association, not cause

Complete Python code

Save the program as ds_heart_disease_analysis.py. The dataset generator or loader, analysis functions, output, and reproducibility controls are included.

"""Descriptive analysis on synthetic heart-risk teaching data."""

from __future__ import annotations

import numpy as np
import pandas as pd


def make_demo_patients(n: int = 900, seed: int = 42) -> pd.DataFrame:
    rng = np.random.default_rng(seed)
    age = rng.integers(30, 81, n)
    cholesterol = np.clip(rng.normal(210 + 0.45 * (age - 50), 35, n), 110, 390)
    resting_bp = np.clip(rng.normal(124 + 0.35 * (age - 50), 15, n), 85, 210)
    max_hr = np.clip(rng.normal(190 - 0.75 * age, 13, n), 70, 205)
    smoker = rng.binomial(1, 0.24, n)
    linear = -7.0 + 0.055 * age + 0.009 * (cholesterol - 180) + 0.02 * (resting_bp - 110) - 0.012 * (max_hr - 130) + 0.7 * smoker
    probability = 1 / (1 + np.exp(-linear))
    outcome = rng.binomial(1, np.clip(probability, 0.01, 0.95))
    return pd.DataFrame({"age": age, "cholesterol": cholesterol.round(1), "resting_bp": resting_bp.round(1), "max_hr": max_hr.round(1), "smoker": smoker, "synthetic_outcome": outcome})


def analyse_patients(data: pd.DataFrame) -> dict[str, object]:
    grouped = data.groupby("synthetic_outcome").agg(patients=("age", "size"), mean_age=("age", "mean"), mean_cholesterol=("cholesterol", "mean"), mean_resting_bp=("resting_bp", "mean"), smoker_rate=("smoker", "mean")).reset_index()
    correlations = data.corr(numeric_only=True)["synthetic_outcome"].drop("synthetic_outcome").sort_values(key=abs, ascending=False)
    return {"outcome_rate": float(data["synthetic_outcome"].mean()), "groups": grouped, "correlations": correlations}


def main() -> None:
    result = analyse_patients(make_demo_patients())
    print(f"Synthetic outcome rate: {result['outcome_rate']:.1%}")
    print(result["groups"].round(2).to_string(index=False))
    print("\nCorrelations:\n", result["correlations"].round(3).to_string())
    print("Education only: this synthetic analysis cannot diagnose or estimate a person's risk.")


if __name__ == "__main__":
    main()

Run the project

  1. Create and activate a virtual environment.
  2. Install dependencies with python -m pip install numpy pandas.
  3. Run python ds_heart_disease_analysis.py.
  4. Reconcile row counts and totals before interpreting patterns.
  5. Read the limitations before substituting any real dataset.

Expected analytical output

A synthetic outcome rate, group profile table, and ranked numeric correlations.

Interpretation and responsible-use limits

Education only. This generated data cannot diagnose heart disease, estimate individual risk, reassure a patient, or guide treatment. Medical analysis requires appropriate clinical datasets, governance, validation, and qualified oversight.

Ways to extend the project

Use a documented research dataset, audit missingness and selection bias, report uncertainty, examine confounding, and design analysis with clinical and statistical reviewers.

Continue learning Data Science

Try the next project, return to the Softenant project library, or explore the Data Science course in Vizag for guided data cleaning, analysis, visualisation, and portfolio feedback.