/
encoders.py
745 lines (651 loc) · 28.3 KB
/
encoders.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
# Copyright (c) 2020-2023, NVIDIA 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 warnings
from typing import List, Optional, TypeVar
import cuml.internals.logger as logger
from cudf import DataFrame, Series
from cuml import Base
from cuml.common.doc_utils import generate_docstring
from cuml.common.exceptions import NotFittedError
from cuml.internals.safe_imports import (
cpu_only_import,
gpu_only_import,
gpu_only_import_from,
)
from cuml.preprocessing import LabelEncoder
np = cpu_only_import("numpy")
cudf = gpu_only_import("cudf")
cp = gpu_only_import("cupy")
cupyx = gpu_only_import("cupyx")
GenericIndex = gpu_only_import_from("cudf", "GenericIndex")
class CheckFeaturesMixIn:
def _check_n_features(self, X, reset: bool = False):
n_features = X.shape[1]
if reset:
self.n_features_in_ = n_features
if hasattr(X, "columns"):
self.feature_names_in_ = [str(c) for c in X.columns]
else:
if not hasattr(self, "n_features_in_"):
raise RuntimeError(
"The reset parameter is False but there is no "
"n_features_in_ attribute. Is this estimator fitted?"
)
if n_features != self.n_features_in_:
raise ValueError(
"X has {} features, but this {} is expecting {} features "
"as input.".format(
n_features,
self.__class__.__name__,
self.n_features_in_,
)
)
class BaseEncoder(Base, CheckFeaturesMixIn):
"""Base implementation for encoding categorical values, uses
:py:class:`~cuml.preprocessing.LabelEncoder` for obtaining unique values.
Parameters
----------
handle : cuml.Handle
Specifies the cuml.handle that holds internal CUDA state for
computations in this model. Most importantly, this specifies the CUDA
stream that will be used for the model's computations, so users can
run different models concurrently in different streams by creating
handles in several streams.
If it is None, a new one is created.
verbose : int or boolean, default=False
Sets logging level. It must be one of `cuml.common.logger.level_*`.
See :ref:`verbosity-levels` for more info.
output_type : {'input', 'array', 'dataframe', 'series', 'df_obj', \
'numba', 'cupy', 'numpy', 'cudf', 'pandas'}, default=None
Return results and set estimator attributes to the indicated output
type. If None, the output type set at the module level
(`cuml.global_settings.output_type`) will be used. See
:ref:`output-data-type-configuration` for more info.
"""
def _set_input_type(self, value):
if self.input_type is None:
self.input_type = value
def _check_input(self, X, is_categories=False):
"""If input is cupy, convert it to a DataFrame with 0 copies."""
if isinstance(X, cp.ndarray):
self._set_input_type("array")
if is_categories:
X = X.transpose()
return DataFrame(X)
else:
self._set_input_type("df")
return X
def _check_input_fit(self, X, is_categories=False):
"""Helper function used in fit, can be overridden in subclasses."""
self._check_n_features(X, reset=True)
return self._check_input(X, is_categories=is_categories)
def _unique(self, inp):
"""Helper function used in fit. Can be overridden in subclasses."""
# Default implementation passes input through directly since this is
# performed in `LabelEncoder.fit()`
return inp
def _fit(self, X, need_drop: bool):
X = self._check_input_fit(X)
if type(self.categories) is str and self.categories == "auto":
self._features = X.columns
self._encoders = {
feature: LabelEncoder(
handle=self.handle,
verbose=self.verbose,
output_type=self.output_type,
handle_unknown=self.handle_unknown,
).fit(self._unique(X[feature]))
for feature in self._features
}
else:
self.categories = self._check_input_fit(self.categories, True)
self._features = self.categories.columns
if len(self._features) != X.shape[1]:
raise ValueError(
"Shape mismatch: if categories is not 'auto',"
" it has to be of shape (n_features, _)."
)
self._encoders = dict()
for feature in self._features:
le = LabelEncoder(
handle=self.handle,
verbose=self.verbose,
output_type=self.output_type,
handle_unknown=self.handle_unknown,
)
self._encoders[feature] = le.fit(self.categories[feature])
if self.handle_unknown == "error":
if self._has_unknown(
X[feature], self._encoders[feature].classes_
):
msg = (
"Found unknown categories in column {0}"
" during fit".format(feature)
)
raise KeyError(msg)
if need_drop:
self.drop_idx_ = self._compute_drop_idx()
self._fitted = True
@property
def categories_(self):
"""Returns categories used for the one hot encoding in the correct order."""
return [self._encoders[f].classes_ for f in self._features]
class OneHotEncoder(BaseEncoder):
"""
Encode categorical features as a one-hot numeric array.
The input to this estimator should be a :py:class:`cuDF.DataFrame` or a
:py:class:`cupy.ndarray`, denoting the unique values taken on by categorical
(discrete) features. The features are encoded using a one-hot (aka 'one-of-K' or
'dummy') encoding scheme. This creates a binary column for each category and returns
a sparse matrix or dense array (depending on the ``sparse`` parameter).
By default, the encoder derives the categories based on the unique values
in each feature. Alternatively, you can also specify the `categories`
manually.
.. note:: a one-hot encoding of y labels should use a LabelBinarizer
instead.
Parameters
----------
categories : 'auto' an cupy.ndarray or a cudf.DataFrame, default='auto'
Categories (unique values) per feature:
- 'auto' : Determine categories automatically from the training data.
- DataFrame/ndarray : ``categories[col]`` holds the categories expected
in the feature col.
drop : 'first', None, a dict or a list, default=None
Specifies a methodology to use to drop one of the categories per
feature. This is useful in situations where perfectly collinear
features cause problems, such as when feeding the resulting data
into a neural network or an unregularized regression.
- None : retain all features (the default).
- 'first' : drop the first category in each feature. If only one
category is present, the feature will be dropped entirely.
- dict/list : ``drop[col]`` is the category in feature col that
should be dropped.
sparse : bool, default=True
This feature is not fully supported by cupy
yet, causing incorrect values when computing one hot encodings.
See https://github.com/cupy/cupy/issues/3223
dtype : number type, default=np.float
Desired datatype of transform's output.
handle_unknown : {'error', 'ignore'}, default='error'
Whether to raise an error or ignore if an unknown categorical feature
is present during transform (default is to raise). When this parameter
is set to 'ignore' and an unknown category is encountered during
transform, the resulting one-hot encoded columns for this feature
will be all zeros. In the inverse transform, an unknown category
will be denoted as None.
handle : cuml.Handle
Specifies the cuml.handle that holds internal CUDA state for
computations in this model. Most importantly, this specifies the CUDA
stream that will be used for the model's computations, so users can
run different models concurrently in different streams by creating
handles in several streams.
If it is None, a new one is created.
verbose : int or boolean, default=False
Sets logging level. It must be one of `cuml.common.logger.level_*`.
See :ref:`verbosity-levels` for more info.
output_type : {'input', 'array', 'dataframe', 'series', 'df_obj', \
'numba', 'cupy', 'numpy', 'cudf', 'pandas'}, default=None
Return results and set estimator attributes to the indicated output
type. If None, the output type set at the module level
(`cuml.global_settings.output_type`) will be used. See
:ref:`output-data-type-configuration` for more info.
Attributes
----------
drop_idx_ : array of shape (n_features,)
``drop_idx_[i]`` is the index in ``categories_[i]`` of the category to
be dropped for each feature. None if all the transformed features will
be retained.
"""
def __init__(
self,
*,
categories="auto",
drop=None,
sparse=True,
dtype=np.float32,
handle_unknown="error",
handle=None,
verbose=False,
output_type=None,
):
super().__init__(
handle=handle, verbose=verbose, output_type=output_type
)
self.categories = categories
self.sparse = sparse
self.dtype = dtype
self.handle_unknown = handle_unknown
self.drop = drop
self._fitted = False
self.drop_idx_ = None
self._features = None
self._encoders = None
self.input_type = None
if sparse and np.dtype(dtype) not in ["f", "d", "F", "D"]:
raise ValueError(
"Only float32, float64, complex64 and complex128 "
"are supported when using sparse"
)
def _validate_keywords(self):
if self.handle_unknown not in ("error", "ignore"):
msg = (
"handle_unknown should be either 'error' or 'ignore', "
"got {0}.".format(self.handle_unknown)
)
raise ValueError(msg)
# If we have both dropped columns and ignored unknown
# values, there will be ambiguous cells. This creates difficulties
# in interpreting the model.
if self.drop is not None and self.handle_unknown != "error":
raise ValueError(
"`handle_unknown` must be 'error' when the drop parameter is "
"specified, as both would create categories that are all "
"zero."
)
def _check_is_fitted(self):
if not self._fitted:
msg = (
"This OneHotEncoder instance is not fitted yet. Call 'fit' "
"with appropriate arguments before using this estimator."
)
raise NotFittedError(msg)
def _compute_drop_idx(self):
"""Helper to compute indices to drop from category to drop."""
if self.drop is None:
return None
elif isinstance(self.drop, str) and self.drop == "first":
return {feature: 0 for feature in self._encoders.keys()}
elif isinstance(self.drop, (dict, list)):
if isinstance(self.drop, list):
self.drop = dict(zip(range(len(self.drop)), self.drop))
if len(self.drop.keys()) != len(self._encoders):
msg = (
"`drop` should have as many columns as the number "
"of features ({}), got {}"
)
raise ValueError(
msg.format(len(self._encoders), len(self.drop.keys()))
)
drop_idx = dict()
for feature in self.drop.keys():
self.drop[feature] = Series(self.drop[feature])
if len(self.drop[feature]) != 1:
msg = (
"Trying to drop multiple values for feature {}, "
"this is not supported."
).format(feature)
raise ValueError(msg)
cats = self._encoders[feature].classes_
if not self.drop[feature].isin(cats).all():
msg = (
"Some categories for feature {} were supposed "
"to be dropped, but were not found in the encoder "
"categories.".format(feature)
)
raise ValueError(msg)
cats = Series(cats)
idx = cats.isin(self.drop[feature])
drop_idx[feature] = cp.asarray(cats[idx].index)
return drop_idx
else:
msg = (
"Wrong input for parameter `drop`. Expected "
"'first', None or a dict, got {}"
)
raise ValueError(msg.format(type(self.drop)))
def _check_input_fit(self, X, is_categories=False):
"""Helper function used in fit. Can be overridden in subclasses."""
return self._check_input(X, is_categories=is_categories)
def _has_unknown(self, X_cat, encoder_cat):
"""Check if X_cat has categories that are not present in encoder_cat."""
return not X_cat.isin(encoder_cat).all()
@generate_docstring(y=None)
def fit(self, X, y=None):
"""Fit OneHotEncoder to X."""
self._validate_keywords()
self._fit(X, True)
return self
@generate_docstring(
y=None,
return_values={
"name": "X_out",
"description": "Transformed input.",
"type": "sparse matrix if sparse=True else a 2-d array",
},
)
def fit_transform(self, X, y=None):
"""
Fit OneHotEncoder to X, then transform X. Equivalent to fit(X).transform(X).
"""
X = self._check_input(X)
return self.fit(X).transform(X)
@generate_docstring(
return_values={
"name": "X_out",
"description": "Transformed input.",
"type": "sparse matrix if sparse=True else a 2-d array",
}
)
def transform(self, X):
"""Transform X using one-hot encoding."""
self._check_is_fitted()
X = self._check_input(X)
cols, rows = list(), list()
col_idx = None
j = 0
try:
for feature in X.columns:
encoder = self._encoders[feature]
col_idx = encoder.transform(X[feature])
idx_to_keep = col_idx.notnull().to_cupy()
col_idx = col_idx.dropna().to_cupy()
# Simple test to auto upscale col_idx type as needed
# First, determine the maximum value we will add assuming
# monotonically increasing up to len(encoder.classes_)
# Ensure we dont go negative by clamping to 0
max_value = int(max(len(encoder.classes_) - 1, 0) + j)
# If we exceed the max value, upconvert
if max_value > np.iinfo(col_idx.dtype).max:
col_idx = col_idx.astype(np.min_scalar_type(max_value))
logger.debug(
"Upconverting column: '{}', to dtype: '{}', "
"to support up to {} classes".format(
feature, np.min_scalar_type(max_value), max_value
)
)
# increase indices to take previous features into account
col_idx += j
# Filter out rows with null values
row_idx = cp.arange(len(X))[idx_to_keep]
if self.drop_idx_ is not None:
drop_idx = self.drop_idx_[feature] + j
mask = cp.ones(col_idx.shape, dtype=bool)
mask[col_idx == drop_idx] = False
col_idx = col_idx[mask]
row_idx = row_idx[mask]
# account for dropped category in indices
col_idx[col_idx > drop_idx] -= 1
# account for dropped category in current cats number
j -= 1
j += len(encoder.classes_)
cols.append(col_idx)
rows.append(row_idx)
cols = cp.concatenate(cols)
rows = cp.concatenate(rows)
val = cp.ones(rows.shape[0], dtype=self.dtype)
ohe = cupyx.scipy.sparse.coo_matrix(
(val, (rows, cols)), shape=(len(X), j), dtype=self.dtype
)
if not self.sparse:
ohe = ohe.toarray()
return ohe
except TypeError as e:
# Append to cols to include the column that threw the error
cols.append(col_idx)
# Build a string showing what the types are
input_types_str = ", ".join([str(x.dtype) for x in cols])
raise TypeError(
"A TypeError occurred while calculating column "
"category indices, most likely due to integer overflow. This "
"can occur when columns have a large difference in the number "
"of categories, resulting in different category code dtypes "
"for different columns."
"Calculated column code dtypes: {}.\n"
"Internal Error: {}".format(input_types_str, repr(e))
)
def inverse_transform(self, X):
"""Convert the data back to the original representation. In case unknown
categories are encountered (all zeros in the one-hot encoding), ``None`` is used
to represent this category.
The return type is the same as the type of the input used by the first
call to fit on this estimator instance.
Parameters
----------
X : array-like or sparse matrix, shape [n_samples, n_encoded_features]
The transformed data.
Returns
-------
X_tr : cudf.DataFrame or cupy.ndarray
Inverse transformed array.
"""
self._check_is_fitted()
if cupyx.scipy.sparse.issparse(X):
# cupyx.scipy.sparse 7.x does not support argmax,
# when we upgrade cupy to 8.x, we should add a condition in the
# if close: `and not cupyx.scipy.sparse.issparsecsc(X)`
# and change the following line by `X = X.tocsc()`
X = X.toarray()
result = DataFrame(columns=self._encoders.keys())
j = 0
for feature in self._encoders.keys():
feature_enc = self._encoders[feature]
cats = feature_enc.classes_
if self.drop is not None:
# Remove dropped categories
dropped_class_idx = Series(self.drop_idx_[feature])
dropped_class_mask = Series(cats).isin(cats[dropped_class_idx])
if len(cats) == 1:
inv = Series(GenericIndex(cats[0]).repeat(X.shape[0]))
result[feature] = inv
continue
cats = cats[~dropped_class_mask]
enc_size = len(cats)
x_feature = X[:, j : j + enc_size]
idx = cp.argmax(x_feature, axis=1)
inv = Series(cats.iloc[idx]).reset_index(drop=True)
if self.handle_unknown == "ignore":
not_null_idx = x_feature.any(axis=1)
inv.iloc[~not_null_idx] = None
elif self.drop is not None:
# drop will either be None or handle_unknown will be error. If
# self.drop is not None, then we can safely assume that all of
# the nulls in each column are the dropped value
dropped_mask = cp.asarray(x_feature.sum(axis=1) == 0).flatten()
if dropped_mask.any():
inv[dropped_mask] = feature_enc.inverse_transform(
Series(self.drop_idx_[feature])
)[0]
result[feature] = inv
j += enc_size
if self.input_type == "array":
try:
result = result.to_cupy()
except ValueError:
warnings.warn(
"The input one hot encoding contains rows with "
"unknown categories. Since device arrays do not "
"support null values, the output will be "
"returned as a DataFrame "
"instead."
)
return result
def get_feature_names(self, input_features=None):
"""Return feature names for output features.
Parameters
----------
input_features : list of str of shape (n_features,)
String names for input features if available. By default,
"x0", "x1", ... "xn_features" is used.
Returns
-------
output_feature_names : ndarray of shape (n_output_features,)
Array of feature names.
"""
self._check_is_fitted()
cats = self.categories_
if input_features is None:
input_features = ["x%d" % i for i in range(len(cats))]
elif len(input_features) != len(self.categories_):
raise ValueError(
"input_features should have length equal to number of "
"features ({}), got {}".format(
len(self.categories_), len(input_features)
)
)
feature_names = []
for i in range(len(cats)):
names = [
input_features[i] + "_" + str(t) for t in cats[i].values_host
]
if self.drop_idx_ is not None and self.drop_idx_[i] is not None:
names.pop(self.drop_idx_[i])
feature_names.extend(names)
return np.array(feature_names, dtype=object)
def get_param_names(self):
return super().get_param_names() + [
"categories",
"drop",
"sparse",
"dtype",
"handle_unknown",
]
def _slice_feat(X, i):
if hasattr(X, "iloc"):
return X[i]
return X[:, i]
def _get_output(
output_type: Optional[str],
input_type: Optional[str],
out: DataFrame,
dtype,
):
if output_type == "input":
if input_type == "array":
output_type = "cupy"
elif input_type == "df":
output_type = "cudf"
if output_type is None:
output_type = "cupy"
if output_type == "cudf":
return out
elif output_type == "cupy":
return out.astype(dtype).to_cupy(na_value=np.nan)
elif output_type == "numpy":
return cp.asnumpy(out.to_cupy(na_value=np.nan, dtype=dtype))
elif output_type == "pandas":
return out.to_pandas()
else:
raise ValueError("Unsupported output type.")
class OrdinalEncoder(BaseEncoder):
def __init__(
self,
*,
categories="auto",
dtype=np.float64,
handle_unknown="error",
handle=None,
verbose=False,
output_type=None,
) -> None:
"""Encode categorical features as an integer array.
The input to this transformer should be an :py:class:`cudf.DataFrame` or a
:py:class:`cupy.ndarray`, denoting the unique values taken on by categorical
(discrete) features. The features are converted to ordinal integers. This
results in a single column of integers (0 to n_categories - 1) per feature.
Parameters
----------
categories : 'auto' an cupy.ndarray or a cudf.DataFrame, default='auto'
Categories (unique values) per feature:
- 'auto' : Determine categories automatically from the training data.
- DataFrame/ndarray : ``categories[col]`` holds the categories expected
in the feature col.
handle_unknown : {'error', 'ignore'}, default='error'
Whether to raise an error or ignore if an unknown categorical feature is
present during transform (default is to raise). When this parameter is set
to 'ignore' and an unknown category is encountered during transform, the
resulting encoded value would be null when output type is cudf
dataframe.
handle : cuml.Handle
Specifies the cuml.handle that holds internal CUDA state for computations in
this model. Most importantly, this specifies the CUDA stream that will be
used for the model's computations, so users can run different models
concurrently in different streams by creating handles in several streams.
If it is None, a new one is created.
verbose : int or boolean, default=False
Sets logging level. It must be one of `cuml.common.logger.level_*`. See
:ref:`verbosity-levels` for more info.
output_type : {'input', 'array', 'dataframe', 'series', 'df_obj', \
'numba', 'cupy', 'numpy', 'cudf', 'pandas'}, default=None
Return results and set estimator attributes to the indicated output
type. If None, the output type set at the module level
(`cuml.global_settings.output_type`) will be used. See
:ref:`output-data-type-configuration` for more info.
"""
super().__init__(
handle=handle, verbose=verbose, output_type=output_type
)
self.categories = categories
self.dtype = dtype
self.handle_unknown = handle_unknown
self.input_type = None
@generate_docstring(y=None)
def fit(self, X, y=None) -> "OrdinalEncoder":
"""Fit Ordinal to X."""
self._fit(X, need_drop=False)
return self
@generate_docstring(
return_values={
"name": "X_out",
"description": "Transformed input.",
"type": "Type is specified by the `output_type` parameter.",
}
)
def transform(self, X):
"""Transform X using ordinal encoding."""
self._check_n_features(X, reset=False)
result = {}
for feature in self._features:
Xi = _slice_feat(X, feature)
col_idx = self._encoders[feature].transform(Xi)
result[feature] = col_idx
r = DataFrame(result)
return _get_output(self.output_type, self.input_type, r, self.dtype)
@generate_docstring(
y=None,
return_values={
"name": "X_out",
"description": "Transformed input.",
"type": "Type is specified by the `output_type` parameter.",
},
)
def fit_transform(self, X, y=None):
"""Fit OrdinalEncoder to X, then transform X. Equivalent to fit(X).transform(X)."""
X = self._check_input(X)
return self.fit(X).transform(X)
def inverse_transform(self, X):
"""Convert the data back to the original representation.
Parameters
----------
X : array-like or sparse matrix, shape [n_samples, n_encoded_features]
The transformed data.
Returns
-------
X_tr : Type is specified by the `output_type` parameter.
Inverse transformed array.
"""
self._check_n_features(X, reset=False)
result = {}
for feature in self._features:
Xi = _slice_feat(X, feature)
inv = self._encoders[feature].inverse_transform(Xi)
result[feature] = inv
r = DataFrame(result)
return _get_output(self.output_type, self.input_type, r, self.dtype)
def get_param_names(self):
return super().get_param_names() + [
"categories",
"dtype",
"handle_unknown",
]