-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader.py
More file actions
267 lines (208 loc) · 6.95 KB
/
dataloader.py
File metadata and controls
267 lines (208 loc) · 6.95 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
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
import zipfile
import gzip
import shutil
from pathlib import Path
from pandas import read_csv, read_excel
from urllib.request import urlretrieve
from sklearn.preprocessing import OrdinalEncoder
def download(url, zip_file):
try:
urlretrieve(url, zip_file)
except Exception as e:
raise RuntimeError(f"Failed to download from {url}: {e}") from e
def extract(zip_file, extract_path):
file_type = zip_file.suffix
try:
if file_type == ".zip":
with zipfile.ZipFile(zip_file, "r") as zip_ref:
zip_ref.extractall(extract_path)
elif file_type == ".gz":
with gzip.open(zip_file, "rb") as f_in, open(extract_path, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
else:
raise ValueError(f"Unknown file type: {file_type}")
except Exception as e:
raise RuntimeError(f"Failed to extract {zip_file}: {e}") from e
def download_and_extract(url, zip_file, extract_path):
download(url, zip_file)
extract(zip_file, extract_path)
def load_adult(dataset_dir):
file = dataset_dir / "adult.data"
if not file.exists():
file.parent.mkdir(parents=True, exist_ok=True)
zip_file = file.parent / "adult.zip"
download_and_extract(
"https://archive.ics.uci.edu/static/public/2/adult.zip",
zip_file,
file.parent,
)
X = read_csv(file, header=None)
X.columns = [
"age",
"workclass",
"fnlwgt",
"education",
"education-num",
"marital-status",
"occupation",
"relationship",
"race",
"sex",
"capital-gain",
"capital-loss",
"hours-per-week",
"native-country",
"income",
]
y = X.pop("income").map({" >50K": 1, " <=50K": 0})
objcol = X.select_dtypes(exclude=["float", "int"]).columns
X[objcol] = OrdinalEncoder().fit_transform(X[objcol])
return X, y
def load_bank(dataset_dir):
file = dataset_dir / "bank-full.csv"
if not file.exists():
file.parent.mkdir(parents=True, exist_ok=True)
zip_file = file.parent / "bank+marketing.zip"
download_and_extract(
"https://archive.ics.uci.edu/static/public/222/bank+marketing.zip",
zip_file,
file.parent,
)
zip_sub_file = file.parent / "bank.zip"
extract(zip_sub_file, file.parent)
X = read_csv(file, sep=";")
y = X.pop("y").map({"yes": 1, "no": 0})
objcol = X.select_dtypes(exclude=["float", "int"]).columns
X[objcol] = OrdinalEncoder().fit_transform(X[objcol])
return X, y
def load_credit(dataset_dir):
file = dataset_dir / "default of credit card clients.xls"
if not file.exists():
file.parent.mkdir(parents=True, exist_ok=True)
zip_file = file.parent / "default+of+credit+card+clients.zip"
download_and_extract(
"https://archive.ics.uci.edu/static/public/350/default+of+credit+card+clients.zip",
zip_file,
file.parent,
)
X = read_excel(file, header=1)
y = X.pop("default payment next month")
return X, y
def load_blood(dataset_dir):
file = dataset_dir / "transfusion.data"
if not file.exists():
file.parent.mkdir(parents=True, exist_ok=True)
zip_file = file.parent / "blood+transfusion+service+center.zip"
download_and_extract(
"https://archive.ics.uci.edu/static/public/176/blood+transfusion+service+center.zip",
zip_file,
file.parent,
)
X = read_csv(file)
X.columns = ["Recency", "Frequency", "Monetary", "Time", "Donated_Blood"]
y = X.pop("Donated_Blood")
return X, y
def load_cancer(dataset_dir):
file = dataset_dir / "wdbc.data"
if not file.exists():
file.parent.mkdir(parents=True, exist_ok=True)
zip_file = file.parent / "breast+cancer+wisconsin+diagnostic.zip"
download_and_extract(
"https://archive.ics.uci.edu/static/public/17/breast+cancer+wisconsin+diagnostic.zip",
zip_file,
file.parent,
)
X = read_csv(file, header=None)
X.columns = [
"ID",
"Diagnosis",
"radius1",
"texture1",
"perimeter1",
"area1",
"smoothness1",
"compactness1",
"concavity1",
"concave_points1",
"symmetry1",
"fractal_dimension1",
"radius2",
"texture2",
"perimeter2",
"area2",
"smoothness2",
"compactness2",
"concavity2",
"concave_points2",
"symmetry2",
"fractal_dimension2",
"radius3",
"texture3",
"perimeter3",
"area3",
"smoothness3",
"compactness3",
"concavity3",
"concave_points3",
"symmetry3",
"fractal_dimension3",
]
y = X.pop("Diagnosis").map({"M": 1, "B": 0})
return X, y
def load_ecoli(dataset_dir):
file = dataset_dir / "ecoli.data"
if not file.exists():
file.parent.mkdir(parents=True, exist_ok=True)
zip_file = file.parent / "ecoli.zip"
download_and_extract(
"https://archive.ics.uci.edu/static/public/39/ecoli.zip",
zip_file,
file.parent,
)
X = read_csv(file, sep="\s+", header=None)
X.columns = ["sequence", "mcg", "gvh", "lip", "chg", "aac", "alm1", "alm2", "class"]
y = X.pop("class").map(
{
"cp": 0,
"im": 1,
"pp": 2,
"imU": 3,
"om": 4,
"omL": 5,
"imL": 6,
"imS": 7,
}
)
X[["sequence"]] = OrdinalEncoder().fit_transform(X[["sequence"]]).astype(int)
return X, y
def load_house(dataset_dir):
file = dataset_dir / "train.csv"
if not file.exists():
print(
"Please manually download the `train.csv` file from:\n"
"https://www.kaggle.com/competitions/house-prices-advanced-regression-techniques/data?select=train.csv \n"
"and place it under the folder `dataset/house/`."
)
X = read_csv(file)
y = X.pop("SalePrice")
objcol = X.select_dtypes(exclude=["float", "int"]).columns
X[objcol] = OrdinalEncoder().fit_transform(X[objcol])
return X, y
def load_data(dataset, strictly_positive=False):
PROJECT_ROOT = Path(__file__).parent
dataset_dir = PROJECT_ROOT / f"dataset/{dataset}"
uci_data = {
"adult": lambda: load_adult(dataset_dir),
"bank": lambda: load_bank(dataset_dir),
"credit": lambda: load_credit(dataset_dir),
"blood": lambda: load_blood(dataset_dir),
"cancer": lambda: load_cancer(dataset_dir),
"ecoli": lambda: load_ecoli(dataset_dir),
"house": lambda: load_house(dataset_dir),
}
if dataset not in uci_data:
raise ValueError(f"Unknown dataset: {dataset}")
X, y = uci_data[dataset]()
if strictly_positive:
X = X.loc[:, (X > 0).all(axis=0)]
return X, y