tubular.nominal.OrdinalEncoderTransformer

class tubular.nominal.OrdinalEncoderTransformer(columns=None, weights_column=None, **kwargs)[source]

Bases: tubular.nominal.BaseNominalTransformer, tubular.mapping.BaseMappingTransformMixin

Transformer to encode categorical variables into ascending rank-ordered integer values variables by mapping it’s levels to the target-mean response for that level. Values will be sorted in ascending order only i.e. categorical level with lowest target mean response to be encoded as 1, the next highest value as 2 and so on.

If a categorical variable contains null values these will not be transformed.

Parameters
  • columns (None or str or list, default = None) – Columns to transform, if the default of None is supplied all object and category columns in X are used.

  • weights_column (str or None) – Weights column to use when calculating the mean response.

  • **kwargs – Arbitrary keyword arguments passed onto BaseTransformer.init method.

weights_column

Weights column to use when calculating the mean response.

Type

str or None

mappings

Created in fit. Dict of key (column names) value (mapping of categorical levels to numeric, ordinal encoded response values) pairs.

Type

dict

__init__(columns=None, weights_column=None, **kwargs)[source]

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

Methods

__init__([columns, weights_column])

Initialize self.

check_is_fitted(attribute)

Check if particular attributes are on the object.

check_mappable_rows(X)

Method to check that all the rows to apply the transformer to are able to be mapped according to the values in the mappings dict.

check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

columns_set_or_check(X)

Function to check or set columns attribute.

fit(X, y)

Identify mapping of categorical levels to rank-ordered integer values by target-mean in ascending order.

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)

Transform method to apply ordinal encoding stored in the mappings attribute to each column in the columns attribute.

check_is_fitted(attribute)

Check if particular attributes are on the object. This is useful to do before running transform to avoid trying to transform data without first running the fit method.

Wrapper for utils.validation.check_is_fitted function.

Parameters

attributes (List) – List of str values giving names of attribute to check exist on self.

check_mappable_rows(X)

Method to check that all the rows to apply the transformer to are able to be mapped according to the values in the mappings dict.

Raises

ValueError – If any of the rows in a column (c) to be mapped, could not be mapped according to the mapping dict in mappings[c].

static check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

X (pd.DataFrame): df containing weight column weights_column (str): name of weight column

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

Parameters

X (pd.DataFrame) – Data to check columns are in.

columns_set_or_check(X)

Function to check or set columns attribute.

If the columns attribute is None then set it to all object and category columns in X. Otherwise run the columns_check method.

Parameters

X (pd.DataFrame) – Data to check columns are in.

fit(X, y)[source]

Identify mapping of categorical levels to rank-ordered integer values by target-mean in ascending order.

If the user specified the weights_column arg in when initialising the transformer the weighted mean response will be calculated using that column.

Parameters
  • X (pd.DataFrame) – Data to with catgeorical variable columns to transform and response_column column specified when object was initialised.

  • y (pd.Series) – Response column or target.

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 – Transformed array.

Return type

ndarray array of shape (n_samples, n_features_new)

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 – Parameter names mapped to their values.

Return type

dict

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.

Return type

estimator instance

transform(X)[source]

Transform method to apply ordinal encoding stored in the mappings attribute to each column in the columns attribute. This maps categorical levels to rank-ordered integer values by target-mean in ascending order.

This method calls the check_mappable_rows method from BaseNominalTransformer to check that all rows can be mapped then transform from BaseMappingTransformMixin to apply the standard pd.Series.map method.

Parameters

X (pd.DataFrame) – Data to with catgeorical variable columns to transform.

Returns

X – Transformed data with levels mapped to ordinal encoded values for categorical variables.

Return type

pd.DataFrame