category-encoders


Namecategory-encoders JSON
Version 2.6.3 PyPI version JSON
download
home_pagehttps://github.com/scikit-learn-contrib/category_encoders
SummaryA collection of sklearn transformers to encode categorical variables as numeric
upload_time2023-10-29 20:29:54
maintainer
docs_urlNone
authorWill McGinnis
requires_python
licenseBSD
keywords python data science machine learning pandas sklearn
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Categorical Encoding Methods
============================

[![Downloads](https://pepy.tech/badge/category-encoders)](https://pepy.tech/project/category-encoders)
[![Downloads](https://pepy.tech/badge/category-encoders/month)](https://pepy.tech/project/category-encoders)
![Test Suite and Linting](https://github.com/scikit-learn-contrib/category_encoders/workflows/Test%20Suite%20and%20Linting/badge.svg)
[![DOI](https://zenodo.org/badge/47077067.svg)](https://zenodo.org/badge/latestdoi/47077067)

A set of scikit-learn-style transformers for encoding categorical 
variables into numeric by means of different techniques.

Important Links
---------------

Documentation: [http://contrib.scikit-learn.org/category_encoders/](http://contrib.scikit-learn.org/category_encoders/)

Encoding Methods
----------------
__Unsupervised:__
 * 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]

Installation
------------

The package requires: `numpy`, `statsmodels`, and `scipy`.

To install the package, execute:

```shell
$ python setup.py install
```

or 

```shell
pip install category_encoders
```

or

```shell
conda install -c conda-forge category_encoders
```

To install the development version, you may use:

```shell
pip install --upgrade git+https://github.com/scikit-learn-contrib/category_encoders
```

Usage
-----

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
from sklearn.datasets import load_boston

# prepare some data
bunch = load_boston()
y = bunch.target
X = pd.DataFrame(bunch.data, columns=bunch.feature_names)

# use binary encoding to encode two categorical features
enc = BinaryEncoder(cols=['CHAS', 'RAD']).fit(X)

# transform the dataset
numeric_dataset = enc.transform(X)
```

And a supervised example:
```python
from category_encoders import *
import pandas as pd
from sklearn.datasets import load_boston

# prepare some data
bunch = 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 features
enc = TargetEncoder(cols=['CHAS', 'RAD'])

# transform the datasets
training_numeric_dataset = enc.fit_transform(X_train, y_train)
testing_numeric_dataset = enc.transform(X_test)
```

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

 1. Kilian Weinberger; Anirban Dasgupta; John Langford; Alex Smola; Josh Attenberg (2009). Feature Hashing for Large Scale Multitask Learning. Proc. ICML.
 2. Contrast Coding Systems for categorical variables.  UCLA: Statistical Consulting Group. From https://stats.idre.ucla.edu/r/library/r-library-contrast-coding-systems-for-categorical-variables/.
 3. Gregory Carey (2003). Coding Categorical Variables. From http://psych.colorado.edu/~carey/Courses/PSYC5741/handouts/Coding%20Categorical%20Variables%202006-03-03.pdf
 4. Owen Zhang - Leave One Out Encoding. From https://datascience.stackexchange.com/questions/10839/what-is-difference-between-one-hot-encoding-and-leave-one-out-encoding
 5. Beyond One-Hot: an exploration of categorical variables. From http://www.willmcginnis.com/2015/11/29/beyond-one-hot-an-exploration-of-categorical-variables/
 6. BaseN Encoding and Grid Search in categorical variables. From http://www.willmcginnis.com/2016/12/18/basen-encoding-grid-search-category_encoders/
 7. 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
 8. Weight of Evidence (WOE) and Information Value Explained. From https://www.listendata.com/2015/03/weight-of-evidence-woe-and-information.html
 9. Empirical Bayes for multiple sample sizes. From http://chris-said.io/2017/05/03/empirical-bayes-for-multiple-sample-sizes/
 10. Simple Count or Frequency Encoding. From https://www.datacamp.com/community/tutorials/encoding-methodologies
 11. Transforming categorical features to numerical features. From https://tech.yandex.com/catboost/doc/dg/concepts/algorithm-main-stages_cat-to-numberic-docpage/
 12. Andrew Gelman and Jennifer Hill (2006). Data Analysis Using Regression and Multilevel/Hierarchical Models. From https://faculty.psau.edu.sa/filedownload/doc-12-pdf-a1997d0d31f84d13c1cdc44ac39a8f2c-original.pdf
 13. 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
 14. Gray Encoding. From https://en.wikipedia.org/wiki/Gray_code 
 15. 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
 16. 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



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/scikit-learn-contrib/category_encoders",
    "name": "category-encoders",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "python data science machine learning pandas sklearn",
    "author": "Will McGinnis",
    "author_email": "will@pedalwrencher.com",
    "download_url": "https://files.pythonhosted.org/packages/3f/21/79a3fdf7998035ddd601ed4df6ac8b1e273ec61b30c05cf18df0042e308f/category_encoders-2.6.3.tar.gz",
    "platform": null,
    "description": "Categorical Encoding Methods\n============================\n\n[![Downloads](https://pepy.tech/badge/category-encoders)](https://pepy.tech/project/category-encoders)\n[![Downloads](https://pepy.tech/badge/category-encoders/month)](https://pepy.tech/project/category-encoders)\n![Test Suite and Linting](https://github.com/scikit-learn-contrib/category_encoders/workflows/Test%20Suite%20and%20Linting/badge.svg)\n[![DOI](https://zenodo.org/badge/47077067.svg)](https://zenodo.org/badge/latestdoi/47077067)\n\nA set of scikit-learn-style transformers for encoding categorical \nvariables into numeric by means of different techniques.\n\nImportant Links\n---------------\n\nDocumentation: [http://contrib.scikit-learn.org/category_encoders/](http://contrib.scikit-learn.org/category_encoders/)\n\nEncoding Methods\n----------------\n__Unsupervised:__\n * Backward Difference Contrast [2][3]\n * BaseN [6]\n * Binary [5]\n * Gray [14]\n * Count [10]\n * Hashing [1]\n * Helmert Contrast [2][3]\n * Ordinal [2][3]\n * One-Hot [2][3]\n * Rank Hot [15]\n * Polynomial Contrast [2][3]\n * Sum Contrast [2][3]\n\n__Supervised:__\n * CatBoost [11]\n * Generalized Linear Mixed Model [12] \n * James-Stein Estimator [9]\n * LeaveOneOut [4]\n * M-estimator [7]\n * Target Encoding [7]\n * Weight of Evidence [8]\n * Quantile Encoder [13]\n * Summary Encoder [13]\n\nInstallation\n------------\n\nThe package requires: `numpy`, `statsmodels`, and `scipy`.\n\nTo install the package, execute:\n\n```shell\n$ python setup.py install\n```\n\nor \n\n```shell\npip install category_encoders\n```\n\nor\n\n```shell\nconda install -c conda-forge category_encoders\n```\n\nTo install the development version, you may use:\n\n```shell\npip install --upgrade git+https://github.com/scikit-learn-contrib/category_encoders\n```\n\nUsage\n-----\n\nAll of the encoders are fully compatible sklearn transformers, so they can be used in pipelines or in your existing \nscripts. Supported input formats include numpy arrays and pandas dataframes. If the cols parameter isn't passed, all \ncolumns with object or pandas categorical data type will be encoded. Please see the docs for transformer-specific \nconfiguration options.\n\nExamples\n--------\nThere are two types of encoders: unsupervised and supervised. An unsupervised example:\n```python\nfrom category_encoders import *\nimport pandas as pd\nfrom sklearn.datasets import load_boston\n\n# prepare some data\nbunch = load_boston()\ny = bunch.target\nX = pd.DataFrame(bunch.data, columns=bunch.feature_names)\n\n# use binary encoding to encode two categorical features\nenc = BinaryEncoder(cols=['CHAS', 'RAD']).fit(X)\n\n# transform the dataset\nnumeric_dataset = enc.transform(X)\n```\n\nAnd a supervised example:\n```python\nfrom category_encoders import *\nimport pandas as pd\nfrom sklearn.datasets import load_boston\n\n# prepare some data\nbunch = load_boston()\ny_train = bunch.target[0:250]\ny_test = bunch.target[250:506]\nX_train = pd.DataFrame(bunch.data[0:250], columns=bunch.feature_names)\nX_test = pd.DataFrame(bunch.data[250:506], columns=bunch.feature_names)\n\n# use target encoding to encode two categorical features\nenc = TargetEncoder(cols=['CHAS', 'RAD'])\n\n# transform the datasets\ntraining_numeric_dataset = enc.fit_transform(X_train, y_train)\ntesting_numeric_dataset = enc.transform(X_test)\n```\n\nFor 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).\n\nFurthermore, you may benefit from following wrappers:\n * PolynomialWrapper, which extends supervised encoders to support polynomial targets\n * NestedCVWrapper, which helps to prevent overfitting  \n\nAdditional examples and benchmarks can be found in the `examples` directory.\n\nContributing\n------------\n\nCategory encoders is under active development, if you'd like to be involved, we'd love to have you. Check out the CONTRIBUTING.md file\nor open an issue on the github project to get started.\n\nReferences\n----------\n\n 1. Kilian Weinberger; Anirban Dasgupta; John Langford; Alex Smola; Josh Attenberg (2009). Feature Hashing for Large Scale Multitask Learning. Proc. ICML.\n 2. Contrast Coding Systems for categorical variables.  UCLA: Statistical Consulting Group. From https://stats.idre.ucla.edu/r/library/r-library-contrast-coding-systems-for-categorical-variables/.\n 3. Gregory Carey (2003). Coding Categorical Variables. From http://psych.colorado.edu/~carey/Courses/PSYC5741/handouts/Coding%20Categorical%20Variables%202006-03-03.pdf\n 4. Owen Zhang - Leave One Out Encoding. From https://datascience.stackexchange.com/questions/10839/what-is-difference-between-one-hot-encoding-and-leave-one-out-encoding\n 5. Beyond One-Hot: an exploration of categorical variables. From http://www.willmcginnis.com/2015/11/29/beyond-one-hot-an-exploration-of-categorical-variables/\n 6. BaseN Encoding and Grid Search in categorical variables. From http://www.willmcginnis.com/2016/12/18/basen-encoding-grid-search-category_encoders/\n 7. 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\n 8. Weight of Evidence (WOE) and Information Value Explained. From https://www.listendata.com/2015/03/weight-of-evidence-woe-and-information.html\n 9. Empirical Bayes for multiple sample sizes. From http://chris-said.io/2017/05/03/empirical-bayes-for-multiple-sample-sizes/\n 10. Simple Count or Frequency Encoding. From https://www.datacamp.com/community/tutorials/encoding-methodologies\n 11. Transforming categorical features to numerical features. From https://tech.yandex.com/catboost/doc/dg/concepts/algorithm-main-stages_cat-to-numberic-docpage/\n 12. Andrew Gelman and Jennifer Hill (2006). Data Analysis Using Regression and Multilevel/Hierarchical Models. From https://faculty.psau.edu.sa/filedownload/doc-12-pdf-a1997d0d31f84d13c1cdc44ac39a8f2c-original.pdf\n 13. 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\n 14. Gray Encoding. From https://en.wikipedia.org/wiki/Gray_code \n 15. 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\n 16. 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 \u201921, https://arxiv.org/abs/2201.11358\n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "A collection of sklearn transformers to encode categorical variables as numeric",
    "version": "2.6.3",
    "project_urls": {
        "Download": "https://github.com/scikit-learn-contrib/category_encoders/tarball/2.6.3",
        "Homepage": "https://github.com/scikit-learn-contrib/category_encoders"
    },
    "split_keywords": [
        "python",
        "data",
        "science",
        "machine",
        "learning",
        "pandas",
        "sklearn"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7fe579a62e5c9c9ddbfa9ff5222240d408c1eeea4e38741a0dc8343edc7ef1ec",
                "md5": "41bbb2b08be42ce025aec405ba92b0ac",
                "sha256": "117775f1775e53a67c9e91842ac9100bc364cddc9f4058188796532bc5b11f1c"
            },
            "downloads": -1,
            "filename": "category_encoders-2.6.3-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "41bbb2b08be42ce025aec405ba92b0ac",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 81858,
            "upload_time": "2023-10-29T20:29:52",
            "upload_time_iso_8601": "2023-10-29T20:29:52.459211Z",
            "url": "https://files.pythonhosted.org/packages/7f/e5/79a62e5c9c9ddbfa9ff5222240d408c1eeea4e38741a0dc8343edc7ef1ec/category_encoders-2.6.3-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3f2179a3fdf7998035ddd601ed4df6ac8b1e273ec61b30c05cf18df0042e308f",
                "md5": "3e124b0e73effc95e8c698f3e38fe7da",
                "sha256": "d9f14705ed4b536eaf9cfc81b76d67a50b2f16f8f3eda498c57d7da19655530c"
            },
            "downloads": -1,
            "filename": "category_encoders-2.6.3.tar.gz",
            "has_sig": false,
            "md5_digest": "3e124b0e73effc95e8c698f3e38fe7da",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 55142,
            "upload_time": "2023-10-29T20:29:54",
            "upload_time_iso_8601": "2023-10-29T20:29:54.366138Z",
            "url": "https://files.pythonhosted.org/packages/3f/21/79a3fdf7998035ddd601ed4df6ac8b1e273ec61b30c05cf18df0042e308f/category_encoders-2.6.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-29 20:29:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "scikit-learn-contrib",
    "github_project": "category_encoders",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "category-encoders"
}
        
Elapsed time: 0.23769s