scikit learn – How to perform logistic lasso in python?

scikit learn – How to perform logistic lasso in python?

The Lasso optimizes a least-square problem with a L1 penalty.
By definition you cant optimize a logistic function with the Lasso.

If you want to optimize a logistic function with a L1 penalty, you can use the LogisticRegression estimator with the L1 penalty:

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
log = LogisticRegression(penalty=l1, solver=liblinear), y)

Note that only the LIBLINEAR and SAGA (added in v0.19) solvers handle the L1 penalty.

You can use glment in Python. Glmnet uses warm starts and active-set convergence so it is extremely efficient. Those techniques make glment faster than other lasso implementations. You can download it from

scikit learn – How to perform logistic lasso in python?

1 scikit-learn: sklearn.linear_model.LogisticRegression

sklearn.linear_model.LogisticRegression from scikit-learn is probably the best:

as @TomDLT said, Lasso is for the least squares (regression) case, not logistic (classification).

from sklearn.linear_model import LogisticRegression

model = LogisticRegression(
    solver=saga,  # or liblinear
    C=regularization_strength), y)

2 python-glmnet: glmnet.LogitNet

You can also use Civis Analytics python-glmnet library. This implements the scikit-learn BaseEstimator API:

# source:

from glmnet import LogitNet

m = LogitNet(
    alpha=1,  # 0 <= alpha <= 1, 0 for ridge, 1 for lasso
m =, y)

Im not sure how to adjust the penalty with LogitNet, but Ill let you figure that out.

3 other


you can also take a fully bayesian approach. rather than use L1-penalized optimization to find a point estimate for your coefficients, you can approximate the distribution of your coefficients given your data. this gives you the same answer as L1-penalized maximum likelihood estimation if you use a Laplace prior for your coefficients. the Laplace prior induces sparsity.

the PyMC folks have a tutorial here on setting something like that up. good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *