-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project2@HeartDieasePrediction.py
303 lines (153 loc) · 5.89 KB
/
Project2@HeartDieasePrediction.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
# coding: utf-8
# In[2]:
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn import metrics
import numpy as np
#import statsmodels.api as sm
#import scipy.stats as st
import seaborn as sn
from sklearn.metrics import confusion_matrix
#import matplotlib.mtab as mlab
get_ipython().run_line_magic('matplotlib', 'inline')
# In[3]:
heart_df = pd.read_csv('/Users/jaswanthjerripothula/Desktop/framingham.csv')
# In[4]:
heart_df.head()
# In[5]:
heart_df.tail()
# In[6]:
heart_df.isnull().sum()
# In[7]:
count = 0
for i in heart_df.isnull().sum(axis = 1):
if i > 0:
count = count + 1
print('Total number of rows with missing values is :: ',count)
print('It is ',round((count / len(heart_df.index))*100),'percent of the entire dataset the rows with missing values are imputed.')
# In[9]:
#Imputation :- The NAN or NULL values in the records will be replaced with proper values depending on the nature of data
#Find the null values
#repeat of 6
heart_df.isnull().sum()
# In[10]:
#repeat of 7
count = 0
for i in heart_df.isnull().sum(axis = 1):
if i > 0:
count = count + 1
print('Total number of rows with missing values is :: ',count)
print('Percentage of rows with missing values :: ',round((count / len(heart_df.index))*100),'%')
# In[11]:
#The data is missing is 14% of the total data which us high we need to impute the values
# In[16]:
heart_df['cigsPerDay'].isnull().sum()
# In[17]:
#There are 29 records with 'cigsPerDay' and we will replace them with mean value
import math
mean_value = heart_df['cigsPerDay'].mean()
mean_value = math.floor(mean_value)
heart_df['cigsPerDay'] = heart_df['cigsPerDay'].fillna(mean_value)
# In[18]:
#NuLL Values have been replaced by mean value
heart_df['cigsPerDay'].isnull().sum()
# In[19]:
#Find the education counts of different types
heart_df['education'].value_counts()
# In[20]:
#the education 1.0 is maximum and we can replace null values for education with modee value
heart_df['education'].fillna(heart_df['education'].mode()[0], inplace = True)
# In[21]:
heart_df['education'].value_counts()
# In[22]:
#Fill the null values in glucose column with mean value
mean_value = heart_df['glucose'].mean()
heart_df['glucose'] = heart_df['glucose'].fillna(mean_value)
# In[23]:
heart_df.isnull().sum()
# In[24]:
#Fill the BPmeds column which has null values with mode
heart_df['BPMeds'].fillna(heart_df['BPMeds'].mode()[0], inplace = True)
#Fill the heartRate column which has null values with mode
heart_df['heartRate'].fillna(heart_df['heartRate'].mode()[0], inplace = True)
# In[25]:
heart_df.isnull().sum()
# In[26]:
#Fill the BMI column which has null values with median
heart_df['BMI'] = heart_df.fillna(heart_df['BMI'].median())
#Fill the totChol column which has null values with mean
mean_value = heart_df['totChol'].mean()
heart_df['totChol'] = heart_df['totChol'].fillna(mean_value)
heart_df.isnull().sum()
# In[27]:
#Imputation is completed as we have no columns with null values in the data or records
# In[30]:
heart_df.head(10)
# In[31]:
heart_df.tail(10)
# In[37]:
def draw_histograms(dataframe, features, rows, cols):
fig = plt.figure(figsize = (20,20))
for i, feature in enumerate(features):
ax = fig.add_subplot(rows,cols,i+1)
dataframe[feature].hist(bins = 20, ax = ax, facecolor = 'midnightblue')
ax.set_title(feature+"Distribution",color = 'DarkRed')
fig.tight_layout()
plt.show()
# In[38]:
draw_histograms(heart_df, heart_df.columns ,6,3)
# In[39]:
heart_df.TenYearCHD.value_counts()
# In[40]:
sn.countplot(x = 'TenYearCHD',data = heart_df)
# In[41]:
heart_df.describe()
# In[42]:
import sklearn
new_features = heart_df[['age','male','cigsPerDay','totChol','sysBP','glucose','TenYearCHD']]
x = new_features.iloc[:,:-1]
y = new_features.iloc[:,-1]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.20, random_state = 5)
# In[45]:
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(x_train,y_train)
y_pred = logreg.predict(x_test)
# In[46]:
sklearn.metrics.accuracy_score(y_test,y_pred)
# In[47]:
#Get the confusion Matrix
cm = confusion_matrix(y_test,y_pred)
conf_matrix = pd.DataFrame(data = cm , columns = ['Predicted : 0','Predicted : 1'],
index = ['Actual : 0','Actual : 1'])
plt.figure(figsize = (8,5))
sn.heatmap(conf_matrix, annot=True , fmt = 'd', cmap = "YlGnBu")
# In[49]:
TP = cm[0,0]
FP = cm[0,1]
FN = cm[1,0]
TN = cm[1,1]
sensitivity = TP / float(TP + FN)
specificity = TN / float(TN + FP)
# In[59]:
print(
'The accuracy of the model = TP + TN / (TP + TN + FP + FN) :: ',(TP + TN)/ float(TP + TN + FP + FN),'\n',
'The Missclassification = 1 - Accuracy :: ',1 - ((TP + TN) / float(TP + TN + FP + FN)),'\n',
'Sensitivity or True Positive Rate = TP / (TP + FN) :: ',TP / float(TP + FN),'\n',
'Specificity or True Negative Rate = TN / (TN + FP) :: ',TN / float(TN + FP),'\n',
'Positive Predictive value = TP / (TP + FP) :: ',TP / float(TP + FP),'\n',
'Negative Predictive value = TN / (TN + FN) :: ',TN / float(TN + FN),'\n',
'Positive Likelihood Ratio = Sensitivity / (1 - specificity) :: ',sensitivity / (1 - specificity),'\n',
'Negative Likelihood Ratio = (1 - Sensitivity) / Specificity :: ',(1 - sensitivity) / specificity)
# In[64]:
from sklearn.metrics import classification_report, confusion_matrix
print('Confusion_matrix :: \n',confusion_matrix(y_test, y_pred))
print('Report :: \n',classification_report(y_test, y_pred))
# In[65]:
print(' Framing Ham Project completed successfully ')
# In[ ]: