tubular.nominal.NominalToIntegerTransformer

class tubular.nominal.NominalToIntegerTransformer(columns=None, start_encoding=0, **kwargs)[source]

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

Transformer to convert columns containing nominal values into integer values.

The nominal levels that are mapped to integers are not ordered in any way.

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.

  • start_encoding (int, default = 0) – Value to start the encoding from e.g. if start_encoding = 0 then the encoding would be {‘A’: 0, ‘B’: 1, ‘C’: 3} etc.. or if start_encoding = 5 then the same encoding would be {‘A’: 5, ‘B’: 6, ‘C’: 7}. Can be positive or negative.

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

start_encoding

Value to start the encoding / mapping of nominal to integer from.

Type

int

mappings

Created in fit. A dict of key (column names) value (mappings between levels and integers for given column) pairs.

Type

dict

inverse_mapping_

Created in inverse_transform. Inverse mapping of mappings. Maps integer value back to categorical levels.

Type

dict

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

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

Methods

__init__([columns, start_encoding])

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

Creates mapping between nominal levels and integer values for categorical variables.

fit_transform(X[, y])

Fit to data, then transform it.

get_params([deep])

Get parameters for this estimator.

inverse_transform(X)

Converts integer values back to categorical / nominal values.

set_params(**params)

Set the parameters of this estimator.

transform(X)

Transform method to apply integer 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=None)[source]

Creates mapping between nominal levels and integer values for categorical variables.

Parameters
  • X (pd.DataFrame) – Data to fit the transformer on, this sets the nominal levels that can be mapped.

  • y (None or pd.DataFrame or pd.Series, default = None) – Optional argument only required for the transformer to work with sklearn pipelines.

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

inverse_transform(X)[source]

Converts integer values back to categorical / nominal values. Does the inverse of the transform method.

Parameters

X (pd.DataFrame) – Data to with integer columns to convert back to catgeorical.

Returns

X – Transformed input X with integers mapped back to categorical levels.

Return type

pd.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.

Return type

estimator instance

transform(X)[source]

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

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 with nominal columns to transform.

Returns

X – Transformed input X with levels mapped accoriding to mappings dict.

Return type

pd.DataFrame