-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPredicting heart disease using Machine Learning.py
164 lines (120 loc) · 5.33 KB
/
Predicting heart disease using Machine Learning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
###########################################################################################################################
# Hassan Shahzad
# 18i-0441
# CS-D
# FAST-NUCES ISB
# chhxnshah@gmail.com
# "Predicting heart disease using Machine Learning"
# The following project was one of the guided projects offered by courera.
###########################################################################################################################
################################################# Predicting Heart Disease from Clinical and Laboratorial Data ################################################################
# Importing Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn import metrics
%matplotlib inline
# Loading the dataset
data = pd.read_csv('heart.csv')
data.head()
# Shape
data.shape
# Variable Types
data.dtypes
######################################################################## EDA and Pre-Processing #################################################################################
############################
## Outcome Variable Count ##
############################
# Creates a graph of patients having heart disease and those without it
sns.catplot(x = 'target', kind = 'count', palette= 'ch:.25', data = data)
# Categorical Predictive Variables
# sex
sns.catplot(x = 'sex', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
# cp:
sns.catplot(x = 'cp', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
# fbs:
sns.catplot(x = 'fbs', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
# restecg:
sns.catplot(x = 'restecg', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
# exang:
sns.catplot(x = 'exang', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
# slope:
sns.catplot(x = 'slope', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
# ca:
sns.catplot(x = 'ca', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
# thal:
sns.catplot(x = 'thal', kind = 'count', hue = 'target', data= data, palette = 'ch:.25')
#########################################
## Distributional Predictive Variables ##
#########################################
data[['age', 'trestbps', 'chol', 'thalach', 'oldpeak']].describe()
sns.displot(x='age', multiple = 'stack', hue = 'target', data= data, palette = 'ch:.25')
sns.displot(x='trestbps', multiple = 'stack', hue = 'target', data= data, palette = 'ch:.25')
sns.displot(x='chol', multiple = 'stack', hue = 'target', data= data, palette = 'ch:.25')
sns.displot(x='thalach', multiple = 'stack', hue = 'target', data= data, palette = 'ch:.25')
sns.displot(x='oldpeak', multiple = 'stack', hue = 'target', data= data, palette = 'ch:.25')
##################################
## Splitting and Pre-Processing ##
##################################
# Defining x_train, x_test, y-train and y_test
x = data.drop('target', axis =1)
y = data['target']
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.2, random_state = 42)
# Scaling the data
sc = StandardScaler().fit(x_train)
x_train = sc.transform(x_train)
x_test = sc.transform(x_test)
########################
## Training The Model ##
########################
# Parameters for grid search
knn = KNeighborsClassifier()
parameters = {'n_neighbors': [3,5,7,9,11], 'weights': ['uniform', 'distance']}
# Fiting training data and grid searching
grid = GridSearchCV(knn, parameters, cv = 4, scoring = 'accuracy')
grid.fit(x_train,y_train)
# Displaying best parameters
print(grid.best_params_)
# Picking the best model
model = grid.best_estimator_
##########################
## Evaluating the Model ##
##########################
# Model score on test data
model.score (x_test,y_test)
# Confusion (Prediction) Matrix
predictions = model.predict(x_test)
cm = metrics.confusion_matrix(y_test, predictions)
cm = pd.DataFrame(cm)
sns.heatmap(cm, annot=True)
plt.show()
# Calculating sensitivity, specificity, PPV and NPV
TP = 28
FP = 2
TN = 27
FN = 4
sensitivity = TP / (TP + FN) *100
specificity = TN / (TN + FP) * 100
ppv = TP / (TP + FP) * 100
npv = TN / (TN + FN) * 100
# Printing sensitivity, specificity, PPV and NPV
print('Sensitivity:', sensitivity,'% ','Specificity:', specificity,'% ','positive predictive value:',ppv,'% ','negative predictive value:',npv,'%' )
# AUC Score
probs = model.predict_proba(x_test)[:, 1]
auc = metrics.roc_auc_score(y_test, probs)
print(auc)
# ROC Curve
fpr, tpr, _ = metrics.roc_curve(y_test,probs)
plt.figure()
plt.grid()
plt.plot(fpr, tpr)
plt.plot([0, 1], [0, 1])
plt.show()
######################################################################################################################
################################################### THE END ##########################################################
######################################################################################################################