8000 Use _ClassNamePrefixFeaturesOutMixin · scikit-learn/scikit-learn@e5766ef · GitHub
[go: up one dir, main page]

Skip to content

Commit e5766ef

Browse files
committed
Use _ClassNamePrefixFeaturesOutMixin
1 parent e080f95 commit e5766ef

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

doc/whats_new/v1.1.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ Changelog
162162
ndarray with `np.nan` when passed a `Float32` or `Float64` pandas extension
163163
array with `pd.NA`. :pr:`21278` by `Thomas Fan`_.
164164

165+
:mod:`sklearn.random_projection`
166+
................................
167+
168+
- |API| Adds :term:`get_feature_names_out` to all transformers in the
169+
:mod:`sklearn.random_projection` module:
170+
:class:`~sklearn.random_projection.GaussianRandomProjection` and
171+
:class:`~sklearn.random_projection.SparseRandomProjection`. :pr:`21330` by
172+
:user:`Loïc Estève <lesteve>`.
173+
165174
Code and Documentation Contributors
166175
-----------------------------------
167176

sklearn/random_projection.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import scipy.sparse as sp
3535

3636
from .base import BaseEstimator, TransformerMixin
37-
from .base import _check_feature_names_in
37+
from .base import _ClassNamePrefixFeaturesOutMixin
3838

3939
from .utils import check_random_state
4040
from .utils.extmath import safe_sparse_dot
@@ -291,7 +291,9 @@ def _sparse_random_matrix(n_components, n_features, density="auto", random_state
291291
return np.sqrt(1 / density) / np.sqrt(n_components) * components
292292

293293

294-
class BaseRandomProjection(TransformerMixin, BaseEstimator, metaclass=ABCMeta):
294+
class BaseRandomProjection(
295+
TransformerMixin, BaseEstimator, _ClassNamePrefixFeaturesOutMixin, metaclass=ABCMeta
296+
):
295297
"""Base class for random projections.
296298
297299
Warning: This class should not be used directly.
@@ -421,25 +423,10 @@ def transform(self, X):
421423
X_new = safe_sparse_dot(X, self.components_.T, dense_output=self.dense_output)
422424
return X_new
423425

424-
def get_feature_names_out(self, input_features=None):
425-
"""Get output feature names for transformation.
426-
427-
Parameters
428-
----------
429-
input_features : array-like of str or None, default=None
430-
Not used, present here for API consistency by convention.
431-
432-
Returns
433-
-------
434-
feature_names_out : ndarray of str objects
435-
Transformed feature names.
436-
"""
437-
_check_feature_names_in(self, input_features)
438-
class_name = self.__class__.__name__.lower()
439-
return np.asarray(
440-
[f"{class_name}{i}" for i in range(self.n_components)],
441-
dtype=object,
442-
)
426+
@property
427+
def _n_features_out(self):
428+
"""Number of transformed output features."""
429+
return self.n_components
443430

444431

445432
class GaussianRandomProjection(BaseRandomProjection):

0 commit comments

Comments
 (0)
0