Python wrapper for glmnet
Project description
This is a Python wrapper for the fortran library used in the R package glmnet. While the library includes linear, logistic, Cox, Poisson, and multiple-response Gaussian, only linear and logistic are implemented in this package.
The API follows the conventions of Scikit-Learn, so it is expected to work with tools from that ecosystem.
Installation
requirements
python-glmnet requires Python version >= 3.6, scikit-learn, numpy, and scipy. Installation from source or via pip requires a Fortran compiler.
conda
conda install -c conda-forge glmnet
pip
pip install glmnet
source
glmnet depends on numpy, scikit-learn and scipy. A working Fortran compiler is also required to build the package. For Mac users, brew install gcc will take care of this requirement.
git clone git@github.com:civisanalytics/python-glmnet.git
cd python-glmnet
python setup.py install
Usage
General
By default, LogitNet and ElasticNet fit a series of models using the lasso penalty (α = 1) and up to 100 values for λ (determined by the algorithm). In addition, after computing the path of λ values, performance metrics for each value of λ are computed using 3-fold cross validation. The value of λ corresponding to the best performing model is saved as the lambda_max_ attribute and the largest value of λ such that the model performance is within cut_point * standard_error of the best scoring model is saved as the lambda_best_ attribute.
The predict and predict_proba methods accept an optional parameter lamb which is used to select which model(s) will be used to make predictions. If lamb is omitted, lambda_best_ is used.
Both models will accept dense or sparse arrays.
Regularized Logistic Regression
from glmnet import LogitNet
m = LogitNet()
m = m.fit(x, y)
Prediction is similar to Scikit-Learn:
# predict labels
p = m.predict(x)
# or probability estimates
p = m.predict_proba(x)
Regularized Linear Regression
from glmnet import ElasticNet
m = ElasticNet()
m = m.fit(x, y)
Predict:
p = m.predict(x)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for glmnet-2.2.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f06475121a105b3d8fde9aebcb668275d6c14ca098590e9484c42bd24db98f40 |
|
MD5 | 08266a90ef9e3016d79c786b9f07873a |
|
BLAKE2b-256 | 9a222f42c49c0ddd972c42abeb753f58e02c12140c2876302ed266b2c945b83d |
Hashes for glmnet-2.2.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09300d1b6e303a0067126277bc1765fbac8b52a43393cd6849a4581faa45b114 |
|
MD5 | aa1dd2114bb8e2bb2450551f3537f77f |
|
BLAKE2b-256 | 251da02ff94865128dff3dcb38f50ff0c0671ee545c5c73f9c18d445355ca798 |
Hashes for glmnet-2.2.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73343fc1f9220c032cf8ecf9631437d835a715bd621e542c28283744dd53d6da |
|
MD5 | 986edfd61db516856a52503854f7353b |
|
BLAKE2b-256 | 7065d6ffe6a8cb6b754b68fa7b29062b6d2ce75d58b756e12aeb48578a71ccf8 |