Skip to content
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 @@ -110,6 +110,13 @@ Changelog
reconstruction of a `X` target when a `Y` parameter is given. :pr:`19680` by
:user:`Robin Thibaut <robinthibaut>`.

- |API| Adds :term:`get_feature_names_out` to all transformers in the
:mod:`~sklearn.cross_decomposition` module:
:class:`cross_decomposition.CCA`,
:class:`cross_decomposition.PLSSVD`,
:class:`cross_decomposition.PLSRegression`,
and :class:`cross_decomposition.PLSCanonical`. :pr:`22119` by `Thomas Fan`_.

:mod:`sklearn.discriminant_analysis`
....................................

Expand Down
12 changes: 10 additions & 2 deletions sklearn/cross_decomposition/_pls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ..base import BaseEstimator, RegressorMixin, TransformerMixin
from ..base import MultiOutputMixin
from ..base import _ClassNamePrefixFeaturesOutMixin
from ..utils import check_array, check_consistent_length
from ..utils.fixes import sp_version
from ..utils.fixes import parse_version
Expand Down Expand Up @@ -156,7 +157,12 @@ def _svd_flip_1d(u, v):


class _PLS(
TransformerMixin, RegressorMixin, MultiOutputMixin, BaseEstimator, metaclass=ABCMeta
_ClassNamePrefixFeaturesOutMixin,
TransformerMixin,
RegressorMixin,
MultiOutputMixin,
BaseEstimator,
metaclass=ABCMeta,
):
"""Partial Least Squares (PLS)

Expand Down Expand Up @@ -361,6 +367,7 @@ def fit(self, X, Y):

self.coef_ = np.dot(self.x_rotations_, self.y_loadings_.T)
self.coef_ = self.coef_ * self._y_std
self._n_features_out = self.x_rotations_.shape[1]
return self

def transform(self, X, Y=None, copy=True):
Expand Down Expand Up @@ -931,7 +938,7 @@ def __init__(
)


class PLSSVD(TransformerMixin, BaseEstimator):
class PLSSVD(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator):
"""Partial Least Square SVD.

This transformer simply performs a SVD on the cross-covariance matrix
Expand Down Expand Up @@ -1079,6 +1086,7 @@ def fit(self, X, Y):
self._y_scores = np.dot(Y, V) # TODO: remove in 1.1
self.x_weights_ = U
self.y_weights_ = V
self._n_features_out = self.x_weights_.shape[1]
return self

# mypy error: Decorated property not supported
Expand Down
16 changes: 16 additions & 0 deletions sklearn/cross_decomposition/tests/test_pls.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,19 @@ def test_pls_constant_y():
pls.fit(x, y)

assert_allclose(pls.x_rotations_, 0)


@pytest.mark.parametrize("Klass", [CCA, PLSSVD, PLSRegression, PLSCanonical])
def test_pls_feature_names_out(Klass):
"""Check `get_feature_names_out` cross_decomposition module."""
X, Y = load_linnerud(return_X_y=True)

est = Klass().fit(X, Y)
names_out = est.get_feature_names_out()

class_name_lower = Klass.__name__.lower()
expected_names_out = np.array(
[f"{class_name_lower}{i}" for i in range(est.x_weights_.shape[1])],
dtype=object,
)
assert_array_equal(names_out, expected_names_out)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe those tests could be extended to also assert that est.transform(X[:1]).shape[1] == expected_names_out.shape[0].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed check is in the common tests:

assert (
len(feature_names_out) == n_features_out
), f"Expected {n_features_out} feature names, got {len(feature_names_out)}"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is already tested in the common test.

1 change: 0 additions & 1 deletion sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ def test_pandas_column_name_consistency(estimator):
# from this list to be tested
GET_FEATURES_OUT_MODULES_TO_IGNORE = [
"cluster",
"cross_decomposition",
"ensemble",
"isotonic",
"kernel_approximation",
Expand Down