skoot.preprocessing.DummyEncoder

class skoot.preprocessing.DummyEncoder(cols=None, as_df=True, sep='_', drop_one_level=True, handle_unknown='ignore', n_jobs=1)[source][source]

Dummy encode categorical data.

A custom one-hot encoding class that is capable of handling previously unseen levels and automatically dropping one level from each categorical feature in order to avoid the dummy variable trap.

Parameters:

cols : array-like, shape=(n_features,), optional (default=None)

The names of the columns on which to apply the transformation. Unlike other BasePDTransformer instances, this should not be left as the default None, since dummying the entire frame could prove very expensive.

as_df : bool, optional (default=True)

Whether to return a Pandas DataFrame in the transform method. If False, will return a Numpy ndarray instead. Since most skoot transformers depend on explicitly-named DataFrame features, the as_df parameter is True by default.

sep : str or unicode, optional (default=’_’)

The string separator between the categorical feature name and the level name.

drop_one_level : bool, optional (default=True)

Whether to drop one level for each categorical variable. This helps avoid the dummy variable trap.

handle_unknown : str or unicode, optional (default=’ignore’)

How to handle the unknown levels. “ignore” will not raise an error for unknown test set levels, but “error” will. “warn” will produce a warning.

n_jobs : int, 1 by default

The number of jobs to use for the encoding. This works by fitting each incremental LabelEncoder in parallel.

If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used.

Attributes

ohe_ (OneHotEncoder) The one hot encoder
le_ (dict[str: LabelEncoder]) A dictionary mapping column names to their respective LabelEncoder instances.
fit_cols_ (list) The list of column names on which the transformer was fit. This is used to validate the presence of the features in the test set during the transform stage.

Methods

fit(X[, y]) Fit the dummy encoder.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
transform(X) Apply the encoding to a dataframe.
__init__(cols=None, as_df=True, sep='_', drop_one_level=True, handle_unknown='ignore', n_jobs=1)[source][source]

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

fit(X, y=None)[source][source]

Fit the dummy encoder.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to fit. The frame will only be fit on the prescribed cols (see __init__) or all of them if cols is None.

y : array-like or None, shape=(n_samples,), optional (default=None)

Pass-through for sklearn.pipeline.Pipeline.

fit_transform(X, y=None, **fit_params)[source]

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)[source]

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.

set_params(**params)[source]

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)[source][source]

Apply the encoding to a dataframe.

This method will encode the features in the test frame with the levels discovered in the fit computation.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to transform. The operation will be applied to a copy of the input data, and the result will be returned.

Returns:

X : pd.DataFrame or np.ndarray, shape=(n_samples, n_features)

The operation is applied to a copy of X, and the result set is returned.

Examples using skoot.preprocessing.DummyEncoder