All of the encoders are fully compatible sklearn transformers, so they can be used in pipelines or in your existing
scripts. Supported input formats include numpy arrays and pandas dataframes. If the cols parameter isn't passed, all
columns with object or pandas categorical data type will be encoded. Please see the docs for transformer-specific
configuration options.
Examples
There are two types of encoders: unsupervised and supervised. An unsupervised example:
python
from category_encoders import *
import pandas as pd
# prepare some data with categorical features
X = pd.DataFrame({
'gender': ['male', 'female', 'female', 'male', 'female'],
'country': ['US', 'UK', 'US', 'CA', 'UK'],
'age': [25, 32, 47, 51, 38],
})
# use binary encoding to encode two categorical features
enc = BinaryEncoder(cols=['gender', 'country']).fit(X)
# transform the dataset
numeric_dataset = enc.transform(X)
And a supervised example:
…
For the transformation of the training data with the supervised methods, you should use fit_transform() method instead of fit().transform(), because these two methods do not have to generate the same result. The difference can be observed with LeaveOneOut encoder, which performs a nested cross-validation for the training data in fit_transform() method (to decrease over-fitting of the downstream model) but uses all the training data for scoring with transform() method (to get as accurate estimates as possible).
Furthermore, you may benefit from following wrappers:
PolynomialWrapper, which extends supervised encoders to support polynomial targets
NestedCVWrapper, which helps to prevent overfitting
Additional examples and benchmarks can be found in the examples directory.
Contributing
Category encoders is under active development, if you'd like to be involved, we'd love to have you. Check out the CONTRIBUTING.md file
or open an issue on the github project to get started.
References
Kilian Weinberger; Anirban Dasgupta; John Langford; Alex Smola; Josh Attenberg (2009). Feature Hashing for Large Scale Multitask Learning. Proc. ICML.
Daniele Miccii-Barreca (2001). A Preprocessing Scheme for High-Cardinality Categorical Attributes in Classification and Prediction Problems. SIGKDD Explor. Newsl. 3, 1. From http://dx.doi.org/10.1145/507533.507538
Carlos Mougan, David Masip, Jordi Nin and Oriol Pujol (2021). Quantile Encoder: Tackling High Cardinality Categorical Features in Regression Problems. Modeling Decisions for Artificial Intelligence, 2021. Springer International Publishing https://link.springer.com/chapter/10.1007%2F978-3-030-85529-1_14
Jacob Buckman, Aurko Roy, Colin Raffel, Ian Goodfellow: Thermometer Encoding: One Hot Way To Resist Adversarial Examples. From https://openreview.net/forum?id=S18Su--CW
Fairness implications of encoding protected categorical attributes. Carlos Mougan, Jose Alvarez, Salvatore Ruggieri, and Steffen Staab. In Proceedings of the 2023 AAAI/ACM Conference on AI, Ethics, and Society, AIES ’21, https://arxiv.org/abs/2201.11358