o
    \i!]                  
   @   s  d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZ d dlmZ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mZmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+m,Z,m-Z- G dd de
Z.G dd de
Z/G dd deZ0e1 Z2e2j3e2j4Z3Z5dd Z6dd Z7ej89de3j:d d e;dfdd e<dfdd e;dfd d e;dfgd!d" Z=ej89d#d d$e3j:d dgd%d& Z>ej89d#d'd d(d d)d gd*d+ Z?ej89d#d,d d$gd-d. Z@ej89d#d/d d0d d1d gd2d3 ZAG d4d5 d5e
ZBd6d7 ZCd8d9 ZDd:d; ZEe-d<d= ZFd>d? ZGej89d@edAdBdCedBdDeddBdEedgdBdEgdFdG ZHe-dHdI ZIdJdK ZJdLdM ZKdNdO ZLdPdQ ZMdRdS ZNdTdU ZOdVdW ZPdXdY ZQdZd[ ZRd\d] ZSd^d_ ZTd`da ZUej89dbe%ed dDe! dcfed dDeUfgddde ZVej89dfeeegdgdh ZWdidj ZXej89dke;dldmge;dndod gfdpdq ZYej89drdsdtgdudv ZZdwdx Z[dS )y    N)Mock)datasets)BaseEstimator)CCAPLSCanonicalPLSRegression)make_friedman1make_regression)PCA)HistGradientBoostingClassifierRandomForestClassifier)NotFittedError)SelectFromModel)
ElasticNetElasticNetCVLassoLassoCVLinearRegressionLogisticRegressionPassiveAggressiveClassifierSGDClassifier)make_pipeline)	LinearSVC)MinimalClassifierassert_allcloseassert_array_almost_equalassert_array_equalskip_if_32bitc                          e Zd Z fddZ  ZS )NaNTagc                       t   }d|j_|S NTsuper__sklearn_tags__
input_tags	allow_nanselftags	__class__ /var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/lib/python3.10/site-packages/sklearn/feature_selection/tests/test_from_model.pyr$   &      
zNaNTag.__sklearn_tags____name__
__module____qualname__r$   __classcell__r,   r,   r*   r-   r   %       r   c                       r   )NoNaNTagc                    r    )NFr"   r'   r*   r,   r-   r$   -   r.   zNoNaNTag.__sklearn_tags__r/   r,   r,   r*   r-   r5   ,   r4   r5   c                       r   )NaNTagRandomForestc                    r    r!   r"   r'   r*   r,   r-   r$   4   r.   z#NaNTagRandomForest.__sklearn_tags__r/   r,   r,   r*   r-   r6   3   r4   r6   c               	   C   sl   t dddd d d} dD ](}t| |d}|tt tt |t W d    n1 s.w   Y  qd S )N皙?
   Talphamax_itershufflerandom_statetol)gobbledigookz.5 * gobbledigook	threshold)	r   r   fitdataypytestraises
ValueError	transform)clfrA   modelr,   r,   r-   test_invalid_input>   s   
rK   c                  C   s.   t  } t| d}|tt |j| u sJ d S )N	estimator)r   r   rB   rC   rD   rM   esttransformerr,   r,   r-   test_input_estimator_unchangedI   s   
rQ   zmax_features, err_type, err_msg   zmax_features ==c                 C      dS )Ng      ?r,   Xr,   r,   r-   <lambda>Z       rV   z3max_features must be an instance of int, not float.c                 C   s   t jd d S NrR   )rC   shaperT   r,   r,   r-   rV   _       c                 C   rS   )Nr,   rT   r,   r,   r-   rV   d   rW   c                 C   sh   t |}tddd}t|| tj d}tj||d |t	t
 W d    d S 1 s-w   Y  d S )N   r   n_estimatorsr=   rM   max_featuresrA   match)reescaper   r   npinfrE   rF   rB   rC   rD   )r`   err_typeerr_msgrI   rP   r,   r,   r-   test_max_features_errorQ   s   

