Home >Backend Development >Python Tutorial >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.
StandardScaler
(sklearn.preprocessing)fit_transform()
train_test_split()
model.predict()
model.predict_proba()
classification_report()
roc_auc_score()
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 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]
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!
<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.
<code class="language-python">print(data.info())</code>
This provides a summary of the dataset's structure and data types.
<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.
<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.
<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.
<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).
<code class="language-python">print(data.info())</code>
A Logistic Regression model is trained using the training data.
<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
.
<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!