{"id":37083247,"url":"https://github.com/ak-gupta/bayte","last_synced_at":"2026-01-14T10:08:33.200Z","repository":{"id":37083460,"uuid":"433277175","full_name":"ak-gupta/bayte","owner":"ak-gupta","description":"Bayesian target encoding with scikit-learn and scipy","archived":false,"fork":false,"pushed_at":"2024-04-20T02:23:47.000Z","size":2519,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-03T02:24:29.407Z","etag":null,"topics":["data-science","python"],"latest_commit_sha":null,"homepage":"https://bayte.readthedocs.io/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ak-gupta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-11-30T03:17:25.000Z","updated_at":"2024-04-19T18:14:14.000Z","dependencies_parsed_at":"2024-04-23T00:00:43.020Z","dependency_job_id":null,"html_url":"https://github.com/ak-gupta/bayte","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/ak-gupta/bayte","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ak-gupta%2Fbayte","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ak-gupta%2Fbayte/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ak-gupta%2Fbayte/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ak-gupta%2Fbayte/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ak-gupta","download_url":"https://codeload.github.com/ak-gupta/bayte/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ak-gupta%2Fbayte/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28416590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T08:38:59.149Z","status":"ssl_error","status_checked_at":"2026-01-14T08:38:43.588Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","python"],"created_at":"2026-01-14T10:08:32.569Z","updated_at":"2026-01-14T10:08:33.192Z","avatar_url":"https://github.com/ak-gupta.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\r\n[![codecov](https://codecov.io/github/ak-gupta/bayte/branch/main/graph/badge.svg?token=S8BUVKF37O)](https://codecov.io/github/ak-gupta/bayte) [![Maintainability](https://api.codeclimate.com/v1/badges/5c0b77d0e9b8f899ee95/maintainability)](https://codeclimate.com/github/ak-gupta/bayte/maintainability) ![PyPI](https://img.shields.io/pypi/v/bayte) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/bayte) [![Documentation Status](https://readthedocs.org/projects/bayte/badge/?version=latest)](https://bayte.readthedocs.io/en/latest/?badge=latest)\r\n\r\n# Overview\r\n\r\nThis package is a lightweight implementation of bayesian target encoding. This implementation is taken\r\nfrom [Slakey et al.](https://arxiv.org/pdf/1904.13001.pdf), with ensemble methodology from [Larionov](https://arxiv.org/pdf/2006.01317.pdf).\r\n\r\nThe encoding proceeds as follows:\r\n\r\n1. User observes and chooses a likelihood for the target variable (e.g. Bernoulli for a binary classification problem),\r\n2. Using [Fink's Compendium of Priors](https://www.johndcook.com/CompendiumOfConjugatePriors.pdf), derive the conjugate prior for the likelihood (e.g. Beta),\r\n3. Use the training data to initialize the hyperparameters for the prior distribution\r\n    * **NOTE**: This process is generally reliant on common interpretations of hyperparameters.\r\n4. Using Fink's Compendium, derive the methodology for generating the posterior distribution,\r\n5. For each level in the categorical variable,\r\n    1. Generate the posterior distribution using the observed target values for the categorical level,\r\n    2. Set the encoding value to a sample from the posterior distribution\r\n        * If a new level has appeared in the dataset, the encoding will be sampled from the prior distribution.\r\n          To disable this behaviour, initialize the encoder with ``handle_unknown=\"error\"``.\r\n\r\nThen, we repeat step 5.2 a total of ``n_estimators`` times, generating a total of ``n_estimators`` training datasets\r\nwith unique encodings. The end model is a vote from each sampled dataset.\r\n\r\nFor reproducibility, you can set the encoding value to the mean of the posterior distribution instead.\r\n\r\n## Installation\r\n\r\nInstall from PyPI:\r\n\r\n```bash\r\npython -m pip install bayte\r\n```\r\n\r\n## Usage\r\n\r\n### Encoding\r\n\r\nLet's create a binary classification dataset.\r\n\r\n```python\r\nimport numpy as np\r\nimport pandas as pd\r\nfrom sklearn.datasets import make_classification\r\n\r\nX, y = make_classification(n_samples=1000, n_features=5, n_informative=2)\r\nX = pd.DataFrame(X)\r\n\r\n# Categorical data\r\nX[5] = np.random.choice([\"red\", \"green\", \"blue\"], size=1000)\r\n```\r\n\r\nImport and fit the encoder:\r\n\r\n```python\r\nimport bayte as bt\r\n\r\nencoder = bt.BayesianTargetEncoder(dist=\"bernoulli\")\r\nencoder.fit(X[[5]], y)\r\n```\r\n\r\nTo encode your categorical data,\r\n\r\n```python\r\nX[5] = encoder.transform(X[[5]])\r\n```\r\n\r\n### Ensemble\r\n\r\nIf you want to utilize the ensemble methodology described above, construct the same dataset\r\n\r\n```python\r\nimport numpy as np\r\nimport pandas as pd\r\nfrom sklearn.datasets import make_classification\r\n\r\nX, y = make_classification(n_samples=1000, n_features=5, n_informative=2)\r\nX = pd.DataFrame(X)\r\n\r\n# Categorical data\r\nX[5] = np.random.choice([\"red\", \"green\", \"blue\"], size=1000)\r\n```\r\n\r\nand import a classifier to supply to the ensemble class\r\n\r\n```python\r\nfrom sklearn.svm import SVC\r\n\r\nimport bayte as bt\r\n\r\nensemble = bt.BayesianTargetClassifier(\r\n    base_estimator=SVC(kernel=\"linear\"),\r\n    encoder=bt.BayesianTargetEncoder(dist=\"bernoulli\")\r\n)\r\n```\r\n\r\nFit the ensemble. **NOTE**: either supply an explicit list of categorical features to `categorical_feature`, or\r\nuse a DataFrame with categorical data types.\r\n\r\n```python\r\nensemble.fit(X, y, categorical_feature=[5])\r\n```\r\n\r\nWhen you call ``predict`` on a novel dataset, note that the encoder will transform your data at runtime and it\r\nwill encode based on the *mean of the posterior distribution*:\r\n\r\n```python\r\nensemble.predict(X)\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fak-gupta%2Fbayte","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fak-gupta%2Fbayte","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fak-gupta%2Fbayte/lists"}