data_preprocessing.categorical_encoder

class CategoricalEncoder(categorical_features=None)[source]

Bases: DataTransformer

Transformer for encoding categorical features using one-hot encoding.

Encodes specified categorical features using one-hot encoding while maintaining numerical features unchanged. Non-declared object-type columns in numerical slots are automatically detected and excluded. Column matching for categorical_features is by exact name.

categorical_features

A list of strings representing the categorical feature names to be one-hot encoded (matched by exact name). If not provided, the encoder will not encode any categorical features.

Type:

list

feature_names_out_

The list of output feature names after transformation.

Type:

list

numerical_columns

The list of numerical feature names identified from the input data.

Type:

list

n_features_in_

The total number of input features in the dataset.

Type:

int

feature_names_in_

The list of input feature names identified during the fit process.

Type:

list

one_hot_encoder

An instance of OneHotEncoder used for transforming categorical features.

Type:

OneHotEncoder

categorical_columns

The list of categorical columns identified from the input data.

Type:

list

non_declared_categorical_features

Object-type columns detected in numerical slots and excluded.

Type:

list

fit(x, y=None)[source]

Fits the OneHotEncoder to the categorical features in the provided DataFrame.

Identifies numerical and categorical columns, detects and excludes non-declared object-type columns from numerical slots, and fits the internal OneHotEncoder.

Parameters:
  • x (DataFrame) – The input data containing all features. Only the specified categorical features will be fitted.

  • y – Ignored. Retained for compatibility with scikit-learn API.

Returns:

The fitted encoder instance (self).

Return type:

CategoricalEncoder

get_feature_names_out(input_features=None)[source]

Returns the names of features after the transformation.

The output names are the concatenation of numerical column names and one-hot encoded categorical column names (e.g. category_A, category_B).

Parameters:

input_features – Unused. Retained for scikit-learn API compatibility.

Returns:

List of output feature names including both numerical and

one-hot encoded features.

Return type:

List[str]

inverse_transform(x)[source]

Reverts transformed data back to original form by decoding one-hot encoded features.

Maps one-hot encoded categorical columns back to their original categorical values and concatenates them with the numerical columns. If no categorical features were encoded, returns the input unchanged.

Parameters:

x (DataFrame) – The transformed input data with one-hot encoded columns.

Returns:

The data with categorical features restored to their original values.

Return type:

DataFrame

Raises:

KeyError – If the one-hot encoded columns are not present in the input data.

transform(x)[source]

Transforms the input DataFrame by applying one-hot encoding to categorical features.

Combines one-hot encoded categorical features with unchanged numerical features. If no categorical features are specified, returns the numerical data unchanged.

Parameters:

x (DataFrame) – The input data to be transformed.

Returns:

The transformed DataFrame with one-hot encoded categorical features

and numerical features.

Return type:

DataFrame

Raises:
  • KeyError – If the input DataFrame is missing any of the features fitted during fit.

  • TypeError – If x is not a pandas DataFrame.