"ri   r`      c                 C   s   t ddd}t|| tj d}|tt}| dur,|j| ks J |jd |jks*J dS t	|dr3J |jd tjd ks?J dS )z>Check max_features_ and output shape for integer max_features.r\   r   r]   r_   NrR   max_features_)
r   r   re   rf   fit_transformrC   rD   rk   rY   hasattrr`   rI   rP   X_transr,   r,   r-   "test_inferred_max_features_integeru   s   
rp   c                 C   rS   rX   r,   rT   r,   r,   r-   rV      rW   c                 C   
   | j d S rX   rY   rT   r,   r,   r-   rV         
 c                 C      t | jd dS NrR   i'  minrY   rT   r,   r,   r-   rV          c                 C   sT   t ddd}t|| tj d}|tt}|j| tksJ |jd |jks(J dS )z?Check max_features_ and output shape for callable max_features.r\   r   r]   r_   rR   N)	r   r   re   rf   rl   rC   rD   rk   rY   rn   r,   r,   r-   #test_inferred_max_features_callable   s   
ry   c                 C   s   t t| d d S )Nr   rj   )roundlenrT   r,   r,   r-   rV      s    c                 C   sf   g dg dg dg dg}g d}t ddd}t|| tj d	}|||}|jd
 |jks1J d S )N)gףp=
?q=
ףpgףp=
?)gRQg{Gzg333333)r|   gQ޿gffffff)gQ?gGz?g?)r   rR   r   rR   r\   r   r]   r_   rR   )r   r   re   rf   rl   rY   rk   )r`   rU   rD   rI   rP   ro   r,   r,   r-   test_max_features_array_like   s   
r}   c                 C   rt   ru   rv   rT   r,   r,   r-   rV      rx   c                 C   rq   rX   rr   rT   r,   r,   r-   rV      rs   c                 C   rS   rX   r,   rT   r,   r,   r-   rV      rW   c                 C   sB   t ddd}t| d}t||tj d}|tt |t dS )z7Tests that the callable passed to `fit` is called on X.2   r   r]   )side_effectr_   N)	r   r   r   re   rf   rl   rC   rD   assert_called_with)r`   rI   mrP   r,   r,   r-   test_max_features_callable_data   s
   
r   c                   @   s   e Zd Zdd ZdddZdS )FixedImportanceEstimatorc                 C   s
   || _ d S N)importances)r(   r   r,   r,   r-   __init__   s   
z!FixedImportanceEstimator.__init__Nc                 C   s   t | j| _d S r   )re   arrayr   feature_importances_)r(   rU   rD   r,   r,   r-   rB      s   zFixedImportanceEstimator.fitr   )r0   r1   r2   r   rB   r,   r,   r,   r-   r      s    r   c               	   C   sX  t jdddddddd\} }| jd }tddd	}t|tj d
}t||tj d}|| |}|| |}t|| tt	dddd}|| |}t
|jj}tj| dd}	td|jd d D ]=}
tt	ddd|
tj d}|| |}t
|jj}tj| dd}t| d d |	d |
 f | d d |d |
 f  qct|jj|jj d S )N  r8      r   F	n_samples
n_featuresn_informativen_redundant
n_repeatedr<   r=   rR   r~   r]   rM   rA   r_   g?*   r:   r=   rL   	mergesort)kind)r   make_classificationrY   r   r   re   rf   rl   r   r   abs
estimator_coef_argsortrange)rU   rD   r`   rO   transformer1transformer2X_new1X_new2scores1candidate_indices1r   scores2candidate_indices2r,   r,   r-   test_max_features   sF   

	


,r   c               	   C   s   t jdddddddd\} }| jd }tg d}td|d D ]-}tt||tj d	}|	| |}t
| d }t|t| |jd |ksNJ q!d S )
Nr   r8   r   r   Fr   rR   )
   r   r   r   r   r   r   rj   rj   rR   )r`   rA   )r   r   rY   re   r   r   r   r   rf   rl   where_get_support_maskr   arange)rU   rD   r`   feature_importancesr   rP   X_newselected_feature_indicesr,   r,   r-   test_max_features_tiebreak   s,   

	r   c            
   	   C   s   t jdddddddd\} }tddd}t|dtj d	}|| |}t|d
