Home > Backend Development > Python Tutorial > Project - Supervised Learning with Python - Lets use Logistic Regression for Predicting the chances of having a Heart Attack

Project - Supervised Learning with Python - Lets use Logistic Regression for Predicting the chances of having a Heart Attack

DDD
Release: 2025-01-18 22:14:11
Original
286 people have browsed it

Project - Supervised Learning with Python - Lets use Logistic Regression for Predicting the chances of having a Heart Attack

This tutorial demonstrates a machine learning project using Python and the LogisticRegression algorithm to predict the likelihood of a heart attack. The dataset, sourced from Kaggle, is analyzed to build a predictive model.

Key Concepts:

  • Logistic Regression
  • StandardScaler (sklearn.preprocessing)
  • fit_transform()
  • train_test_split()
  • model.predict()
  • model.predict_proba()
  • classification_report()
  • roc_auc_score()

Project Goal:

This project aims to illustrate the practical application of Logistic Regression in predicting heart attack risk based on patient data. We'll leverage Python's capabilities to build and evaluate this predictive model.

The Jupyter Notebook and dataset are available here:

Notebook: https://www.php.cn/link/aa3f874fb850d8908be9af3a69af4289

Dataset: https://www.php.cn/link/4223a1d5b9e017dda51515829140e5d2 (Kaggle source: https://www.php.cn/link/5bb77e5c6d452aee283844d47756dc05)

Future Plans:

Future tutorials will explore additional machine learning concepts, focusing on supervised and unsupervised learning, as outlined in this Kaggle roadmap: https://www.php.cn/link/4bea9e07f447fd088811cc81697a4d4e [#Machine Learning Engineer Roadmap for 2025]

Target Audience:

This tutorial is designed for Python enthusiasts interested in learning machine learning, particularly those new to the field. It builds upon a previous tutorial covering Linear Regression.

Feel free to experiment with the notebook and explore different machine learning models!

Step-by-Step Guide:

Step 1: Data Loading

import pandas as pd

data = pd.read_csv('heart-disease-prediction.csv')
print(data.head())
Copy after login
Copy after login

This loads the dataset using pandas.

Step 2: Exploratory Data Analysis (EDA)

print(data.info())
Copy after login
Copy after login

This provides a summary of the dataset's structure and data types.

Step 3: Handling Missing Data

print(data.isnull().sum())
data.fillna(data.mean(), inplace=True)
print(data.isnull().sum())
Copy after login
Copy after login

Missing values are identified and filled using the mean of each column.

Step 4: Data Preprocessing

X = data[['age', 'totChol','sysBP','diaBP', 'cigsPerDay','BMI','glucose']]
y = data['TenYearCHD']
Copy after login
Copy after login

Relevant features (X) and the target variable (y) are selected.

Step 5: Data Normalization

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)
Copy after login
Copy after login

Data is normalized using StandardScaler for improved model performance.

Step 6: Data Splitting

import pandas as pd

data = pd.read_csv('heart-disease-prediction.csv')
print(data.head())
Copy after login
Copy after login

The dataset is split into training and testing sets (80/20 split).

Step 7: Model Training

print(data.info())
Copy after login
Copy after login

A Logistic Regression model is trained using the training data.

Step 8: Model Evaluation

print(data.isnull().sum())
data.fillna(data.mean(), inplace=True)
print(data.isnull().sum())
Copy after login
Copy after login

The model's performance is evaluated using the classification_report and roc_auc_score.

Step 9: Model Prediction

X = data[['age', 'totChol','sysBP','diaBP', 'cigsPerDay','BMI','glucose']]
y = data['TenYearCHD']
Copy after login
Copy after login

The trained model is used to predict heart disease risk for a new patient.

Additional patient data is provided for further practice:

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)
Copy after login
Copy after login

The above is the detailed content of Project - Supervised Learning with Python - Lets use Logistic Regression for Predicting the chances of having a Heart Attack. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template