LassoCV#

  • Implementa un modelo Lasso con ajuste iterativo a lo largo de una trayectoria de regularización.

  • Para la estimación de los parámetros se minimiza la función objetivo:

\min_w \frac{1}{2n} ||Xw -y||_2^2 + \alpha * ||w||_1

[1]:
from sklearn.linear_model import LassoCV

lassoCV = LassoCV(
    # --------------------------------------------------------------------------
    # Length of the path. eps=1e-3 means that alpha_min / alpha_max = 1e-3.
    eps=1e-3,
    # --------------------------------------------------------------------------
    # Number of alphas along the regularization path, used for each l1_ratio.
    n_alphas=100,
    # --------------------------------------------------------------------------
    # List of alphas where to compute the models. If None alphas are set
    # automatically.
    alphas=None,
    # ---------------------------------------------------------------------
    # Whether to fit the intercept for this model.
    fit_intercept=True,
    # --------------------------------------------------------------------------
    # The maximum number of iterations.
    max_iter=1000,
    # --------------------------------------------------------------------------
    # The tolerance for the optimization: if the updates are smaller than tol,
    # the optimization code checks the dual gap for optimality and continues
    # until it is smaller than tol.
    tol=1e-4,
    # --------------------------------------------------------------------------
    # Determines the cross-validation splitting strategy. Possible inputs for
    # cv are:
    # * None, to use the default 5-fold cross-validation,
    # * int, to specify the number of folds.
    # * CV splitter,
    # * An iterable yielding (train, test) splits as arrays of indices.
    cv=None,
    # --------------------------------------------------------------------------
    # When set to True, forces the coefficients to be positive.
    positive=False,
    # --------------------------------------------------------------------------
    # The seed of the pseudo random number generator that selects a random
    # feature to update. Used when selection == ‘random’. Pass an int for
    # reproducible output across multiple function calls.
    random_state=None,
    # --------------------------------------------------------------------------
    # If set to ‘random’, a random coefficient is updated every iteration rather
    # than looping over features sequentially by default. This (setting to
    # ‘random’) often leads to significantly faster convergence especially when
    # tol is higher than 1e-4.
    selection='cyclic',
)
[2]:
from sklearn.datasets import load_diabetes

X, y = load_diabetes(return_X_y=True)
[3]:
lassoCV.fit(X, y)
lassoCV.score(X, y)
[3]:
0.5174210668198782
[4]:
lassoCV.alpha_
[4]:
0.00375376715269185
[5]:
lassoCV.coef_
[5]:
array([  -6.49469328, -235.99308032,  521.7443693 ,  321.0607768 ,
       -569.43813385,  302.45319289,   -0.        ,  143.69851474,
        669.92267515,   66.83551067])
[6]:
lassoCV.intercept_
[6]:
152.133484162896
[7]:
lassoCV.alphas_
[7]:
array([2.14804358e+00, 2.00327263e+00, 1.86825876e+00, 1.74234437e+00,
       1.62491619e+00, 1.51540228e+00, 1.41326924e+00, 1.31801962e+00,
       1.22918951e+00, 1.14634625e+00, 1.06908635e+00, 9.97033507e-01,
       9.29836786e-01, 8.67168899e-01, 8.08724617e-01, 7.54219285e-01,
       7.03387429e-01, 6.55981471e-01, 6.11770515e-01, 5.70539230e-01,
       5.32086795e-01, 4.96225926e-01, 4.62781959e-01, 4.31592004e-01,
       4.02504148e-01, 3.75376715e-01, 3.50077581e-01, 3.26483524e-01,
       3.04479627e-01, 2.83958719e-01, 2.64820852e-01, 2.46972813e-01,
       2.30327672e-01, 2.14804358e-01, 2.00327263e-01, 1.86825876e-01,
       1.74234437e-01, 1.62491619e-01, 1.51540228e-01, 1.41326924e-01,
       1.31801962e-01, 1.22918951e-01, 1.14634625e-01, 1.06908635e-01,
       9.97033507e-02, 9.29836786e-02, 8.67168899e-02, 8.08724617e-02,
       7.54219285e-02, 7.03387429e-02, 6.55981471e-02, 6.11770515e-02,
       5.70539230e-02, 5.32086795e-02, 4.96225926e-02, 4.62781959e-02,
       4.31592004e-02, 4.02504148e-02, 3.75376715e-02, 3.50077581e-02,
       3.26483524e-02, 3.04479627e-02, 2.83958719e-02, 2.64820852e-02,
       2.46972813e-02, 2.30327672e-02, 2.14804358e-02, 2.00327263e-02,
       1.86825876e-02, 1.74234437e-02, 1.62491619e-02, 1.51540228e-02,
       1.41326924e-02, 1.31801962e-02, 1.22918951e-02, 1.14634625e-02,
       1.06908635e-02, 9.97033507e-03, 9.29836786e-03, 8.67168899e-03,
       8.08724617e-03, 7.54219285e-03, 7.03387429e-03, 6.55981471e-03,
       6.11770515e-03, 5.70539230e-03, 5.32086795e-03, 4.96225926e-03,
       4.62781959e-03, 4.31592004e-03, 4.02504148e-03, 3.75376715e-03,
       3.50077581e-03, 3.26483524e-03, 3.04479627e-03, 2.83958719e-03,
       2.64820852e-03, 2.46972813e-03, 2.30327672e-03, 2.14804358e-03])