@@ -305,13 +305,11 @@ def __init__(
305
305
n_components = "auto" ,
306
306
* ,
307
307
eps = 0.1 ,
308
- dense_output = False ,
309
308
compute_inverse_components = False ,
310
309
random_state = None ,
311
310
):
312
311
self .n_components = n_components
313
312
self .eps = eps
314
- self .dense_output = dense_output
315
313
self .compute_inverse_components = compute_inverse_components
316
314
self .random_state = random_state
317
315
@@ -405,45 +403,11 @@ def fit(self, X, y=None):
405
403
self .n_components_ , n_features
406
404
).astype (X .dtype , copy = False )
407
405
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
-
414
406
if self .compute_inverse_components :
415
407
self .inverse_components_ = self ._compute_inverse_components ()
416
408
417
409
return self
418
410
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
-
447
411
@property
448
412
def _n_features_out (self ):
449
413
"""Number of transformed output features.
@@ -582,13 +546,12 @@ def __init__(
582
546
super ().__init__ (
583
547
n_components = n_components ,
584
548
eps = eps ,
585
- dense_output = True ,
586
549
compute_inverse_components = compute_inverse_components ,
587
550
random_state = random_state ,
588
551
)
589
552
590
553
def _make_random_matrix (self , n_components , n_features ):
591
- """ Generate the random projection matrix.
554
+ """Generate the random projection matrix.
592
555
593
556
Parameters
594
557
----------
@@ -600,16 +563,34 @@ def _make_random_matrix(self, n_components, n_features):
600
563
601
564
Returns
602
565
-------
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.
607
568
"""
608
569
random_state = check_random_state (self .random_state )
609
570
return _gaussian_random_matrix (
610
571
n_components , n_features , random_state = random_state
611
572
)
612
573
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
+
613
594
614
595
class SparseRandomProjection (BaseRandomProjection ):
615
596
"""Reduce dimensionality through sparse random projection.
@@ -759,15 +740,15 @@ def __init__(
759
740
super ().__init__ (
760
741
n_components = n_components ,
761
742
eps = eps ,
762
- dense_output = dense_output ,
763
743
compute_inverse_components = compute_inverse_components ,
764
744
random_state = random_state ,
765
745
)
766
746
747
+ self .dense_output = dense_output
767
748
self .density = density
768
749
769
750
def _make_random_matrix (self , n_components , n_features ):
770
- """ Generate the random projection matrix
751
+ """Generate the random projection matrix
771
752
772
753
Parameters
773
754
----------
@@ -779,13 +760,33 @@ def _make_random_matrix(self, n_components, n_features):
779
760
780
761
Returns
781
762
-------
782
- components : {ndarray, sparse matrix} of shape \
783
- (n_components, n_features)
784
-
DD87
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.
785
765
786
766
"""
787
767
random_state = check_random_state (self .random_state )
788
768
self .density_ = _check_density (self .density , n_features )
789
769
return _sparse_random_matrix (
790
770
n_components , n_features , density = self .density_ , random_state = random_state
791
771
)
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