-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy path_encoders.py
More file actions
287 lines (230 loc) · 9.83 KB
/
_encoders.py
File metadata and controls
287 lines (230 loc) · 9.83 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
from typing import Any, List, Optional, Union
import dask
import dask.array as da
import numpy as np
import pandas as pd
import sklearn.preprocessing
from .._typing import ArrayLike, DataFrameType, DTypeLike, SeriesType
from ..base import DaskMLBaseMixin
from ..utils import check_array
from .label import _encode, _encode_dask_array
class OneHotEncoder(DaskMLBaseMixin, sklearn.preprocessing.OneHotEncoder):
"""Encode categorical integer features as a one-hot numeric array.
.. versionadded:: 0.8.0
.. note::
This requires scikit-learn 0.20.0 or newer.
The input to this transformer should be an array-like of integers, strings,
or categoricals, denoting the 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.
By default, the encoder derives the categories based on
1. For arrays, the unique values in each feature
2. For DataFrames, the CategoricalDtype information for each feature
Alternatively, for arrays, you can also specify the `categories` manually.
This encoding is needed for feeding categorical data to many scikit-learn
estimators, notably linear models and SVMs with the standard kernels.
Note: a one-hot encoding of y labels should use a LabelBinarizer
instead.
Read more in the :ref:`User Guide <preprocessing_categorical_features>`.
Parameters
----------
categories : 'auto' or a list of lists/arrays of values.
Categories (unique values) per feature:
- 'auto' : Determine categories automatically from the training data.
- list : ``categories[i]`` holds the categories expected in the ith
column. The passed categories should not mix strings and numeric
values within a single feature, and should be sorted in case of
numeric values.
The used categories can be found in the ``categories_`` attribute.
drop : None, default=None
The option to drop one of the categories per feature is not yet supported.
sparse : boolean, default=True
Will return sparse matrix if set True else will return an array.
dtype : number type, default=np.float64
Desired dtype of output.
handle_unknown : 'error'
Whether to raise an error or ignore if an unknown categorical feature
is present during transform (default is to raise). The option to
ignore unknown categories is not currently implemented.
Attributes
----------
categories_ : list of arrays
The categories of each feature determined during fitting
(in order of the features in X and corresponding with the output
of ``transform``).
dtypes_ : list of dtypes
For DataFrame input, the CategoricalDtype information associated
with each feature. For arrays, this is a list of Nones.
Notes
-----
There are a few differences from scikit-learn.
Examples
--------
Given a dataset with two features, we let the encoder find the unique
values per feature and transform the data to a binary one-hot encoding.
>>> from dask.ml.preprocessing import OneHotEncoder
>>> import numpy as np
>>> import dask.array as da
>>> enc = OneHotEncoder()
>>> X = da.from_array(np.array([['A'], ['B'], ['A'], ['C']]), chunks=2)
>>> enc.fit(X)
... # doctest: +ELLIPSIS
OneHotEncoder(categorical_features=None, categories=None,
dtype=<... 'numpy.float64'>, handle_unknown='error',
n_values=None, sparse=True)
>>> enc.categories_
[array(['A', 'B', 'C'], dtype='<U1')]
>>> enc.transform(X)
dask.array<concatenate, shape=(4, 3), dtype=float64, chunksize=(2, 3)>
"""
_legacy_mode = False
def __init__(
self,
n_values: Optional[int] = None,
categorical_features: Optional[pd.Categorical] = None,
categories: Union[str, ArrayLike] = "auto",
drop: Optional[bool] = None,
sparse: bool = True,
dtype: DTypeLike = np.float64,
handle_unknown: str = "error",
):
if drop is not None:
raise NotImplementedError("drop != None is not implemented yet.")
super(OneHotEncoder, self).__init__(
categories=categories,
sparse=sparse,
dtype=dtype,
handle_unknown=handle_unknown,
)
@classmethod
def _get_param_names(cls: Any) -> List[str]:
return ["categories", "drop", "dtype", "sparse", "dtype", "handle_unknown"]
def get_params(self, deep: bool = True):
return super().get_params(deep)
def fit(
self,
X: Union[ArrayLike, DataFrameType],
y: Optional[Union[ArrayLike, SeriesType]] = None,
) -> "OneHotEncoder":
if self.handle_unknown == "ignore":
raise NotImplementedError("handle_unkown='ignore' is not implemented yet.")
if self.handle_unknown != "error":
msg = "handle_unknown must be 'error'." "got {0}.".format(
self.handle_unknown
)
raise ValueError(msg)
if isinstance(X, (pd.Series, pd.DataFrame)) or dask.is_dask_collection(X):
self._fit(X, handle_unknown=self.handle_unknown)
else:
super(OneHotEncoder, self).fit(X, y=y)
return self
def _fit(
self,
X: Union[ArrayLike, DataFrameType],
handle_unknown: str = "error",
force_all_finite: bool = True,
):
X = self._validate_data(
X, accept_dask_dataframe=True, dtype=None, preserve_pandas_dataframe=True
)
self._check_n_features(X, reset=True)
self._check_feature_names(X, reset=True)
if isinstance(X, np.ndarray):
return super(OneHotEncoder, self)._fit(
X, handle_unknown=handle_unknown, force_all_finite=force_all_finite
)
is_array = isinstance(X, da.Array)
if is_array:
_, n_features = X.shape
else:
n_features = len(X.columns)
if self.categories != "auto":
for cats in self.categories:
if not np.all(np.sort(cats) == np.array(cats)):
raise ValueError("Unsorted categories are not yet supported")
if len(self.categories) != n_features:
raise ValueError(
"Shape mismatch: if n_values is an array,"
" it has to be of shape (n_features,)."
)
self.categories_ = []
self.dtypes_: List[Optional[pd.CategoricalDtype]] = []
if is_array:
for i in range(n_features):
Xi = X[:, i]
if self.categories == "auto":
cats = _encode(Xi)
else:
cats = np.array(self.categories[i], dtype=X.dtype)
self.categories_.append(cats)
self.dtypes_.append(None)
else:
for i in range(len(X.columns)):
Xi = X.iloc[:, i]
if self.categories != "auto":
categories = self.categories[i]
Xi = Xi.astype(pd.CategoricalDtype(categories))
else:
if not pd.api.types.is_categorical_dtype(Xi.dtype):
raise ValueError(
"All columns must be Categorical dtype when "
"'categories=\"auto\"'."
)
cats = _encode(Xi, uniques=Xi.cat.categories)
self.categories_.append(cats)
self.dtypes_.append(Xi.dtype)
self.categories_ = dask.compute(self.categories_)[0]
def transform(
self, X: Union[ArrayLike, DataFrameType]
) -> Union[ArrayLike, DataFrameType]:
return self._transform(X)
def _transform_new(
self, X: Union[ArrayLike, DataFrameType]
) -> Union[ArrayLike, DataFrameType]:
return self._transform(X)
def _transform(
self, X: Union[ArrayLike, DataFrameType], handle_unknown: str = "error"
) -> Union[ArrayLike, DataFrameType]:
X = check_array(
X, accept_dask_dataframe=True, dtype=None, preserve_pandas_dataframe=True
)
is_array = isinstance(X, da.Array)
if is_array:
_, n_features = X.shape
else:
n_features = len(X.columns)
if is_array:
# We encode each column independently, as they have different categories.
Xs = [
_encode_dask_array(
X[:, i],
uniques=self.categories_[i],
encode=True,
onehot_dtype=self.dtype,
)[1]
for i in range(n_features)
]
X = da.concatenate(Xs, axis=1)
if not self.sparse:
X = X.map_blocks(lambda x: x.toarray(), dtype=self.dtype)
else:
import dask.dataframe as dd
X = X.copy()
if not len(X.columns) == len(self.categories_):
raise ValueError(
"Number of columns ({}) does not match number "
"of categories_ ({})".format(len(X.columns), len(self.categories_))
)
for i, (col, dtype) in enumerate(zip(X.columns, self.dtypes_)):
Xi = X.iloc[:, i]
if not pd.api.types.is_categorical_dtype(Xi.dtype):
Xi = Xi.astype(dtype)
X[col] = Xi
if Xi.dtype != dtype:
raise ValueError(
"Different CategoricalDtype for fit and transform. "
"{!r} != {!r}".format(Xi.dtype, dtype)
)
return dd.get_dummies(X, sparse=self.sparse, dtype=self.dtype)
return X