sklearn.linear_model.ElasticNet?
class sklearn.linear_model.ElasticNet(alpha=1.0, *, l1_ratio=0.5, fit_intercept=True, normalize=False, precompute=False, max_iter=1000, copy_X=True, tol=0.0001, warm_start=False, positive=False, random_state=None, selection='cyclic')
將L1和L2先驗組合作為正則化器的線性回歸。
最小化目標函數:
如果你有興趣分別控制L1和L2懲罰,這等效于:
這里:
參數對應于glmnet R包中的alpha,而alpha對應于glmnet中的lambda參數。具體來說,是lasso懲罰。目前,是不可靠的,除非你提供你自己的alpha序列。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
alpha | float, default=1.0 乘以懲罰項的常數。默認為1.0。有關此參數的確切數學含義,請參見注釋。 alpha = 0 等同于由LinearRegression 對象求解的普通最小二乘。由于數字原因,不建議alpha = 0 與Lasso 對象一起使用。鑒于此,你應該使用LinearRegression 對象。 |
l1_ratio | float, default=0.5 Elastic-Net(彈性網)混合參數,取值范圍 0 <= l1_ratio <= 1 。僅在penalty='elasticnet' 時使用。設置l1_ratio=0 等同于使用L2懲罰,而設置l1_ratio=1 等同于使用L1懲罰。對于0 < l1_ratio <1 ,懲罰是L1和L2的組合。 |
fit_intercept | bool, default=True 是否估計截距。如果為 False ,則假定數據已經中心化。 |
normalize | bool, default=Falsefit_intercept 設置為False 時,將忽略此參數。如果為True,則在回歸之前通過減去均值并除以l2-范數來對回歸變量X進行歸一化。如果你希望標準化,請先使用 sklearn.preprocessing.StandardScaler ,然后調用fit 估算器并設置normalize=False 。 |
precompute | bool or array-like of shape (n_features, n_features), default=False 是否使用預先計算的Gram矩陣來加快計算速度。Gram矩陣也可以作為參數傳遞。對于稀疏輸入,此選項始終是 True 以保持稀疏性。 |
max_iter | int, default=1000 最大迭代次數。 |
copy_X | default=True 如果 True ,將復制X;否則X可能會被覆蓋。 |
tol | float, default=1e-4 優化的容忍度:如果更新小于 tol ,優化代碼將檢查對偶間隙的最優性,并一直持續到它小于tol 為止。 |
warm_start | bool, default=False 設置為True時,重用前面調用的解決方案來進行初始化,否則,只清除前面的解決方案。請參閱詞匯表。 |
positive | bool, default=False 設置為 True 時,強制系數為正。 |
random_state | int, RandomState instance, default=None 更新特征隨機選擇的偽隨機數生成器種子。在 selection =='random'時使用。在多個函數調用之間傳遞同一個整數實現可重復的輸出。請參閱詞匯表。 |
selection | {‘cyclic’, ‘random’}, default=’cyclic’ 如果設置為“random”,則隨機系數將在每次迭代時更新,而不是默認情況下按順序遍歷特征。這(設置為“random”)通常會導致收斂更快,尤其是當tol高于1e-4時。 |
屬性 | 說明 |
---|---|
coef_ | ndarray of shape (n_features,) or (n_targets, n_features) 參數向量(目標函數公式中的w) |
sparse_coef_ |
sparse matrix of shape (n_features, 1) or (n_targets, n_features) 擬合 coef_ 的稀疏表示 |
intercept_ | float or ndarray of shape (n_targets,) 目標函數中的截距。 |
n_iter_ | list of int 坐標下降求解器運行達到指定容忍度的迭代次數。 |
另見
通過交叉驗證選擇最佳模型的彈性網模型。
通過增量訓練實現彈性凈回歸。
通過彈性懲罰(
SGDClassifier(loss="log", penalty="elasticnet")
)實現邏輯回歸。
注
為了避免不必要的內存重復,fit方法的X參數應該作為一個Fortran-contiguous numpy數組直接傳遞。
示例
>>> from sklearn.linear_model import ElasticNet
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_features=2, random_state=0)
>>> regr = ElasticNet(random_state=0)
>>> regr.fit(X, y)
ElasticNet(random_state=0)
>>> print(regr.coef_)
[18.83816048 64.55968825]
>>> print(regr.intercept_)
1.451...
>>> print(regr.predict([[0, 0]]))
[1.451...]
方法
方法 | 說明 |
---|---|
fit (X, y[, sample_weight, check_input]) |
用坐標下降算法擬合模型。 |
get_params ([deep]) |
獲取此估計器的參數。 |
path (X, y, *[, l1_ratio, eps, n_alphas, …]) |
計算具有坐標下降的彈性網路徑。 |
predict (X) |
使用線性模型進行預測。 |
score (X, y[, sample_weight]) |
返回預測的確定系數R ^ 2。 |
set_params (**params) |
設置此估算器的參數。 |
__init__(alpha=1.0, *, l1_ratio=0.5, fit_intercept=True, normalize=False, precompute=False, max_iter=1000, copy_X=True, tol=0.0001, warm_start=False, positive=False, random_state=None, selection='cyclic')
初始化self, 請參閱help(type(self))以獲得準確的說明。
fit(X, y, sample_weight=None, check_input=True)
用坐標下降法擬合模型。
參數 | 說明 |
---|---|
X | {ndarray, sparse matrix} of (n_samples, n_features) 樣本數據。 |
y | {ndarray, sparse matrix} of shape (n_samples,) or (n_samples, n_targets) 目標值。如有必要,將強制轉換為X的數據類型 |
sample_weight | float or array-like of shape (n_samples,), default=None 樣本權重。 |
check_input | bool, default=True 允許繞過多個輸入檢查。除非你知道自己要做什么,否則不要使用此參數。 |
注
坐標下降法是一次考慮數據的每一列的算法,因此如有必要,它會自動將X輸入轉換為Fortran-contiguous的numpy數組。
為了避免內存重新分配,建議直接使用這種格式在內存中分配初始數據。
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 估計器實例。 |
property sparse_coef_
擬合coef_
的稀疏表示