Skip to content

ENH PLS target values (Y) inverse-transform #19680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ Changelog
`pos_label` to specify the positive class label.
:pr:`21032` by :user:`Guillaume Lemaitre <glemaitre>`.

:mod:`sklearn.cross_decomposition`
..................................

- |Enhancement| :func:`cross_decomposition._PLS.inverse_transform` now allows
reconstruction of a `X` target when a `Y` parameter is given. :pr:`19680` by
:user:`Robin Thibaut <robinthibaut>`.

:mod:`sklearn.ensemble`
...........................

Expand Down
24 changes: 20 additions & 4 deletions sklearn/cross_decomposition/_pls.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def transform(self, X, Y=None, copy=True):

return x_scores

def inverse_transform(self, X):
def inverse_transform(self, X, Y=None):
"""Transform data back to its original space.

Parameters
Expand All @@ -407,10 +407,17 @@ def inverse_transform(self, X):
New data, where `n_samples` is the number of samples
and `n_components` is the number of pls components.

Y : array-like of shape (n_samples, n_components)
New target, where `n_samples` is the number of samples
and `n_components` is the number of pls components.

Returns
-------
self : ndarray of shape (n_samples, n_features)
Return the reconstructed array.
X_reconstructed : ndarray of shape (n_samples, n_features)
Return the reconstructed `X` data.

Y_reconstructed : ndarray of shape (n_samples, n_targets)
Return the reconstructed `X` target. Only returned when `Y` is given.

Notes
-----
Expand All @@ -420,10 +427,19 @@ def inverse_transform(self, X):
X = check_array(X, dtype=FLOAT_DTYPES)
# From pls space to original space
X_reconstructed = np.matmul(X, self.x_loadings_.T)

# Denormalize
X_reconstructed *= self._x_std
X_reconstructed += self._x_mean

if Y is not None:
Y = check_array(Y, dtype=FLOAT_DTYPES)
# From pls space to original space
Y_reconstructed = np.matmul(Y, self.y_loadings_.T)
# Denormalize
Y_reconstructed *= self._y_std
Y_reconstructed += self._y_mean
return X_reconstructed, Y_reconstructed

return X_reconstructed

def predict(self, X, copy=True):
Expand Down
2 changes: 2 additions & 0 deletions sklearn/cross_decomposition/tests/test_pls.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def test_pls_canonical_basics():
# Check that inverse_transform works
X_back = pls.inverse_transform(Xt)
assert_array_almost_equal(X_back, X)
_, Y_back = pls.inverse_transform(Xt, Yt)
assert_array_almost_equal(Y_back, Y)


def test_sanity_check_pls_regression():
Expand Down