-
Notifications
You must be signed in to change notification settings - Fork 27
/
MAR.py
569 lines (493 loc) · 27.4 KB
/
MAR.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env python3
print('starting importing')
# Parameters
datasets_name = ['pol', 'house_16H', 'MagicTelescope']
ms = 'MAR'
perc = 0.4
fold_splits = 5
# if status fast, MIDAS will be not executed otherwise, since the slower will be not executed
fast_track = True
print('fast track status:')
print(fast_track)
if fast_track:
print('when fast track active, MIDAS is not executed')
########### Description ################
# This script is for a small benchmarking of missing data
# It is using XGBoost as a model and different metrics to test effect of missing values
# All the algorithms are described in the correspective article, check it
# Correspective tutorial and others: https://github.com/SalvatoreRa/tutorial
# Feel free to use, in case you find useful for your research cite it
import pandas as pd
import xgboost as xgb
import time
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score, precision_score, recall_score
from sklearn.metrics import confusion_matrix, balanced_accuracy_score, cohen_kappa_score
from sklearn.metrics import average_precision_score, matthews_corrcoef, mean_absolute_error
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelBinarizer
from sklearn.experimental import enable_iterative_imputer # noqa
from sklearn.impute import IterativeImputer, KNNImputer, SimpleImputer
# Use wget download only once, you can just download from github and leave the utils_NA.py in the folder
#import wget
#wget.download('https://raw.githubusercontent.com/SalvatoreRa/tutorial/main/machine learning/utility/utils_NA.py')
from utils_NA import *
import torch
import seaborn as sns
import time
from sklearn.model_selection import KFold
from sklearn.impute import SimpleImputer
from datasets import load_dataset
from qolmat.imputations.diffusions.ddpms import TabDDPM
from qolmat.benchmark import metrics
from qolmat.imputations import imputers
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
import MIDASpy as md
print('import complete')
start_time = time.time()
measures = ['dataset', 'missing value', 'percentage', 'imputation', 'score', 'MAE', 'RMSR',
'Kullback-Leibler', 'Energy distance', 'Fréchet', 'Wasserstein', 'time']
results = pd.DataFrame( columns = measures)
for j in datasets_name:
#select the dataset
# you can find additional dataset in the repository
# here we are testing only three datasets but you can enlarge the comparison
data_dir = "https://raw.githubusercontent.com/SalvatoreRa/tutorial/main/datasets/"
df = pd.read_csv(data_dir+j +'.csv',sep=';')
X, y = df.iloc[:, :-1], df.iloc[:, -1]
lb = LabelBinarizer()
y =lb.fit_transform(y)
print(datasets_name)
# first we just start with the baseline
# here we use as a classifier XGBoost but you can change with another on
# we do 5 fold cross validation
kf = KFold(n_splits=fold_splits)
X = np.array(X)
for train_index, test_index in kf.split(X):
algo_time = time.time()
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train, y_train)
y_test_pred = clf.predict(X_test)
m = [j, 'none', 'none', 'none', accuracy_score(y_test, y_test_pred),
0, 0,0,0,0,0, time.time() - algo_time]
results.loc[len(results)] = m
# we are introducing missing data in different percentage
# feel free to change the percentage that you prefer
# here we are using just 4 for reducing time
print('missing data introduction')
for perc in [0.1, 0.3, 0.4, 0.5]:
print("The missing value percentage is {}".format(perc))
X_miss_mcar = produce_NA(df.iloc[:, :-1], p_miss=perc, mecha="MAR", p_obs=0.5)
X_miss_mcar = X_miss_mcar['X_incomp'].detach().numpy()
X_miss_mcar = np.where(X_miss_mcar=='nan', np.nan, X_miss_mcar )
kf = KFold(n_splits=fold_splits)
for train_index, test_index in kf.split(X_miss_mcar):
X_train, X_test = X_miss_mcar[train_index], X_miss_mcar[test_index]
y_train, y_test = y[train_index], y[test_index]
# we are starting now the process of imputation
# for each case we will measure the score
# the score represents the accuracy in this case
# we are also measure the total time needed from imputation to classification
# we are also measuring the accuracy of the reconstruction
# for example MAE is the absolute error when you compare the imputed and the
# original dataset
# Qolmat allows different measure so we will use different ones and store them
# for more information about these metrics: https://qolmat.readthedocs.io/en/latest/api.html#metrics
# check also here: https://dcor.readthedocs.io/en/latest/theory.html
print('Start imputation')
#### Imputing with mean
# we are using here just the mean, basically for each feature we use the mean value
# we are using scikit-learn simple imputer which is fast and used implementation
# more information about imputation with scikit-learn here: https://scikit-learn.org/stable/modules/impute.html
# additional information about simpleImputer: https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html
# Replace missing values using a descriptive statistic (e.g. mean, median, or most frequent) along each column, or using a constant value.
print('Imputation with Mean')
algo_time = time.time()
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
imp.fit(X_train)
X_train_imp =imp.transform(X_train)
X_test_imp =imp.transform(X_test)
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
# we are using different metrics to check here the difference between imputed
# and real dataset
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'Mean', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Imputing with the Median
# we are using here just the median, the principle is the same for the mean
# we are using the median because is common method
print('Imputation with Median')
imp = SimpleImputer(missing_values=np.nan, strategy='median')
imp.fit(X_train)
X_train_imp =imp.transform(X_train)
X_test_imp =imp.transform(X_test)
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'median', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Imputing with 0
# we are zero-imputation or called constant imputation
# you can use another value but it is common practice using the zero
print('Imputation with zero')
algo_time = time.time()
imp = SimpleImputer(missing_values=np.nan, strategy="constant", fill_value=0)
imp.fit(X_train)
X_train_imp =imp.transform(X_train)
X_test_imp =imp.transform(X_test)
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'zero', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Imputing with KNN imputer
# we are testing two different option for the KNNimputer one from scikit-learn
# this is the scikit-learn version
# Imputation for completing missing values using k-Nearest Neighbors.
# notice that the impiuter use the values from the neighbours
# additional information here: https://scikit-learn.org/stable/modules/generated/sklearn.impute.KNNImputer.html
print('Imputation with KNN - scikit')
algo_time = time.time()
imp = KNNImputer(missing_values=np.nan)
imp.fit(X_train)
X_train_imp =imp.transform(X_train)
X_test_imp =imp.transform(X_test)
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'KNN scikit', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Imputing with Iterative imputation
# The IterativeImputer class is very flexible - it can be used with a variety of estimators to do round-robin regression, treating every variable as an output in turn.
# Multivariate imputer that estimates each feature from all the others.
#A strategy for imputing missing values by modeling each feature with missing values as a function of other features in a round-robin fashion.
# This come from scikit-learn and can be seen as MICE
# for more info: https://scikit-learn.org/stable/modules/generated/sklearn.impute.IterativeImputer.html
# and also here: https://scikit-learn.org/stable/auto_examples/impute/plot_iterative_imputer_variants_comparison.htm
#
print('Imputation with Iterative imputer')
algo_time = time.time()
imp = IterativeImputer(random_state=0)
imp.fit(X_train)
X_train_imp =imp.transform(X_train)
X_test_imp =imp.transform(X_test)
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'Iterative', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Imputing with TabDDPM
# Diffusion model for tabular data based on Denoising Diffusion Probabilistic Models (DDPM)
# diffusion model, from Qolmat
# here for more infor: https://qolmat.readthedocs.io/en/latest/generated/qolmat.imputations.diffusions.ddpms.TabDDPM.html
# original article: https://arxiv.org/abs/2107.03502
#
#
print('Imputation with TabDDPM')
algo_time = time.time()
imp = TabDDPM()
#X_train_imp =imp.fit_transform(pd.DataFrame(X_train[:100,:]))
imp.fit(pd.DataFrame(X_train))
X_train_imp =imp.predict(pd.DataFrame(X_train[:,:]))
imp.fit(pd.DataFrame(X_test))
X_test_imp =imp.predict(pd.DataFrame(X_test[:,:]))
#X_test_imp =imp.fit_transform(pd.DataFrame(X_test[:100,:]))
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'TabDDPM', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Imputing with kNN imputer - fancyimputer
# alternative algorithm
#
print('Imputation with kNN - fancy imputer')
algo_time = time.time()
imp = imputers.ImputerKNN()
X_train_imp =imp.fit_transform(pd.DataFrame(X_train[:,:]))
X_test_imp =imp.fit_transform(pd.DataFrame(X_test[:,:]))
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'KNN 2', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Imputing with MIDAS
# MIDASpy is a Python package for multiply imputing missing data using deep learning methods.
# according to the authors: The MIDASpy algorithm offers significant accuracy and efficiency advantages over other multiple imputation strategies, particularly when applied to large datasets with complex features.
# here for additional info: https://github.com/MIDASverse/MIDASpy/tree/master
#
if not fast_track:
print('Imputation with MIDAS')
algo_time = time.time()
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_train_scaled = pd.DataFrame(X_train_scaled, columns =[str(i) for i in range(X_train.shape[1])] )
na_loc = X_train_scaled.isnull()
X_train_scaled[na_loc] = np.nan
imputer = md.Midas(layer_structure= [16,16], vae_layer = False, seed= 89, input_drop = 0.75)
imputer.build_model(X_train_scaled)
imputer.train_model(training_epochs = 15)
imputations = imputer.generate_samples(m=10).output_list
stacked_array = np.stack(imputations)
X_train_imp = np.mean(stacked_array, axis=0)
scaler = MinMaxScaler()
X_test_scaled = scaler.fit_transform(X_test)
X_test_scaled = pd.DataFrame(X_test_scaled, columns =[str(i) for i in range(X_test.shape[1])] )
na_loc = X_test_scaled.isnull()
X_test_scaled[na_loc] = np.nan
imputer = md.Midas(layer_structure= [16,16], vae_layer = False, seed= 89, input_drop = 0.75)
imputer.build_model(X_test_scaled)
imputer.train_model(training_epochs = 15)
imputations = imputer.generate_samples(m=10).output_list
stacked_array = np.stack(imputations)
X_test_imp = np.mean(stacked_array, axis=0)
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train_imp, y_train)
y_test_pred = clf.predict(X_test_imp)
mae = mean_absolute_error(np.array(df.iloc[:, :-1])[train_index], X_train_imp)
df_mask = np.array(np.full((X_train_imp.shape[0],
X_train_imp.shape[1],), True, dtype=bool))
RMSR = metrics.root_mean_squared_error(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
RMSR= np.sum(RMSR)
print(RMSR)
KL = metrics.kl_divergence(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
KL= np.sum(KL)
print(KL)
E = metrics.sum_energy_distances(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
E= np.sum(E)
print(E)
FR = metrics.frechet_distance(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
FR= np.sum(FR)
print(FR)
WA = metrics.dist_wasserstein(pd.DataFrame(np.array(df.iloc[:, :-1])[train_index]),
pd.DataFrame(X_train_imp), pd.DataFrame(df_mask))
WA= np.sum(WA)
print(WA)
m = [j, ms, perc, 'MIDAS', accuracy_score(y_test, y_test_pred), mae,
RMSR, KL, E, FR, WA, time.time() - algo_time]
results.loc[len(results)] = m
#### Training without imputing
# different algorithm are not sensible to NA
# for example decision tree are not sensible to the NA (they can treat them internally)
# since ensemble are decision tree, we can try the algorithm without imputation
#
#
print('algorithm not sensible to NA')
algo_time = time.time()
clf = xgb.XGBClassifier(random_state=42)
clf.fit(X_train, y_train)
y_test_pred = clf.predict(X_test)
m = [j, ms, perc, 'XGBoost internal', accuracy_score(y_test, y_test_pred),
0, 0,0,0,0,0, time.time() - algo_time]
results.loc[len(results)] = m
print('finished loop')
print('saving data')
# saving data
results.to_csv('missing_data_' + ms +'.csv')
print('plotting global')
# Plotting
# we are saving the global data in this case.
# we are dividing for percentage for better understand the trend
# we are saving a plot for each measure
for i in ['score', 'MAE', 'RMSR','Kullback-Leibler', 'Energy distance',
'Fréchet', 'Wasserstein', 'time']:
plot=sns.boxplot(data=results, x="imputation", y=i, hue ='percentage')
plt.xticks(rotation=45)
fig = plot.get_figure()
fig.savefig(ms+ '_'+i +'.jpeg')
plt.close()
print('plot local')
# Plotting
# we are saving the local data in this case.
# we are dividing for percentage for better understand the trend,
# a single plot for percentage
# we are saving a plot for each measure
for j in [0.1, 0.3, 0.4, 0.5]:
res = results[results['percentage']==j]
for i in ['score', 'MAE', 'RMSR','Kullback-Leibler', 'Energy distance',
'Fréchet', 'Wasserstein', 'time']:
plot=sns.boxplot(data=res, x="imputation", y=i)
plt.xticks(rotation=45)
fig = plot.get_figure()
fig.savefig(ms+ '_'+str(j)+'_'+i +'.jpeg')
plt.close()
print('script finished')
print("--- %s seconds ---" % (time.time() - start_time))