-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_management.py
167 lines (128 loc) · 6.43 KB
/
data_management.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
from sklearn.utils import shuffle
import params
from tmfg_bootstrapped import *
class DataManager:
def __init__(self, dataset_id, seed):
self.dataset_id = dataset_id
self.seed = seed
np.random.seed(self.seed)
#self.root_folder = None
self.X = None
self.y = None
self.X_train = None
self.y_train = None
self.X_val = None
self.y_val = None
self.X_test = None
self.y_test = None
self.__download_open_ml_dataset()
self.__customize_data()
self.numerical_features = None
self.categorical_features = None
def __get_feature_types(self, dataset):
feature_types = dataset.features
feature_names = feature_types.keys()
numerical_features = []
categorical_features = []
for feature_name in feature_names:
if feature_types[feature_name].data_type == 'numeric':
numerical_features.append(feature_name)
else:
categorical_features.append(feature_name)
return numerical_features, categorical_features
def __download_open_ml_dataset(self):
#self.root_folder = f'./Homological_FS/HCNN_Classifier/Dataset_{self.dataset_id}/Seed_{self.seed}/openml/'
#generate_folder_structure(self.root_folder)
#shutil.rmtree(self.root_folder)
self.X = pd.read_csv(f'./data/{self.dataset_id}/X.csv')
self.y = pd.read_csv(f'./data/{self.dataset_id}/y.csv')
if params.SHUFFLE_DATA_BEFORE_SPLITTING:
self.X, self.y = shuffle(self.X, self.y)
self.X.reset_index(drop=True, inplace=True)
self.X = self.X.to_numpy()
self.y = self.y.to_numpy()
'''while True:
try:
shutil.rmtree(self.root_folder)
openml.config.cache_directory = os.path.expanduser(self.root_folder)
dataset = open_data.get_dataset(self.dataset_id)
self.X, self.y, categorical_indicator, attribute_names = dataset.get_data(dataset_format="dataframe", target=dataset.default_target_attribute)
shutil.rmtree(self.root_folder)
break
except:
try:
shutil.rmtree(self.root_folder)
openml.config.cache_directory = os.path.expanduser(self.root_folder)
task = openml.tasks.get_task(self.dataset_id) # download the OpenML task
dataset = task.get_dataset()
print(dataset)
self.X, self.y, categorical_indicator, attribute_names = dataset.get_data(dataset_format="dataframe", target=dataset.default_target_attribute)
break
except:
time.sleep(30)
print(f'Downloading error for dataset {self.dataset_id}. Trying again in 30 secs...')
continue
label_encoder = LabelEncoder()
self.y = label_encoder.fit_transform(np.array(self.y).ravel())
columns_names = []
for n, i in enumerate(self.X.columns):
columns_names.append(n)
self.X.columns = columns_names
if params.SHUFFLE_DATA_BEFORE_SPLITTING:
self.X, self.y = shuffle(self.X, self.y)
self.X.reset_index(drop=True, inplace=True)
self.X = self.X.to_numpy()
mask = ~np.any(np.isnan(self.X), axis=1)
self.X = self.X[mask]
self.y = self.y[mask]'''
def __customize_data(self):
upper_bound_train_test = int(len(self.X) * params.TEST_PERCENTAGE)
upper_bound_train_val = int((len(self.X) - upper_bound_train_test) * params.VALIDATION_PERCENTAGE)
# Isolate numerical features.
'''self.X = self.X[self.numerical_features]
self.X.reset_index(drop=True, inplace=True)
self.X.columns = np.arange(0, self.X.shape[1]).tolist()'''
# Isolate categorical features.
'''self.X_cat = self.X[self.categorical_features]
self.X_cat.reset_index(drop=True, inplace=True)
self.X_cat.columns = np.arange(0, self.X_cat.shape[1]).tolist()'''
self.X_train = self.X[:-upper_bound_train_test]
self.y_train = self.y[:-upper_bound_train_test]
self.X_val = self.X_train[-upper_bound_train_val:]
self.y_val = self.y_train[-upper_bound_train_val:]
self.X_train = self.X_train[:-upper_bound_train_val]
self.y_train = self.y_train[:-upper_bound_train_val]
self.X_test = self.X[-upper_bound_train_test:]
self.y_test = self.y[-upper_bound_train_test:]
self.X_train = pd.DataFrame(self.X_train)
self.X_val = pd.DataFrame(self.X_val)
self.X_test = pd.DataFrame(self.X_test)
def get_data(self):
return self.X_train, self.X_val, self.X_test, self.y_train, self.y_val, self.y_test
class HomologicalDataManager:
def __init__(self, X_train, X_val, X_test, tmfg_iterations, tmfg_confidence, tmfg_similarity, seed):
self.X_train = X_train
self.X_val = X_val
self.X_test = X_test
self.tmfg_iterations = tmfg_iterations
self.tmfg_confidence = tmfg_confidence
self.tmfg_similarity = tmfg_similarity
self.seed = seed
self.__get_stat_robust_tmfg()
def __get_stat_robust_tmfg(self):
cliques, separators, original_tmfg, _, adjacency_matrix = TMFG_Bootstrapped(df=self.X_train,
correlation_type=self.tmfg_similarity,
number_of_repetitions=self.tmfg_iterations,
confidence_level=self.tmfg_confidence,
parallel=True,
seed=self.seed).compute_tmfg_bootstrapping()
c = nx.degree_centrality(adjacency_matrix)
keys = np.array(list(c.keys()))
values = np.array(list(c.values()))
nodes_list = sorted(list(keys[values != 0]))
self.X_train = self.X_train[nodes_list]
self.X_val = self.X_val[nodes_list]
self.X_test = self.X_test[nodes_list]
self.number_selected_features = len(nodes_list)
def get_homological_data(self):
return self.X_train, self.X_val, self.X_test, self.number_selected_features