@@ -305,13 +305,11 @@ def __init__(
305305 n_components = "auto" ,
306306 * ,
307307 eps = 0.1 ,
308- dense_output = False ,
309308 compute_inverse_components = False ,
310309 random_state = None ,
311310 ):
312311 self .n_components = n_components
313312 self .eps = eps
314- self .dense_output = dense_output
315313 self .compute_inverse_components = compute_inverse_components
316314 self .random_state = random_state
317315
@@ -405,45 +403,11 @@ def fit(self, X, y=None):
405403 self .n_components_ , n_features
406404 ).astype (X .dtype , copy = False )
407405
408- # Check contract
409- assert self .components_ .shape == (self .n_components_ , n_features ), (
410- "An error has occurred the self.components_ matrix has "
411- " not the proper shape."
412- )
413-
414406 if self .compute_inverse_components :
415407 self .inverse_components_ = self ._compute_inverse_components ()
416408
417409 return self
418410
419- def transform (self , X ):
420- """Project the data by using matrix product with the random matrix.
421-
422- Parameters
423- ----------
424- X : {ndarray, sparse matrix} of shape (n_samples, n_features)
425- The input data to project into a smaller dimensional space.
426-
427- Returns
428- -------
429- X_new : {ndarray, sparse matrix} of shape (n_samples, n_components)
430- Projected array.
431- """
432- check_is_fitted (self )
433- X = self ._validate_data (
434- X , accept_sparse = ["csr" , "csc" ], reset = False , dtype = [np .float64 , np .float32 ]
435- )
436-
437- if X .shape [1 ] != self .components_ .shape [1 ]:
438- raise ValueError (
439- "Impossible to perform projection:"
440- "X at fit stage had a different number of features. "
441- "(%s != %s)" % (X .shape [1 ], self .components_ .shape [1 ])
442- )
443-
444- X_new = safe_sparse_dot (X , self .components_ .T , dense_output = self .dense_output )
445- return X_new
446-
447411 @property
448412 def _n_features_out (self ):
449413 """Number of transformed output features.
@@ -582,13 +546,12 @@ def __init__(
582546 super ().__init__ (
583547 n_components = n_components ,
584548 eps = eps ,
585- dense_output = True ,
586549 compute_inverse_components = compute_inverse_components ,
587550 random_state = random_state ,
588551 )
589552
590553 def _make_random_matrix (self , n_components , n_features ):
591- """ Generate the random projection matrix.
554+ """Generate the random projection matrix.
592555
593556 Parameters
594557 ----------
@@ -600,16 +563,34 @@ def _make_random_matrix(self, n_components, n_features):
600563
601564 Returns
602565 -------
603- components : {ndarray, sparse matrix} of shape \
604- (n_components, n_features)
605- The generated random matrix. Sparse matrix will be of CSR format.
606-
566+ components : ndarray of shape (n_components, n_features)
567+ The generated random matrix.
607568 """
608569 random_state = check_random_state (self .random_state )
609570 return _gaussian_random_matrix (
610571 n_components , n_features , random_state = random_state
611572 )
612573
574+ def transform (self , X ):
575+ """Project the data by using matrix product with the random matrix.
576+
577+ Parameters
578+ ----------
579+ X : {ndarray, sparse matrix} of shape (n_samples, n_features)
580+ The input data to project into a smaller dimensional space.
581+
582+ Returns
583+ -------
584+ X_new : ndarray of shape (n_samples, n_components)
585+ Projected array.
586+ """
587+ check_is_fitted (self )
588+ X = self ._validate_data (
589+ X , accept_sparse = ["csr" , "csc" ], reset = False , dtype = [np .float64 , np .float32 ]
590+ )
591+
592+ return X @ self .components_ .T
593+
613594
614595class SparseRandomProjection (BaseRandomProjection ):
615596 """Reduce dimensionality through sparse random projection.
@@ -759,15 +740,15 @@ def __init__(
759740 super ().__init__ (
760741 n_components = n_components ,
761742 eps = eps ,
762- dense_output = dense_output ,
763743 compute_inverse_components = compute_inverse_components ,
764744 random_state = random_state ,
765745 )
766746
747+ self .dense_output = dense_output
767748 self .density = density
768749
769750 def _make_random_matrix (self , n_components , n_features ):
770- """ Generate the random projection matrix
751+ """Generate the random projection matrix
771752
772753 Parameters
773754 ----------
@@ -779,13 +760,33 @@ def _make_random_matrix(self, n_components, n_features):
779760
780761 Returns
781762 -------
782- components : {ndarray, sparse matrix} of shape \
783- (n_components, n_features)
784- The generated random matrix. Sparse matrix will be of CSR format.
763+ components : sparse matrix of shape (n_components, n_features)
764+ The generated random matrix in CSR format.
785765
786766 """
787767 random_state = check_random_state (self .random_state )
788768 self .density_ = _check_density (self .density , n_features )
789769 return _sparse_random_matrix (
790770 n_components , n_features , density = self .density_ , random_state = random_state
791771 )
772+
773+ def transform (self , X ):
774+ """Project the data by using matrix product with the random matrix.
775+
776+ Parameters
777+ ----------
778+ X : {ndarray, sparse matrix} of shape (n_samples, n_features)
779+ The input data to project into a smaller dimensional space.
780+
781+ Returns
782+ -------
783+ X_new : {ndarray, sparse matrix} of shape (n_samples, n_components)
784+ Projected array. It is a sparse matrix only when the input is sparse and
785+ `dense_output = False`.
786+ """
787+ check_is_fitted (self )
788+ X = self ._validate_data (
789+ X , accept_sparse = ["csr" , "csc" ], reset = False , dtype = [np .float64 , np .float32 ]
790+ )
791+
792+ return safe_sparse_dot (X , self .components_ .T , dense_output = self .dense_output )
0 commit comments