sklearn.linear_model.ElasticNetCV?
class sklearn.linear_model.ElasticNetCV(*, l1_ratio=0.5, eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, precompute='auto', max_iter=1000, tol=0.0001, cv=None, copy_X=True, verbose=0, n_jobs=None, positive=False, random_state=None, selection='cyclic')
沿著正則化路徑具有迭代擬合的彈性網模型。
有關交叉驗證估算器,請參閱詞匯表條目。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
l1_ratio | float or list of float, default=0.5 0到1之間的浮點數傳遞給彈性網(在L1和L2罰分之間縮放)。對于 l1_ratio = 0 是L2懲罰。l1_ratio = 1 是L1懲罰。對于0 < l1_ratio < 1 ,懲罰是L1和L2的組合。此參數可以是列表,在這種情況下,通過交叉驗證測試不同的值,并使用給出最佳預測得分的值。注意,l1_ratio值列表的一個很好的選擇通常是使更多的值接近1(即Lasso),少接近0(即Ridge),如[.1, .5, .7, .9, .95, .99, 1] |
eps | float, default=1e-3 路徑的長度。 eps=1e-3 意味著alpha_min / alpha_max = 1e-3 。 |
n_alphas | int, default=100 沿正則化路徑的Alpha個數,用于每個l1_ratio。 |
alphas | ndarray, 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 。 |
precompute | ‘auto’, bool or array-like of shape (n_features, n_features), default=’auto’ 是否使用預先計算的Gram矩陣來加快計算速度。Gram矩陣也可以作為參數傳遞。 |
max_iter | int, default=1000 最大迭代次數。 |
tol | float, default=1e-4 優化的容忍度:如果更新小于 tol ,則優化代碼檢查對偶間隙的最優性,并繼續進行直到其小于tol 。 |
cv | int, cross-validation generator or iterable, default=None 確定交叉驗證拆分策略。可能的輸入是: - None,使用默認的5折交叉驗證 - int,用于指定折疊數。 - CV分割器, - 可迭代生成(分割訓練、測試)索引數組。 對于int / None輸入,使用 KFold 。有關可在此處使用的各種交叉驗證策略,請參閱用戶指南。 在0.22版本中更改: cv 如果是“None”,默認值從3更改為5。 |
copy_X | bool, default=True 如果 True ,將復制X;否則X可能會被覆蓋。 |
verbose | bool or int, default=0 日志詳細程度。 |
n_jobs | int, default=None 交叉驗證期間要使用的CPU核心數量。 除非在上下文中設置了 joblib.parallel_backend ,否則None 表示1 。 -1 表示使用所有處理器。有關更多詳細信息,請參見詞匯表。 |
positive | bool, default=False 設置為時 True ,強制系數為正。 |
random_state | int, RandomState instance, default=None 更新特征隨機選擇的偽隨機數生成器種子。在 selection =='random'時使用。在多個函數調用之間傳遞同一個整數實現可重復的輸出。請參閱詞匯表。 |
selection | {‘cyclic’, ‘random’}, default=’cyclic’ 如果設置為“random”,則隨機系數將在每次迭代時更新,而不是默認情況下按順序遍歷特征。這(設置為“random”)通常會導致收斂更快,尤其是當tol高于1e-4時。 |
屬性 | 說明 |
---|---|
alpha_ | float 交叉驗證選擇的懲罰量 |
l1_ratio_ | float 交叉驗證所選擇的l1和l2懲罰之間的折中 |
coef_ | ndarray of shape (n_features,) or (n_targets, n_features) 參數向量(目標函數公式中的w)。 |
intercept_ | float or ndarray of shape (n_targets, n_features) 目標函數中的截距。 |
mse_path_ | ndarray of shape (n_l1_ratio, n_alpha, n_folds) 每次折疊l1_ratio和alpha組合下測試集的均方誤差。 |
alphas_ | ndarray of shape (n_alphas,) or (n_l1_ratio, n_alphas) 對于每個l1_ratio,用于擬合的alpha網格。 |
n_iter_ | int 坐標下降求解器運行的迭代次數,以達到指定容忍度的最優alpha。 |
另見
注
有關示例,請參見 examples / linear_model / plot_lasso_model_selection.py。
為了避免不必要的內存重復,fit方法的X參數應該作為一個Fortran-contiguous numpy數組直接傳遞。
參數l1_ratio對應于glmnet R包中的alpha,而alpha對應于glmnet中的lambda參數。更具體的說,優化目標是:
如果你有興趣分別控制L1和L2懲罰,這等效于:
這里:
示例
>>> from sklearn.linear_model import ElasticNetCV
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_features=2, random_state=0)
>>> regr = ElasticNetCV(cv=5, random_state=0)
>>> regr.fit(X, y)
ElasticNetCV(cv=5, random_state=0)
>>> print(regr.alpha_)
0.199...
>>> print(regr.intercept_)
0.398...
>>> print(regr.predict([[0, 0]]))
[0.398...]
方法
方法 | 說明 |
---|---|
fit (X, y) |
用坐標下降法擬合模型。 |
get_params ([deep]) |
獲取此估計器的參數。 |
path (X, y, *[, l1_ratio, eps, n_alphas, …]) |
計算具有坐標下降的彈性網路徑。 |
predict (X) |
使用線性模型進行預測。 |
score (X, y[, sample_weight]) |
返回預測的確定系數R ^ 2。 |
set_params (**params) |
設置此估算器的參數。 |
__init__(*, l1_ratio=0.5, eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, precompute='auto', max_iter=1000, tol=0.0001, cv=None, copy_X=True, verbose=0, n_jobs=None, positive=False, 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, *, l1_ratio=0.5, 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, check_input=True, **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) 目標值。 |
l1_ratio | float, default=0.5 0到1之間的數字傳遞給彈性網(在L1和L2罰分之間縮放)。 l1_ratio=1 對應于Lasso。 |
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 時允許)。 |
check_input | bool, default=True 跳過輸入驗證檢查,包括提供的Gram矩陣(假設提供),假設在check_input = False時由調用方處理。 |
**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的指定容差所進行的迭代次數。(當 return_n_iter 設置為True 時返回)。 |
另見
注
有關示例,請參見 examples / linear_model / plot_lasso_coordinate_descent_path.py。
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 估計器實例。 |