
    Q3jKO              	          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
mZ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 d dl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& d dl'm(Z( d dl)m*Z* ejV                  jY                  dddg      ejV                  jY                  dddg      ejV                  jY                  dddg      d                      Z-ejV                  jY                  dddg      ejV                  jY                  dddg      d               Z.ejV                  jY                  dddg      ejV                  jY                  dddg      d1d              Z/d Z0d Z1d Z2ejV                  jY                  dddg      d         Z3ejV                  jY                  dg d!      ejV                  jY                  dddg      d"               Z4ejV                  jY                  d#d$d%g      d&        Z5d' Z6d( Z7ejV                  jY                  d)d*d+gd*d+gfd*d+g ed*       ed+      d,fd*d+gd- fg      d.        Z8d/ Z9d0 Z:y)2    N)parallel_backend)assert_allclose)ColumnTransformer)load_diabetes	load_irismake_classificationmake_regression)DummyClassifier)RandomForestClassifierRandomForestRegressor)SimpleImputer)permutation_importance)LinearRegressionLogisticRegression)
get_scorermean_squared_errorr2_score)train_test_split)make_pipeline)KBinsDiscretizerOneHotEncoderStandardScalerscale)_convert_container)_NotAnArrayn_jobs      max_samples      ?      ?sample_weightonesc           
      P   t         j                  j                  d      }d}t        d      \  }}||j	                  d|j
                  d         z   j                  dd	      }t        j                  ||g      }|d
k(  rt        j                  |      n|}t        dd      }	|	j                  ||       t        |	|||||| |      }
|
j                  j
                  |j
                  d	   |fk(  sJ t        j                  |
j                  d   |
j                  d d kD        sJ y )N*      T)
return_X_yMbP?r   r   sizer   r#   
   n_estimatorsrandom_state)r"   	n_repeatsr/   r   r   )nprandomRandomStater   normalshapereshapehstack	ones_liker   fitr   importancesallimportances_mean)r   r   r"   rngr0   Xyy_with_little_noiseweightsclfresults              a/DATA/.local/lib/python3.12/site-packages/sklearn/inspection/tests/test_permutation_importance.py9test_permutation_importance_correlated_feature_regressionrE      s    ))


#CID)DAqszzAGGAJzGGPPQSUVW
		1)*+A!.&!8bll1omG
Rb
ACGGAqM#			F ##
I'>>>> 66&))"-0G0G0LLMMM    c           	      |   t        j                  d      }t        j                  j	                  d      }d}t               }|j                  |j                  }}||j                  d|j                  d         z   j                  dd      }|j                  ||j                  	      }||d
<   t        dd      }	|	j                  ||       t        |	||||| |      }
|
j                   j                  |j                  d   |fk(  sJ t        j"                  |
j$                  d   |
j$                  d d kD        sJ y )Npandasr%   r&   r(   r   r)   r+   r   )columnscorrelated_featurer,   r-   r0   r/   r   r   )pytestimportorskipr1   r2   r3   r   datatargetr4   r5   r6   	DataFramefeature_namesr   r9   r   r:   r;   r<   )r   r   pdr=   r0   datasetr>   r?   r@   rB   rC   s              rD   @test_permutation_importance_correlated_feature_regression_pandasrT   D   s-   
 
		X	&B ))


#CIkG<<qAszzAGGAJzGGPPQSUVW 	Q 5 56A1A
 br
BCGGAqM#		F ##
I'>>>> 66&))"-0G0G0LLMMMrF   c           	         t         j                  j                  |      }d}d}d}d}d}||z   }	t        j                  |      }
|j	                  |
|      }t        j
                  |
d | D cg c]  }||k(  j                  dd       c}      }|j                  t         j                        }||k  sJ t        j                  ||j                  ||      gd      }|j                  ||	fk(  sJ t        ||d|	      \  }}}}t        d|
      }|j                  ||       |j                  }|d | }||d  }|j!                         |j#                         k  sJ t%        |||||| |      }|j&                  j                  |j                  d   |fk(  sJ |j(                  d | }|j(                  |d  }t!        t        j*                  |            dkD  sJ |j!                         dk  sJ |j#                         dkD  sJ y c c}w )Nr&     r   r   )r*   r+   )axisr    )	test_sizer/   r-   rK   gHz>g?g333333?)r1   r2   r3   arangechoicer7   r6   astypefloat32concatenaterandnr5   r   r   r9   feature_importances_maxminr   r:   r<   abs)r   r   seedr=   r0   	n_samples	n_classesn_informative_featuresn_noise_features
