Repository navigation
Expand file tree
/
Copy pathloader_regression.py
More file actions
337 lines (270 loc) · 12 KB
/
Copy pathloader_regression.py
File metadata and controls
337 lines (270 loc) · 12 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
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
# ===============================================================================
# Copyright 2020-2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
import logging
import os
from pathlib import Path
from typing import Any
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_openml, fetch_california_housing
from sklearn.preprocessing import StandardScaler
from .loader_utils import retrieve
def abalone(dataset_dir: Path) -> bool:
"""
https://archive.ics.uci.edu/ml/machine-learning-databases/abalone
abalone x train dataset (3341, 8)
abalone y train dataset (3341, 1)
abalone x test dataset (836, 8)
abalone y train dataset (836, 1)
"""
dataset_name = 'abalone'
os.makedirs(dataset_dir, exist_ok=True)
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data'
local_url = os.path.join(dataset_dir, os.path.basename(url))
if not os.path.isfile(local_url):
logging.info(f'Started loading {dataset_name}')
retrieve(url, local_url)
logging.info(f'{dataset_name} is loaded, started parsing...')
abalone: Any = pd.read_csv(local_url, header=None)
abalone[0] = abalone[0].astype('category').cat.codes
X = abalone.iloc[:, :-1].values
y = abalone.iloc[:, -1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=0)
for data, name in zip((X_train, X_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def california_housing(dataset_dir: Path) -> bool:
"""
california_housing x train dataset (18576, 8)
california_housing y train dataset (18576, 1)
california_housing x test dataset (2064, 8)
california_housing y train dataset (2064, 1)
"""
dataset_name = 'california_housing'
os.makedirs(dataset_dir, exist_ok=True)
X, y = fetch_california_housing(return_X_y=True, as_frame=False,
data_home=dataset_dir)
X = pd.DataFrame(X)
y = pd.DataFrame(y)
logging.info(f'{dataset_name} is loaded, started parsing...')
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.1, random_state=42)
scaler = StandardScaler().fit(x_train, y_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
for data, name in zip((x_train, x_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def fried(dataset_dir: Path) -> bool:
"""
fried x train dataset (32614, 10)
fried y train dataset (32614, 1)
fried x test dataset (8154, 10)
fried y train dataset (8154, 1)
"""
dataset_name = 'fried'
os.makedirs(dataset_dir, exist_ok=True)
X, y = fetch_openml(
name='fried', return_X_y=True, as_frame=False, data_home=dataset_dir)
X = pd.DataFrame(X)
y = pd.DataFrame(y)
logging.info(f'{dataset_name} is loaded, started parsing...')
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
for data, name in zip((x_train, x_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def medical_charges_nominal(dataset_dir: Path) -> bool:
"""
medical_charges_nominal x train dataset (130452, 11)
medical_charges_nominal y train dataset (130452, 1)
medical_charges_nominal x test dataset (32613, 11)
medical_charges_nominal y train dataset (32613, 1)
"""
dataset_name = 'medical_charges_nominal'
os.makedirs(dataset_dir, exist_ok=True)
X, y = fetch_openml(name='medical_charges_nominal', return_X_y=True,
as_frame=False, data_home=dataset_dir)
X = pd.DataFrame(X)
y = pd.DataFrame(y)
logging.info(f'{dataset_name} is loaded, started parsing...')
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
scaler = StandardScaler().fit(x_train, y_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
scaler = StandardScaler().fit(y_train)
y_train = scaler.transform(y_train)
y_test = scaler.transform(y_test)
for data, name in zip((x_train, x_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def mortgage_first_q(dataset_dir: Path) -> bool:
"""
# TODO: add an loading instruction
"""
return False
def twodplanes(dataset_dir: Path) -> bool:
"""
twodplanes x train dataset (106288, 10)
twodplanes y train dataset (106288, 1)
twodplanes x test dataset (70859, 10)
twodplanes y train dataset (70859, 1)
"""
dataset_name = 'twodplanes'
os.makedirs(dataset_dir, exist_ok=True)
X, y = fetch_openml(
name='BNG(2dplanes)', return_X_y=True, as_frame=False, data_home=dataset_dir)
X = pd.DataFrame(X)
y = pd.DataFrame(y)
logging.info(f'{dataset_name} is loaded, started parsing...')
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.4, random_state=42)
for data, name in zip((x_train, x_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def year_prediction_msd(dataset_dir: Path) -> bool:
"""
YearPredictionMSD dataset from UCI repository
https://archive.ics.uci.edu/ml/datasets/yearpredictionmsd
year_prediction_msd x train dataset (463715, 90)
year_prediction_msd y train dataset (463715, 1)
year_prediction_msd x test dataset (51630, 90)
year_prediction_msd y train dataset (51630, 1)
"""
dataset_name = 'year_prediction_msd'
os.makedirs(dataset_dir, exist_ok=True)
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00203/' \
'YearPredictionMSD.txt.zip'
local_url = os.path.join(dataset_dir, os.path.basename(url))
if not os.path.isfile(local_url):
logging.info(f'Started loading {dataset_name}')
retrieve(url, local_url)
logging.info(f'{dataset_name} is loaded, started parsing...')
year = pd.read_csv(local_url, header=None)
X = year.iloc[:, 1:].to_numpy(dtype=np.float32)
y = year.iloc[:, 0].to_numpy(dtype=np.float32)
X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=False,
train_size=463715,
test_size=51630)
for data, name in zip((X_train, X_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def yolanda(dataset_dir: Path) -> bool:
"""
yolanda x train dataset (130452, 11)
yolanda y train dataset (130452, 1)
yolanda x test dataset (32613, 11)
yolanda y train dataset (32613, 1)
"""
dataset_name = 'yolanda'
os.makedirs(dataset_dir, exist_ok=True)
X, y = fetch_openml(name='yolanda', return_X_y=True,
as_frame=False, data_home=dataset_dir)
X = pd.DataFrame(X)
y = pd.DataFrame(y)
logging.info(f'{dataset_name} is loaded, started parsing...')
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
scaler = StandardScaler().fit(x_train, y_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
for data, name in zip((x_train, x_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def airline_regression(dataset_dir: Path) -> bool:
"""
yolanda x train dataset (8500000, 9)
yolanda y train dataset (8500000, 1)
yolanda x test dataset (1500000, 9)
yolanda y train dataset (1500000, 1)
"""
dataset_name = 'airline_regression'
os.makedirs(dataset_dir, exist_ok=True)
X, y = fetch_openml(name='Airlines_DepDelay_10M', return_X_y=True,
as_frame=False, data_home=dataset_dir)
X = pd.DataFrame(X)
y = pd.DataFrame(y)
logging.info(f'{dataset_name} is loaded, started parsing...')
x_train, x_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
scaler = StandardScaler().fit(x_train, y_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
scaler = StandardScaler().fit(y_train)
y_train = scaler.transform(y_train)
y_test = scaler.transform(y_test)
for data, name in zip((x_train, x_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True
def higgs_10500K(dataset_dir: Path) -> bool:
"""
Higgs dataset from UCI machine learning repository
https://archive.ics.uci.edu/ml/datasets/HIGGS
Classification task. n_classes = 2.
higgs_10500K X train dataset (10500000, 28)
higgs_10500K y train dataset (10500000, 1)
higgs_10500K X test dataset (500000, 28)
higgs_10500K y test dataset (500000, 1)
"""
dataset_name = 'higgs_10500K'
os.makedirs(dataset_dir, exist_ok=True)
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00280/HIGGS.csv.gz'
local_url = os.path.join(dataset_dir, os.path.basename(url))
if not os.path.isfile(local_url):
logging.info(f'Started loading {dataset_name}')
retrieve(url, local_url)
logging.info(f'{dataset_name} is loaded, started parsing...')
nrows_train, nrows_test, dtype = 10500000, 500000, np.float32
data: Any = pd.read_csv(local_url, delimiter=",", header=None,
compression="gzip", dtype=dtype,
nrows=nrows_train + nrows_test)
X = data[data.columns[1:]]
y = data[data.columns[0:1]]
x_train, x_test, y_train, y_test = train_test_split(
X, y, train_size=nrows_train, test_size=nrows_test, shuffle=False)
for data, name in zip((x_train, x_test, y_train, y_test),
('x_train', 'x_test', 'y_train', 'y_test')):
filename = f'{dataset_name}_{name}.npy'
np.save(os.path.join(dataset_dir, filename), data)
logging.info(f'dataset {dataset_name} is ready.')
return True