dask_ml.preprocessing
.DummyEncoder¶
-
class
dask_ml.preprocessing.
DummyEncoder
(columns: Optional[Sequence[Any]] = None, drop_first: bool = False)¶ Dummy (one-hot) encode categorical columns.
Parameters: - columns : sequence, optional
The columns to dummy encode. Must be categorical dtype. Dummy encodes all categorical dtype columns by default.
- drop_first : bool, default False
Whether to drop the first category in each column.
Attributes: - columns_ : Index
The columns in the training data before dummy encoding
- transformed_columns_ : Index
The columns in the training data after dummy encoding
- categorical_columns_ : Index
The categorical columns in the training data
- noncategorical_columns_ : Index
The rest of the columns in the training data
- categorical_blocks_ : dict
Mapping from column names to slice objects. The slices represent the positions in the transformed array that the categorical column ends up at
- dtypes_ : dict
Dictionary mapping column name to either
- instances of CategoricalDtype (pandas >= 0.21.0)
- tuples of (categories, ordered)
Notes
This transformer only applies to dask and pandas DataFrames. For dask DataFrames, all of your categoricals should be known.
The inverse transformation can be used on a dataframe or array.
Examples
>>> data = pd.DataFrame({"A": [1, 2, 3, 4], ... "B": pd.Categorical(['a', 'a', 'a', 'b'])}) >>> de = DummyEncoder() >>> trn = de.fit_transform(data) >>> trn A B_a B_b 0 1 1 0 1 2 1 0 2 3 1 0 3 4 0 1
>>> de.columns_ Index(['A', 'B'], dtype='object')
>>> de.non_categorical_columns_ Index(['A'], dtype='object')
>>> de.categorical_columns_ Index(['B'], dtype='object')
>>> de.dtypes_ {'B': CategoricalDtype(categories=['a', 'b'], ordered=False)}
>>> de.categorical_blocks_ {'B': slice(1, 3, None)}
>>> de.fit_transform(dd.from_pandas(data, 2)) Dask DataFrame Structure: A B_a B_b npartitions=2 0 int64 uint8 uint8 2 ... ... ... 3 ... ... ... Dask Name: get_dummies, 4 tasks
Methods
fit
(X, dask.dataframe.core.DataFrame], y, …)Determine the categorical columns to be dummy encoded. fit_transform
(X[, y])Fit to data, then transform it. get_params
([deep])Get parameters for this estimator. inverse_transform
(X, …)Inverse dummy-encode the columns in X set_params
(**params)Set the parameters of this estimator. transform
(X, dask.dataframe.core.DataFrame], …)Dummy encode the categorical columns in X -
__init__
(columns: Optional[Sequence[Any]] = None, drop_first: bool = False)¶ Initialize self. See help(type(self)) for accurate signature.
-
fit
(X: Union[pandas.core.frame.DataFrame, dask.dataframe.core.DataFrame], y: Union[ArrayLike, dask.dataframe.core.Series, pandas.core.series.Series, None] = None) → dask_ml.preprocessing.data.DummyEncoder¶ Determine the categorical columns to be dummy encoded.
Parameters: - X : pandas.DataFrame or dask.dataframe.DataFrame
- y : ignored
Returns: - self
-
fit_transform
(X, y=None, **fit_params)¶ Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Parameters: - X : array-like of shape (n_samples, n_features)
Input samples.
- y : array-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_params : dict
Additional fit parameters.
Returns: - X_new : ndarray array of shape (n_samples, n_features_new)
Transformed array.
-
get_params
(deep=True)¶ Get parameters for this estimator.
Parameters: - deep : bool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns: - params : dict
Parameter names mapped to their values.
-
inverse_transform
(X: Union[ArrayLike, pandas.core.frame.DataFrame, dask.dataframe.core.DataFrame]) → Union[pandas.core.frame.DataFrame, dask.dataframe.core.DataFrame]¶ Inverse dummy-encode the columns in X
Parameters: - X : array or dataframe
Either the NumPy, dask, or pandas version
Returns: - data : DataFrame
Dask array or dataframe will return a Dask DataFrame. Numpy array or pandas dataframe will return a pandas DataFrame
-
set_params
(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.Parameters: - **params : dict
Estimator parameters.
Returns: - self : estimator instance
Estimator instance.
-
transform
(X: Union[pandas.core.frame.DataFrame, dask.dataframe.core.DataFrame], y: Union[ArrayLike, dask.dataframe.core.Series, pandas.core.series.Series, None] = None) → Union[pandas.core.frame.DataFrame, dask.dataframe.core.DataFrame]¶ Dummy encode the categorical columns in X
Parameters: - X : pd.DataFrame or dd.DataFrame
- y : ignored
Returns: - transformed : pd.DataFrame or dd.DataFrame
Same type as the input