n_featuresclassesr?   cr>   X_trainX_testy_trainy_testrB   tree_importancesinformative_tree_importancesnoisy_tree_importancesrinformative_importancesnoisy_importancess                            rD   1test_robustness_to_high_cardinality_noisy_featureru   l   sF    ))


%CIII'*::J
 ii	"G

7
+A
		G<S=S4TU4TqAF##B*4TUVA	A "I--- 	399Y0@ABKA77y*----
 (8	1#($GVWf !ac
BCGGGW
 //#34K5K#L -.D.EF'++-0F0J0J0LLLL 		A ==1771:y"9999  001H2HI**+A+BC rvv'()D000  "T))) #&&(4///w Vs   -Hc                     t         j                  j                  d      } d}t        j                  dddt         j                  gg dg      j
                  }t        j                  g d      }t        t               t        d	            }|j                  ||       t        ||||| 
      }|j                  j                  |j                  d   |fk(  sJ t        j                  |j                  d   |j                  d d kD        sJ t         j                  j                  d      } t        ||||| 
      }|j                  j                  |j                  d   |fk(  sJ t        j                  |j                  |j                        rJ t        j                  |j                  d   |j                  d d kD        sJ y )Nr%      r!          @      @)r   r   r   r   r   r   r   r   lbfgssolverr0   r/   r   r+   r   )r1   r2   r3   arraynanTr   r   r   r9   r   r:   r5   r;   r<   allclose)r=   r0   r>   r?   rB   rC   result2s          rD   'test_permutation_importance_mixed_typesr      s~   
))


#CI 	3S"&&)<89;;A
A
);7)K
LCGGAqM#CAQTUF##
I'>>>> 66&))"-0G0G0LLMMM ))


"C$S!Q)RUVG$$Y(????{{6--w/B/BCCC 66'**2.1I1I#21NNOOOrF   c                     t        j                  d      } t        j                  j	                  d      }d}| j                  dddt        j                  gg dd      }t        j                  g d	      }t        t               t                     }t        d
|dgfdt               dgfg      }t        |t        d            }|j                  ||       t        |||||      }|j                   j"                  |j"                  d   |fk(  sJ t        j$                  |j&                  d   |j&                  d d kD        sJ y )NrH   r%   r&   r!   rx   ry   )abr   r   )col1col2rz   numr   catr   r{   r|   r~   r   r+   )rL   rM   r1   r2   r3   rP   r   r   r   r   r   r   r   r   r9   r   r:   r5   r;   r<   )	rR   r=   r0   r>   r?   num_preprocess
preprocessrB   rC   s	            rD   .test_permutation_importance_mixed_types_pandasr      s   			X	&B
))


#CI 	sCbff5?STUA
A"=?N4DEN"
&	*UMOfX,NOJ 
$6g$F
GCGGAqM#CAQTUF##
I'>>>> 66&))"-0G0G0LLMMMrF   c                      t        ddd      \  } }t        |       } t        |      }t               j                  | |      }d|j                  dz  z  }t        || |dd      }t        ||j                  d	d
       y )N  r,   r   rd   rh   r/   r   2   neg_mean_squared_error)r0   scoringg?gư>)rtolatol)r	   r   r   r9   coef_r   r   r<   )r>   r?   lrexpected_importancesresultss        rD   -test_permutation_importance_linear_regressionr      s~    SRaHDAqaAaA				1	%B rxx{?$
AqB(@G g66TrF   r   c           	         t        ddd      \  }}t               j                  ||      }t        |||ddd|       }|d   j	                         }|d   j                         }||z
  d	kD  sJ t        |||ddd
      }t        |d   |d          t        d      5  t        |||ddd
      }d d d        t        d   |d          y # 1 sw Y   xY w)Nr   r,   r   r   r&   r   rK   r:   333333?r   )r0   r/   r   	threading)r	   r   r9   r   ra   r`   r   r   )	r   r>   r?   r   importance_sequentialimp_minimp_maximportance_processesimportance_threadings	            rD   ;test_permutation_importance_equivalence_sequential_parallelr     s    
 SRaHDAq				1	%B2
