wrap-glmnet
wrap_glmnet is a small Python wrapper around python-glmnet's
LogitNet classifier. It keeps the glmnet elastic-net logistic regression
solver, but exposes a more sklearn-like estimator and adds control over
glmnet's internal cross-validation behavior.
Why it exists
python-glmnet is useful for regularized logistic regression, but its
classifier API has a few rough edges for sklearn workflows. This package
provides GlmnetLogitNetWrapper, which is intended to be used in place of
glmnet.LogitNet when you need:
- sklearn-compatible cloning and fitted attributes such as
n_features_in_andfeature_names_in_ - explicit selection of whether predictions use
lambda_1seor the best-performing cross-validation lambda - sklearn-style class weights
- custom internal cross-validation splitters, including group-aware splitters
- multiclass ROC-AUC and deviance/log-loss scorers for glmnet's internal CV
- held-out CV predicted probabilities and per-fold CV scores
- predictable
decision_functionandpredict_probaoutput shapes for binary, multiclass, single-lambda, and multi-lambda predictions
How it works
Importing wrap_glmnet patches selected python-glmnet internals so glmnet's
lambda-path scoring can use an optional sklearn-style internal_cv splitter.
The patched scoring path also forwards groups to scorers that accept it,
records _cv_scores_, and, by default, stores held-out predicted probabilities
in cv_pred_probs_ with shape (n_samples, n_classes, n_lambdas).
GlmnetLogitNetWrapper delegates the actual model fit to an inner
glmnet.LogitNet instance. Constructor keyword arguments not handled by the
wrapper are passed through to LogitNet.
By default, predictions, coef_, intercept_, cv_mean_score_final_, and
cv_standard_error_final_ use lambda_1se (lambda_best_ in
python-glmnet). Set use_lambda_1se=False to use the lambda with the best
mean CV score (lambda_max_), or call switch_lambda(...) after fitting to
get a copied fitted model with the other lambda choice.
Installation
The package requires Python 3.10 or newer.
pip install wrap_glmnet
For local development:
pip install -r requirements_dev.txt
pip install -e .
Usage
from sklearn.model_selection import StratifiedGroupKFold
from wrap_glmnet import GlmnetLogitNetWrapper
clf = GlmnetLogitNetWrapper(
alpha=1.0,
n_lambda=100,
internal_cv=StratifiedGroupKFold(n_splits=3),
scoring=GlmnetLogitNetWrapper.rocauc_scorer,
require_cv_group_labels=True,
)
clf.fit(X_train, y_train, groups=groups)
labels = clf.predict(X_test)
probabilities = clf.predict_proba(X_test)
# Compare the default lambda_1se model to the best-CV-score lambda.
best_cv_lambda_clf = clf.switch_lambda(use_lambda_1se=False)
best_cv_probabilities = best_cv_lambda_clf.predict_proba(X_test)
If internal_cv is not supplied, the wrapper uses glmnet's normal internal CV
setup with n_splits (default: 3). If require_cv_group_labels=True, calling
fit(...) without groups raises an error.
class_weight accepts sklearn-style values such as a class-to-weight mapping or
"balanced". When both class_weight and sample_weight are supplied, the
wrapper multiplies them and normalizes the resulting sample weights before
fitting.
Important behavior and limitations
- This package is focused on
glmnet.LogitNetclassification, not every glmnet model type. - It patches
python-glmnetfunctions at import time, so it relies onpython-glmnetinternals remaining compatible. store_cv_predicted_probabilities=Trueis the default and can use substantial memory for large datasets or long lambda paths. Disable it if you do not needcv_pred_probs_.- Multiclass probabilities are computed from the wrapper's decision scores with softmax normalization; binary probabilities use sigmoid-style normalization.
Development
make test
make lint
make docs
Tests cover sklearn cloning, scorer behavior, group-aware CV, lambda switching, stored CV outputs, plotting, and output shapes.
Changelog
0.0.1
- First release on PyPI.
Release files for wrap-glmnet 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wrap_glmnet-0.1.3.tar.gz | 26.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wrap_glmnet-0.1.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size:40.0 kB
Release files / wrap_glmnet-0.1.3.tar.gz
| Download URL | wrap_glmnet-0.1.3.tar.gz |
|---|---|
| Size | 26.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7933bed3b8b278dc6b51af6e25c7354cc16107971bb84676a4c743542d523561
|
|
BLAKE2b-256 checksum How to use checksums |
2b9309084eb493afd82a3f0f3c86d4eddd0c0505bc6911977eb77b2150a0c096
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / wrap_glmnet-0.1.3-py2.py3-none-any.whl
| Download URL | wrap_glmnet-0.1.3-py2.py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
af345c81b6c83fbad2c837221fe6c41263df142ebb39ed3e8f05af7d72c7f95c
|
|
BLAKE2b-256 checksum How to use checksums |
8933839c1f40d87940b38df88d88d117310ccdb07f054873bbfba319aaddcacc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|