Python implementation of elastic-net regularized generalized linear models.
I follow the same approach and notations as in Friedman, J., Hastie, T., & Tibshirani, R. (2010) and the accompanying widely popular R package.
The key difference is that I use ordinary batch gradient descent instead of co-ordinate descent, which is very fast for N x p
of up to 10000 x 1000
.
You can find some resources here.
Clone the repository.
$ git clone http://github.com/pavanramkumar/pyglmnet
Install pyglmnet
using setup.py
as following
$ python setup.py develop install
Here is an example on how to use GLM
class.
import numpy as np
import scipy.sparse as sps
from scipy.stats import zscore
from pyglmnet import GLM
# create class of Generalized Linear model
model = GLM(distr='poisson', verbose=True, alpha=0.05)
n_samples, n_features = 10000, 100
# coefficients
beta0 = np.random.normal(0.0, 1.0, 1)
beta = sps.rand(n_features, 1, 0.1)
beta = np.array(beta.todense())
# training data
Xr = np.random.normal(0.0, 1.0, [n_samples, n_features])
yr = model.simulate(beta0, beta, Xr)
# testing data
Xt = np.random.normal(0.0, 1.0, [n_samples, n_features])
yt = model.simulate(beta0, beta, Xt)
# fit Generalized Linear Model
model.fit(zscore(Xr), yr)
# we'll get .fit_params after .fit(), here we get one set of fit parameters
fit_param = model.fit_params[-2]
You can also work through given Jupyter notebook demo
pyglmnet_example.ipynb
A more extensive tutorial on posing and fitting the GLM is in
glmnet_tutorial.ipynb
MIT License Copyright (c) 2016 Pavan Ramkumar