-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline5.py
153 lines (135 loc) · 7.17 KB
/
pipeline5.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
import os
from time import localtime, strftime
import numpy as np
import matplotlib.pyplot as plt
from sklearn.impute import KNNImputer
from build.class_gain import GAIN
from build.class_misgan import MisGAN
from build.utils import normalization, load_dataset
np.random.seed(666)
MISS_RATE = 0.2
LIST_DATASETS = ["breast", "credit", "letter", "news", "spam", "wine_red", "wine_white"]
NB_NEIGHBOURS = 50 # For 1000 rows
NB_EPOCHS_GAIN = 20000 # For 1000 rows
NB_EPOCHS_MISGAN = 5000 # For 1000 rows
NB_REPEAT_TRAIN = 20
NB_REPEAT_IMPUTATION = 50
rmse_gain = np.zeros((NB_REPEAT_TRAIN, len(LIST_DATASETS)))
rmse_misgan = np.zeros((NB_REPEAT_TRAIN, len(LIST_DATASETS)))
rmse_knn1 = np.zeros((NB_REPEAT_TRAIN, len(LIST_DATASETS)))
rmse_knn2 = np.zeros((NB_REPEAT_TRAIN, len(LIST_DATASETS)))
for i1 in range(NB_REPEAT_TRAIN):
print(f"\n== Repeat {i1} ==", flush=True)
for i2 in range(len(LIST_DATASETS)):
dataset_name = LIST_DATASETS[i2]
original_data = load_dataset(dataset_name)
original_data = np.array(original_data, dtype="float32")
mult_fact = original_data.shape[0] / 1000.0 # Multiplicative factor for epochs and nb_neighbours
print(f"-> Dataset = {dataset_name} {original_data.shape} | Time: ", end="", flush=True)
print(strftime("%Y-%m-%d %H:%M:%S", localtime()), flush=True)
r = np.random.uniform(size=original_data.shape)
miss_mask = (r < MISS_RATE)
nb_miss_val = np.sum(miss_mask)
miss_data = np.copy(original_data)
miss_data[miss_mask] = np.nan
norm_data, norm_params = normalization(miss_data)
norm_full_data, _ = normalization(original_data, norm_params=norm_params)
myGAIN = GAIN(dim=original_data.shape[1]) # For GAIN and MisGAN
myMisGAN = MisGAN(dim=original_data.shape[1])
cur_gain_epochs = int(NB_EPOCHS_GAIN / mult_fact)
cur_misgan_epochs = int(NB_EPOCHS_MISGAN / mult_fact)
print(f"Train GAIN for {cur_gain_epochs} epochs...")
myGAIN.train(norm_data, batch_size=128, epochs=cur_gain_epochs)
print(f"Train MisGAN for {cur_misgan_epochs} epochs...")
myMisGAN.train(norm_data, batch_size=128, epochs=cur_misgan_epochs)
impute_gain = np.zeros_like(miss_data)
impute_misgan = np.zeros_like(miss_data)
for i3 in range(NB_REPEAT_IMPUTATION):
impute_gain += myGAIN.impute(norm_data)
impute_misgan += myMisGAN.impute(norm_data)
impute_gain /= NB_REPEAT_IMPUTATION
impute_misgan /= NB_REPEAT_IMPUTATION
rmse_gain[i1, i2] = np.sqrt(np.sum((impute_gain - norm_full_data) ** 2) / nb_miss_val)
rmse_misgan[i1, i2] = np.sqrt(np.sum((impute_misgan - norm_full_data) ** 2) / nb_miss_val)
print("KNN starts... ", end="") # For KNN
print(strftime("%Y-%m-%d %H:%M:%S", localtime()))
cur_knn_neighbours = int(NB_NEIGHBOURS * mult_fact)
print(f"KNN uses {cur_knn_neighbours} neighbours")
myKNN1 = KNNImputer(n_neighbors=cur_knn_neighbours, weights="uniform", metric="nan_euclidean")
myKNN1.fit(norm_data)
impute_knn1 = myKNN1.transform(norm_data)
myKNN2 = KNNImputer(n_neighbors=cur_knn_neighbours, weights="distance", metric="nan_euclidean")
myKNN2.fit(norm_data)
impute_knn2 = myKNN2.transform(norm_data)
rmse_knn1[i1, i2] = np.sqrt(np.sum((impute_knn1 - norm_full_data) ** 2) / nb_miss_val)
rmse_knn2[i1, i2] = np.sqrt(np.sum((impute_knn2 - norm_full_data) ** 2) / nb_miss_val)
if not os.path.exists("results/pipeline5/"):
os.makedirs("results/pipeline5/")
if not os.path.exists("results/pipeline5/plots"):
os.makedirs("results/pipeline5/plots")
np.save("results/pipeline5/rmse_gain.npy", rmse_gain)
np.save("results/pipeline5/rmse_misgan.npy", rmse_misgan)
np.save("results/pipeline5/rmse_knn1.npy", rmse_knn1)
np.save("results/pipeline5/rmse_knn2.npy", rmse_knn2)
# Plot 1: Complete performances for everyone
mu1 = np.mean(rmse_gain, axis=0)
std1 = np.std(rmse_gain, axis=0)
mu2 = np.mean(rmse_misgan, axis=0)
std2 = np.std(rmse_misgan, axis=0)
mu3 = np.mean(rmse_knn1, axis=0)
std3 = np.std(rmse_knn1, axis=0)
mu4 = np.mean(rmse_knn2, axis=0)
std4 = np.std(rmse_knn2, axis=0)
x_ticks_locs = np.arange(0.0, 7.0)
plt.figure(figsize=(8, 4))
plt.errorbar(x_ticks_locs-0.15, mu1, yerr=std1, color="indigo", marker=".", capsize=5.0, ls="", label="GAIN")
plt.errorbar(x_ticks_locs-0.05, mu2, yerr=std2, color="grey", marker=".", capsize=5.0, ls="", label="MisGAN")
plt.errorbar(x_ticks_locs+0.05, mu3, yerr=std3, color="teal", marker=".", capsize=5.0, ls="", label="KNN-uniform")
plt.errorbar(x_ticks_locs+0.15, mu4, yerr=std4, color="peru", marker=".", capsize=5.0, ls="", label="KNN-distance")
plt.xticks(x_ticks_locs, LIST_DATASETS, rotation ="vertical")
plt.ylabel("RMSE")
plt.title("Missing rate 20% (MCAR)")
plt.legend()
plt.tight_layout()
plt.savefig("results/pipeline5/plots/rmse_datasets1.pdf")
# Plot 2: Remove the bad performances for the dataset News
mu1 = np.mean(rmse_gain, axis=0)[[0, 1, 2, 4, 5, 6]]
std1 = np.std(rmse_gain, axis=0)[[0, 1, 2, 4, 5, 6]]
mu2 = np.mean(rmse_misgan, axis=0)[[0, 1, 2, 4, 5, 6]]
std2 = np.std(rmse_misgan, axis=0)[[0, 1, 2, 4, 5, 6]]
mu3 = np.mean(rmse_knn1, axis=0)[[0, 1, 2, 4, 5, 6]]
std3 = np.std(rmse_knn1, axis=0)[[0, 1, 2, 4, 5, 6]]
mu4 = np.mean(rmse_knn2, axis=0)[[0, 1, 2, 4, 5, 6]]
std4 = np.std(rmse_knn2, axis=0)[[0, 1, 2, 4, 5, 6]]
x_ticks_locs = np.arange(0.0, 6.0)
x_ticks_names = ["breast", "credit", "letter", "spam", "wine_red", "wine_white"]
plt.figure(figsize=(8, 4))
plt.errorbar(x_ticks_locs-0.15, mu1, yerr=std1, color="indigo", marker=".", capsize=5.0, ls="", label="GAIN")
plt.errorbar(x_ticks_locs-0.05, mu2, yerr=std2, color="grey", marker=".", capsize=5.0, ls="", label="MisGAN")
plt.errorbar(x_ticks_locs+0.05, mu3, yerr=std3, color="teal", marker=".", capsize=5.0, ls="", label="KNN-uniform")
plt.errorbar(x_ticks_locs+0.15, mu4, yerr=std4, color="peru", marker=".", capsize=5.0, ls="", label="KNN-distance")
plt.xticks(x_ticks_locs, x_ticks_names, rotation ="vertical")
plt.ylabel("RMSE")
plt.title("Missing rate 20% (MCAR)")
plt.legend()
plt.tight_layout()
plt.savefig("results/pipeline5/plots/rmse_datasets2.pdf")
# Plot 3: Remove the performances of MisGAN (only GAIN vs KNN)
mu1 = np.mean(rmse_gain, axis=0)[[0, 1, 2, 4, 5, 6]]
std1 = np.std(rmse_gain, axis=0)[[0, 1, 2, 4, 5, 6]]
mu3 = np.mean(rmse_knn1, axis=0)[[0, 1, 2, 4, 5, 6]]
std3 = np.std(rmse_knn1, axis=0)[[0, 1, 2, 4, 5, 6]]
mu4 = np.mean(rmse_knn2, axis=0)[[0, 1, 2, 4, 5, 6]]
std4 = np.std(rmse_knn2, axis=0)[[0, 1, 2, 4, 5, 6]]
x_ticks_locs = np.arange(0.0, 6.0)
x_ticks_names = ["breast", "credit", "letter", "spam", "wine_red", "wine_white"]
plt.figure(figsize=(8, 4))
plt.errorbar(x_ticks_locs-0.15, mu1, yerr=std1, color="indigo", marker=".", capsize=5.0, ls="", label="GAIN")
plt.errorbar(x_ticks_locs, mu3, yerr=std3, color="teal", marker=".", capsize=5.0, ls="", label="KNN-uniform")
plt.errorbar(x_ticks_locs+0.15, mu4, yerr=std4, color="peru", marker=".", capsize=5.0, ls="", label="KNN-distance")
plt.xticks(x_ticks_locs, x_ticks_names, rotation ="vertical")
plt.ylabel("RMSE")
plt.title("Missing rate 20% (MCAR)")
plt.legend()
plt.tight_layout()
plt.savefig("results/pipeline5/plots/rmse_datasets3.pdf")