
    _nd`)                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d Ze j                            d          e j                            dd          e j                            dd          d                                     Ze j                            dd          d             Ze j                            dd          d             Ze j                            d          e j                            dd          e j                            dd          d                                     Ze j                            d ed                    e j                            dd          e j                            ddd dgfddgfg          d                                     Z e j                            d          d             Z!d Z"d Z#d  Z$e j                            dd!          d"             Z%e j                            d#d$d%d&ej&        d'f          d(             Z'd) Z(d* Z)dS )+    N)assert_array_equal)StandardScaler)make_pipeline)SequentialFeatureSelector)make_regression
make_blobs)LinearRegression)HistGradientBoostingRegressor)cross_val_score)KMeansc                      d} t          |           \  }}t          t                      |           }t          j        t
          d          5  |                    ||           d d d            d S # 1 swxY w Y   d S )N   
n_featuresn_features_to_selectz#n_features_to_select must be eithermatchr   r   r	   pytestraises
ValueErrorfit)r   Xysfss       Olib/python3.11/site-packages/sklearn/feature_selection/tests/test_sequential.pytest_bad_n_features_to_selectr      s    Jj111DAq
#$4$6$6Z
X
X
XC	z)N	O	O	O  1                 s   A22A69A6z)ignore:Leaving `n_features_to_select` to 	direction)forwardbackwardr   )   r   	   autoc                 b   d}t          |d          \  }}t          t                      || d          }|                    ||           |dv r|dz  }|                    d          j        d         |k    sJ |j        |k    sJ |                    |          j        d	         |k    sJ d S )
N
   r   r   random_state   r   r   cv)r$   NTindicesr"   r   r   r	   r   get_supportshapen_features_to_select_	transform)r   r   r   r   r   r   s         r   test_n_features_to_selectr3      s     JjqAAADAq
#1	  C GGAqMMM~--)Q??4?((.q15IIIII$(<<<<<==!!$(<<<<<<<    c                    d}d}t          |d          \  }}t          t                      d|| d          }|                    ||           |dz
  }|                    d	
          j        d         |k    sJ |j        |k    sJ |                    |          j        d         |k    sJ |                    d	
          j        d         |j        k    sJ dS )zlCheck the behaviour of `n_features_to_select="auto"` with different
    values for the parameter `tol`.
    r&   MbP?r   r'   r$   r)   r   tolr   r+   r"   Tr,   Nr.   )r   r   r8   r   r   r   max_features_to_selects          r   test_n_features_to_select_autor:   0   s     J
CjqAAADAq
##  C GGAqMMM'!^??4?((.q15KKKKK$(>>>>>==!!$(>>>>>??4?((.q1S5NNNNNNNr4   c                    t          ddd          \  }}d}t          t                      d|| d          }|                    ||           |                    |          }t
          j                            d          }t          t          t          |j        d	                             t          |                    d
                    z
            }t          j        ||dd|                    |          f         ddt
          j        f         g          }|                    t          t          |j                                      }	t          j        ||	d	          }
t%          t                      ||d                                          }t%          t                      ||d                                          }t%          t                      ||d                                          }t%          t                      |
|d                                          }||k    sJ | dk    r||z
  |k    sJ ||z
  |k    sJ dS ||z
  |k    sJ ||z
  |k    sJ dS )av  Check the behaviour stopping criterion for feature selection
    depending on the values of `n_features_to_select` and `tol`.

    When `direction` is `'forward'`, select a new features at random
    among those not currently selected in selector.support_,
    build a new version of the data that includes all the features
    in selector.support_ + this newly selected feature.
    And check that the cross-validation score of the model trained on
    this new dataset variant is lower than the model with
    the selected forward selected features or at least does not improve
    by more than the tol margin.

    When `direction` is `'backward'`, instead of adding a new feature
    to selector.support_, try to remove one of those selected features at random
    And check that the cross-validation score is either decreasing or
    not improving by more than the tol margin.
    2   r&   r   )r   n_informativer(   r6   r$   r)   r7   r"   Tr,   N)axis)r+   r    )r   r   r	   r   r2   nprandomRandomStatelistsetranger0   r/   hstackchoicenewaxisr1   deleter   mean)r   r   r   r8   r   
selected_Xrngadded_candidatesadded_Xremoved_candidate	removed_Xplain_cv_scoresfs_cv_scoreadded_cv_scoreremoved_cv_scores                  r   ,test_n_features_to_select_stopping_criterionrT   J   sm   ( bKKKDAq
C
##  C GGAqMMMq!!J
)


"
"CCagaj 1 122SQU9V9V5W5WWXXiqqq#**-.../BJ?	
 G 

4c.G(H(H#I#IJJ	*&7a@@@I$%5%7%7A!DDDIIKKN"#3#5#5z1KKKPPRRL$%5%7%7!JJJOOQQN&'7'9'99aANNNSSUU>))))I~-#5555//C777777-#5555 </C777777r4   zn_features_to_select, expected))g?r"   )g      ?r&   )g      ?r   c                     t          d          \  }}t          t                      || d          }|                    ||           |j        |k    sJ d S )Nr&   r   r)   r*   )r   r   r	   r   r1   )r   r   expectedr   r   r   s         r   test_n_features_to_select_floatrW      sk     b)))DAq
#1	  C GGAqMMM$000000r4   seedr&   z0n_features_to_select, expected_selected_featuresr)   r"   c                 d   t           j                            |           }d}|                    |d          }d|d d df         z  d|d d df         z  z
  }t	          t                      ||d          }|                    ||           t          |                    d          |           d S )	Nd      r   r&   r)   r*   Tr,   )	r?   r@   rA   randnr   r	   r   r   r/   )	rX   r   r   expected_selected_featuresrK   	n_samplesr   r   r   s	            r   test_sanityr_      s     )


%
%CI		)QA	AaaadGb1QQQT7l"A
#1	  C GGAqMMMst446PQQQQQr4   c                      t          d          \  } }t          j                            |           } t	          t                      dd          }|                    | |           |                    |            d S )Nr&   r   r$   r)   r   r+   )r   scipysparse
csr_matrixr   r	   r   r2   r   r   r   s      r   test_sparse_supportrf      sy     b)))DAq""A
#A  C GGAqMMMMM!r4   c                  4   t           j                            d          } d\  }}t          ||d          \  }}|                     dd||ft
                    }t           j        ||<   t          t                      dd          }|	                    ||           |
                    |           t          j        t          d	          5  t          t                      dd          	                    ||           d d d            d S # 1 swxY w Y   d S )
Nr   )(      r(   r)   )sizedtyper$   ra   zInput X contains NaNr   )r?   r@   rA   r   randintboolnanr   r
   r   r2   r   r   r   r	   )rK   r^   r   r   r   nan_maskr   s          r   test_nan_supportrq      sR    )


"
"C!Iz9jqAAADAq{{1ay*&=T{JJH&AhK
#%''f  C GGAqMMMMM!	z)?	@	@	@  !V	
 	
 	

#a)))	                 s   3DDDc                     d\  } }t          | |d          \  }}t          t                      t                                }t	          |dd          }|                    ||           |                    |           t	          t                      dd          }t          t                      |          }|                    ||           |                    |           d S )Nr<   r[   r   rj   r$   r)   ra   )r   r   r   r	   r   r   r2   )r^   r   r   r   piper   s         r   test_pipeline_supportru      s     "Iz9jqAAADAq ))+;+=+=>>D
#Dv!
L
L
LCGGAqMMMMM! $A  C ))3//DHHQNNNNN1r4   c                     d\  } }t          | |d          \  }}d}t          j        t          |          5  t	          t                                                    ||           ddd           dS # 1 swxY w Y   dS )z@Check that we raise a FutureWarning with `n_features_to_select`.rs   r   rj   z4Leaving `n_features_to_select` to None is deprecatedr   N)r   r   warnsFutureWarningr   r	   r   )r^   r   r   r   warn_msgs        r   test_raise_deprecation_warningrz      s    !Iz9jqAAADAqEH	m8	4	4	4 @ @!"2"4"45599!Q???@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @s   0A55A9<A9)r)   r[   c                     t          d          \  }}t          t          d          |           }|                    |           |                    |          j        d         | k    sJ d S )Nri   r   r"   )n_initr   )r   r   r   r   r2   r0   )r   r   r   r   s       r   test_unsupervised_model_fitr}      sz    
 ###DAq
#a1  C GGAJJJ==!!$(<<<<<<<r4   r   no_validationy              ?gX@r[   c                    t          d          \  }}t          t                      d          }t          j        t
          t          f          5  |                    ||            d d d            d S # 1 swxY w Y   d S )N   r   r[   r   )r   r   r   r   r   	TypeErrorr   r   )r   r   clustersr   s       r   test_no_y_validation_model_fitr     s     ***KAx
#  C
 
	:.	/	/  1                 s   A55A9<A9c                      t          dd          \  } }t          t                      ddd          }t          j        t
          d	          5  |                    | |           d
d
d
           d
S # 1 swxY w Y   d
S )z?Check that we raise an error when tol<0 and direction='forward'r&   r   r'   r$   r    MbPr   r   r8   ztol must be positiver   Nr   re   s      r   test_forward_neg_tol_errorr     s    bq999DAq
##	  C 
z)?	@	@	@  1                 s   A33A7:A7c                     t          dd          \  } }t                      }|                    | |                              | |          }t	          |ddd          }|                    | |          }|                    ||                              ||          }d|                                                                cxk     r| j        d         k     sn J ||k     sJ d	S )
z`Check that SequentialFeatureSelector works negative tol

    non-regression test for #25525
    r&   r   r'   r$   r!   r   r   r"   N)	r   r	   r   scorer   fit_transformr/   sumr0   )r   r   lrinitial_scorer   Xr	new_scores          r   test_backward_neg_tolr   )  s    
 bq999DAq			BFF1aLL&&q!,,M
#
#	  C 
		1a	 	 Br1##B**Is  $$&&3333333333}$$$$$$r4   )*r   rb   numpyr?   numpy.testingr   sklearn.preprocessingr   sklearn.pipeliner   sklearn.feature_selectionr   sklearn.datasetsr   r   sklearn.linear_modelr	   sklearn.ensembler
   sklearn.model_selectionr   sklearn.clusterr   r   markfilterwarningsparametrizer3   r:   rT   rW   rD   r_   rf   rq   ru   rz   r}   ro   r   r   r    r4   r   <module>r      s         , , , , , , 0 0 0 0 0 0 * * * * * * ? ? ? ? ? ? 8 8 8 8 8 8 8 8 1 1 1 1 1 1 : : : : : : 3 3 3 3 3 3 " " " " " "   GHH&=>>/1BCC= = DC ?> IH=* &=>>O O ?>O2 &=>>:8 :8 ?>:8z GHH&=>>$ 
1 
1  ?> IH
1 r++&=>>6	
QF	
QC R R  ?> ,+R( GHH	 	 IH	  *  .@ @ @ /88
= 
= 98
= D"&!DEE
 
 FE
  % % % % %r4   