LassoLarsCV#

  • Implementa un modelo Lasso con el algoritmo LARS y validación cruzada.

  • 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 LassoLarsCV

lassoLarsCV = LassoLarsCV(
    # ---------------------------------------------------------------------
    # Whether to fit the intercept for this model.
    fit_intercept=True,
    # --------------------------------------------------------------------------
    # The maximum number of iterations.
    max_iter=1000,
    # --------------------------------------------------------------------------
    # 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,
    # --------------------------------------------------------------------------
    # The maximum number of points on the path used to compute the residuals in
    # the cross-validation.
    max_n_alphas=1000,
    # --------------------------------------------------------------------------
    # When set to True, forces the coefficients to be positive.
    positive=False,
)
[2]:
from sklearn.datasets import load_diabetes

X, y = load_diabetes(return_X_y=True)
[3]:
lassoLarsCV.fit(X, y)
lassoLarsCV.score(X, y)
[3]:
0.5174156640832634
[4]:
lassoLarsCV.alpha_
[4]:
0.003924609657690745
[5]:
lassoLarsCV.coef_
[5]:
array([  -6.38023877, -235.78204366,  521.84673313,  320.95581554,
       -567.6979934 ,  300.65889983,    0.        ,  144.25715975,
        669.14051769,   66.7677334 ])
[6]:
lassoLarsCV.intercept_
[6]:
152.133484162896
[7]:
lassoLarsCV.alphas_
[7]:
array([2.14804358, 2.01202214, 1.02465091, 0.71509814, 0.29441072,
       0.20086946, 0.15602894, 0.04520626, 0.01239262, 0.01151185,
       0.00493726, 0.00392461])