-
Notifications
You must be signed in to change notification settings - Fork 55
/
categorical.py
1033 lines (760 loc) · 30.1 KB
/
categorical.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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Categorical features transformerrs."""
from itertools import combinations
from typing import List
from typing import Optional
from typing import Sequence
from typing import Union
from typing import cast
import numpy as np
from pandas import DataFrame
from pandas import Series
from pandas import concat
from sklearn.preprocessing import OneHotEncoder
from sklearn.utils.murmurhash import murmurhash3_32
from ..dataset.base import LAMLDataset
from ..dataset.np_pd_dataset import CSRSparseDataset
from ..dataset.np_pd_dataset import NumpyDataset
from ..dataset.np_pd_dataset import PandasDataset
from ..dataset.roles import CategoryRole
from ..dataset.roles import NumericRole
from .base import LAMLTransformer
# type - something that can be convered to pandas dataset
NumpyOrPandas = Union[NumpyDataset, PandasDataset]
NumpyOrSparse = Union[NumpyDataset, CSRSparseDataset]
def categorical_check(dataset: LAMLDataset):
"""Check if all passed vars are categories.
Raises AssertionError if non-categorical features are present.
Args:
dataset: LAMLDataset to check.
"""
roles = dataset.roles
features = dataset.features
for f in features:
assert roles[f].name == "Category", "Only categories accepted in this transformer"
def oof_task_check(dataset: LAMLDataset):
"""Check if all passed vars are categories.
Args:
dataset: Input.
"""
task = dataset.task
assert task.name in [
"binary",
"reg",
], "Only binary and regression tasks supported in this transformer"
def multiclass_task_check(dataset: LAMLDataset):
"""Check if all passed vars are categories.
Args:
dataset: Input.
"""
task = dataset.task
assert task.name in ["multiclass"], "Only multiclass tasks supported in this transformer"
def encoding_check(dataset: LAMLDataset):
"""Check if all passed vars are categories.
Args:
dataset: Input.
"""
roles = dataset.roles
features = dataset.features
for f in features:
assert roles[
f
].label_encoded, "Transformer should be applied to category only after label encoding. Feat {0} is {1}".format(
f, roles[f]
)
class LabelEncoder(LAMLTransformer):
"""Simple LabelEncoder in order of frequency.
Labels are integers from 1 to n. Unknown category encoded as 0.
NaN is handled as a category value.
Args:
subs: Subsample to calculate freqs. If None - full data.
random_state: Random state to take subsample.
"""
_fit_checks = (categorical_check,)
_transform_checks = ()
_fname_prefix = "le"
# _output_role = CategoryRole(np.int32, label_encoded=True)
_fillna_val = 0
def __init__(self, subs: Optional[int] = None, random_state: int = 42):
self.subs = subs
self.random_state = random_state
self._output_role = CategoryRole(np.int32, label_encoded=True)
def _get_df(self, dataset: NumpyOrPandas) -> DataFrame:
"""Get df and sample.
Args:
dataset: Input dataset.
Returns:
Subsample.
"""
dataset = dataset.to_pandas()
df = dataset.data
if self.subs is not None and df.shape[0] >= self.subs:
subs = df.sample(n=self.subs, random_state=self.random_state)
else:
subs = df
return subs
def fit(self, dataset: NumpyOrPandas):
"""Estimate label frequencies and create encoding dicts.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
self.
"""
# set transformer names and add checks
super().fit(dataset)
# set transformer features
# convert to accepted dtype and get attributes
roles = dataset.roles
subs = self._get_df(dataset)
self.dicts = {}
for i in subs.columns:
role = roles[i]
# TODO: think what to do with this warning
co = role.unknown
cnts = (
subs[i]
.value_counts(dropna=False)
.reset_index()
.sort_values([i, "index"], ascending=[False, True])
.set_index("index")
)
vals = cnts[cnts[i] > co].index.values
self.dicts[i] = Series(np.arange(vals.shape[0], dtype=np.int32) + 1, index=vals)
return self
def transform(self, dataset: NumpyOrPandas) -> NumpyDataset:
"""Transform categorical dataset to int labels.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
Numpy dataset with encoded labels.
"""
# checks here
super().transform(dataset)
# convert to accepted dtype and get attributes
dataset = dataset.to_pandas()
df = dataset.data
# transform
new_arr = np.empty(dataset.shape, dtype=self._output_role.dtype)
for n, i in enumerate(df.columns):
# to be compatible with OrdinalEncoder
if i in self.dicts:
new_arr[:, n] = df[i].map(self.dicts[i]).fillna(self._fillna_val).values
else:
new_arr[:, n] = df[i].values.astype(self._output_role.dtype)
# create resulted
output = dataset.empty().to_numpy()
output.set_data(new_arr, self.features, self._output_role)
return output
class OHEEncoder(LAMLTransformer):
"""Simple OneHotEncoder over label encoded categories.
Args:
make_sparse: Create sparse matrix.
total_feats_cnt: Initial features number.
dtype: Dtype of new features.
"""
_fit_checks = (categorical_check, encoding_check)
_transform_checks = ()
_fname_prefix = "ohe"
@property
def features(self) -> List[str]:
"""Features list."""
return self._features
def __init__(
self,
make_sparse: Optional[bool] = None,
total_feats_cnt: Optional[int] = None,
dtype: type = np.float32,
):
self.make_sparse = make_sparse
self.total_feats_cnt = total_feats_cnt
self.dtype = dtype
if self.make_sparse is None:
assert self.total_feats_cnt is not None, "Param total_feats_cnt should be defined if make_sparse is None"
def fit(self, dataset: NumpyOrPandas):
"""Calc output shapes.
Automatically do ohe in sparse form if approximate fill_rate < `0.2`.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
self.
"""
# set transformer names and add checks
for check_func in self._fit_checks:
check_func(dataset)
# set transformer features
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
data = dataset.data
max_idx = data.max(axis=0)
min_idx = data.min(axis=0)
# infer make sparse
if self.make_sparse is None:
fill_rate = self.total_feats_cnt / (self.total_feats_cnt - max_idx.shape[0] + max_idx.sum())
self.make_sparse = fill_rate < 0.2
# create ohe
self.ohe = OneHotEncoder(
categories=[np.arange(x, y + 1, dtype=np.int32) for (x, y) in zip(min_idx, max_idx)],
# drop=np.ones(max_idx.shape[0], dtype=np.int32),
dtype=self.dtype,
sparse=self.make_sparse,
handle_unknown="ignore",
)
self.ohe.fit(data)
features = []
for cats, name in zip(self.ohe.categories_, dataset.features):
# cats = cats[cats != 1]
features.extend(["ohe_{0}__{1}".format(x, name) for x in cats])
self._features = features
return self
def transform(self, dataset: NumpyOrPandas) -> NumpyOrSparse:
"""Transform categorical dataset to ohe.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
Numpy dataset with encoded labels.
"""
# checks here
super().transform(dataset)
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
data = dataset.data
# transform
data = self.ohe.transform(data)
# create resulted
output = dataset.empty()
if self.make_sparse:
output = output.to_csr()
output.set_data(data, self.features, NumericRole(self.dtype))
return output
class FreqEncoder(LabelEncoder):
"""Labels are encoded with frequency in train data.
Labels are integers from 1 to n. Unknown category encoded as 1.
"""
_fit_checks = (categorical_check,)
_transform_checks = ()
_fname_prefix = "freq"
# _output_role = NumericRole(np.float32)
_fillna_val = 1
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._output_role = NumericRole(np.float32)
def fit(self, dataset: NumpyOrPandas):
"""Estimate label frequencies and create encoding dicts.
Args:
dataset: Pandas or Numpy dataset of categorical features
Returns:
self.
"""
# set transformer names and add checks
LAMLTransformer.fit(self, dataset)
# set transformer features
# convert to accepted dtype and get attributes
dataset = dataset.to_pandas()
df = dataset.data
self.dicts = {}
for i in df.columns:
# we make assertion in checks, so cast is ok
# TODO: think what to do with this warning
cnts = df[i].value_counts(dropna=False)
self.dicts[i] = cnts[cnts > 1]
return self
class TargetEncoder(LAMLTransformer):
"""Out-of-fold target encoding.
Limitation:
- Required .folds attribute in dataset - array of int from 0 to n_folds-1.
- Working only after label encoding.
Args:
alphas: Smooth coefficients.
"""
_fit_checks = (categorical_check, oof_task_check, encoding_check)
_transform_checks = ()
_fname_prefix = "oof"
def __init__(self, alphas: Sequence[float] = (0.5, 1.0, 2.0, 5.0, 10.0, 50.0, 250.0, 1000.0)):
self.alphas = alphas
@staticmethod
def binary_score_func(candidates: np.ndarray, target: np.ndarray) -> int:
"""Score candidates alpha with logloss metric.
Args:
candidates: Candidate oof encoders.
target: Target array.
Returns:
Index of best encoder.
"""
target = target[:, np.newaxis]
scores = -(target * np.log(candidates) + (1 - target) * np.log(1 - candidates)).mean(axis=0)
idx = scores.argmin()
return idx
@staticmethod
def reg_score_func(candidates: np.ndarray, target: np.ndarray) -> int:
"""Score candidates alpha with mse metric.
Args:
candidates: Candidate oof encoders.
target: Target array.
Returns:
Index of best encoder.
"""
target = target[:, np.newaxis]
scores = ((target - candidates) ** 2).mean(axis=0)
idx = scores.argmin()
return idx
def fit(self, dataset: NumpyOrPandas):
"""Fit encoder."""
super().fit_transform(dataset)
def fit_transform(self, dataset: NumpyOrPandas) -> NumpyDataset:
"""Calc oof encoding and save encoding stats for new data.
Args:
dataset: Pandas or Numpy dataset of categorical label encoded features.
Returns:
NumpyDataset - target encoded features.
"""
# set transformer names and add checks
super().fit(dataset)
# set transformer features
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
data = dataset.data
target = dataset.target.astype(np.int32)
score_func = self.binary_score_func if dataset.task.name == "binary" else self.reg_score_func
folds = dataset.folds
n_folds = folds.max() + 1
alphas = np.array(self.alphas)[np.newaxis, :]
self.encodings = []
prior = target.mean()
# folds priors
f_sum = np.zeros(n_folds, dtype=np.float64)
f_count = np.zeros(n_folds, dtype=np.float64)
np.add.at(f_sum, folds, target)
np.add.at(f_count, folds, 1)
folds_prior = (f_sum.sum() - f_sum) / (f_count.sum() - f_count)
oof_feats = np.zeros(data.shape, dtype=np.float32)
for n in range(data.shape[1]):
vec = data[:, n]
# calc folds stats
enc_dim = vec.max() + 1
f_sum = np.zeros((enc_dim, n_folds), dtype=np.float64)
f_count = np.zeros((enc_dim, n_folds), dtype=np.float64)
np.add.at(f_sum, (vec, folds), target)
np.add.at(f_count, (vec, folds), 1)
# calc total stats
t_sum = f_sum.sum(axis=1, keepdims=True)
t_count = f_count.sum(axis=1, keepdims=True)
# calc oof stats
oof_sum = t_sum - f_sum
oof_count = t_count - f_count
# calc candidates alpha
candidates = (
(oof_sum[vec, folds, np.newaxis] + alphas * folds_prior[folds, np.newaxis])
/ (oof_count[vec, folds, np.newaxis] + alphas)
).astype(np.float32)
idx = score_func(candidates, target)
# write best alpha
oof_feats[:, n] = candidates[:, idx]
# calc best encoding
enc = ((t_sum[:, 0] + alphas[0, idx] * prior) / (t_count[:, 0] + alphas[0, idx])).astype(np.float32)
self.encodings.append(enc)
output = dataset.empty()
self.output_role = NumericRole(np.float32, prob=output.task.name == "binary")
output.set_data(oof_feats, self.features, self.output_role)
return output
def transform(self, dataset: NumpyOrPandas) -> NumpyOrSparse:
"""Transform categorical dataset to target encoding.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
Numpy dataset with encoded labels.
"""
# checks here
super().transform(dataset)
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
data = dataset.data
# transform
out = np.zeros(data.shape, dtype=np.float32)
for n, enc in enumerate(self.encodings):
out[:, n] = enc[data[:, n]]
# create resulted
output = dataset.empty()
output.set_data(out, self.features, self.output_role)
return output
class MultiClassTargetEncoder(LAMLTransformer):
"""Out-of-fold target encoding for multiclass task.
Limitation:
- Required .folds attribute in dataset - array of int from 0 to n_folds-1.
- Working only after label encoding
"""
_fit_checks = (categorical_check, multiclass_task_check, encoding_check)
_transform_checks = ()
_fname_prefix = "multioof"
@property
def features(self) -> List[str]:
"""List of features."""
return self._features
def __init__(self, alphas: Sequence[float] = (0.5, 1.0, 2.0, 5.0, 10.0, 50.0, 250.0, 1000.0)):
self.alphas = alphas
@staticmethod
def score_func(candidates: np.ndarray, target: np.ndarray) -> int:
"""Choose the best encoder.
Args:
candidates: np.ndarray.
target: np.ndarray.
Returns:
index of best encoder.
"""
target = target[:, np.newaxis, np.newaxis]
scores = -np.log(np.take_along_axis(candidates, target, axis=1)).mean(axis=0)[0]
idx = scores.argmin()
return idx
def fit_transform(self, dataset: NumpyOrPandas) -> NumpyDataset:
"""Estimate label frequencies and create encoding dicts.
Args:
dataset: Pandas or Numpy dataset of categorical label encoded features.
Returns:
NumpyDataset - target encoded features.
"""
# set transformer names and add checks
for check_func in self._fit_checks:
check_func(dataset)
# set transformer features
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
data = dataset.data
target = dataset.target.astype(np.int32)
n_classes = target.max() + 1
self.n_classes = n_classes
folds = dataset.folds
n_folds = folds.max() + 1
alphas = np.array(self.alphas)[np.newaxis, np.newaxis, :]
self.encodings = []
# prior
prior = cast(np.ndarray, np.arange(n_classes)[:, np.newaxis] == target).mean(axis=1)
# folds prior
f_sum = np.zeros((n_classes, n_folds), dtype=np.float64)
f_count = np.zeros((1, n_folds), dtype=np.float64)
np.add.at(f_sum, (target, folds), 1)
np.add.at(f_count, (0, folds), 1)
# N_classes x N_folds
folds_prior = ((f_sum.sum(axis=1, keepdims=True) - f_sum) / (f_count.sum(axis=1, keepdims=True) - f_count)).T
oof_feats = np.zeros(data.shape + (n_classes,), dtype=np.float32)
self._features = []
for i in dataset.features:
for j in range(n_classes):
self._features.append("{0}_{1}__{2}".format("multioof", j, i))
for n in range(data.shape[1]):
vec = data[:, n]
# calc folds stats
enc_dim = vec.max() + 1
f_sum = np.zeros((enc_dim, n_classes, n_folds), dtype=np.float64)
f_count = np.zeros((enc_dim, 1, n_folds), dtype=np.float64)
np.add.at(f_sum, (vec, target, folds), 1)
np.add.at(f_count, (vec, 0, folds), 1)
# calc total stats
t_sum = f_sum.sum(axis=2, keepdims=True)
t_count = f_count.sum(axis=2, keepdims=True)
# calc oof stats
oof_sum = t_sum - f_sum
oof_count = t_count - f_count
# (N x N_classes x 1 + 1 x 1 x N_alphas * N x N_classes x 1) / (N x 1 x 1 + N x 1 x 1) -> N x N_classes x N_alphas
candidates = (
(oof_sum[vec, :, folds, np.newaxis] + alphas * folds_prior[folds, :, np.newaxis])
/ (oof_count[vec, :, folds, np.newaxis] + alphas)
).astype(np.float32)
# norm over 1 axis
candidates /= candidates.sum(axis=1, keepdims=True)
idx = self.score_func(candidates, target)
oof_feats[:, n] = candidates[..., idx]
enc = ((t_sum[..., 0] + alphas[0, 0, idx] * prior) / (t_count[..., 0] + alphas[0, 0, idx])).astype(
np.float32
)
enc /= enc.sum(axis=1, keepdims=True)
self.encodings.append(enc)
output = dataset.empty()
output.set_data(
oof_feats.reshape((data.shape[0], -1)),
self.features,
NumericRole(np.float32, prob=True),
)
return output
def transform(self, dataset: NumpyOrPandas) -> NumpyOrSparse:
"""Transform categorical dataset to target encoding.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
Numpy dataset with encoded labels.
"""
# checks here
super().transform(dataset)
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
data = dataset.data
# transform
out = np.zeros(data.shape + (self.n_classes,), dtype=np.float32)
for n, enc in enumerate(self.encodings):
out[:, n] = enc[data[:, n]]
out = out.reshape((data.shape[0], -1))
# create resulted
output = dataset.empty()
output.set_data(out, self.features, NumericRole(np.float32, prob=True))
return output
class MultioutputTargetEncoder(LAMLTransformer):
"""Out-of-fold target encoding for multi:reg and multilabel task.
Limitation:
- Required .folds attribute in dataset - array of int from 0 to n_folds-1.
- Working only after label encoding
"""
_fit_checks = ()
_transform_checks = ()
_fname_prefix = "multioutgoof"
@property
def features(self) -> List[str]:
"""Return feature list."""
return self._features
def __init__(self, alphas: Sequence[float] = (0.5, 1.0, 2.0, 5.0, 10.0, 50.0, 250.0, 1000.0)):
self.alphas = alphas
@staticmethod
def reg_score_func(candidates: np.ndarray, target: np.ndarray) -> int:
"""Compute statistics for regression tasks.
Args:
candidates: np.ndarray.
target: np.ndarray.
Returns:
index of best encoder.
"""
target = target[:, :, np.newaxis]
scores = ((target - candidates) ** 2).mean(axis=0)
idx = scores[0].argmin()
return idx
@staticmethod
def class_score_func(candidates: np.ndarray, target: np.ndarray) -> int:
"""Compute statistics for each class.
Args:
candidates: np.ndarray.
target: np.ndarray.
Returns:
index of best encoder.
"""
target = target[:, :, np.newaxis]
scores = -(target * np.log(candidates) + (1 - target) * np.log(1 - candidates)).mean(axis=0)
idx = scores[0].argmin()
return idx
def fit_transform(self, dataset):
"""Estimate label frequencies and create encoding dicts.
Args:
dataset: Pandas or Numpy dataset of categorical label encoded features.
Returns:
NumpyDataset - target encoded features.
"""
# set transformer names and add checks
for check_func in self._fit_checks:
check_func(dataset)
# set transformer features
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
score_func = self.class_score_func if dataset.task.name == "multilabel" else self.reg_score_func
data = dataset.data
target = dataset.target.astype(np.float32)
n_classes = int(target.shape[1])
self.n_classes = n_classes
folds = dataset.folds.astype(int)
n_folds = int(folds.max() + 1)
alphas = np.array(self.alphas)[np.newaxis, np.newaxis, :]
self.encodings = []
# prior
prior = cast(np.ndarray, target).mean(axis=0)
# folds prior
f_sum = np.zeros((n_folds, n_classes), dtype=np.float64)
f_count = np.zeros((1, n_folds), dtype=np.float64)
np.add.at(f_sum, (folds,), target)
np.add.at(f_count, (0, folds), 1)
f_sum = f_sum.T
# N_classes x N_folds
folds_prior = ((f_sum.sum(axis=1, keepdims=True) - f_sum) / (f_count.sum(axis=1, keepdims=True) - f_count)).T
oof_feats = np.zeros(data.shape + (n_classes,), dtype=np.float32)
self._features = []
for i in dataset.features:
for j in range(n_classes):
self._features.append("{0}_{1}__{2}".format("multioof", j, i))
for n in range(data.shape[1]):
vec = data[:, n].astype(int)
# calc folds stats
enc_dim = int(vec.max() + 1)
f_sum = np.zeros((enc_dim, n_folds, n_classes), dtype=np.float64)
f_count = np.zeros((enc_dim, 1, n_folds), dtype=np.float64)
np.add.at(
f_sum,
(
vec,
folds,
),
target,
)
np.add.at(f_count, (vec, 0, folds), 1)
f_sum = np.moveaxis(f_sum, 2, 1)
# calc total stats
t_sum = f_sum.sum(axis=2, keepdims=True)
t_count = f_count.sum(axis=2, keepdims=True)
# calc oof stats
oof_sum = t_sum - f_sum
oof_count = t_count - f_count
# (N x N_classes x 1 + 1 x 1 x N_alphas * N x N_classes x 1) / (N x 1 x 1 + N x 1 x 1) -> N x N_classes x N_alphas
candidates = (
(oof_sum[vec, :, folds, np.newaxis] + alphas * folds_prior[folds, :, np.newaxis])
/ (oof_count[vec, :, folds, np.newaxis] + alphas)
).astype(np.float32)
# norm over 1 axis
candidates /= candidates.sum(axis=1, keepdims=True)
idx = score_func(candidates, target)
oof_feats[:, n] = candidates[..., idx]
enc = ((t_sum[..., 0] + alphas[0, 0, idx] * prior) / (t_count[..., 0] + alphas[0, 0, idx])).astype(
np.float32
)
enc /= enc.sum(axis=1, keepdims=True)
self.encodings.append(enc)
output = dataset.empty()
output.set_data(
oof_feats.reshape((data.shape[0], -1)),
self.features,
NumericRole(np.float32, prob=dataset.task.name == "multilabel"),
)
return output
def transform(self, dataset):
"""Transform categorical dataset to target encoding.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
Numpy dataset with encoded labels.
"""
# checks here
super().transform(dataset)
# convert to accepted dtype and get attributes
dataset = dataset.to_numpy()
data = dataset.data
# transform
out = np.zeros(data.shape + (self.n_classes,), dtype=np.float32)
for n, enc in enumerate(self.encodings):
out[:, n] = enc[data[:, n].astype(int)]
out = out.reshape((data.shape[0], -1))
# create resulted
output = dataset.empty()
output.set_data(out, self.features, NumericRole(np.float32, prob=dataset.task.name == "multilabel"))
return output
class CatIntersectstions(LabelEncoder):
"""Build label encoded intertsections of categorical variables.
Args:
intersections: Columns to create intersections.
Default is None - all.
max_depth: Max intersection depth.
"""
_fit_checks = (categorical_check,)
_transform_checks = ()
_fname_prefix = "inter"
def __init__(
self,
subs: Optional[int] = None,
random_state: int = 42,
intersections: Optional[Sequence[Sequence[str]]] = None,
max_depth: int = 2,
):
super().__init__(subs, random_state)
self.intersections = intersections
self.max_depth = max_depth
@staticmethod
def _make_category(df: DataFrame, cols: Sequence[str]) -> np.ndarray:
"""Make hash for category interactions.
Args:
df: Input DataFrame
cols: List of columns
Returns:
Hash np.ndarray.
"""
res = np.empty((df.shape[0],), dtype=np.int32)
for n, inter in enumerate(zip(*(df[x] for x in cols))):
h = murmurhash3_32("_".join(map(str, inter)), seed=42)
res[n] = h
return res
def _build_df(self, dataset: NumpyOrPandas) -> PandasDataset:
"""Perform encoding.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
Dataset.
"""
dataset = dataset.to_pandas()
df = dataset.data
roles = {}
new_df = DataFrame(index=df.index)
for comb in self.intersections:
name = "({0})".format("__".join(comb))
new_df[name] = self._make_category(df, comb)
roles[name] = CategoryRole(
object,
unknown=max((dataset.roles[x].unknown for x in comb)),
label_encoded=True,
)
output = dataset.empty()
output.set_data(new_df, new_df.columns, roles)
return output
def fit(self, dataset: NumpyOrPandas):
"""Create label encoded intersections and save mapping.
Args:
dataset: Pandas or Numpy dataset of categorical features.
Returns:
self.
"""
# set transformer names and add checks
for check_func in self._fit_checks:
check_func(dataset)
if self.intersections is None:
self.intersections = []
for i in range(2, min(self.max_depth, len(dataset.features)) + 1):
self.intersections.extend(list(combinations(dataset.features, i)))
inter_dataset = self._build_df(dataset)
return super().fit(inter_dataset)
def transform(self, dataset: NumpyOrPandas) -> NumpyDataset:
"""Create label encoded intersections and apply mapping.
Args:
dataset: Pandas or Numpy dataset of categorical features
Returns:
Transformed dataset.
"""
inter_dataset = self._build_df(dataset)
return super().transform(inter_dataset)
class OrdinalEncoder(LabelEncoder):
"""Encoding ordinal categories into numbers.
Number type categories passed as is,
object type sorted in ascending lexicographical order.
"""
_fit_checks = (categorical_check,)
_transform_checks = ()
_fname_prefix = "ord"
# _output_role = NumericRole(np.float32)
_fillna_val = np.nan
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._output_role = NumericRole(np.float32)
def fit(self, dataset: NumpyOrPandas):
"""Estimate label frequencies and create encoding dicts.