sklearn中的组件解释偏最小二乘方差

use*_*236 6 scikit-learn pls

我正在尝试使用来自 sklearn 的代码执行 PLSRegression,并且我想保留那些解释某种程度差异的组件,例如在 PCA 中。

有没有办法知道 PLS 中每个分量解释了多少方差

小智 3

我也有计算每个组件的解释方差的相同要求。我是 PLS 新手,母语不是英语,请参考我的解决方案。

背景:如果您选择“deflation_mode”作为“regression”,这是默认选项。估计的 Y 可以通过“PLSRegression”[1]中的表达式计算:

Y = TQ' + 错误

其中 T 是 x_scores_,Q 是 y_loadings_ 该表达式可以提供所有主成分的估计 Y。因此,如果我们想知道第一主成分解释了多少方差,我们可以使用 x_scores_ 和 y_loadings_ 的第一个向量来计算估计的 Y1:

Y1 = T[0]Q[0]' + 错误

请参阅下面的 Python 代码,该代码计算每个分量的 R 平方。

import numpy as np
from sklearn.cross_decomposition import PLSRegression
from sklearn.metrics import r2_score

pls = PLSRegression(n_components=3)
pls.fit(X,Y_true)
r2_sum = 0
for i in range(0,3):
        Y_pred=np.dot(pls.x_scores_[:,i].reshape(-1,1),pls.y_loadings_[:,i].reshape(-1,1).T)*naY.std(axis=0, ddof=1)+naY.mean(axis=0)
        r2_sum += round(r2_score(Y_true,Y_pred),3) 
        print('R2 for %d component: %g' %(i+1,round(r2_score(Y_true,Y_pred),3)))
print('R2 for all components (): %g' %r2_sum) #Sum of above
print('R2 for all components (): %g' %round(r2_score(Y_true,pls.predict(X)),3)) #Calcuted from PLSRegression's 'predict' function.
Run Code Online (Sandbox Code Playgroud)

输出:

R2 for 1 component: 0.633
R2 for 2 component: 0.221
R2 for 3 component: 0.104
R2 for all components: 0.958
R2 for all components: 0.958
Run Code Online (Sandbox Code Playgroud)

[1] 请注意这个表达方式。在不同的计算方法中,“分数”、“重量”和“负载”的术语和值可能会略有不同。