sklearn.kernel_ridge Kernel Ridge Regression?
class sklearn.kernel_ridge.KernelRidge(alpha=1, *, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=None)
[源碼]
內核嶺回歸。
內核嶺回歸(KRR)將嶺回歸(具有L2-范數正則化的線性最小二乘)與內核技巧結合在一起。因此,它學習了由各個內核和數據產生的空間中的線性函數。對于非線性內核,這對應于原始空間中的非線性函數。
KRR學習的模型的形式與支持向量回歸(SVR)相同。但是,使用了不同的損失函數:KRR使用平方誤差損失,而支持向量回歸使用epsilon不敏感損失,兩者均與L2正則化結合。與SVR相比,擬合KRR模型可以封閉形式進行,對于中等規模的數據集通常更快。另一方面,學習的模型是非稀疏的,因此比SVR慢,后者在預測時學習epsilon> 0的稀疏模型。
該估計器量有對多元回歸的內置支持(即,當y是形狀為[n_samples,n_targets]的二維數組時)。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
alpha | float or array-like of shape (n_targets,) 正則強度;必須為正浮點數。正則化改善了問題的狀況,并減少了估計的方差。較大的值表示更強的正則化。在其他線性模型中,Alpha對應于1/(2C),如logisticReturnation或sklearn.svm.LinearSVC.如果數組被傳遞,則假定懲罰是特定于目標的.因此它們必須在數量上對應。公式見Ridge regression and classification。 |
kernel | string or callable, default=”linear” 內部使用的內核映射。此參數直接傳遞給 sklearn.metrics.pairwise.pairwise_kernel 。如果kernel 是字符串,則必須是pairwise.PAIRWISE_KERNEL_FUNCTIONS 中的指標之一。如果kernel “預先計算”,則假定X為內核矩陣。另外,如果kernel 是可調用函數,則在每對實例(行)上調用它,并記錄結果值。可調用對象應將X的兩行作為輸入,并以單個數字返回相應的內核值。這意味著sklearn.metrics.pairwise 不允許調用from ,因為它們在矩陣而不是單個樣本上運行。請使用標識內核的字符串代替。 |
gamma | float, default=None RBF,拉普拉斯算子,多項式,指數chi2和sigmoid kernels的Gamma參數。默認值的解釋留給內核;請參閱sklearn.metrics.pairwise的文檔。被其他內核忽略。 |
degree | float, default=3 多項式內核的度。被其他內核忽略。 |
coef0 | float, default=1 多項式和sigmoid kernels的系數為零。被其他內核忽略。 |
kernel_params | mapping of string to any, optional 作為可調用對象傳遞的內核函數的其他參數(關鍵字參數)。 |
屬性 | 說明 |
---|---|
dual_coef_ | ndarray of shape (n_samples,) or (n_samples, n_targets) 核空間中權重向量的表示 |
X_fit_ | {ndarray, sparse matrix} of shape (n_samples, n_features) 訓練數據,這也是預測所必需的。如果kernel ==“ precomputed”,則它是形狀為(n_samples,n_samples)的預計算訓練矩陣。 |
另見
線性嶺回歸。
支持使用libsvm實現的向量回歸。
參考文獻
1 Kevin P. Murphy “Machine Learning: A Probabilistic Perspective”, The MIT Press chapter 14.4.3, pp. 492-493
實例
>>> from sklearn.kernel_ridge import KernelRidge
>>> import numpy as np
>>> n_samples, n_features = 10, 5
>>> rng = np.random.RandomState(0)
>>> y = rng.randn(n_samples)
>>> X = rng.randn(n_samples, n_features)
>>> clf = KernelRidge(alpha=1.0)
>>> clf.fit(X, y)
KernelRidge(alpha=1.0)
方法 | 說明 |
---|---|
fit (self, X[, y, sample_weight]) |
擬合內核嶺回歸模型 |
get_params (self[, deep]) |
獲取此估計量的參數。 |
predict (self, X) |
使用內核嶺模型進行預測 |
score (self, X, y[, sample_weight]) |
返回預測的確定系數R ^ 2。 |
set_params (self, **params) |
設置此估算量的參數。 |
__init__(self, alpha=1, *, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=None)
[源碼]
初始化self, 請參閱help(type(self))以獲得準確的說明
fit(self, X, y=None, sample_weight=None)
[源碼]
擬合內核嶺回歸模型
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 訓練數據。如果kernel ==“ precomputed”,則它是形狀為(n_samples,n_samples)的預計算內核矩陣。 |
y | array-like of shape (n_samples,) or (n_samples, n_targets) 目標值 |
sample_weight | float or array-like of shape [n_samples] 每個樣本的單獨權重,如果未通過則忽略。 |
返回值 | 說明 |
---|---|
self | returns an instance of self. |
get_params(self, deep=True)
[源碼]
獲取此估計量的參數。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 如果為True,則將返回此估算量和作為估算量的所包含子對象的參數。 |
返回值 | 說明 |
---|---|
C | ndarray of shape (n_samples,) or (n_samples, n_targets) 參數名稱映射到其值。 |
score(self, X, y, sample_weight=None)
[源碼]
返回預測的確定系數R ^ 2。
系數R^2定義為(1-u/v),其中u是平方的殘差和((y_true-y_pred)2).sum(),v是平方和總和((y_true-y_true.means())2).sum()。最高得分可能為1.0,也可能為負(因為該模型可能會更差)。如果常量模型總是預測y的期望值,而不考慮輸入特征,R^2則會得到分數0.0。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 測試數據。對于某些估計量,可以是預先計算的內核矩陣或通用對象列表,而不是shape =(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 self.predict(X) wrt. y.的R^2 |
在回歸函數上調用score時使用的R2 score使用0.23版本中的multioutput='uniform_average'來保持與R2\u score的默認值一致。這樣處理會影響所有多輸出回歸函數的評分方法(MultiOutputRegressor除外)。
set_params(self, **params)
[源碼]
設置此估算量的參數。
該方法適用于簡單估計量以及嵌套對象(例如pipelines)。后者具有形式的參數。<component>__<parameter>
這樣就可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估算量參數。 |
返回值 | 說明 |
---|---|
self | object 估計量實例。 |