Lasso#
Modelo de regresión lineal con penalización L1.
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
Tiende a preferir soluciones con pocos coeficientes diferentes de cero, reduciendo el número de características sobre las cuales la solución es dependiente.
Bajo ciertas condiciones, puede recuperar el conjunto exacto de coeficientes diferentes de cero.
La implementación de sklearn usa el algoritmo de coordenadas descendentes para estimar los parámetros del modelo.
[1]:
from sklearn.linear_model import Lasso
lasso = Lasso(
# --------------------------------------------------------------------------
# Constant that multiplies the L1 term, controlling regularization strength.
# alpha must be a non-negative float i.e. in [0, inf). When alpha = 0, the
# objective is equivalent to ordinary least squares, solved by the
# LinearRegression object.
alpha=1.0,
# ---------------------------------------------------------------------
# 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',
)
[2]:
from sklearn.datasets import load_diabetes
X, y = load_diabetes(return_X_y=True)
[3]:
lasso.fit(X, y)
lasso.score(X, y)
[3]:
0.357378738231628
[4]:
lasso.coef_
[4]:
array([ 0. , -0. , 367.70385976, 6.29885756,
0. , 0. , -0. , 0. ,
307.6054181 , 0. ])
[5]:
lasso.intercept_
[5]:
152.133484162896