Predicting Coronary Heart Disease¶
This tutorial will discuss how to build classification model and how to evaluate a model.
Topics covered in this tutorial
- Basic exploration of data before building models
- Encoding categorical features
- Splitting datasets into train and test datasets
- Build a Logistics Regression Model
- How logit funtion, log odds, odds and probabilities are related
- How to find probabilities from the logistic model
- Find overall accuracy of the model
- Understand Confusion matrix
- Understand TPR, FPR, Precision, Recall, Sensitivity & Speficity
- Understand ROC and how it is used
- Find optimal Cutoff probability
Here is an intersting problem of understanding what factors contribute to CHD and can CHD be predicted by building an analytical model.
The next two sections will introduce some basics of CHD, where the dataset is derived from and what are the attributes available in the dataset.
What is coronary heart disease?¶
Coronary heart disease (CHD) is when your coronary arteries (the arteries that supply your heart muscle with oxygen-rich blood) become narrowed by a gradual build-up of fatty material within their walls. These arteries can become narrowed through build-up of plaque, which is made up of cholesterol and other substances. Narrowed arteries can cause symptoms, such as chest pain (angina), shortness of breath, and fatigue.
Dataset Description¶
Data is avaialable at: http://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/ And header informtion is available at: http://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.info.txt
A retrospective sample of males in a heart-disease high-risk region of the Western Cape, South Africa. There are roughly two controls per case of CHD. Many of the CHD positive men have undergone blood pressure reduction treatment and other programs to reduce their risk factors after their CHD event. In some cases the measurements were made after these treatments. These data are taken from a larger dataset, described in Rousseauw et al, 1983, South African Medical Journal.
Import and load the dataset¶
import pandas as pd
import numpy as np
saheart_ds = pd.read_csv( "SAheart.data" )
saheart_ds.head()
saheart_ds.columns
saheart_ds.info()
The class label int the column chd indicates if the person has a coronary heart disease: negative (0) or positive (1).
Attributes description:
- sbp: systolic blood pressure
- tobacco: cumulative tobacco (kg)
- ldl: low densiity lipoprotein cholesterol
- adiposity: the size of the hips compared to the person's height
- famhist: family history of heart disease (Present, Absent)
- typea: type-A behavior
- obesity: BMI index
- alcohol: current alcohol consumption
- age: age at onset
import matplotlib.pyplot as plt
import seaborn as sn
%matplotlib inline
import missingno
missingno.matrix( saheart_ds )
There are no missing information. This is good news as we do not have to impute any data.
Exploratory Data Analysis¶
Number of observations available for people with CHD and without CHD¶
saheart_ds.chd.value_counts()
chd_df = pd.DataFrame( saheart_ds.chd.value_counts() )
chd_df
chd_df['has_chd'] = chd_df.index
chd_df
sn.barplot( x = 'has_chd', y = 'chd', data = chd_df )
famhist_chd = pd.crosstab( saheart_ds.famhist, saheart_ds.chd )
famhist_chd
famhist_chd = famhist_chd.unstack().reset_index()
famhist_chd
famhist_chd.columns = ['chd', 'famhist', 'total']
sn.barplot( famhist_chd.famhist,
famhist_chd.total,
hue = famhist_chd.chd )
Note:¶
It can be observed that the chances of CHD for people having family history is higher compared to people with no famility history.
How all the variable are inter-related?¶
We can draw a pair plot and understand the relationship between variables.
saheart_ds_sub = saheart_ds[['sbp', 'tobacco', 'ldl'
, 'adiposity', 'typea', 'obesity'
, 'alcohol', 'age', 'chd']]
sn.pairplot( saheart_ds_sub
, hue = "chd"
, palette="husl")