sklearn.linear_model.MultiTaskLassoCV?
class sklearn.linear_model.MultiTaskLassoCV(*, eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, max_iter=1000, tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=None, random_state=None, selection='cyclic')
有關交叉驗證估算器,請參閱詞匯表條目。
MultiTaskLasso的優化目標是:
這里
即每一行的范數之和。
在用戶指南中閱讀更多內容。
0.15版的新功能。
參數 | 說明 |
---|---|
eps | float, default=1e-3 路徑的長度。 eps=1e-3 意味著alpha_min / alpha_max = 1e-3 。 |
n_alphas | int, default=100 沿正則化路徑的Alpha個數。 |
alphas | array-like, default=None 用于計算模型的alpha列表。如果為None自動設置Alpha。 |
fit_intercept | bool, default=True 是否計算該模型的截距。如果設置為false,則在計算中將不使用截距(即數據應中心化)。 |
normalize | bool, default=Falsefit_intercept 設置為False 時,將忽略此參數。如果為True,則在回歸之前通過減去均值并除以l2-范數來對回歸變量X進行歸一化。如果你希望標準化,請先使用 sklearn.preprocessing.StandardScaler ,然后調用fit 估算器并設置normalize=False 。 |
max_iter | int, default=1000 最大迭代次數。 |
tol | float, default=1e-4 優化的容忍度:如果更新小于 tol ,則優化代碼檢查對偶間隙的最優性,并繼續進行直到其小于tol 。 |
copy_X | bool, default=True 如果 True ,將復制X;否則X可能會被覆蓋。 |
cv | int, cross-validation generator or iterable, default=None 確定交叉驗證拆分策略。可能的輸入是: - None,使用默認的5折交叉驗證 - int,用于指定折數。 - CV分割器, - 可迭代生成(分割訓練、測試)索引數組。 對于int / None輸入,使用 KFold 。有關可在此處使用的各種交叉驗證策略,請參閱用戶指南。 在0.22版本中更改: cv 如果是“None”,默認值從3更改為5。 |
verbose | bool or int, default=0 日志詳細程度。 |
n_jobs | int, default=None 交叉驗證期間要使用的CPU核心數量。 除非在上下文中設置了 joblib.parallel_backend ,否則None 表示1 。 -1 表示使用所有處理器。有關更多詳細信息,請參見詞匯表。 |
random_state | int, RandomState instance, default=None 更新特征隨機選擇的偽隨機數生成器種子。在 selection =='random'時使用。在多個函數調用之間傳遞同一個整數實現可重復的輸出。請參閱詞匯表。 |
selection | {‘cyclic’, ‘random’}, default=’cyclic’ 如果設置為“random”,則隨機系數將在每次迭代時更新,而不是默認情況下按順序遍歷特征。這(設置為“random”)通常會導致收斂更快,尤其是當tol高于1e-4時。 |
屬性 | 說明 |
---|---|
intercept_ | ndarray of shape (n_tasks,) 目標函數中的截距。 |
coef_ | ndarray of shape (n_tasks, n_features) 參數向量(目標函數公式中的w)。 coef_ 存儲W 的轉置W.T 。 |
alpha_ | float 通過交叉驗證選擇的懲罰量。 |
mse_path_ | ndarray of shape (n_alphas, n_folds) or (n_l1_ratio, n_alphas, n_folds) 每一折中不同的alpha對應測試集的均方誤差。 |
alphas_ | ndarray of shape (n_alphas,) or (n_l1_ratio, n_alphas) 用于擬合的Alpha網格 |
n_iter_ | int 坐標下降求解器運行的迭代次數,以達到最佳alpha的指定容忍度。 |
另見
注
用坐標下降算法擬合模型。
為避免不必要的內存重復,fit方法的X和y參數應作為Fortran-contiguous numpy數組直接傳遞。
示例
>>> from sklearn.linear_model import MultiTaskLassoCV
>>> from sklearn.datasets import make_regression
>>> from sklearn.metrics import r2_score
>>> X, y = make_regression(n_targets=2, noise=4, random_state=0)
>>> reg = MultiTaskLassoCV(cv=5, random_state=0).fit(X, y)
>>> r2_score(y, reg.predict(X))
0.9994...
>>> reg.alpha_
0.5713...
>>> reg.predict(X[:1,])
array([[153.7971..., 94.9015...]])
方法
方法 | 說明 |
---|---|
fit (X, y) |
用坐標下降法擬合線性模型 |
get_params ([deep]) |
獲取此估計器的參數。 |
path (X, y, *[, eps, n_alphas, alphas, …]) |
計算具有坐標下降的套索路徑。 |
predict (X) |
使用線性模型進行預測。 |
score (X, y[, sample_weight]) |
返回預測的確定系數R ^ 2。 |
set_params (**params) |
設置此估算器的參數。 |
__init__(*, eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, max_iter=1000, tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=None, random_state=None, selection='cyclic')
初始化self, 請參閱help(type(self))以獲得準確的說明。
fit(X, y)
用坐標下降法擬合線性模型。
在Alpha網格上擬合模型,并通過交叉驗證估算出最佳Alpha。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 訓練數據。直接作為Fortran-contiguous數據傳遞,以避免不必要的內存重復。如果y是單輸出,則X可以是稀疏的。 |
y | array-like of shape (n_samples,) or (n_samples, n_targets) 目標值。 |
get_params(deep=True)
獲取此估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
static 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矩陣來加快計算速度。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;否則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的指定容差所進行的迭代次數。 |
另見
注
有關示例,請參見 examples / linear_model / plot_lasso_coordinate_descent_path.py。
為避免不必要的內存重復,fit方法的X參數應作為Fortran-contiguous的numpy數組直接傳遞。
請注意,在某些情況下,Lars求解器可能會明顯更快地實現此功能。特別是,線性插值可用于檢索lars_path輸出的值之間的模型系數。
示例
將lasso_path和lars_path進行插值比較:
>>> X = np.array([[1, 2, 3.1], [2.3, 5.4, 4.3]]).T
>>> y = np.array([1, 2, 3.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]]
predict(X)
[源碼]
使用線性模型進行預測。
參數 | 說明 |
---|---|
X | array_like or sparse matrix, shape (n_samples, n_features) 樣本數據 |
返回值 | 說明 |
---|---|
C | array, shape (n_samples,) 返回預測值。 |
score(X, y, sample_weight=None)
[源碼]
返回預測的確定系數R ^ 2。
系數R ^ 2定義為(1- u / v),其中u是殘差平方和((y_true-y_pred)** 2).sum(),而v是總平方和((y_true- y_true.mean())** 2).sum()。可能的最高得分為1.0,并且也可能為負(因為該模型可能會更差)。一個常數模型總是預測y的期望值,而不考慮輸入特征,得到的R^2得分為0.0。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 測試樣本。對于某些估計器,這可以是預先計算的內核矩陣或通用對象列表,形狀為(n_samples,n_samples_fitted),其中n_samples_fitted是用于擬合估計器的樣本數。 |
y | array-like of shape (n_samples,) or (n_samples, n_outputs) X的真實值。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
返回值 | 說明 |
---|---|
score | float 預測值與真實值的R^2。 |
注
調用回歸器中的score
時使用的R2分數,multioutput='uniform_average'
從0.23版開始使用 ,與r2_score
默認值保持一致。這會影響多輸出回歸的score
方法( MultiOutputRegressor
除外)。
set_params(**params)
[源碼]
設置此估計器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。后者具有形式為 <component>__<parameter>
的參數,這樣就可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估計器參數。 |
返回值 | 說明 |
---|---|
self | object 估計器實例。 |