@@ -69,7 +69,7 @@ def test_simple_reconstruction(self):
6969 def test_simple_prediction (self ):
7070 """
7171 Check that PCovR with a full eigendecomposition at mixing=0
72- can fully reconstruct the input properties .
72+ can reproduce a linear regression result .
7373 """
7474 for space in ["feature" , "sample" , "auto" ]:
7575 with self .subTest (space = space ):
@@ -91,13 +91,14 @@ def test_lr_with_x_errors(self):
9191 and that the prediction error increases with `mixing`
9292 """
9393 prev_error = - 1.0
94+ Ytrue = self .Y .ravel ()
9495
9596 for mixing in np .linspace (0 , 1 , 11 ):
9697 pcovr = self .model (mixing = mixing , n_components = 2 , tol = 1e-12 )
9798 pcovr .fit (self .X , self .Y )
9899
99100 Yp = pcovr .predict (X = self .X )
100- error = np .linalg .norm (self . Y - Yp ) ** 2.0 / np .linalg .norm (self . Y ) ** 2.0
101+ error = np .linalg .norm (Ytrue - Yp ) ** 2.0 / np .linalg .norm (Ytrue ) ** 2.0
101102
102103 with self .subTest (error = error ):
103104 self .assertFalse (np .isnan (error ))
@@ -111,14 +112,15 @@ def test_lr_with_t_errors(self):
111112 projection and that the prediction error increases with `mixing`.
112113 """
113114 prev_error = - 1.0
115+ Ytrue = self .Y .ravel ()
114116
115117 for mixing in np .linspace (0 , 1 , 11 ):
116118 pcovr = self .model (mixing = mixing , n_components = 2 , tol = 1e-12 )
117119 pcovr .fit (self .X , self .Y )
118120
119121 T = pcovr .transform (self .X )
120122 Yp = pcovr .predict (T = T )
121- error = np .linalg .norm (self . Y - Yp ) ** 2.0 / np .linalg .norm (self . Y ) ** 2.0
123+ error = np .linalg .norm (Ytrue - Yp ) ** 2.0 / np .linalg .norm (Ytrue ) ** 2.0
122124
123125 with self .subTest (error = error ):
124126 self .assertFalse (np .isnan (error ))
@@ -481,10 +483,10 @@ def test_none_regressor(self):
481483 self .assertTrue (pcovr .regressor is None )
482484 self .assertTrue (pcovr .regressor_ is not None )
483485
484- def test_incompatible_coef_shape (self ):
486+ def test_incompatible_coef_dim (self ):
485487 # self.Y is 2D with one target
486488 # Don't need to test X shape, since this should
487- # be caught by sklearn's _validate_data
489+ # be caught by sklearn's validate_data
488490 regressor = Ridge (alpha = 1e-8 , fit_intercept = False , tol = 1e-12 )
489491 regressor .fit (self .X , self .Y )
490492 pcovr = self .model (mixing = 0.5 , regressor = regressor )
@@ -499,14 +501,23 @@ def test_incompatible_coef_shape(self):
499501 "have dimension 2" ,
500502 )
501503
504+ def test_incompatible_coef_shape (self ):
502505 # Shape mismatch (number of targets)
506+ Y_double = np .column_stack ((self .Y , self .Y ))
507+ Y_triple = np .column_stack ((Y_double , self .Y ))
508+
509+ regressor = Ridge (alpha = 1e-8 , fit_intercept = False , tol = 1e-12 )
510+ regressor .fit (self .X , Y_double )
511+
512+ pcovr = self .model (mixing = 0.5 , regressor = regressor )
513+
503514 with self .assertRaises (ValueError ) as cm :
504- pcovr .fit (self .X , np . column_stack (( self . Y , self . Y )) )
515+ pcovr .fit (self .X , Y_triple )
505516 self .assertEqual (
506517 str (cm .exception ),
507518 "The regressor coefficients have a shape incompatible with the supplied "
508519 "target space. The coefficients have shape %r and the targets have shape %r"
509- % (regressor .coef_ .shape , np . column_stack (( self . Y , self . Y )) .shape ),
520+ % (regressor .coef_ .shape , Y_triple .shape ),
510521 )
511522
512523
0 commit comments