-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtabular.py
More file actions
371 lines (299 loc) · 12.5 KB
/
tabular.py
File metadata and controls
371 lines (299 loc) · 12.5 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import os
import sys
import numpy as np
import pandas as pd
import torch
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder, FunctionTransformer, LabelEncoder
from sklearn.feature_extraction.text import CountVectorizer, HashingVectorizer
from sklearn.linear_model import LogisticRegression, LinearRegression
import lightgbm as lgb
from sklearn.svm import SVC, SVR
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.ensemble import ExtraTreesClassifier, ExtraTreesRegressor
from sklearn.decomposition import PCA, TruncatedSVD
from sklearn.model_selection import cross_val_score, cross_validate
from sklearn.model_selection import KFold, StratifiedKFold, GroupKFold
from sklearn.metrics import accuracy_score, cohen_kappa_score, make_scorer
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.feature_selection import RFECV
from sklearn.metrics import recall_score, roc_auc_score, classification_report, confusion_matrix
from sklearn.utils import class_weight
from sklearn.metrics import precision_recall_fscore_support as score
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.model_selection import train_test_split, ShuffleSplit
from sklearn.model_selection import KFold, StratifiedKFold, GroupKFold
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import class_weight
import json
import joblib
import keras
from keras import ops
from keras.layers import Activation
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
# Hyperparameters distributions
from scipy.stats import randint
from scipy.stats import uniform
# Model selection
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.model_selection import cross_val_score
# Metrics
from sklearn.metrics import average_precision_score
from sklearn.metrics import make_scorer
import os
from sklearn.preprocessing import RobustScaler, QuantileTransformer, PowerTransformer
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from sklearn.utils.multiclass import unique_labels
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.preprocessing import RobustScaler
from sklearn.preprocessing import QuantileTransformer, PowerTransformer
from sklearn.preprocessing import LabelEncoder, OrdinalEncoder
def set_device():
if torch.cuda.is_available():
device = torch.device("cuda")
print("CUDA is available. Using GPU.")
elif torch.backends.mps.is_available():
device = torch.device("mps")
print("MPS is available. Using Metal.")
else:
device = torch.device("cpu")
print("Using CPU.")
return device
def gelu(x):
return 0.5 * x * (1 + ops.tanh(ops.sqrt(2 / np.pi) * (x + 0.044715 * ops.power(x, 3))))
keras.utils.get_custom_objects().update({'custom_gelu': Activation(gelu)})
class Mish(Activation):
'''
Mish Activation Function.
see: https://github.com/digantamisra98/Mish/blob/master/Mish/TFKeras/mish.py
.. math::
mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x}))
Shape:
- Input: Arbitrary. Use the keyword argument `input_shape`
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.
- Output: Same shape as the input.
Examples:
>>> X = Activation('Mish', name="conv1_act")(X_input)
'''
def __init__(self, activation, **kwargs):
super(Mish, self).__init__(activation, **kwargs)
self.__name__ = 'Mish'
def mish(inputs):
return inputs * ops.tanh(ops.softplus(inputs))
keras.utils.get_custom_objects().update({'mish': Mish(mish)})
class ItemFilterOut():
def __init__(self, keys):
self.keys = keys
def fit(self, x, y=None):
return self
def transform(self, data_dict):
to_drop = [key for key in self.keys if key in data_dict]
return data_dict.drop(to_drop, axis=1)
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, y, **fit_params)
return self.transform(X)
class ItemSelector():
def __init__(self, key):
self.key = key
def fit(self, x, y=None):
return self
def transform(self, data_dict):
return data_dict[self.key].fillna('UNK')
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, y, **fit_params)
return self.transform(X)
class SplitInput():
def __init__(self, sequence):
self.sequence = sequence
def fit(self, X, y=None):
for op in self.sequence:
op.fit(X)
return self
def transform(self, X, y=None):
return [op.transform(X) for op in self.sequence]
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, **fit_params)
return self.transform(X)
class LEncoder(BaseEstimator, TransformerMixin):
def __init__(self):
self.encoders = dict()
self.dictionary_size = list()
self.unk = -1
def fit(self, X, y=None, **fit_params):
for col in range(X.shape[1]):
le = LabelEncoder()
le.fit(X.iloc[:, col].fillna('_nan'))
le_dict = dict(zip(le.classes_, le.transform(le.classes_)))
if '_nan' not in le_dict:
max_value = max(le_dict.values())
le_dict['_nan'] = max_value
max_value = max(le_dict.values())
le_dict['_unk'] = max_value
self.unk = max_value
self.dictionary_size.append(len(le_dict))
col_name = X.columns[col]
self.encoders[col_name] = le_dict
return self
def transform(self, X, y=None, **fit_params):
output = list()
for col in range(X.shape[1]):
col_name = X.columns[col]
le_dict = self.encoders[col_name]
emb = X.iloc[:, col].fillna('_nan').apply(lambda x: le_dict.get(x, le_dict['_unk'])).values
output.append(emb)
return output
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, y, **fit_params)
return self.transform(X)
class ToString(BaseEstimator, TransformerMixin):
def fit(self, X, y=None, **fit_params):
return self
def transform(self, X, y=None, **fit_params):
return X.astype(str)
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, y, **fit_params)
return self.transform(X)
class TabularTransformer(BaseEstimator, TransformerMixin):
def __init__(self, numeric=list(), ordinal=list(),
lowcat=list(), highcat=list()):
self.numeric = numeric
self.ordinal = ordinal
self.lowcat = lowcat
self.highcat = highcat
self.mvi = multivariate_imputer = IterativeImputer(estimator=ExtraTreesRegressor(n_estimators=300, n_jobs=-2),
initial_strategy='median')
self.uni = univariate_imputer = SimpleImputer(strategy='median',
add_indicator=True)
self.nmt = numeric_transformer = Pipeline(steps=[
('normalizer', QuantileTransformer(n_quantiles=600,
output_distribution='normal',
random_state=42)),
('imputer', univariate_imputer),
('scaler', StandardScaler())])
self.ohe = generic_categorical_transformer = Pipeline(steps=[
('string_converter', ToString()),
('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
('onehot', OneHotEncoder(handle_unknown='ignore'))])
self.lle = label_enc_transformer = Pipeline(steps=[
('string_converter', ToString()),
('label_encoder', LEncoder())])
self.ppl = ColumnTransformer(
transformers=[
('num', numeric_transformer, self.numeric+self.ordinal),
('ohe', generic_categorical_transformer, self.lowcat+self.ordinal),
], remainder='drop')
def fit(self, X, y=None, **fit_params):
_ = self.ppl.fit(X)
if len(self.highcat) > 0:
_ = self.lle.fit(X[self.highcat])
return self
def shape(self, X, y=None, **fit_params):
numeric_shape = self.ppl.transform(X.iloc[[0],:]).shape[1]
categorical_size = self.lle.named_steps['label_encoder'].dictionary_size
return [numeric_shape] + categorical_size
def transform(self, X, y=None, **fit_params):
Xn = self.ppl.transform(X)
if len(self.highcat) > 0:
return [Xn] + self.lle.transform(X[self.highcat])
else:
return Xn
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, y, **fit_params)
return self.transform(X)
class DataGenerator(keras.utils.Sequence):
"""
Generates data for Keras
X: a pandas DataFrame
y: a pandas Series, a NumPy array or a List
"""
def __init__(self, X, y,
tabular_transformer=None,
batch_size=32,
shuffle=False,
dict_output=False,
device=None,
**kwargs):
super().__init__(**kwargs)
'Initialization'
self.X = X
try:
# If a pandas Series, converting to a NumPy array
self.y = y.values
except:
self.y = np.array(y)
self.tbt = tabular_transformer
self.tabular_transformer = tabular_transformer
self.batch_size = batch_size
self.shuffle = shuffle
self.dict_output = dict_output
self.indexes = self._build_index()
self.on_epoch_end()
self.item = 0
self.device = device
def _build_index(self):
"""
Builds an index from data
"""
return np.arange(len(self.y))
def on_epoch_end(self):
"""
At the end of every epoch, shuffle if required
"""
if self.shuffle:
np.random.shuffle(self.indexes)
def __len__(self):
"""
Returns the number of batches per epoch
"""
return int(len(self.indexes) / self.batch_size) + 1
def __iter__(self):
"""
returns an iterable
"""
for i in range(self.__len__()):
self.item = i
yield self.__getitem__(index=i)
self.item = 0
def __next__(self):
return self.__getitem__(index=self.item)
def __call__(self):
return self.__iter__()
def __data_generation(self, selection):
if self.tbt is not None:
if self.dict_output:
dct = {'input_'+str(j) : arr for j, arr in enumerate(self.tbt.transform(self.X.iloc[selection, :]))}
return dct, self.y[selection]
else:
return self.tbt.transform(self.X.iloc[selection, :]), self.y[selection]
else:
return self.X.iloc[selection, :], self.y[selection]
def __getitem__(self, index):
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
samples, labels = self.__data_generation(indexes)
if self.device:
if isinstance(samples, list):
# Cast numeric part to float32, keep categorical as is (long)
samples_torch = [torch.from_numpy(samples[0].astype(np.float32)).to(self.device)]
samples_torch.extend([torch.from_numpy(s).to(self.device) for s in samples[1:]])
samples = samples_torch
else:
# Only numeric data
samples = torch.from_numpy(samples.astype(np.float32)).to(self.device)
# Cast labels to float32 for the loss function
labels = torch.from_numpy(labels.astype(np.float32)).to(self.device)
return samples, labels
if __name__ == "__main__":
pass