8
8
9
9
import numpy as np
10
10
import scipy
11
- from scipy .linalg import eig
12
- from scipy .sparse .linalg import eigs as speig
11
+ from scipy .linalg import eigh
12
+ from scipy .sparse .linalg import eigsh
13
13
from sklearn .base import (
14
14
BaseEstimator ,
15
15
MetaEstimatorMixin ,
@@ -142,6 +142,11 @@ def fit(self, X, y=None, warm_start=False):
142
142
force_all_finite = not tags .get ("allow_nan" , True ),
143
143
multi_output = True ,
144
144
)
145
+ if len (y .shape ) == 1 :
146
+ # force y to have multi_output 2D format even when it's 1D, since
147
+ # many functions, most notably PCov routines, assume an array storage
148
+ # format, most notably to compute (y @ y.T)
149
+ y = y .reshape ((len (y ), 1 ))
145
150
else :
146
151
X = check_array (
147
152
X ,
@@ -659,6 +664,8 @@ def _init_greedy_search(self, X, y, n_to_select):
659
664
features and computes their initial importance score.
660
665
"""
661
666
667
+ self .X_ref_ = X
668
+ self .y_ref_ = y
662
669
self .X_current_ = X .copy ()
663
670
if y is not None :
664
671
self .y_current_ = y .copy ()
@@ -760,15 +767,16 @@ def _compute_pi(self, X, y=None):
760
767
)
761
768
762
769
if self .k < pcovr_distance .shape [0 ] - 1 :
763
- v , U = speig (pcovr_distance , k = self .k , tol = 1e-12 )
770
+ v , U = eigsh (pcovr_distance , k = self .k , tol = 1e-12 )
764
771
else :
765
- v , U = eig (pcovr_distance )
772
+ v , U = eigh (pcovr_distance )
766
773
U = U [:, np .flip (np .argsort (v ))]
767
774
pi = (np .real (U )[:, : self .k ] ** 2.0 ).sum (axis = 1 )
768
775
769
776
return pi
770
777
771
778
def _orthogonalize (self , last_selected ):
779
+
772
780
if self ._axis == 1 :
773
781
self .X_current_ = X_orthogonalizer (
774
782
x1 = self .X_current_ , c = last_selected , tol = self .tolerance
@@ -777,18 +785,17 @@ def _orthogonalize(self, last_selected):
777
785
self .X_current_ = X_orthogonalizer (
778
786
x1 = self .X_current_ .T , c = last_selected , tol = self .tolerance
779
787
).T
780
-
781
788
if self .y_current_ is not None :
782
789
if self ._axis == 1 :
783
790
self .y_current_ = Y_feature_orthogonalizer (
784
791
self .y_current_ , X = self .X_selected_ , tol = self .tolerance
785
792
)
786
793
else :
787
794
self .y_current_ = Y_sample_orthogonalizer (
788
- self .y_current_ ,
789
- self .X_current_ ,
790
- y_ref = self .y_selected_ ,
791
- X_ref = self .X_selected_ ,
795
+ self .y_ref_ ,
796
+ self .X_ref_ ,
797
+ y_ref = self .y_selected_ [: self . n_selected_ ] ,
798
+ X_ref = self .X_selected_ [: self . n_selected_ ] ,
792
799
tol = self .tolerance ,
793
800
)
794
801
0 commit comments