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
DDDOriginal
2025-01-18 22:14:11244browse

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

<code class="language-python">import pandas as pd

data = pd.read_csv('heart-disease-prediction.csv')
print(data.head())</code>

This loads the dataset using pandas.

Step 2: Exploratory Data Analysis (EDA)

<code class="language-python">print(data.info())</code>

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

Step 3: Handling Missing Data

<code class="language-python">print(data.isnull().sum())
data.fillna(data.mean(), inplace=True)
print(data.isnull().sum())</code>

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

Step 4: Data Preprocessing

<code class="language-python">X = data[['age', 'totChol','sysBP','diaBP', 'cigsPerDay','BMI','glucose']]
y = data['TenYearCHD']</code>

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

Step 5: Data Normalization

<code class="language-python">from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)</code>

Data is normalized using StandardScaler for improved model performance.

Step 6: Data Splitting

<code class="language-python">import pandas as pd

data = pd.read_csv('heart-disease-prediction.csv')
print(data.head())</code>

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

Step 7: Model Training

<code class="language-python">print(data.info())</code>

A Logistic Regression model is trained using the training data.

Step 8: Model Evaluation

<code class="language-python">print(data.isnull().sum())
data.fillna(data.mean(), inplace=True)
print(data.isnull().sum())</code>

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

Step 9: Model Prediction

<code class="language-python">X = data[['age', 'totChol','sysBP','diaBP', 'cigsPerDay','BMI','glucose']]
y = data['TenYearCHD']</code>

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

Additional patient data is provided for further practice:

<code class="language-python">from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)</code>

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:
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