sklearn.linear_model.Ridge?

class sklearn.linear_model.Ridge(alpha=1.0, *, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, solver='auto', random_state=None)

[源碼]

具有L2正則化的線性最小二乘法。

最小化目標函數:

該模型解決了一個回歸模型,其中損失函數是線性最小二乘函數,而正則化由L2-范數給出。也稱為Ridge回歸或Tikhonov正則化。此估計器對多元回歸(即當y是形狀的二維數組(n_samples,n_targets)時)具有內置的支持。

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

參數 說明
alpha float, default=1.0
正則強度,必須為正浮點數。正則化改善了問題的狀況,并減少了估計的方差。較大的值表示更強的正則化。在其他線性模型中,Alpha對應于1 / (2C),例如 LogisticRegressionsklearn.svm.LinearSVC。如果傳遞了數組,則假定懲罰特定于目標。因此,它們在數量上必須一致。
fit_intercept bool, default=True
是否計算此模型的截距。如果設置為false,則在計算中將不使用截距(即Xy已經中心化)。
normalize bool, default=False
fit_intercept設置為False 時,將忽略此參數。如果為True,則在回歸之前通過減去均值并除以l2-范數來對回歸變量X進行歸一化。如果你希望標準化,請先使用 sklearn.preprocessing.StandardScaler,然后調用fit 估算器并設置normalize=False
copy_X bool, default=True
如果為True,將復制X;否則,X可能會被覆蓋。
max_iter int, default=None
共軛梯度求解器的最大迭代次數。對于“ sparse_cg”和“ lsqr”求解器,默認值由scipy.sparse.linalg確定。對于“sag”求解器,默認值為1000。
tol float, default=1e-3
優化算法的精度。
solver {‘auto’, ‘svd’, ‘cholesky’, ‘lsqr’, ‘sparse_cg’, ‘sag’, ‘saga’}, default=’auto’
用于計算例程的求解器:
- “auto”會根據數據類型自動選擇求解器。
- “ svd”使用X的奇異值分解來計算Ridge系數。對于奇異矩陣,比“ Cholesky”更穩定。
- 'cholestsky'使用標準的scipy.linalg.solve函數獲得封閉形式的解決方案。
- 'sparse_cg'使用scipy.sparse.linalg.cg中的共軛梯度求解器。作為一種迭代算法,對于大規模數據(可以設置tolmax_iter),此求解器比“ Cholesky”更合適。
- “ lsqr”使用專用的正則化最小二乘算法scipy.sparse.linalg.lsqr。它是最快的,并且使用了迭代過程。
- “sag”使用隨機平均梯度下降,而“saga”是它的無偏和更靈活的版本。這兩種方法都使用了迭代過程,當n_samples和n_features都很大時,通常比其他求解器更快。請注意, “sag”和“saga”的快速收斂只能保證在具有大致相同規模的特性上。你可以使用sklearn.preprocessing中的縮放器對數據進行預處理。

最后五個求解器均支持密集和稀疏數據。但是,當fit_intercept為True 時,僅'sag'和'sparse_cg'支持稀疏型輸入。

0.17版本中的新功能:隨機平均梯度下降求解器。

0.19版中的新功能 :SAGA求解器。
random_state int, RandomState instance, default=None
solver=='sag'或'saga'時使用,用于隨機打亂數據。有關詳細信息,請參見詞匯表

0.17版中的新功能:random_state支持隨機平均梯度。
屬性 說明
coef_ ndarray of shape (1, n_features) or (n_classes, n_features)
權重向量。
intercept_ float or ndarray of shape (n_targets,)
決策函數中的截距。如果設置fit_intercept = False,則截距為0.0 。
n_iter_ None or ndarray of shape (n_targets,)
每個目標的實際迭代次數。僅適用于sag和lsqr求解器。其他求解器將返回None。

0.17版本中的新功能。

另見

RidgeClassifier

嶺分類器

RidgeCV

內建交叉驗證的Ridge回歸

sklearn.kernel_ridge.KernelRidge

內核嶺回歸將嶺回歸與內核技巧結合在一起

示例

>>> from sklearn.linear_model import Ridge
>>> import numpy as np
>>> n_samples, n_features = 105
>>> rng = np.random.RandomState(0)
>>> y = rng.randn(n_samples)
>>> X = rng.randn(n_samples, n_features)
>>> clf = Ridge(alpha=1.0)
>>> clf.fit(X, y)
Ridge()

方法

方法 說明
fit(X, y[, sample_weight]) 擬合Ridge回歸模型。
get_params([deep]) 獲取此估計器的參數。
predict(X) 使用線性模型進行預測。
score(X, y[, sample_weight]) 返回預測的確定系數R ^ 2。
set_params(**params) 設置此估計器的參數。
__init__(alpha=1.0, *, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, solver='auto', random_state=None)

[源碼]

初始化self, 請參閱help(type(self))以獲得準確的說明。

fit(X, y, sample_weight=None)

[源碼]

擬合Ridge分類器模型。

參數 說明
X {ndarray, sparse matrix} of shape (n_samples, n_features)
訓練數據
y ndarray of shape (n_samples,)
目標值。
sample_weight float or ndarray of shape (n_samples,), default=None
每個樣本的權重,如果使用浮點數,每個樣品的權重都相同。
返回值 說明
self returns an instance of self.
返回估計器的實例。
get_params(deep=True)

[源碼]

獲取此估計器的參數。

參數 說明
deep bool, default=True
如果為True,返回此估計器和所包含子對象的參數。
返回值 說明
params mapping of string to any
參數名稱映射到其值。
predict(X)

[源碼]

預測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
估計器實例。