Wrap Glmnet
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wrap_glmnet-0.1.2.tar.gz.
File metadata
- Download URL: wrap_glmnet-0.1.2.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a928dabcfa5d7f818eb77f7db1dbe380b993a3876680cd1ce5985a853b68cfc
|
|
| MD5 |
f082f8cab6463122b2a835c5014f1062
|
|
| BLAKE2b-256 |
841b6394e822df85c0f22e7df9a9a350eb088008a95e849de517fee752681fe3
|
File details
Details for the file wrap_glmnet-0.1.2-py2.py3-none-any.whl.
File metadata
- Download URL: wrap_glmnet-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e0ee9652447b31cb47efdf7ace3e12a2309cb0f5a86f06facb94a5ae9705a6f
|
|
| MD5 |
a70d24e1cee2c2d7848a0599fa3f0415
|
|
| BLAKE2b-256 |
e9f8368a23145c34b08328d2e94f3dc496490b2fbf4a5a073852e0e5f94bf51c
|