sklearn.cross_decomposition.PLSRegression?
class sklearn.cross_decomposition.PLSRegression(n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)[source]
PLS回歸
PLSRegression實現一維響應時稱為PLS2或PLS1的PLS 2塊回歸。 此類繼承_PLS,其mode=”A”,deflation_mode =“regression”,norm_y_weights = False且algorithm=“ nipals”。
在用戶指南中閱讀更多內容。
0.8版的新功能。
參數 | 說明 |
---|---|
n_components | int, (default 2) 要保留的組件數。 |
scale | boolean, (default True) 是否收縮數據 |
max_iter | an integer, (default 500) NIPALS內循環的最大迭代次數(僅當algorithm =“ nipals”時使用) |
tol | non-negative real 迭代算法中使用的公差默認為1e-06。 |
copy | boolean, default True 是否應在副本上進行收縮。 除非您不擔心副作用,否則將默認值設為True |
屬性 | 說明 |
---|---|
x_weights_ | array, [p, n_components] X塊的權重向量。 |
y_weights_ | array, [q, n_components] Y塊的權重向量。 |
x_loadings_ | array, [p, n_components] X塊的加載向量。 |
y_loadings_ | array, [q, n_components] Y塊的加載向量。 |
x_scores_ | array, [n_samples, n_components] X得分。 |
y_scores_ | array, [n_samples, n_components] Y得分。 |
x_rotations_ | array, [p, n_components] X塊的潛在旋轉。 |
y_rotations_ | array, [q, n_components] Y塊的潛在旋轉。 |
coef_ | array, [p, q] 線性模型的系數:Y = X coef_ + Err |
n_iter_ | array-like 每個組件的NIPALS內部循環的迭代次數。 |
注
矩陣:
T: x_scores_
U: y_scores_
W: x_weights_
C: y_weights_
P: x_loadings_
Q: y_loadings_
計算如下:
X = T P.T + Err and Y = U Q.T + Err
T[:, k] = Xk W[:, k] for k in range(n_components)
U[:, k] = Yk C[:, k] for k in range(n_components)
x_rotations_ = W (P.T W)^(-1)
y_rotations_ = C (Q.T C)^(-1)
其中Xk和Yk是迭代k的殘差矩陣。
對于每個分量k,找到優化的權重u,v:
max corr(Xk u,Yk v)* std(Xk u)std(Yk u),使得| u | = 1
注意,它使得分和塊內方差之間的相關性最大化。
X(Xk + 1)塊的殘差矩陣是通過對當前X分數x_score的收縮獲得的。
Y(Yk + 1)塊的殘差矩陣是通過對當前X分數進行收縮獲得的。這將執行稱為PLS2的PLS回歸。該模式是面向預測的。
此實現與R語言提供的3個PLS包(R項目)提供的結果相同:
帶函數pls(X,Y,mode =“regression”)的“mixOmics”
帶函數plsreg2(X,Y)的“plspm”
帶函數oscorespls.fit(X,Y)的“pls”
參考資料
Jacob A. Wegelin. A survey of Partial Least Squares (PLS) methods, with emphasis on the two-block case. Technical Report 371, Department of Statistics, University of Washington, Seattle, 2000.
法語版但仍然可以參考: Tenenhaus, M. (1998). La regression PLS: theorie et pratique. Paris: Editions Technic.
示例
>>> from sklearn.cross_decomposition import PLSRegression
>>> X = [[0., 0., 1.], [1.,0.,0.], [2.,2.,2.], [2.,5.,4.]]
>>> Y = [[0.1, -0.2], [0.9, 1.1], [6.2, 5.9], [11.9, 12.3]]
>>> pls2 = PLSRegression(n_components=2)
>>> pls2.fit(X, Y)
PLSRegression()
>>> Y_pred = pls2.predict(X)
方法 | 說明 |
---|---|
fit (self, X, Y) |
訓練數據模型 |
fit_transform (self, X[, y]) |
在訓練集上學習并應用降維 |
get_params (self[, deep]) |
獲取此估計量的參數。 |
inverse_transform (self, X) |
將數據轉換回其原始空間。 |
predict (self, X[, copy]) |
應用在訓練集上學到的降維。 |
score (self, X, y[, sample_weight]) |
返回預測的確定系數R^2。 |
set_params (self, **params) |
設置此估算器的參數。 |
transform (self, X[, Y, copy]) |
應用在訓練集上學到的降維。 |
__init__(self, n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)
初始化self。有關準確的簽名,請參見help(type(self))。
fit(self, X, Y)
訓練數據。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 訓練向量,其中n_samples是樣本數,n_features是預測變量數。 |
Y | array-like of shape (n_samples, n_targets) 目標向量,其中n_samples是樣本數,n_targets是響應變量數。 |
fit_transform(self, X, y=None)
在訓練集上學習并應用降維。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 訓練向量,其中n_samples是樣本數,n_features是預測變量數。 |
y | array-like of shape (n_samples, n_targets) 目標向量,其中n_samples是樣本數,n_targets是響應變量數。 |
返回值 | 說明 |
---|---|
如果未指定Y,則為x_scores,否則為(x_scores,y_scores)。 | - |
get_params(self, deep=True)
獲取此估計量的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和作為估算器的所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
inverse_transform(self, X)
將數據轉換回其原始空間。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_components) 新數據,其中n_samples是樣本數,n_components是pls分量數。 |
返回值 | 說明 |
---|---|
x_reconstructed | array-like of shape (n_samples, n_features) |
注
僅當n_components = n_features時,此轉換才是精確的
predict(self, X, copy=True)
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 訓練向量,其中n_samples是樣本數,n_features是預測變量數。 |
copy | boolean, default True 是復制X和Y,還是執行就地歸一化。 |
注
該調用需要估計p x q矩陣,這在高維空間中可能是個問題。
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.mean())** 2).sum()。 可能的最高得分為1.0,并且可能為負(因為該模型可能會更差)。 不管輸入特征如何,常數模型始終預測y的期望值將獲得0.0的R^2分數。
參數 | 說明 |
---|---|
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)對于y的R^2 |
注
使用版本0.23的multioutput ='uniform_average'在回歸器上調用score時,使用的R2分數,來保持與r2_score的默認值一致。 這會影響所有多輸出回歸器的score方法(MultiOutputRegressor除外)。
set_params(self, **params)
設置此估算器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。 后者的參數格式為<component> __ <parameter>
,以便可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估算器參數。 |
返回值 | 說明 |
---|---|
self | object 估算器實例。 |
transform(self, X, Y=None, copy=True)
應用在訓練集上學到的降維。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 訓練向量,其中n_samples是樣本數,n_features是預測變量數。 |
Y | array-like of shape (n_samples, n_targets) 目標向量,其中n_samples是樣本數,n_targets是響應變量數。 |
copy | boolean, default True 是復制X和Y,還是直接執行規范化. |
返回值 |
---|
如果未指定Y,則為x_scores,否則為(x_scores,y_scores)。 |