sklearn.linear_model.RidgeCV?
class sklearn.linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0), *, fit_intercept=True, normalize=False, scoring=None, cv=None, gcv_mode=None, store_cv_values=False)
內置交叉驗證的Ridge分類器。
有關交叉驗證估算器,請參閱詞匯表。
默認情況下,它執行泛化的交叉驗證,這是一種有效的“留一法”交叉驗證的形式。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
alphas | ndarray of shape (n_alphas,), default=(0.1, 1.0, 10.0) 要嘗試的Alpha值數組。正則強度,必須為正浮點數。正則化改善了問題的狀況,并減少了估計的方差。較大的值表示更強的正則化。在其他線性模型中,Alpha對應于 1 / (2C) ,例如 LogisticRegression 或sklearn.svm.LinearSVC ,如果使用通用交叉驗證,則alpha必須為正。 |
fit_intercept | bool, default=True 是否計算此模型的截距。如果設置為false,則在計算中將不使用截距(即數據應已經中心化)。 |
normalize | bool, default=Falsefit_intercept 設置為False 時,將忽略此參數。如果為True,則在回歸之前通過減去均值并除以l2-范數來對回歸變量X進行歸一化。如果你希望標準化,請先使用 sklearn.preprocessing.StandardScaler ,然后調用fit 估算器并設置normalize=False 。 |
scoring | string, callable, default=None 字符串(請參閱模型評估文檔)或帶 scorer(estimator, X, y) 簽名的評分器可調用對象 。 |
cv | int, cross-validation generator or an iterable, default=None 確定交叉驗證拆分策略。可能輸入是: - None,使用有效的“留一法”交叉驗證(也稱為通用交叉驗證) - 整數,用于指定折疊數。 - CV分割器, - 可迭代生成(分割訓練、測試)索引數組。 對于整數或None,如果 y 是二分類或多分類,則使用sklearn.model_selection.StratifiedKFold ,否則使用sklearn.model_selection.KFold 。有關可在此處使用的各種交叉驗證策略,請參閱用戶指南。 |
gcv_mode | {‘auto’, ‘svd’, eigen’}, default=’auto’ 指示執行通用交叉驗證時使用哪種策略的標志。選項有: 'auto' : use 'svd' if n_samples > n_features, otherwise use 'eigen' 'svd' : force use of singular value decomposition of X when X is dense, eigenvalue decomposition of X^T.X when X is sparse. 'eigen' : force computation via eigendecomposition of X.X^T<br /> “auto”模式是默認模式,用于根據訓練數據的形狀選擇兩者中更合適的選項。 |
store_cv_values | bool, default=False 是否將與每個alpha對應的交叉驗證值存儲在 cv_values_ 屬性中(請參見下文)。該參數僅與cv=None (即使用通用交叉驗證)兼容。 |
屬性 | 說明 |
---|---|
cv_values_ | ndarray of shape (n_samples, n_alphas) or shape (n_samples, n_targets, n_alphas), optional 每個alpha的交叉驗證值(當 store_cv_values=True 和 cv=None )。fit() 被調用之后,此屬性將包含均方誤差(默認)或 {loss,score}_func 函數(如果在構造函數中提供)的值。 |
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 。 |
alpha_ | float 估計的正則化參數。 |
best_score_ | float 具有最佳alpha值的基計器得分。 |
另見
嶺回歸
嶺分類器
帶有內置交叉驗證的Ridge分類器
示例
>>> from sklearn.datasets import load_diabetes
>>> from sklearn.linear_model import RidgeCV
>>> X, y = load_diabetes(return_X_y=True)
>>> clf = RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X, y)
>>> clf.score(X, y)
0.5166...
方法
方法 | 說明 |
---|---|
fit (X, y[, sample_weight]) |
擬合帶有交叉驗證的Ridge回歸模型。 |
get_params ([deep]) |
獲取此估計器的參數。 |
predict (X) |
使用線性模型進行預測。 |
score (X, y[, sample_weight]) |
返回預測的確定系數R ^ 2。 |
set_params (**params) |
設置此估計器的參數。 |
__init__(alphas=(0.1, 1.0, 10.0), *, fit_intercept=True, normalize=False, scoring=None, cv=None, gcv_mode=None, store_cv_values=False)
初始化self, 請參閱help(type(self))以獲得準確的說明。
fit(X,y,sample_weight = None )
[源碼]
擬合帶有交叉驗證的Ridge回歸模型。
參數 | 說明 |
---|---|
X | ndarray of shape (n_samples, n_features) 訓練數據。使用GCV時,如有必要,將其強制轉換為float64。 |
y | ndarray of shape (n_samples,) 如有必要,將強制轉換為X的類型。 |
sample_weight | float or ndarray of shape (n_samples,), default=None 每個樣本的權重,如果使用浮點數,每個樣品的權重都相同。 |
返回值 | 說明 |
---|---|
self | object |
注
當提供樣本權重時,所選的超參數可能取決于我們是使用廣義交叉驗證(cv=None或cv='auto')還是其他形式的交叉驗證,因為只有廣義交叉驗證在計算驗證分數時才考慮樣本權重。
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 估計器實例。 |