-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDA.m
326 lines (257 loc) · 9.06 KB
/
EDA.m
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
%% Machine Learning Project (EDA Part)
%% Load the data
data = readtable('diabetes.csv');
rng(1); % For reproducibility
%% Exploratory Data Analysis
headers = {'Pregnancies','Glucose','BloodPressure',...
'SkinThickness','Insulin','BMI',...
'DiabetesPedigreeFunction','Age'};
% Data dimension
size(data)
% Dataframe contents
head(data)
% Check for missing values
sum(isempty(data))
% Who have diabetes
diabet = data(find(data.Outcome == 1),:);
head(diabet)
% Healthy
healthy = data(find(data.Outcome == 0),:);
head(healthy)
% Find how many missing value for every feature
sum(ismissing(diabet))
sum(ismissing(healthy))
% Replace zeros with avarage values (do it seperately)
diabet.Glucose(diabet.Glucose == 0) = mean(diabet.Glucose);
diabet.BloodPressure(diabet.BloodPressure == 0) = mean(diabet.BloodPressure);
diabet.SkinThickness(diabet.SkinThickness == 0) = mean(diabet.SkinThickness);
diabet.Insulin(diabet.Insulin == 0) = mean(diabet.Insulin); % should ask the teacher
diabet.BMI(diabet.BMI == 0) = mean(diabet.BMI);
healthy.Glucose(healthy.Glucose == 0) = mean(healthy.Glucose);
healthy.BloodPressure(healthy.BloodPressure == 0) = mean(healthy.BloodPressure);
healthy.SkinThickness(healthy.SkinThickness == 0) = mean(healthy.SkinThickness);
healthy.Insulin(healthy.Insulin == 0) = mean(healthy.Insulin); % should ask the teacher
healthy.BMI(healthy.BMI == 0) = mean(healthy.BMI);
% Convert tables to double for computations
diabetD = table2array(diabet);
healthyD = table2array(healthy);
% Bring together into data and convert
dataD = [diabetD; healthyD];
% Piechart to show either diabetes or non-diabetes
figure
pie([sum(dataD(:,end) == 1),sum(dataD(:,end) == 0)])
legend({'Diabetes','Healthy'})
title("Ratio of Patient's Conditions")
% histograms for every feature
% figure
% for i=1:size(dataD,2)
% subplot(3,3,i)
% histogram(dataD(:,i))
% title(headers{i})
% end
% Descriptive statistics for who have diabetes
% compute mean
mdataDD = mean(diabetD);
% compute the standard deviation
sigmaDD = std(diabetD);
% compute the median
mediandatDD = median(diabetD);
% Loop over every feauture for who have diabetes
% Initialize matrices
Q1 = zeros(1,size(diabetD,2));
Q2 = zeros(1,size(diabetD,2));
Q3 = zeros(1,size(diabetD,2));
Noutliers = zeros(1,size(diabetD,2));
for i=1:size(diabetD,2)
% rank-transform the data and scale to 1
y = tiedrank(diabetD(:,i)) / size(diabetD,1);
% find the values closest to 25% and 75% of the data
q1 = dsearchn(y,.25);
q3 = dsearchn(y,.75);
% get two values in the data
Q1(:,i) = diabetD(q1,i);
Q3(:,i) = diabetD(q3,i);
% compute Interquartile Range (IQR)
IQR = Q3(:,i)-Q1(:,i);
% Compute Semi Interquartile Deviation (SID)
SID = IQR/2;
% determine extreme Q1 outliers (e.g., x < Q1 - 3*IQR)
ix = find(diabetD(:,i)<Q1(:,i)-3*IQR);
if length(ix)>0
outliersQ1 = diabetD(ix,i);
else
outliersQ1 = [];
end
% determine extreme Q3 outliers (e.g., x > Q1 + 3*IQR)
iy = find(diabetD(:,i)>Q1(:,i)+3*IQR);
if length(iy)>0
outliersQ3 = diabetD(iy,i);
else
outliersQ3 = [];
end
% compute total number of outliers
Noutliers(:,i) = length(outliersQ1)+length(outliersQ3);
% replace outlier with mean data
diabetD(ix,i) = mdataDD(i);
diabetD(iy,i) = mdataDD(i);
end
% display results
disp(['Mean: ',num2str(mdataDD)]);
disp(['Standard Deviation: ',num2str(sigmaDD)]);
disp(['Median: ',num2str(mediandatDD)]);
disp(['Minimum Value: ',num2str(min(diabetD))]);
disp(['Maximum Value: ',num2str(max(diabetD))]);
disp(['25th Percentile: ',num2str(Q1)]);
disp(['50th Percentile: ',num2str(mediandatDD)]);
disp(['75th Percentile: ',num2str(Q3)]);
disp(['Semi Interquartile Deviation: ',num2str(SID)]);
disp(['Number of outliers: ',num2str(Noutliers)]);
% Descriptive statistics for who have not diabetes
% compute mean
mdataDH = mean(healthyD);
% compute the standard deviation
sigmaDH = std(healthyD);
% compute the median
mediandatDH = median(healthyD);
% Loop over every feauture for who have diabetes
% Initialize matrices
Q1 = zeros(1,size(healthyD,2));
Q2 = zeros(1,size(healthyD,2));
Q3 = zeros(1,size(healthyD,2));
Noutliers = zeros(1,size(healthyD,2));
for i=1:size(healthyD,2)
% rank-transform the data and scale to 1
y = tiedrank(healthyD(:,i)) / size(healthyD,1);
% find the values closest to 25% and 75% of the data
q1 = dsearchn(y,.25);
q3 = dsearchn(y,.75);
% get two values in the data
Q1(:,i) = healthyD(q1,i);
Q3(:,i) = healthyD(q3,i);
% compute Interquartile Range (IQR)
IQR = Q3(:,i)-Q1(:,i);
% Compute Semi Interquartile Deviation (SID)
SID = IQR/2;
% determine extreme Q1 outliers (e.g., x < Q1 - 3*IQR)
ix = find(healthyD(:,i)<Q1(:,i)-3*IQR);
if length(ix)>0
outliersQ1 = healthyD(ix,i);
else
outliersQ1 = [];
end
% determine extreme Q3 outliers (e.g., x > Q1 + 3*IQR)
iy = find(healthyD(:,i)>Q1(:,i)+3*IQR);
if length(iy)>0
outliersQ3 = healthyD(iy,i);
else
outliersQ3 = [];
end
% compute total number of outliers
Noutliers(:,i) = length(outliersQ1)+length(outliersQ3);
% replace outlier with mean data
healthyD(ix,i) = mdataDH(i);
healthyD(iy,i) = mdataDH(i);
end
% display results
disp(['Mean: ',num2str(mdataDH)]);
disp(['Standard Deviation: ',num2str(sigmaDH)]);
disp(['Median: ',num2str(mediandatDH)]);
disp(['Minimum Value: ',num2str(min(healthyD))]);
disp(['Maximum Value: ',num2str(max(healthyD))]);
disp(['25th Percentile: ',num2str(Q1)]);
disp(['50th Percentile: ',num2str(mediandatDH)]);
disp(['75th Percentile: ',num2str(Q3)]);
disp(['Semi Interquartile Deviation: ',num2str(SID)]);
disp(['Number of outliers: ',num2str(Noutliers)]);
% Visualize summary statistics with box plot
figure, boxplot(diabetD(:,1:end-1))
xticklabels(headers)
title('Diabets Data Statistics with patients')
figure, boxplot(healthyD(:,1:end-1))
xticklabels(headers)
title('Diabets Data Statistics with healthy people')
dataD = [diabetD;healthyD];
% Compute correlation matrix
cormat = corr(dataD(:,1:end-1));
cormatL = tril(cormat);
% Make the heatmap
figure
h =heatmap(headers,headers,cormatL)
h.Title = 'Correlation Matrix';
colormap jet
% Scatter Plots
figure
scatter(diabetD(:,8),diabetD(:,1))
hold on,
scatter(healthyD(:,8),healthyD(:,1))
legend({'Diabetes','Healthy'})
xlabel('Age'),ylabel('Pregnancies')
title('Age vs. Pregnancies')
figure
scatter(diabetD(:,2),diabetD(:,5))
hold on,
scatter(healthyD(:,2),healthyD(:,5))
legend({'Diabetes','Healthy'})
xlabel('Glucose'),ylabel('Insulin')
title('Glucose vs. Insulin')
figure
scatter(diabetD(:,6),diabetD(:,4))
hold on,
scatter(healthyD(:,6),healthyD(:,4))
legend({'Diabetes','Healthy'})
xlabel('BMI'),ylabel('SkinThickness')
title('BMI vs. SkinThickness')
%% Data Normalization and outliers
% z-score for who have disease
datazD = (diabetD-mdataDD) / sigmaDD;
mdatazD = mean(datazD);
sdatazD = std(datazD);
figure
plot(datazD,'s','markersize',8,'markerfacecolor','r')
xlabel('Data index'), ylabel('Data value')
title([ 'Mean = ' num2str(round(mdatazD,2)) '; std = ' num2str(round(sdatazD,2)) ])
% min-max scaling for who have disease
% get min and max
dataMinDD = min(diabetD);
dataMaxDD = max(diabetD);
% now min-max scale
dataSD = zeros(268,8);
for i=1:8
dataSD(:,i) = (diabetD(:,i)-dataMinDD(i)) / (dataMaxDD(i)-dataMinDD(i));
end
% min-max scaling for who have not disease
% get min and max
dataMinDH = min(healthyD);
dataMaxDH = max(healthyD);
% now min-max scale
dataSH = zeros(500,8);
for i=1:8
dataSH(:,i) = (healthyD(:,i)-dataMinDH(i)) / (dataMaxDH(i)-dataMinDH(i));
end
%% Normalization (different normalization methods)
% Normalize the diabetes data
% Determine max. values
max_diab = max(diabetD);
% Divide each column by their max. value
dataSD = bsxfun(@rdivide,diabetD,max_diab);
% Normalize the healthy data
% Determine max. values
max_heal = max(healthyD);
% Divide each column by their max. value
dataSH = bsxfun(@rdivide,healthyD,max_heal);
%% Feature Extraction
% Concatanete data
dataM = [dataSD;dataSH];
% Glucose-Insulin
F1 = dataM(:,2) .* dataM(:,5);
F1 = F1/max(F1);
% BMI-SkinThickness
F2 = dataM(:,6) .* dataM(:,4);
F2 = F2 / max(F2);
% Add features to the data
dataF = [dataM(:,1:end-1),F1,F2];
% Show data in a table format
dataT = array2table(dataF,'VariableNames',[headers {'Glucose/Insulin',...
'BMI/SkinThickness'}]);
head(dataT)
%% end.