File tree 3 files changed +35
-1
lines changed 3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 34
34
import scipy .sparse as sp
35
35
36
36
from .base import BaseEstimator , TransformerMixin
37
+ from .base import _check_feature_names_in
37
38
38
39
from .utils import check_random_state
39
40
from .utils .extmath import safe_sparse_dot
@@ -420,6 +421,26 @@ def transform(self, X):
420
421
X_new = safe_sparse_dot (X , self .components_ .T , dense_output = self .dense_output )
421
422
return X_new
422
423
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
+ )
443
+
423
444
424
445
class GaussianRandomProjection (BaseRandomProjection ):
425
446
"""Reduce dimensionality through Gaussian random projection.
Original file line number Diff line number Diff line change @@ -372,7 +372,6 @@ def test_pandas_column_name_consistency(estimator):
372
372
"manifold" ,
373
373
"neighbors" ,
374
374
"neural_network" ,
375
- "random_projection" ,
376
375
]
377
376
378
377
Original file line number Diff line number Diff line change @@ -359,3 +359,17 @@ def test_johnson_lindenstrauss_min_dim():
359
359
Regression test for #17111: before #19374, 32-bit systems would fail.
360
360
"""
361
361
assert johnson_lindenstrauss_min_dim (100 , eps = 1e-5 ) == 368416070986
362
+
363
+
364
+ @pytest .mark .parametrize ("random_projection_cls" , all_RandomProjection )
365
+ def test_random_projection_feature_names_out (random_projection_cls ):
366
+ random_projection = random_projection_cls (n_components = 2 )
367
+ random_projection .fit (data )
368
+ names_out = random_projection .get_feature_names_out ()
369
+ class_name_lower = random_projection_cls .__name__ .lower ()
370
+ expected_names_out = np .array (
371
+ [f"{ class_name_lower } { i } " for i in range (random_projection .n_components_ )],
372
+ dtype = object ,
373
+ )
374
+
375
+ assert_array_equal (names_out , expected_names_out )
You can’t perform that action at this time.
0 commit comments