ElasticNetCV#

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

[1]:
from sklearn.datasets import load_diabetes

X, y = load_diabetes(return_X_y=True)
[2]:
from sklearn.linear_model import ElasticNetCV

elasticNetCV = ElasticNetCV(
    # --------------------------------------------------------------------------
    # Float between 0 and 1 passed to ElasticNet (scaling between l1 and l2
    # penalties). For l1_ratio = 0 the penalty is an L2 penalty.
    # For l1_ratio = 1 it is an L1 penalty. For 0 < l1_ratio < 1, the penalty is
    # a combination of L1 and L2 This parameter can be a list, in which case the
    # different values are tested by cross-validation and the one giving the
    # best prediction score is used. Note that a good choice of list of values
    # for l1_ratio is often to put more values close to 1 (i.e. Lasso) and less
    # close to 0 (i.e. Ridge), as in [.1, .5, .7, .9, .95, .99, 1].
    l1_ratio=0.5,
    # --------------------------------------------------------------------------
    # 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',
)
[3]:
elasticNetCV.fit(X, y)

elasticNetCV.score(X, y)
[3]:
0.45446547279310257
[4]:
elasticNetCV.l1_ratio_
[4]:
0.5
[5]:
elasticNetCV.alphas_
[5]:
array([4.29608715e+00, 4.00654526e+00, 3.73651751e+00, 3.48468874e+00,
       3.24983238e+00, 3.03080456e+00, 2.82653847e+00, 2.63603924e+00,
       2.45837902e+00, 2.29269250e+00, 2.13817270e+00, 1.99406701e+00,
       1.85967357e+00, 1.73433780e+00, 1.61744923e+00, 1.50843857e+00,
       1.40677486e+00, 1.31196294e+00, 1.22354103e+00, 1.14107846e+00,
       1.06417359e+00, 9.92451852e-01, 9.25563919e-01, 8.63184009e-01,
       8.05008295e-01, 7.50753431e-01, 7.00155162e-01, 6.52967047e-01,
       6.08959254e-01, 5.67917438e-01, 5.29641704e-01, 4.93945625e-01,
       4.60655343e-01, 4.29608715e-01, 4.00654526e-01, 3.73651751e-01,
       3.48468874e-01, 3.24983238e-01, 3.03080456e-01, 2.82653847e-01,
       2.63603924e-01, 2.45837902e-01, 2.29269250e-01, 2.13817270e-01,
       1.99406701e-01, 1.85967357e-01, 1.73433780e-01, 1.61744923e-01,
       1.50843857e-01, 1.40677486e-01, 1.31196294e-01, 1.22354103e-01,
       1.14107846e-01, 1.06417359e-01, 9.92451852e-02, 9.25563919e-02,
       8.63184009e-02, 8.05008295e-02, 7.50753431e-02, 7.00155162e-02,
       6.52967047e-02, 6.08959254e-02, 5.67917438e-02, 5.29641704e-02,
       4.93945625e-02, 4.60655343e-02, 4.29608715e-02, 4.00654526e-02,
       3.73651751e-02, 3.48468874e-02, 3.24983238e-02, 3.03080456e-02,
       2.82653847e-02, 2.63603924e-02, 2.45837902e-02, 2.29269250e-02,
       2.13817270e-02, 1.99406701e-02, 1.85967357e-02, 1.73433780e-02,
       1.61744923e-02, 1.50843857e-02, 1.40677486e-02, 1.31196294e-02,
       1.22354103e-02, 1.14107846e-02, 1.06417359e-02, 9.92451852e-03,
       9.25563919e-03, 8.63184009e-03, 8.05008295e-03, 7.50753431e-03,
       7.00155162e-03, 6.52967047e-03, 6.08959254e-03, 5.67917438e-03,
       5.29641704e-03, 4.93945625e-03, 4.60655343e-03, 4.29608715e-03])
[6]:
elasticNetCV.coef_
[6]:
array([  28.49978746,  -86.0271142 ,  312.29147591,  204.85390348,
          3.74693477,  -30.66162901, -153.60181641,  117.55501783,
        267.95924523,  111.98545801])
[7]:
elasticNetCV.intercept_
[7]:
152.133484162896