sklearn.linear_model.lasso_path?

sklearn.linear_model.lasso_path(X, y, *, eps=0.001, n_alphas=100, alphas=None, precompute='auto', Xy=None, copy_X=True, coef_init=None, verbose=False, return_n_iter=False, positive=False, **params)

[源碼]

計算具有坐標下降的套索路徑

套索優化功能針對單輸出和多輸出而變化。

對于單輸出任務,它是:

對于多輸出任務,它是:

其中:

即每一行的范數之和。

用戶指南中閱讀更多內容。

參數 返回值
X {array-like, sparse matrix} of shape (n_samples, n_features)
用于訓練的數據。直接作為Fortran-contiguous數據傳遞,避免不必要的內存重復。如果y是單輸出,則X 可以是稀疏的。
y {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs)
目標值
eps float, default=1e-3
路徑的長度。eps=1e-3意思是 alpha_min / alpha_max = 1e-3
n_alphas int, default=100
正則化路徑中的Alpha數
alphas ndarray, default=None
用于在其中計算模型的alpha列表。如果為None,則自動設置Alpha。
precompute ‘auto’, bool or array-like of shape (n_features, n_features), default=’auto’
是否使用預先計算的Gram矩陣來加快計算速度。如果設置'auto'讓我們決定。Gram矩陣也可以作為參數被傳遞。
Xy array-like of shape (n_features,) or (n_features, n_outputs), default=None
可以預先計算Xy = np.dot(XT,y)。僅當預先計算了Gram矩陣時才有用。
copy_X bool, default=True
如果為True,將復制X;否則,它可能會被覆蓋。
coef_init ndarray of shape (n_features, ), default=None
系數的初始值。
verbose bool or int, default=False
詳細程度。
return_n_iter bool, default=False
是否返回迭代次數。
positive bool, default=False
如果設置為True,則強制系數為正。(僅當y.ndim == 1時允許)。
**params kwargs
關鍵字參數傳遞給坐標下降求解器。
返回值 說明
alphas ndarray of shape (n_alphas,)
沿模型計算路徑的Alpha。
coefs ndarray of shape (n_features, n_alphas) or (n_outputs, n_features, n_alphas)
沿路徑的系數。
dual_gaps ndarray of shape (n_alphas,)
每個alpha優化結束時的雙重間隔。
n_iters list of int
坐標下降優化器為達到每個alpha的指定公差所進行的迭代次數。

另見:

lars_path

Lasso

LassoLars

LassoCV

LassoLarsCV

sklearn.decomposition.sparse_encode

有關示例,請參閱examples / linear_model / plot_lasso_coordinate_descent_path.py

為避免不必要的內存重復,fit方法的X參數應作為 Fortran-contiguous的numpy數組直接被傳遞。

請注意,在某些情況下,Lars求解器可能會更快地實現這個功能。特別是,線性插值可用于檢索lars_path輸出值之間的模型系數。

示例

將lasso_path和lars_path與插值進行比較:

>>> X = np.array([[123.1], [2.35.44.3]]).T
>>> y = np.array([123.1])
>>> # Use lasso_path to compute a coefficient path
>>> _, coef_path, _ = lasso_path(X, y, alphas=[5.1..5])
>>> print(coef_path)
[[0.         0.         0.46874778]
 [0.2159048  0.4425765  0.23689075]]
>>> # Now use lars_path and 1D linear interpolation to compute the
>>> # same path
>>> from sklearn.linear_model import lars_path
>>> alphas, active, coef_path_lars = lars_path(X, y, method='lasso')
>>> from scipy import interpolate
>>> coef_path_continuous = interpolate.interp1d(alphas[::-1],
...                                             coef_path_lars[:, ::-1])
>>> print(coef_path_continuous([5.1..5]))
[[0.         0.         0.46915237]
 [0.2159048  0.4425765  0.23668876]]

sklearn.linear_model.lasso_path使用示例?