Encode Category
- class encode_category.CategoricalEncoder(encoding_methods: str | Dict[str, str] | None = 'one_hot', columns: List[str] | None = None, categories: Dict[str, List[str]] | None = None, target: str | None = None, drop_first: bool = False, **kwargs: Any)[source]
Class CategoricalEncoder provides methods for encoding categorical variables, including label encoding, one-hot encoding, ordinal encoding, binary encoding, target encoding, frequency encoding, and more.
The class is designed to be compatible with scikit-learn pipelines.
- fit(X: DataFrame, y: Series | None = None) CategoricalEncoder[source]
Fits the encoders to the data.
- Parameters:
X (pd.DataFrame) – Input DataFrame.
y (pd.Series, optional) – Target variable. Required for target encoding.
- Returns:
Fitted encoder.
- Return type:
- fit_transform(X: DataFrame, y: Series | None = None) DataFrame[source]
Fits the encoders and transforms the data.
- Parameters:
X (pd.DataFrame) – Input DataFrame.
y (pd.Series, optional) – Target variable. Required for target encoding.
- Returns:
Transformed DataFrame.
- Return type:
pd.DataFrame
- get_feature_names_out(input_features: List[str] | None = None) List[str][source]
Get output feature names for transformation.
- Parameters:
input_features (List[str], optional) – List of input feature names. If None, uses self.columns.
- Returns:
List of output feature names.
- Return type:
List[str]
- get_params(deep: bool = True) Dict[str, Any][source]
Get parameters for this estimator.
- Parameters:
deep (bool) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
Parameter names mapped to their values.
- Return type:
Dict[str, Any]
- inverse_transform(X: DataFrame) DataFrame[source]
Inverse transforms the data back to original categories.
- Parameters:
X (pd.DataFrame) – Encoded DataFrame.
- Returns:
DataFrame with original categories.
- Return type:
pd.DataFrame
- set_params(**params: Any) CategoricalEncoder[source]
Set the parameters of this estimator.
- Parameters:
**params – Estimator parameters.
- Returns:
Returns self.
- Return type: