Package: category-encoders
Categorical Encoding MethodsA set of scikit-learn-style transformers for encoding categorical
variables into numeric by means of different techniques.Important LinksDocumentation:http://contrib.scikit-learn.org/category_encoders/Encoding MethodsUnsupervised:Backward Difference Contrast [2][3]BaseN [6]Binary [5]Gray [14]Count [10]Hashing [1]Helmert Contrast [2][3]Ordinal [2][3]One-Hot [2][3]Rank Hot [15]Polynomial Contrast [2][3]Sum Contrast [2][3]Supervised:CatBoost [11]Generalized Linear Mixed Model [12]James-Stein Estimator [9]LeaveOneOut [4]M-estimator [7]Target Encoding [7]Weight of Evidence [8]Quantile Encoder [13]Summary Encoder [13]InstallationThe package requires:numpy,statsmodels, andscipy.To install the package, execute:$pythonsetup.pyinstallorpipinstallcategory_encodersorcondainstall-cconda-forgecategory_encodersTo install the development version, you may use:pipinstall--upgradegit+https://github.com/scikit-learn-contrib/category_encodersUsageAll 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.ExamplesThere are two types of encoders: unsupervised and supervised. An unsupervised example:fromcategory_encodersimport*importpandasaspdfromsklearn.datasetsimportload_boston# prepare some databunch=load_boston()y=bunch.targetX=pd.DataFrame(bunch.data,columns=bunch.feature_names)# use binary encoding to encode two categorical featuresenc=BinaryEncoder(cols=['CHAS','RAD']).fit(X)# transform the datasetnumeric_dataset=enc.transform(X)And a supervised example:fromcategory_encodersimport*importpandasaspdfromsklearn.datasetsimportload_boston# prepare some databunch=load_boston()y_train=bunch.target[0:250]y_test=bunch.target[250:506]X_train=pd.DataFrame(bunch.data[0:250],columns=bunch.feature_names)X_test=pd.DataFrame(bunch.data[250:506],columns=bunch.feature_names)# use target encoding to encode two categorical featuresenc=TargetEncoder(cols=['CHAS','RAD'])# transform the datasetstraining_numeric_dataset=enc.fit_transform(X_train,y_train)testing_numeric_dataset=enc.transform(X_test)For the transformation of thetrainingdata with the supervised methods, you should usefit_transform()method instead offit().transform(), because these two methodsdo nothave to generate the same result. The difference can be observed with LeaveOneOut encoder, which performs a nested cross-validation for thetrainingdata infit_transform()method (to decrease over-fitting of the downstream model) but uses all the training data for scoring withtransform()method (to get as accurate estimates as possible).Furthermore, you may benefit from following wrappers:PolynomialWrapper, which extends supervised encoders to support polynomial targetsNestedCVWrapper, which helps to prevent overfittingAdditional examples and benchmarks can be found in theexamplesdirectory.ContributingCategory 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.ReferencesKilian Weinberger; Anirban Dasgupta; John Langford; Alex Smola; Josh Attenberg (2009). Feature Hashing for Large Scale Multitask Learning. Proc. ICML.Contrast Coding Systems for categorical variables.  UCLA: Statistical Consulting Group. Fromhttps://stats.idre.ucla.edu/r/library/r-library-contrast-coding-systems-for-categorical-variables/.Gregory Carey (2003). Coding Categorical Variables. Fromhttp://psych.colorado.edu/~carey/Courses/PSYC5741/handouts/Coding%20Categorical%20Variables%202006-03-03.pdfOwen Zhang - Leave One Out Encoding. Fromhttps://datascience.stackexchange.com/questions/10839/what-is-difference-between-one-hot-encoding-and-leave-one-out-encodingBeyond One-Hot: an exploration of categorical variables. Fromhttp://www.willmcginnis.com/2015/11/29/beyond-one-hot-an-exploration-of-categorical-variables/BaseN Encoding and Grid Search in categorical variables. Fromhttp://www.willmcginnis.com/2016/12/18/basen-encoding-grid-search-category_encoders/Daniele Miccii-Barreca (2001). A Preprocessing Scheme for High-Cardinality Categorical Attributes in Classification and Prediction Problems. SIGKDD Explor. Newsl. 3, 1. Fromhttp://dx.doi.org/10.1145/507533.507538Weight of Evidence (WOE) and Information Value Explained. Fromhttps://www.listendata.com/2015/03/weight-of-evidence-woe-and-information.htmlEmpirical Bayes for multiple sample sizes. Fromhttp://chris-said.io/2017/05/03/empirical-bayes-for-multiple-sample-sizes/Simple Count or Frequency Encoding. Fromhttps://www.datacamp.com/community/tutorials/encoding-methodologiesTransforming categorical features to numerical features. Fromhttps://tech.yandex.com/catboost/doc/dg/concepts/algorithm-main-stages_cat-to-numberic-docpage/Andrew Gelman and Jennifer Hill (2006). Data Analysis Using Regression and Multilevel/Hierarchical Models. Fromhttps://faculty.psau.edu.sa/filedownload/doc-12-pdf-a1997d0d31f84d13c1cdc44ac39a8f2c-original.pdfCarlos 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 Publishinghttps://link.springer.com/chapter/10.1007%2F978-3-030-85529-1_14Gray Encoding. Fromhttps://en.wikipedia.org/wiki/Gray_codeJacob Buckman, Aurko Roy, Colin Raffel, Ian Goodfellow: Thermometer Encoding: One Hot Way To Resist Adversarial Examples. Fromhttps://openreview.net/forum?id=S18Su--CWFairness 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