LassoLars 是一个使用LARS算法实现的lasso模型。和基于坐标下降的实现不同的是,它产生的是精确的解,和一个函数标准系数一样是精确线性的。
1.1 数学描述
这个算法和逐步回归很相似,但是除了在每一步包含变量之外,估计的参数沿着每一个对应的残差的对角方向增长。(待校正)。 并不是给出一个向量结果,而是在LARS的解中包含了一个曲线,用来表示每个参数向量的L1-norm 值的解, 所有系数路径存在 coef_path_
数组中,大小为(n_features,max_features+1),其中第一列总是0.
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()