-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parkinsons project_V1_2.py
369 lines (194 loc) · 8.31 KB
/
Parkinsons project_V1_2.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
# coding: utf-8
# # 1.Data Preprocessing
# <div class="alert alert-block alert-info">
# <b>Load dataset and import librairies
# </div>
# ### Import NumPy for numerical calculation, Pandas for handling data and visualization with Seaborn and Matplotlib
# In[1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
from pylab import rcParams
rcParams['figure.figsize']=(12,6)
sns.set
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
warnings.filterwarnings("ignore", category=DeprecationWarning)
# ### Import the dataset
# In[91]:
parkinsons_data=pd.read_csv('parkinsons.csv')
# In[92]:
# Using shape function, we can observe the dimensions of the data
parkinsons_data.shape
# <font color='green'>There are 24 columns and 195 observations</font>
# In[93]:
# We can observe the dataset using the head()function, which returns the first five records from the dataset
parkinsons_data.head()
# <font color='green'>This project is a classification problem, from which we predict the binary variable "status" which can either be ill with Parkinson disease or not </font>
# In[94]:
# The info() method shows some of the characteristics of the data
parkinsons_data.info()
# <font color='green'>We can see that we have mainly float or numeric data except for "name" and "status" columns</font>
# <div class="alert alert-block alert-info">
# <b>Statistical insights
# </div>
# In[95]:
parkinsons_data.describe()
# <div class="alert alert-block alert-info">
# <b>Exploratory Data Analysis (EDA)
# </div>
# #### Let's build subplots of the features with boxplot or whisker plot to see the minimum, 1srt quantile, median and max.
# #### Each time the feature is related to the status (0 or 1 for healthy or Parkinson disease)
# In[96]:
fig, axes=plt.subplots(5,5,figsize=(15,15))
axes=axes.flatten()
for i in range(1,len(parkinsons_data.columns)-1):
sns.boxplot(x='status', y=parkinsons_data.iloc[:,i], data=parkinsons_data, orient='v', ax=axes[i])
plt.tight_layout()
plt.show()
# <font color='green'>Thanks to these subplots we can easily see the outliers, the points that lie outside the whiskers.</font>
# In[97]:
# Let's have a closer look to the "PPE" variable
parkinsons_data.boxplot(column=['PPE'])
plt.show
# There are some outliers between 0.4 and 0.5 and beyond 0.5
# <div class="alert alert-block alert-info">
# <b></b> Data Cleaning
# ### Removing duplicates and finding missing values are important, otherwise our models can lead us to incorrect conclusions
# In[98]:
duplicate_Values=parkinsons_data.duplicated()
print(duplicate_Values.sum())
parkinsons_data[duplicate_Values]
# <font color='green'>There are no duplicate variables</font>
# In[99]:
print(parkinsons_data.isnull().sum())
# <font color='green'>Luckily, this dataset does not contain any missing values</font>
# <div class="alert alert-block alert-info">
# <b>Correlation Analysis
# </div>
# In[100]:
corr= parkinsons_data.corr()
corr
# <font color='green'>We don't have a clear visualization</font>
# ### We create a heatmap using Seaborn
# In[101]:
sns.heatmap(parkinsons_data.corr(),annot=True,cmap='RdYlGn')
# <font color='green'>As it shown, it is not hard to find following pairs of highly correlated features:<br> Spread1 and PPE = 0,96</font>
# ### For a better visualization let's see below
# In[129]:
sns.heatmap(corr[(corr>0.9)],annot=True,cmap='PuBu')
# <font color='green'>We can also check the correlation between "spread1" and "PPE" variables thanks to "regplot" which plots the scartterplot plus the fitted regression line for the data.</font>
# In[130]:
sns.regplot(x='spread1', y='PPE', data=parkinsons_data)
# ### Let's go deeper with the P-value to know the significance of the correlation estimate when p-value is <br> < 0.001 leads to strong evidence <br> < 0.05 leads to moderate evidence <br> < 0.1 leads to weak evidence <br> > 0.1 leads to no evidence
# In[131]:
from scipy import stats
# In[132]:
pearson_coef, p_value = stats.pearsonr(parkinsons_data['spread1'], parkinsons_data['PPE'])
print ("The Pearson Correlation coefficient is", pearson_coef, " with a P-value of P =", p_value)
# <font color='green'>The linear relationship is strong between "spread1" and "PPE"(app. 0.96) and the correlation is statistically very significant</font>
# # 2.Modeling
# <div class="alert alert-block alert-info">
# <b>Let's explore some models, tune it to optimize its performance with the better predictability rate
# </div>
# In[133]:
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn import tree
from os import system
from sklearn import metrics
# In[134]:
# As we have seen previously, we can remove the non-numeric columns "name" and "status"
X = parkinsons_data.drop(['status', 'name'], axis = 1)
Y = parkinsons_data.status
# ### Decision Tree Model
# In[135]:
# Splitting Data into 70% Training data and 30% Testing Data
X_train, X_test, y_train, y_test = train_test_split(X, Y,train_size=0.7, test_size=0.3, random_state=42)
print(len(X_train)),print(len(X_test))
# In[136]:
# Applying decision tree model
decision_tree = DecisionTreeClassifier(criterion='entropy',max_depth=6,random_state=100,min_samples_leaf=5)
decision_tree.fit(X_train, y_train)
decision_tree.score(X_test , y_test)
# In[137]:
y_pred = decision_tree.predict(X_test)
confusion_matrix(y_test,y_pred)
# In[138]:
count_misclassified = (y_test != y_pred).sum()
print('Misclassified samples in Decision Tree: {}'.format(count_misclassified))
# <font color='green'>We've got 9 outliers samples</font>
# ### Random Forest
# In[139]:
randomforest = RandomForestClassifier(n_estimators = 50)
randomforest = randomforest.fit(X_train, y_train)
y_pred = randomforest.predict(X_test)
randomforest.score(X_test , y_test)
# In[140]:
count_misclassified = (y_test != y_pred).sum()
print('Misclassified samples in Random Forest: {}'.format(count_misclassified))
# <font color='green'>We've got 4 outliers samples</font>
# In[141]:
feature_imp = pd.Series(randomforest.feature_importances_,index=X.columns).sort_values(ascending=False)
feature_imp
# Creating a bar plot
sns.barplot(x=feature_imp, y=feature_imp.index)
# Add labels to your graph
plt.xlabel('Feature Importance Score')
plt.ylabel('Features')
plt.title("Visualizing Important Features")
plt.legend()
plt.show()
# ### KNN
# In[142]:
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
knn.score(X_test,y_test)
# In[143]:
y_pred = knn.predict(X_test)
count_misclassified = (y_test != y_pred).sum()
print('Misclassified samples in KNN: {}'.format(count_misclassified))
# <font color='green'>We've got 6 outliers samples</font>
# ### ADABoost
# In[144]:
adb = AdaBoostClassifier( n_estimators= 50)
adb = adb.fit(X_train,y_train)
y_pred = adb.predict(X_test)
adb.score(X_test , y_test)
# In[145]:
count_misclassified = (y_test != y_pred).sum()
print('Misclassified samples in Ada Boosting: {}'.format(count_misclassified))
# <font color='green'>We've got 9 outliers samples</font>
# In[146]:
from sklearn.dummy import DummyClassifier
from sklearn.metrics import plot_confusion_matrix
clf_dummy= DummyClassifier(random_state=42)
clf_dummy.fit(X_train, y_train)
y_pred = clf_dummy.predict(X_test)
plot_confusion_matrix(estimator=clf_dummy, X=X_test, y_true=y_test, normalize='true', cmap='Blues')
# In[147]:
y_train.value_counts(normalize=True)
# <font color='green'>We have more people with Parkinson disease than people with not Parkinson <br> So the dummy classifier is predicting more people with the disease</font>
# # Performance evaluation
# <div class="alert alert-block alert-info">
# <b>With the confusion matrix we can have some metrics such as the accuracy, F1 score and the precision
# </div>
# In[148]:
print(accuracy_score(y_test, y_pred))
# In[149]:
cm = confusion_matrix(y_test, y_pred)
print (cm)
# In[150]:
print(classification_report(y_test, y_pred))