LARS Lasso

LassoLars 是一个使用 LARS 算法的 lasso 模型,不同于基于坐标下降法的实现,它可以得到一个精确解,也就是一个关于自身参数标准化后的一个分段线性解。

>>> from sklearn import linear_model
>>> reg = linear_model.LassoLars(alpha=.1)
>>> reg.fit([[0, 0], [1, 1]], [0, 1])
LassoLars(alpha=0.1, copy_X=True, eps=..., fit_intercept=True,
     fit_path=True, max_iter=500, normalize=True, positive=False,
     precompute='auto', verbose=False)
>>> reg.coef_
array([ 0.717157...,  0.        ])

Lars 算法提供了一个几乎无代价的沿着正则化参数的系数的完整路径,因此常利用函数 lars_path 来取回路径。

1.1.8.1. 数学表达式
该算法和逐步回归非常相似,但是它没有在每一步包含变量,它估计的参数是根据与 其他剩余变量的联系来增加的。

在 LARS 的解中,没有给出一个向量的结果,而是给出一条曲线,显示参数向量的 L1 范式的每个值的解。 完全的参数路径存在 coef_path_ 下。它的 size 是 (n_features, max_features+1)。 其中第一列通常是全 0 列。

例子

Computes Lasso Path along the regularization parameter using the LARS algorithm on the diabetes dataset. Each color represents a different feature of the coefficient vector, and this is displayed as a function of the regularization parameter.

print(__doc__)

# Author: Fabian Pedregosa <[email protected]>
#         Alexandre Gramfort <[email protected]>
# License: BSD 3 clause

import numpy as np
import matplotlib.pyplot as plt

from sklearn import linear_model
from sklearn import datasets

diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target

print("Computing regularization path using the LARS ...")
alphas, _, coefs = linear_model.lars_path(X, y, method='lasso', verbose=True)

xx = np.sum(np.abs(coefs.T), axis=1)
xx /= xx[-1]

plt.plot(xx, coefs.T)
ymin, ymax = plt.ylim()
plt.vlines(xx, ymin, ymax, linestyle='dashed')
plt.xlabel('|coef| / max|coef|')
plt.ylabel('Coefficients')
plt.title('LASSO Path')
plt.axis('tight')
plt.show()

猜你喜欢

转载自blog.csdn.net/xiligey1/article/details/81427777