dask_ml.preprocessing.OrdinalEncoder

class dask_ml.preprocessing.OrdinalEncoder(columns=None)

Ordinal (integer) encode categorical columns.

Parameters:
columns : sequence, optional

The columns to encode. Must be categorical dtype. Encodes all categorical dtype columns by default.

Attributes:
columns_ : Index

The columns in the training data before/after encoding

categorical_columns_ : Index

The categorical columns in the training data

noncategorical_columns_ : Index

The rest of the columns in the training data

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'])})
>>> enc = OrdinalEncoder()
>>> trn = enc.fit_transform(data)
>>> trn
   A  B
0  1  0
1  2  0
2  3  0
3  4  1
>>> enc.columns_
Index(['A', 'B'], dtype='object')
>>> enc.non_categorical_columns_
Index(['A'], dtype='object')
>>> enc.categorical_columns_
Index(['B'], dtype='object')
>>> enc.dtypes_
{'B': CategoricalDtype(categories=['a', 'b'], ordered=False)}
>>> enc.fit_transform(dd.from_pandas(data, 2))
Dask DataFrame Structure:
                   A     B
npartitions=2
0              int64  int8
2                ...   ...
3                ...   ...
Dask Name: assign, 8 tasks

Methods

fit(X[, y]) Determine the categorical columns to be encoded.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Inverse ordinal-encode the columns in X
set_params(**params) Set the parameters of this estimator.
transform(X[, y]) Ordinal encode the categorical columns in X
__init__(columns=None)

Initialize self. See help(type(self)) for accurate signature.

fit(X, y=None)

Determine the categorical columns to be 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 : numpy array of shape [n_samples, n_features]

Training set.

y : numpy array of shape [n_samples]

Target values.

Returns:
X_new : numpy array of shape [n_samples, n_features_new]

Transformed array.

get_params(deep=True)

Get parameters for this estimator.

Parameters:
deep : boolean, optional

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
params : mapping of string to any

Parameter names mapped to their values.

inverse_transform(X)

Inverse ordinal-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 pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns:
self
transform(X, y=None)

Ordinal 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