d}|| |}t|dd
d	}|| |}|jd t|jd |jd ksNJ |	t
| jd tjd d f }	t|| d d |	d f  d S )Nr   r8   r   r   Fr   r~   r]   r_   g{Gz?r   rR   )r   r   r   r   re   rf   rl   rY   rw   rH   r   newaxisr   )
rU   rD   rO   r   r   r   r   transformer3X_new3selected_indicesr,   r,   r-   test_threshold_and_max_features   s&   
	$$r   c            	   	   C   s   t jdddddddd\} }tddd}td	d
gtjtjgD ]A\}}t||d}|| | t	|j
ds7J || }|jd | jd k sHJ |j
j}t|||k}t|| d d |f  qd S )Nr   r8   r   r   Fr   r~   r]   meanmedianr   r   rR   )r   r   r   zipre   r   r   r   rB   rm   r   rH   rY   r   r   r   )	rU   rD   rO   rA   funcrP   r   r   feature_maskr,   r,   r-   test_feature_importances  s(   


r   c               	   C   s   t jdddddddd\} }t|j}||dk  d9  < tddd}t|d	}|j| |d d
 | }|j| ||d
 | }t	||krJJ |j| |d| d
 | }t	||ksaJ d S )Nd   r8   r   r   Fr   rR   )r=   fit_interceptrL   )sample_weight)
r   r   re   onesrY   r   r   rB   r   all)rU   rD   r   rO   rP   maskweighted_maskreweighted_maskr,   r,   r-   test_sample_weight3  s*   

r   rM   r7   r   r   r=   l1_ratior=   c              	   C   sh   t jdddddddd\}}t| d}||| ||}t|jjdk}t	||d d |f  d S )	Nr   r8   r   r   Fr   rL   gh㈵>)
r   r   r   rB   rH   re   r   r   r   r   )rM   rU   rD   rP   r   r   r,   r,   r-   test_coef_default_thresholdO  s   



r   c            
   
   C   s   t jddddddddd\} }t }tdd	gtjtjgD ]U\}}d
dtjfD ]J}tt ||d}|	| | t
|jds?J || }|jd
 | jd
 k sPJ |	| | tjj|jd|d}|||k}	t|| d d |	f  q'qd S )Nr   r8   r   r   Fr   )r   r   r   r   r   r<   r=   	n_classesr   r   rR   rj   )rM   rA   
norm_orderr   )axisord)r   r   r   r   re   r   r   rf   r   rB   rm   r   rH   rY   linalgnormr   r   )
rU   rD   rO   rA   r   orderrP   r   r   r   r,   r,   r-   test_2d_coefk  s4   

r   c                  C   s   t dddd d} t| d}|jttttd |j}|jttttd |j}||u s/J |t}|	t
ttftttf t||t tt d}t|drYJ d S )Nr   Fr\   )r=   r<   r;   r>   rL   classespartial_fit)r   r   r   rC   rD   re   uniquer   rH   rB   vstackconcatenater   r   rm   )rO   rP   	old_model	new_modelX_transformr,   r,   r-   test_partial_fit  s   

 r   c                  C   sL   t dd} t| d}|tt |jdd |tt |jjdks$J d S )Nr   r   rL   r   )estimator__C)r   r   rB   rC   rD   
set_paramsr   CrN   r,   r,   r-   test_calling_fit_reinitializes  s   