AqAAa[ $M2668G#M2668GWs""" 2
AqAAa ]+-B=-Q
 
+	&51! 
 
' ]+-B=-Q	 
'	&s   C		C)Nr   r   c           	      ^   t        j                  d      }t        ddd      \  }}|j                  |      }t	        ddd	      }|j                  |j                  d
d            }t        j                  ||g      }|j                  j                  dk(  sJ |j                  |j                               }t        |j                        }|||<   ||   j                  |j                  k(  sJ t        j                  t        |            j!                  t"              |_        t'        ddd      }	|	j)                  ||       d}
t+        |	|||
d| |      }|d   j-                         }|d   j/                         }||z
  dkD  sJ t+        |	|||
d| |      }t1        |d   |d          y )NrH   d   r&   r   r      ordinalaveraged_inverted_cdf)n_binsencodequantile_methodr+   r   f)r.   	max_depthr/   rK   r:   r   )rL   rM   r	   rP   r   fit_transformr6   r1   r7   dtypekindCategoricalravellenrI   rY   r[   strindexr   r9   r   ra   r`   r   )r   r   rR   r>   r?   X_dfbinner
cat_columnnew_col_idxrfr0   importance_arrayr   r   importance_dataframes                  rD   7test_permutation_importance_equivalence_array_dataframer   .  s   
 
		X	&B SQQGDAq<<?D /F
 %%aiiA&67J 			1j/"A77<<3 
 0 0 23Jdll#K"D""j&6&6666 3t9%,,S1DJ	A	KBFF1aLI-
		 }-113G}-113GWs""" 2
	 ')=m)LrF   
input_typer   rH   c                 0   t        d      d}}t        ||d      \  }}|j                  dkD  sJ t        ||       }t	        d      j                  ||      }d}t        ||||d	
      }t        j                  ||f      }t        ||j                         y )Ng     j@rw   r   r   g    .Aprior)strategyr&   r   )r0   r   )intr   nbytesr   r
   r9   r   r1   zerosr   r:   )	r   rd   rh   r>   r?   rB   r0   rr   r   s	            rD   /test_permutation_importance_large_memmaped_datar   t  s      HazI
DAq 88c>>1j)A
7
+
/
/1
5C IsAqIaHA 88Z$;<(!--8rF   c            	         t         j                  j                  d      } d}d}|dz  }| j                  dd||f      }t        j                  |      }d|d |df   z  |d |df   z   |d | ||d df   d||d df   z  z   ||d  t        d      }|j                  ||       t        |||dd	d
      }|j                  d   |j                  d   z  }|t        j                  dd      k(  sJ t        j                  |      }	t        |||dd	d
|	      }|j                  d   |j                  d   z  }
|
t        j                  |d      k(  sJ t        j                  t        j                  d|      t        j                  d|      g      }	|j                  |||	       t        |||dd	d
|	      }|j                  d   |j                  d   z  }||z  t        j                  dd      k(  sJ y )Nr   rV   r   g        r(   r   F)fit_interceptneg_mean_absolute_error   r/   r   r0   g{Gz?r/   r   r0   r"   g    _Br!   )r1   r2   r3   r4   r   r   r9   r   r<   rL   approxr#   r7   repeat)r=   rd   rh   n_half_samplesxr?   r   pix1_x2_imp_ratio_w_nonewx1_x2_imp_ratio_w_onesx1_x2_imp_ratio_ws               rD   )test_permutation_importance_sample_weightr     s&   
 ))


"CIJ!^N

3	:67A
AQ122Q7I5JJAo~>?A-.Q~7I5J1JJAno 
	.BFF1aL
 
 
Aqq*Cs
B  003b6I6I!6LL!V]]1d%;;;; 		A	
		)
B  003b6I6I!6LL!V]]3I4%PPPP 			299X~6		#~8VWXAFF1aO	
		)
B ++A.1D1DQ1GG55q$9OOOOrF   c            
         d } t        j                  ddgddgg      }t        j                  ddg      }t        j                  ddg      }t               }|j                  ||       	 t	        |||d| d       t        j                  t
              5  t	        |||d| d|       d d d        y # t
        $ r t        j                  d       Y Uw xY w# 1 sw Y   y xY w)	Nc                      y)Nr    	estimatorr>   r?   s      rD   	my_scorerzJtest_permutation_importance_no_weights_scoring_function.<locals>.my_scorer  s    rF   r   r   r   rw   r   zpermutation_test raised an error when using a scorer function that does not accept sample_weight even though sample_weight was Noner   )	r1   r   r   r9   r   	TypeErrorrL   failraises)r   r   r?   r   r   s        rD   7test_permutation_importance_no_weights_scoring_functionr     s     	1a&1a&!"A
!QA
!QA		BFF1aL
r1aaVWX 
y	!11i1TU	
 
"	!  
%	

 
"	!s   *B0 C0CCCz list_single_scorer, multi_scorerr2r   r   r   c                 r    t        || j                  |            t        || j                  |             dS )Nr   )r   predictr   r   s      rD   <lambda>r     s5    q)"3"3A"67+=aARARSTAU+V*V%rF   c           	      D   t        ddd      \  }}t               j                  ||      }t        |||d|d      }t	        |j                               t	        |       k(  sJ | D ]8  }||   }t        |||d|d      }t        |j                  |j                         : y )Nr   r,   r   r   r   r   r   )r	   r   r9   r   setkeysr   r:   )	list_single_scorermulti_scorerr   r?   r   multi_importancescorermulti_resultsingle_results	            rD   (test_permutation_importance_multi_metricr     s    0 SRaHDAq				1	%B-
Aqq,! $$&'3/A+BBBB$'/.11f
 	00-2K2KL %rF   c                  ,   t        j                  dg      j                  } t        j                  g d      }t               }|j	                  | |       d}t        j                  t        |      5  t        || |d       ddd       y# 1 sw Y   yxY w)zjCheck that a proper error message is raised when `max_samples` is not
    set to a valid input value.
    )r!   rx   ry   g      @rz   z max_samples must be <= n_samples)matchr&   )r   N)	r1   r   r   r   r9   rL   r   
ValueErrorr   )r>   r?   rB   err_msgs       rD   -test_permutation_importance_max_samples_errorr     sl     	&'(**A
A

CGGAqM1G	z	1sAqa8 
2	1	1s   1B

Bc                      t        ddgddgddgddgddgddgddgddgg      } t        g d      }t        d      }|j                  | |       t        || |dd       y	)
z6Check that `__array_function__` (NEP18) is not called.r   r   r   rw   )r   r   r   r   r   r   r   r   r   )r/   r~   N)r   r   r9   r   )r>   r?   r   s      rD   5test_permutation_importance_array_function_not_calledr      sq    aVaVaVaVaVaVaVaQRVTUA,-A"2IMM!Q9aaaHrF   )r%   );numpyr1   rL   joblibr   numpy.testingr   sklearn.composer   sklearn.datasetsr   r   r   r	   sklearn.dummyr
   sklearn.ensembler   r   sklearn.imputer   sklearn.inspectionr   sklearn.linear_modelr   r   sklearn.metricsr   r   r   sklearn.model_selectionr   sklearn.pipeliner   sklearn.preprocessingr   r   r   r   sklearn.utils._testingr   sklearn.utils.estimator_checksr   markparametrizerE   rT   ru   r   r   r   r   r   r   r   r   r   r   r   r   rF   rD   <module>r	     s     # ) -  * J ( 5 E 
 5 * X X 5 6 Aq6*c
34.9 N : 4 + NF Aq6*c
3#N 4 +#NL Aq6*c
3L0 4 +L0^P<N0$ c
3% 4%P <0c
3A 4 1AH ':;9 <9,:Pz
> &
(	)D2J+KL+, &*45M*N	
 +,	
(M)(M*9 IrF   