-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
273 lines (222 loc) · 9.28 KB
/
dataset.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
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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
# sys.path.append(os.path.dirname(os.path.abspath('')))
import json
import joblib
import numpy as np
import pandas as pd
from tqdm.notebook import tqdm
from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
from sqlalchemy import sql
from sqlalchemy import Table, MetaData
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
# In[ ]:
def dump_scaler(obj, name, file_path=None):
if not str(name).endswith(".scaler"):
name = str(name) + ".scaler"
if file_path is None:
file_path = os.path.abspath('')
full_path = file_path + '/'+ str(name)
joblib.dump(obj, full_path)
def dump_encoder(obj, name, file_path=None):
if not str(name).endswith(".encoder"):
name = str(name) + ".encoder"
if file_path is None:
file_path = os.path.abspath('')
full_path = file_path + '/'+ str(name)
joblib.dump(obj, full_path)
# In[ ]:
class Dataset(object):
def __init__(self, data: pd.DataFrame, label=None, params=None,
categorical_feature=[], scaler_path=None, encoder_path=None):
self.handle = None
self._data = None
self._label = None
self.data = data.dropna()
self.label = label
self.categorical_feature = categorical_feature
self.params = params
if params is None:
raise ValueError("Specify params")
self.scaler_path = scaler_path
self.encoder_path = encoder_path
self.is_construct = False
def __repr__(self):
return "<{0}> {1}".format(
self.__class__.__name__,
json.dumps(
self.export_value(self.params),
sort_keys=True,
indent=4,
separators=(',', ': '),
),
)
def export_value(self, data):
if isinstance(data, dict):
data = dict((k, self.export_value(v))
for k, v in data.items()
if 'data' not in k)
elif isinstance(data, list):
data = [self.export_value(v) for v in data]
elif isinstance(data, pd.DataFrame):
data = data.to_dict()
elif isinstance(data, pd.Series):
data = data.to_dict()
elif isinstance(data, MinMaxScaler):
data = data.scale_.tolist()
elif isinstance(data, LabelEncoder):
data = data.classes_.tolist()
return data
def get_numerical_cols(self):
numerical_subset = []
if self.params is not None:
if 'numerical_cols' in self.params:
numerical_subset = self.params['numerical_cols']
assert type(numerical_subset)==list, "numerical_cols should be list."
elif 'scale_cols' in self.params:
numerical_subset = self.params['scale_cols']
assert type(numerical_subset)==list, "numerical_cols should be list."
return numerical_subset
def get_scale_cols(self):
scale_subset = []
if self.params is not None:
if 'scale_cols' in self.params:
scale_subset = self.params['scale_cols']
assert type(scale_subset)==list, "scale_cols should be list."
elif 'numerical_cols' in self.params:
scale_subset = self.params['numerical_cols']
assert type(scale_subset)==list, "scale_cols should be list."
return scale_subset
def get_label_encode_cols(self):
label_encode_subset = []
if self.params is not None:
if 'label_encode_cols' in self.params:
label_encode_subset = self.params['label_encode_cols']
assert type(label_encode_subset)==list, "label_encode_cols should be list."
return label_encode_subset
def get_field(self, field_name):
if field_name == 'label':
if field_name in self.data.columns:
self.label = self.data.pop(field_name)
return self.label
elif field_name in self.data:
return self.data[field_name]
else:
raise ValueError("Unknown field_name")
def get_label(self):
if self.label is None:
self.label = self.get_field('label')
return self.label
def set_field(self, field_name, data):
if field_name == 'label':
self.label = data
else:
self.data[field_name] = data
return self
def set_label(self, label):
if label is not None:
self.set_field('label', label)
self.label = self.get_field('label')
self._label = self.label
return self
def transform_fields(self, trans_type=None):
if trans_type == 'numerical_cols':
numerical_cols = self.get_numerical_cols()
self._data[numerical_cols] = self.data[numerical_cols].apply(pd.to_numeric,
errors='ignore')
elif trans_type == 'scale_cols':
scale_cols = self.get_scale_cols()
if self.scaler_path is not None:
self.scaler = joblib.load(self.scaler_path)
self._data[scale_cols] = pd.DataFrame(
self.scaler.inverse_transform(self.data[scale_cols]), columns=scale_cols)
else:
self.scaler = MinMaxScaler()
self._data[scale_cols] = pd.DataFrame(
self.scaler.fit_transform(self.data[scale_cols]), columns=scale_cols)
elif trans_type == 'categorical_cols':
categorical_cols = self.categorical_feature
dummies = pd.get_dummies(self.data[categorical_cols])
self._data = pd.concat([self._data, dummies], axis=1)
elif trans_type == 'label_encode_cols':
label_encode_cols = self.get_label_encode_cols()
if self.encoder_path is not None:
self.label_encoder = joblib.load(self.encoder_path)
self._data[label_encode_cols] = pd.DataFrame(
self.label_encoder.inverse_transform(self.data[label_encode_cols]))
else:
for col in label_encode_cols:
self.label_encoder = LabelEncoder()
self._data[col] = pd.DataFrame(
self.label_encoder.fit_transform(self.data[col]))
return self
def full_transform(self):
if self._data is None:
self._data = pd.DataFrame()
if self.params is not None:
if 'numerical_cols' in self.params:
self.transform_fields(trans_type='numerical_cols')
if 'scale_cols' in self.params:
self.transform_fields(trans_type='scale_cols')
if 'categorical_cols' in self.params:
self.transform_fields(trans_type='categorical_cols')
if 'label_encode_cols' in self.params:
self.transform_fields(trans_type='label_encode_cols')
return self
def _lazy_init(self, data, label=None, params=None,
categorical_feature=None):
if data is None:
return self
params = {} if params is None else params
if isinstance(categorical_feature, list) and categorical_feature:
categorical_feature = pd.Index(categorical_feature)
if categorical_feature.isin(self.data.columns).all():
self.params['categorical_cols'] = list(categorical_feature)
if label is not None:
self.set_label(label)
self.get_label()
self.full_transform()
def construct(self):
if self._data is None:
self._lazy_init(self.data, label=self.label, params=self.params,
categorical_feature=self.categorical_feature)
self.is_construct = True
return self
def train_eval_test_split(self, eval_size, test_size, random_state=27):
if not self.is_construct:
self.construct()
n = len(self.data)
e = round(n * eval_size)
t = round(n * test_size)
assert eval_size+test_size < 1, "size bigger than 1"
if n == 0:
raise ValueError
elif eval_size == 0 and test_size == 0:
raise ValueError
np.random.seed(random_state)
full_idxs = range(n)
eval_idx = np.random.choice(full_idxs, size=e,
replace=False)
full_idxs = list(set(full_idxs) - set(eval_idx))
test_idx = np.random.choice(full_idxs, size=t,
replace=False)
full_idxs = list(set(full_idxs) - set(test_idx))
tpl = ()
for idxs in [full_idxs, eval_idx, test_idx]:
dataset = Dataset(pd.DataFrame(), params=self.params,
categorical_feature=self.categorical_feature)
for k, v in dataset.__dict__.items():
attr = self.__dict__[k]
if k in ['data', '_data', 'label', '_label']:
dataset.__dict__[k] = attr[attr.index.isin(idxs)]
tpl += (dataset,)
return tpl
# In[ ]:
# !jupyter nbconvert --to script dataset.ipynb
# In[ ]: