jasmainak / pyglmnet

Python implementation of elastic-net regularized generalized linear models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pyglmnet

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.

Simulating data and fitting a GLM in 5 minutes

Clone the repository.

$ git clone http://github.com/pavanramkumar/pyglmnet

Install pyglmnet using setup.py as following

$ python setup.py develop install

Documentation

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

Tutorial

A more extensive tutorial on posing and fitting the GLM is in glmnet_tutorial.ipynb

Author

Contributors

License

MIT License Copyright (c) 2016 Pavan Ramkumar

About

Python implementation of elastic-net regularized generalized linear models

License:MIT License


Languages

Language:Jupyter Notebook 86.3%Language:Python 13.7%