Elastic-Net#

  • Es un modelo de regresión lineal que combina los regularizadores L1 y L2.

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

\min_w \frac{1}{2n} ||Xw -y||_2^2 + \alpha * \text{l1_ratio} * ||w||_1 + 0.5 * \alpha * (1 - \text{l1_ratio}) * ||w||_2^2

[1]:
from sklearn.datasets import load_diabetes

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

elasticNet = ElasticNet(
    # --------------------------------------------------------------------------
    # Constant that multiplies the penalty terms. alpha = 0 is equivalent to an
    # ordinary least square, solved by the LinearRegression object.
    alpha=1.0,
    # --------------------------------------------------------------------------
    # The ElasticNet mixing parameter, with 0 <= l1_ratio <= 1. 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.
    l1_ratio=0.5,
    # ---------------------------------------------------------------------
    # Whether to fit the intercept for this model.
    fit_intercept=True,
    # ---------------------------------------------------------------------
    # Maximum number of iterations.
    max_iter=1000,
    # --------------------------------------------------------------------------
    # The tolerance for the optimization
    tol=1e-4,
    # --------------------------------------------------------------------------
    # When set to True, reuse the solution of the previous call to fit as
    # initialization, otherwise, just erase the previous solution.
    warm_start=False,
    # --------------------------------------------------------------------------
    # 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',
)

elasticNet.fit(X, y)

elasticNet.score(X, y)
[2]:
0.008834748998299613
[3]:
elasticNet.predict(X)[0:10]
[3]:
array([152.47026911, 151.30027724, 152.28156137, 152.18914366,
       151.86848678, 151.33155287, 151.55248058, 152.26126753,
       152.27687719, 152.37436142])
[4]:
elasticNet.coef_
[4]:
array([ 0.35901793,  0.        ,  3.25976736,  2.20434042,  0.52864562,
        0.25093515, -1.86136322,  2.11445407,  3.10583468,  1.76985102])
[5]:
elasticNet.intercept_
[5]:
152.13348416289594