0
$\begingroup$

I performed partial least squares regression using Python's sklearn.cross_decomposition.PLSRegression using the example data in the sklearn docs. I am surprised that X @ coef_ + intercept_ does not equal Y_pred. Can someone please explain?

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)
[email protected]_ + pls2.intercept_

returns

array([[ 6.80991986,  6.88073249],
       [ 6.24687317,  6.24590503],
       [16.37620337, 16.72659034],
       [27.32746904, 28.13552828]])

but Y_pred is

array([[ 0.26087869,  0.15302213],
       [ 0.60667302,  0.45634164],
       [ 6.46856199,  6.48931562],
       [11.7638863 , 12.00132061]])
$\endgroup$

2 Answers 2

2
$\begingroup$

I think you need to center and scale your X matrix before multiplying by the coefficients.

$\endgroup$
2
$\begingroup$

Scikit-learn is open-source, so you always can check the source code

        # Normalize
        X -= self._x_mean
        X /= self._x_std
        # TODO(1.3): change `self._coef_` to `self.coef_`
        Ypred = X @ self._coef_.T
        return Ypred + self.intercept_

where

    @property
    def coef_(self):
        [...]
        return self._coef_.T
$\endgroup$
1
  • $\begingroup$ thats exactly what I did to give my answer :) $\endgroup$
    – bdeonovic
    Commented Oct 3, 2022 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.