logit-encode
| Version | |
| Status | |
| Documentation | |
| Tools | |
| Compatibility | |
| Stats | |
What is logit-encode?
logit-encode encodes categorical features with values from a logit
(logistic regression) model of a binary or multiclass outcome. Each
category is replaced by one of:
- its log odds: the average log odds of the outcome for rows in that category,
- its coefficient: the log odds ratio of the outcome for that category against a reference category, or
- its marginal effect: the average change in the probability of the outcome from moving a row from the reference category to that category.
The logit model can be fit with every column at once (model = 'joint',
the default), so that each column's values are adjusted for the others, or
separately for each column (model = 'univariate'). Either
scikit-learn or
statsmodels can fit it.
Why use logit-encode?
- A full scikit-learn transformer.
LogitEncoderworks in pipelines, column transformers, cross-validation, and grid searches, supportsset_outputandget_feature_names_out, and passes scikit-learn'scheck_estimator. - The
category_encodersinterface. It has the samecols,drop_invariant,return_df,handle_missing, andhandle_unknownoptions as the encoders incategory_encoders. - No target leakage. Like scikit-learn's
TargetEncoder, it cross-fits the training rows, so each row is encoded by a model that never saw its outcome. - Shrinkage for rare categories. The model's L2 (or L1) penalty pulls categories with few rows toward the reference category.
- Interpretable values. The encodings are quantities that analysts already report from logit models.
- Multiclass outcomes. An outcome with K classes gets K - 1 columns per categorical column, one for each class against the most frequent class.
- Uncertainty columns.
return_uncertainty = Trueadds each value's standard error and each category's number of training rows, so a model can discount rare categories. - Interactions.
interactions = [('state', 'county')]encodes combinations of columns as columns of their own. - Fast. The model is fit on the distinct combinations of categories and outcome, weighted by their counts, which gives the same estimates as fitting on every row.
See the benchmarks
for how it compares with every category_encoders encoder.
Getting started
Requirements
Python 3.11 or newer, with scikit-learn 1.8 or newer, statsmodels, and
category_encoders.
Installation
To install logit-encode, use pip:
pip install logit-encode
Usage
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from logit_encode import LogitEncoder
X = pd.DataFrame({
'color': ['red', 'blue', 'red', 'green', 'blue', 'red'] * 50,
'size': ['S', 'M', 'L', 'M', 'S', 'L'] * 50})
y = pd.Series([1, 0, 1, 0, 0, 1] * 50)
# Encode the training data (cross-fitted) and new data.
encoder = LogitEncoder(encoding = 'log_odds')
encoded = encoder.fit_transform(X, y)
new = encoder.transform(pd.DataFrame({'color': ['red'], 'size': ['XL']}))
# Or use it in a pipeline.
pipeline = make_pipeline(
LogitEncoder(encoding = 'marginal_effect', return_uncertainty = True),
LogisticRegression())
scores = cross_val_score(pipeline, X, y, scoring = 'roc_auc')
The tutorial explains each option.
Contributing
Contributors are always welcome. Feel free to grab an issue to work on or make a suggested improvement. If you wish to contribute, please read the Contribution Guide and Code of Conduct.
Similar Projects
category_encodershas more than 20 encoders, including target, weight of evidence, and generalized linear mixed model (GLMMEncoder) encoders.LogitEncoderfollows its interface.- scikit-learn's
TargetEncodercross-fits shrunken category means, asLogitEncodercross-fits logit models.
Citation
If you use logit-encode in your work, please cite it. GitHub's "Cite this
repository" button uses the same information, from
CITATION.cff.
Yung, Corey Rayburn. 2026. logit-encode: Encoding Categorical Features with Logit Models (version 0.1.0). Computer software. https://github.com/WithPrecedent/logit-encode.
@software{yung_logit_encode_2026,
author = {Yung, Corey Rayburn},
title = {{logit-encode}: Encoding Categorical Features with Logit Models},
year = {2026},
version = {0.1.0},
license = {Apache-2.0},
url = {https://github.com/WithPrecedent/logit-encode}
}
Acknowledgments
The benchmarks use datasets from OpenML.
License
Use of this repository is authorized under the Apache Software License 2.0.
Metadata
Release files for logit-encode 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| logit_encode-0.1.0.tar.gz | 468.0 kB | Without provenance | Details for logit_encode-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| logit_encode-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for logit_encode-0.1.0-py3-none-any.whl |
Total release size: 496.2 kB
Release files / logit_encode-0.1.0.tar.gz
| Download URL | logit_encode-0.1.0.tar.gz |
|---|---|
| Size | 468.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e01cc543227a3cd077f79ef5075b0898f177d19eb1d2a8489592da1ced30e398
|
|
BLAKE2b-256 checksum How to use checksums |
dd69fbf84ce656cecac4b7d177d412a3d54e9b7d2f02e69a4c04ca0bbb009d60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.12 {"installer":{"name":"uv","version":"0.13.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / logit_encode-0.1.0-py3-none-any.whl
| Download URL | logit_encode-0.1.0-py3-none-any.whl |
|---|---|
| Size | 28.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a508e8d62fbe0f708492fce56ed239a81b24efa30bc6a9b4593ad113eee6c407
|
|
BLAKE2b-256 checksum How to use checksums |
649120dbf4b3718958d61f3dfaee24f9de0dc437e465df44852436ef34573ec5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.12 {"installer":{"name":"uv","version":"0.13.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for logit-encode 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.