This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom_transformers.py
721 lines (502 loc) · 18.3 KB
/
custom_transformers.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import pdb
from sklearn.base import TransformerMixin, BaseEstimator
from pandas import DataFrame, concat, get_dummies
from numpy import ndarray, array, arange, sort, argsort, vectorize
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.utils import check_array
def binarise(x):
""" Convert array to array of bools
Convert any non-zero value in `x` to 1,
otherwise it is 0
Parameters
----------
x: numpy.ndarray
Returns
-------
numpy.ndarray
"""
x[x > 0] = 1
x[x != 1] = 0
assert (x < 0).sum() == 0
return x
def dict_transform(x, d):
"""Numpy vectorised dictionary mapper
Vectorised method to use values of `x` as keys to map to values
in `d`.
Parameters
----------
x: numpy.ndarray
d: dict
Notes:
------
Used in `LabelOneHotEncoder.fit` but defined here because
of list comprehension scoping.
"""
return vectorize(d.__getitem__)(x)
class pdInit(BaseEstimator, TransformerMixin):
""" sklearn transformer: ensures input feature order is consistent
Attributes
----------
column_order_: pandas.Index
Column order of fitted data
"""
def __init__(self):
pass
def fit(self, X, y=None):
"""
Parameters
----------
X: pandas.DataFrame
Design matrix
y: dummy, optional
Returns
-------
self
"""
if not isinstance(X, DataFrame):
raise TypeError('pdIinit expected type pandas.DataFrame')
self.column_order_ = X.columns
return self
def transform(self, X, y=None):
"""
Parameters
----------
X: pandas.DataFrame
Design matrix
y: dummy, optional
Returns
-------
DataFrame
Column order same as fitted data
"""
if not isinstance(X, DataFrame):
raise TypeError('pdIinit expected type pandas.DataFrame')
return X.loc[:, self.column_order_]
class LabelOneHotEncoder(BaseEstimator, TransformerMixin):
""" Sklearn transformer: Label input to range of integers and then one-hot-encode
Attributes
----------
label_encoder_l_: list of dicts
Gives mapping between `classes` and their integer labels
n_values: list of ints
Number of labels within each column
one_hot_encoder_: sklearn.preprocessing.OneHotEncoder
OneHotEncoder acting on labels
"""
def __init__(self, classes=None):
"""
Parameters
----------
classes: list, optional
List of classes (numbers or strings) to be mapped
to `int` starting from zero in order.
"""
self.classes = classes
def fit(self, X, y=None):
"""
Parameters
----------
X: numpy.ndarray
Input to be encoded as label and subsequently one-hotted
y: dummy, optional
Returns
-------
self
"""
X = check_array(X)
self.label_encoder_l_ = [{v:k for k, v in enumerate(c)} for c in self.classes]
self.n_values_ = [len(enc.keys()) for enc in self.label_encoder_l_]
X_new = array([dict_transform(x, d) for x, d in zip(X.T, self.label_encoder_l_)]).T
self.one_hot_encoder_ = OneHotEncoder(n_values=self.n_values_)
self.one_hot_encoder_.fit(X_new)
return self
def transform(self, X, y=None):
"""
Parameters
----------
X: numpy.ndarray
Input to be encoded as label and subsequently one-hotted
y: dummy, optional
Returns
-------
sparse matrix
1 in i-th position implies i-th rule applies
"""
X = check_array(X)
return self.one_hot_encoder_.transform(array([dict_transform(x, d) for x, d in zip(X.T, self.label_encoder_l_)]).T)
class pdCategoricalTransformer(BaseEstimator, TransformerMixin):
""" sklearn transformer: Process categorical variables into dummy variables
Take all columns with dtype `object` (cast as `str`) not in `ignore_cols`
and convert to dummy variables. If binary, column name
remains the same. If categorical, prefix 'columnname__'
is added to dummy column.
Attributes
----------
bin_cols_: list of str
column names that are binary
cat_cols_: list of str
column names that are categorical
bin_refs_: dict
Keys are entries in `bin_cols_`, values are a list of two stored values.
First corresponds to zero, second to one.
cat_refs_: dict
Keys are entries in `cat_cols_`, values are a list of unique values
cat_drops_: dict
Keys are entries in `cat_cols_`, values are the reference category to drop
(If `drop_first=True`)
column_order_: list of str
Order of columns of fitted data to ensure transformed data retains feature ordering
"""
def __init__(self, max_cat=25, ignore_cols=None, drop_first=True):
"""
Parameters
----------
max_cat : int, optional
Maximum unique variables allowed to restrict
feature space getting too large
ignore_cols : list or str, optional
Columns to not convert to dummy variables
drop_first: bool, default: True
If True delete most prevalent indicator for each category
"""
self.max_cat = max_cat
self.ignore_cols = ignore_cols
self.drop_first = drop_first
def fit(self, X, y=None):
"""
Parameters
----------
X: DataFrame
Input data
y: dummy, optional
Returns
-------
self
"""
X = X.copy()
# List of columns to consider
cat_cols = set(X.columns[X.dtypes == object])
if self.ignore_cols is not None:
cat_cols = cat_cols - set(self.ignore_cols)
# See attributes
self.bin_cols_ = []
self.cat_cols_ = []
self.bin_refs_ = {}
self.cat_refs_ = {}
self.cat_drops_ = {}
for cn in cat_cols:
uniq = X[cn].astype(str).unique() # Unique column entries
if uniq.size == 1:
raise ValueError('Column {} only has one value, suggest dropping'.format(cn))
elif uniq.size == 2: # Binary
self.bin_cols_.append(cn)
uniq.sort()
self.bin_refs_[cn] = uniq.tolist() # Store what value corresponds to one
X.loc[:, cn] = X[cn] == uniq[1]
elif uniq.size <= self.max_cat: # Categorical
self.cat_cols_.append(cn)
self.cat_refs_[cn] = uniq.tolist() # Store dummy columns
dummies = get_dummies(X[cn], prefix='{}_'.format(cn), drop_first=False)
if self.drop_first:
drop = dummies.mean().argmax() # Store most prevalent category to drop
self.cat_drops_[cn] = drop
dummies = dummies.drop(drop, 1)
else:
pass
X = X.join(dummies).drop(cn, 1)
else:
raise ValueError('Column {} has {}>{} values. Increase `max_cat`.'.format(cn, uniq.size, self.max_cat))
self.column_order_ = X.columns.tolist() # Store column order
return self
def transform(self, X, y=None):
"""
Parameters
----------
X: DataFrame
Input data
y: dummy, optional
Returns
-------
DataFrame
"""
for cn in self.bin_cols_: # Binary
refs = self.bin_refs_[cn]
# Raise ValueError if unseen values
new = list(set(X[cn].astype(str).unique())-set(refs))
if len(new) > 0:
raise ValueError('Unseen values in {} for column {}'.format(X[cn].astype(str).unique(), cn))
X.loc[:, cn] = X[cn] == refs[1]
for cn in self.cat_cols_: # Categorical
refs = self.cat_refs_[cn]
dummies = get_dummies(X[cn], prefix='{}_'.format(cn), drop_first=False)
# Set values in train but not test to zero
missing = list(set(refs)-set(X[cn].astype(str).unique()))
for v in missing:
dummies['{}__{}'.format(cn, v)] = 0
# Raise ValueError if unseen values
new = list(set(X[cn].astype(str).unique())-set(refs))
if len(new) > 0:
raise ValueError('Unseen values in {} for column {}'.format(X[cn].astype(str).unique(), cn))
# Drop reference category
if self.drop_first:
drop = self.cat_drops_[cn]
dummies = dummies.drop(drop, 1)
else:
pass
X = X.join(dummies).drop(cn, 1)
assert X.shape[1] == len(self.column_order_), 'Size of fit {} and transform {} do not match'.format(X.shape[1], len(self.column_order_))
return X.loc[:, self.column_order_]
class ColumnExtractor(BaseEstimator, TransformerMixin):
""" Sklearn transformer: Extract column(s) (or complement of) within a pipeline.
"""
def __init__(self, column, complement=False):
"""
Parameters
----------
column: list-like or string
column(s) to extract from data
complement: bool, default: False
If True take all columns except those in `column`
"""
self.column = column
self.complement = complement
def transform(self, X, y=None):
"""Extract columns from input data
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
DataFrame with features to be extracted by name,
or array with features to be extracted by number
y: dummy, optional
Returns
-------
pandas.DataFrame
If `X` is a DataFrame
numpy.ndarray
If `X` is an array
"""
# Ensure `self.column` is list-like
if not isinstance(self.column, (list, tuple, ndarray)):
self.column = [self.column]
if isinstance(X, DataFrame):
if self.complement: # Take complement by name
return X.drop(self.column, 1)
else: # Take requested columns by name
return X.loc[:, self.column]
else:
if self.complement: # Take complement by number (preserve order)
return X[:, sort(list(set(arange(X.shape[1]))- set(self.column)))]
else: # Take requested columns by number
return X[:, self.column]
def fit(self, X, y=None):
""" Dummy method for sklearn API compatibility
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: dummy, optional
Returns
-------
self
"""
return self
class X_flatten(BaseEstimator, TransformerMixin):
"""Sklearn transformer: flatten data within a pipeline
Flatten and convert data to a numpy array.
Notes
-----
Primary use case is to format data into the right format
for something like `sklearn.feature_extraction.text.TfidfVectorizer`
"""
def fit(self, X, y=None):
""" Dummy method for sklearn API compatibility
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: dummy, optional
Returns
-------
self
"""
return self
def transform(self, X, y=None):
"""Apply flatten to data
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
Data to flatten
y: dummy, optional
Returns
-------
numpy.ndarray
"""
if isinstance(X, DataFrame):
return X.values.flatten()
else:
return X.flatten()
class pdFunctionTransformer(BaseEstimator, TransformerMixin):
"""Sklearn transformer: apply a function to dataframe
Wrapper function to apply a function to data within a sklearn
pipeline.
Examples
--------
>>> transformer = pdFunctionTransformer(binarise)
>>> transformer.transform(X) # Binarise data in X
"""
def __init__(self, fun, keep_cols=True):
"""
Parameters
----------
fun: function
Function to apply to data
keep_cols: bool, default: true
If True return a DataFrame rather than array
"""
self.fun = fun
self.keep_cols = keep_cols
def fit(self, X, y=None):
""" Dummy method for sklearn API compatibility
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: dummy, optional
Returns
-------
self
"""
return self
def transform(self, X, y=None):
""" Apply `fun` to `X`
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
Data to apply function to
y: dummy, optional
Returns
-------
pandas.DataFrame
If `keep_cols=True`
numpy.ndarray
If `keep_cols=False`
"""
if isinstance(X, DataFrame):
if self.keep_cols:
return DataFrame(self.fun(X.values), columns=X.columns)
else:
return self.fun(X.values)
else:
return self.fun(X)
class pdVectorizer(BaseEstimator, TransformerMixin):
"""Sklearn transformer: vectorise data
Wrapper transformer to vectorise a DataFrame
and output a DataFrame with columns as new feature names.
Examples
--------
>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> tf = pdVectorizer(TfidfVectorizer(max_features=100))
>>> tf.fit_transform(X)
"""
def __init__(self, tf):
"""
Parameters
----------
tf: sklearn Vectorizer transformer
"""
self.tf = tf
def fit(self, X, y=None):
""" Fit `tf`
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: dummy, optional
Returns
-------
self
"""
self.tf.fit(X, y)
return self
def transform(self, X, y=None):
""" Transform `X` with `tf` and output into DataFrame
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: dummy, optional
Returns
-------
pandas.DataFrame
DataFrame of vectorized counts corresponding to features created by `tf`
"""
return DataFrame(self.tf.transform(X).todense(),
columns=map(lambda x: 'W:'+x, self.tf.get_feature_names()))
# columns=map(lambda x: x, self.tf.get_feature_names()))
class pdTransformer(BaseEstimator, TransformerMixin):
"""Sklearn transformer: apply transformer to data
Pandas wrapper to output DataFrame from a transformer.
"""
def __init__(self, tf):
"""
Parameters
----------
tf: sklearn transformer
"""
self.tf = tf
def fit(self, X, y=None):
""" Dummy method for sklearn API compatibility
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: dummy, optional
Returns
-------
self
"""
self.tf.fit(X, y)
return self
def transform(self, X, y=None):
""" Apply `tf` to data and output DataFrame
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: dummy, optional
Returns
-------
DataFrame
"""
return DataFrame(self.tf.transform(X), columns=X.columns)
class pdFeatureUnion(BaseEstimator, TransformerMixin):
""" Pandas wrapper around `sklearn.pipeline.FeatureUnion`
Extend `sklearn.pipeline.FeatureUnion` to work with DataFrames.
"""
def __init__(self, union):
"""
union: sklearn.pipeline.FeatureUnion
FeatureUnion object to put into DataFrame
"""
self.union = union
def fit(self, X, y=None):
""" Fit `union`
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: pandas.Series or numpy.ndarray, optional
Returns
-------
self
"""
self.union.fit(X, y)
return self
def transform(self, X, y=None):
""" Take transformers from `union` and concatenate
into DataFrame
Parameters
----------
X: pandas.DataFrame or numpy.ndarray
y: pandas.Series or numpy.ndarray, optional
Returns
-------
pandas.DataFrame
Unionised features
"""
df = DataFrame([])
for _, transformer in self.union.transformer_list:
df = concat([df, transformer.transform(X).reset_index(drop=True)], 1)
return df