Repository navigation
Expand file tree
/
Copy pathdownload_data.py
More file actions
61 lines (53 loc) · 2.42 KB
/
Copy pathdownload_data.py
File metadata and controls
61 lines (53 loc) · 2.42 KB
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
import os
import openml
import numpy as np
import pickle
from sklearn.preprocessing import LabelEncoder
def save_suite(suite_id, dir_name, save_categorical_indicator=False, regression=True):
benchmark_suite = openml.study.get_suite(suite_id) # obtain the benchmark suite
for task_id in benchmark_suite.tasks: # iterate over all tasks
task = openml.tasks.get_task(task_id) # download the OpenML task
dataset = task.get_dataset()
print(f"Downloading dataset {dataset.name}")
# retrieve categorical data for encoding
X, y, categorical_indicator, attribute_names = dataset.get_data(
dataset_format="dataframe", target=dataset.default_target_attribute
)
X = np.array(X).astype(np.float32)
if regression:
y = np.array(y).astype(np.int32)
else:
le = LabelEncoder()
y = le.fit_transform(np.array(y))
filepath = "{}/data_{}".format(dir_name, dataset.name)
# create nested directories
dirname = os.path.dirname(filepath)
if len(dirname.split('/')) > 1 and not os.path.exists(dirname):
os.makedirs(dirname)
with open(filepath, "wb") as f:
if save_categorical_indicator:
pickle.dump((X, y, categorical_indicator), f)
else:
pickle.dump((X, y), f)
suites_id = {"numerical_regression": 297,
"numerical_classification": 298,
"categorical_regression": 299,
"categorical_classification": 304}
print("Saving datasets from suite: {}".format("numerical_regression"))
save_suite(suites_id["numerical_regression"],
"data/numerical_only/regression",
save_categorical_indicator=False)
print("Saving datasets from suite: {}".format("numerical_classification"))
save_suite(suites_id["numerical_classification"],
"data/numerical_only/balanced",
save_categorical_indicator=False,
regression=False)
print("Saving datasets from suite: {}".format("categorical_regression"))
save_suite(suites_id["categorical_regression"],
"data/num_and_cat/regression",
save_categorical_indicator=True)
print("Saving datasets from suite: {}".format("categorical_classification"))
save_suite(suites_id["categorical_classification"],
"data/num_and_cat/balanced",
save_categorical_indicator=True,
regression=False)