r   c                  C   s  t ddddd d} t| }|tt |t}| tt t| dd}t|t| |tt |j| us9J t| dd}|tt t|t| t ddddd d} t| dd}d}tj	t
|d	 |tt W d    n1 svw   Y  tj	t
|d	 |tt W d    n1 sw   Y  tj	t
|d	 |t W d    n1 sw   Y  t dddd d
tt} t| dd}|tt t|jj| j |tt t|jj| j d S )Nr7   r8   Tr   r9   )prefitFzEWhen `prefit=True`, `estimator` is expected to be a fitted estimator.ra   )r:   r;   r<   r>   )r   r   rB   rC   rD   rH   r   r   rE   rF   r   r   r   r   )rI   rJ   r   rh   r,   r,   r-   test_prefit  s<   
r   c                  C   s   t ddd} | tt t| ddd d}d}tjt|d	 |t W d
   n1 s.w   Y  d}|j	|d tjt
dd	 |t W d
   d
S 1 sSw   Y  d
S )z:Check the interaction between `prefit` and `max_features`.r\   r   r]   Tc                 S   rq   rX   rr   rT   r,   r,   r-   rV     rs   z*test_prefit_max_features.<locals>.<lambda>r   r`   z[When `prefit=True` and `max_features` is a callable, call `fit` before calling `transform`.ra   Ng      @)r`   z!`max_features` must be an integer)r   rB   rC   rD   r   rE   rF   r   rH   r   rG   )rM   rJ   rh   r`   r,   r,   r-   test_prefit_max_features  s   "r   c                  C   sx   t dddd\} }tg ddd}t|d}|| | | }| }tdd	 t| j	d
 D | }t
|| dS )zWCheck if ElasticNetCV works with a list of floats.

    Non-regression test for #30936.r\   r   r   )r   r   r=   )g      ?      ?g      ?r   rL   c                 S   s   g | ]}d | qS xr,   ).0ir,   r,   r-   
<listcomp>  s    z;test_get_feature_names_out_elasticnetcv.<locals>.<listcomp>rR   N)r	   r   r   rB   get_feature_names_outget_supportre   r   r   rY   r   )rU   rD   rM   selector	names_outr   expectedr,   r,   r-   'test_get_feature_names_out_elasticnetcv  s   
"r   c                  C   s   t ddd} | tt t| ddd}t|j}d| d}tjt	|d	 |
  W d
   n1 s4w   Y  |tt |
 }|dgksJJ d
S )z;Check the interaction between prefit and the feature names.rj   r   r]   TrR   r   zThis z_ instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.ra   Nx3)r   rB   rC   rD   r   typer0   rE   rF   r   r   )rI   rJ   namerh   feature_namesr,   r,   r-   !test_prefit_get_feature_names_out  s   


r   c                  C   sn   t ddd} t| dd}|tt |t}| tt dt| j }| j|k}t	|td d |f  d S )Nr~   r   r]   z0.5*meanr@   r   )
r   r   rB   rC   rD   rH   re   r   r   r   )rO   rJ   r   rA   r   r,   r,   r-   test_threshold_string  s   

r   c                  C   s\   t ddddd d} t| dd}|tt |t}d|_|jd	 |tjd	 ks,J d S )
Nr7   r8   Tr   r9   z
0.1 * meanr@   z
1.0 * meanrR   )r   r   rB   rC   rD   rH   rA   rY   )rI   rJ   r   r,   r,   r-    test_threshold_without_refitting  s   
"r   c                  C   s@   t dd} t| d}t }tj|d< tj|d< |tt d S )Nr   r   rL   rR   )	r   r   rC   copyre   nanrf   rB   rD   )rI   rJ   nan_datar,   r,   r-   test_fit_accepts_nan_inf'  s   



r   c                  C   sL   t ddd} t }t| d}||t tj|d< tj|d< |	| d S )Nr   r   r]   rL   rR   )
r6   rC   r   r   rB   rD   re   r   rf   rH   )rI   r   rJ   r,   r,   r-   test_transform_accepts_nan_inf4  s   


r   c                  C   sL   t  } t| d}| jjdu sJ t }t|d}| jjdu s$J d S )NrL   TF)r   r   r$   r%   r&   r5   )allow_nan_estrJ   
no_nan_estr,   r,   r-   'test_allow_nan_tag_comes_from_estimatorB  s   

r   c                 C   s   t | jS r   )re   r   explained_variance_)pca_estimatorr,   r,   r-   _pca_importancesL  s   r   zestimator, importance_getterz$named_steps.logisticregression.coef_c                 C   s6   t | d|d}|tt |tjd dksJ d S )Nr   )rA   importance_getterrR   )r   rB   rC   rD   rH   rY   )rM   r   r   r,   r,   r-   test_importance_getterP  s
   r   PLSEstimatorc                 C   sJ   t dddd\}}| dd}tt||||}|||dks#J dS )	zCheck the behaviour of SelectFromModel with PLS estimators.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/12410
    r~   r8   r   r   r   r=   rR   )n_componentsr   N)r   r   r   rB   score)r   rU   rD   rM   rJ   r,   r,   r-   test_select_from_model_plsb  s   
r  c                     s   t d tjddd\ } t j} fdd}tt |d | }t	|j
 j t| }||k s7J t  tdt | jdd	  W d
   d
S 1 sWw   Y  d
S )zvSelectFromModel works with estimators that do not support feature_names_in_.

    Non-regression test for #21949.
    pandasTas_frame
return_X_yc                    s   t  jd S rX   )re   r   rY   rL   rT   r,   r-   r   x  s   zHtest_estimator_does_not_support_feature_names.<locals>.importance_getter)r   errorrR   r   N)rE   importorskipr   	load_irissetcolumnsr   r   rB   r   feature_names_in_r   warningscatch_warningssimplefilterUserWarningrH   iloc)rD   all_feature_namesr   r   feature_names_outr,   rT   r-   -test_estimator_does_not_support_feature_nameso  s    


"r  zerror, err_msg, max_featuresz max_features == 10, must be <= 4r8   zmax_features == 5, must be <= 4c                 C   s   | j d d S rX   rr   r   r,   r,   r-   rV     rZ   c                 C   sf   t jdddd\}}tj| |d tt |dj||ddgd W d	   d	S 1 s,w   Y  d	S )
zDTest that partial_fit from SelectFromModel validates `max_features`.r   r   r   r   ra   rM   r`   rR   r   N)r   r   rE   rF   r   r   r   )r  rh   r`   rU   rD   r,   r,   r-   &test_partial_fit_validate_max_features  s   	
"r  r  TFc                 C   sb   t d tj| dd\}}tt ddj||g dd}| r(t|j|j	 d	S t
|dr/J d	S )
zITest that partial_fit from SelectFromModel validates `feature_names_in_`.r  Tr  r   r  )r   rR   rj   r   r  N)rE   r	  r   r
  r   r   r   r   r  r  rm   )r  rU   rD   r   r,   r,   r-   'test_partial_fit_validate_feature_names  s   

r  c                  C   s~   t t d} d}d}tjt|d}| ttt W d   n1 s%w   Y  t	|j
jts3J |t|j
jv s=J dS )zCheck that we raise the proper AttributeError when the estimator
    does not implement the `partial_fit` method, which is decorated with
    `available_if`.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/28108
    rL   z5This 'SelectFromModel' has no attribute 'partial_fit'z8'LinearRegression' object has no attribute 'partial_fit'ra   N)r   r   rE   rF   AttributeErrorrB   rC   rD   r   
isinstancevalue	__cause__str)
from_model	outer_msg	inner_msg	exec_infor,   r,   r-   )test_from_model_estimator_attribute_error  s   
r"  )\rc   r  unittest.mockr   numpyre   rE   sklearnr   sklearn.baser   sklearn.cross_decompositionr   r   r   sklearn.datasetsr   r	   sklearn.decompositionr
   sklearn.ensembler   r   sklearn.exceptionsr   sklearn.feature_selectionr   sklearn.linear_modelr   r   r   r   r   r   r   r   sklearn.pipeliner   sklearn.svmr   sklearn.utils._testingr   r   r   r   r   r   r5   r6   r
  irisrC   targetrD   rK   rQ   markparametrizerY   rG   	TypeErrorri   rp   ry   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r"  r,   r,   r,   r-   <module>   s    (
	




	+



	
	+





