
    /ii<                     	   d Z ddlZddlZddlmZmZ 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mZmZ ddlmZ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$ 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/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ ddlAmBZBmCZCmDZD ddlEmFZFmGZG  e@d          ZH e            ZIeHJ                    eIjK        jL                  ZMeIjN        eM         eI_N        eIjK        eM         eI_K         e            ZOeHJ                    eOjK        jL                  ZMeOjN        eM         eO_N        eOjK        eM         eO_K        d ZPe	jQ        R                    d eeGeFz   dddddddddddddd dddd!gg d"                    d#             ZSd$ ZTe	jQ        R                    d%eGeFz             d&             ZU G d' d(e          ZVd) ZWd* ZXd+ ZYd, ZZd- Z[d. Z\d/ Z]d0 Z^e	jQ        j_        d1             Z`d2 Zae	jQ        j_        d3             Zbd4 Zcdcd6Zdd7 Zed8 Zfd9 Zgd: Zhd; Zid< Zj G d= d>e          Zk G d? d@e          Zle	jQ        R                    dAeeg          e	jQ        R                    dBddg          e	jQ        R                    dCddg          e	jQ        R                    dDdEdFg          dG                                                 ZmdH ZndI ZodJ ZpdK ZqdL ZrdM ZsdN ZtdO ZudP ZvdQ ZwdR ZxdS Zye	jQ        R                    dT e edUV                    df e edUV                    df e e#                      df e e5                      dfg          dW             Zz edX          e	jQ        R                    dY e edUZ          dU[           e edUZ          dU[          g          d\                         Z{e	jQ        R                    d]e9d^d^fe8d_d`fe7d_d^fg           edX          da                         Z|e	jQ        R                    dY e edUZ          dU[           e edUZ          dU[          g          db             Z}dS )dzE
Testing for the bagging ensemble module (sklearn.ensemble.bagging).
    N)cycleproduct)config_context)BaseEstimator)load_diabetes	load_irismake_hastie_10_2)DummyClassifierDummyRegressor)AdaBoostClassifierAdaBoostRegressorBaggingClassifierBaggingRegressorHistGradientBoostingClassifierHistGradientBoostingRegressorRandomForestClassifierRandomForestRegressor)_get_n_samples_bootstrap)SelectKBest)LogisticRegression
Perceptron)GridSearchCVParameterGridtrain_test_split)KNeighborsClassifierKNeighborsRegressor)make_pipeline)FunctionTransformerscale)SparseRandomProjection)SVCSVR)"ConsumingClassifierWithOnlyPredict)ConsumingClassifierWithoutPredictLogProba&ConsumingClassifierWithoutPredictProba	_Registrycheck_recorded_metadata)DecisionTreeClassifierDecisionTreeRegressor)check_random_state)assert_allcloseassert_array_almost_equalassert_array_equal)CSC_CONTAINERSCSR_CONTAINERSc                     t          d          } t          t          j        t          j        |           \  }}}}t          ddgddgddgddgd	          }d t                      t          d
          t          d          t                      t                      g}t          |t          |                    D ]<\  }}t          d|| dd|                    ||                              |           =d S )Nr   random_state      ?      ?      TFmax_samplesmax_features	bootstrapbootstrap_features   max_iter   )	max_depth)	estimatorr2   n_estimators )r*   r   irisdatatargetr   r
   r   r(   r   r!   zipr   r   fitpredict)	rngX_trainX_testy_trainy_testgrid
estimatorsparamsrA   s	            i/var/www/html/bet.cuttalo.com/ml/venv/lib/python3.11/site-packages/sklearn/ensemble/tests/test_bagging.pytest_classificationrS   H   s*   
Q

C'7	4;S( ( ($GVWf :F#'-		
 	
 D 	B+++J !uZ'8'899 0 0	 	
	
 	
 		
 	

 #gw

0 0    z sparse_container, params, methodr3   r?   Tr7   r4   r6   Fr9   r:   r;   r8   r:   r;   )rI   predict_probapredict_log_probadecision_functionc                     G d dt                     }t          d          }t          t          t          j                  t          j        |          \  }}}} | |          }	 | |          }
t          d |dd          dd	|                    |	|          } t          ||          |
          }t          d |dd          dd	|                    ||          } t          ||          |          }t          ||           t          |	          d
 |j        D             }t          fd|D                       sJ d S )Nc                   "     e Zd ZdZ fdZ xZS )-test_sparse_classification.<locals>.CustomSVC7SVC variant that records the nature of the training setc                 t    t                                          ||           t          |          | _        | S NsuperrH   type
data_type_selfXy	__class__s      rR   rH   z1test_sparse_classification.<locals>.CustomSVC.fit   -    GGKK1"1ggDOKrT   __name__
__module____qualname____doc__rH   __classcell__rh   s   @rR   	CustomSVCr\      >        EE	 	 	 	 	 	 	 	 	rT   rq   r   r1   linearovr)kerneldecision_function_shaper5   rA   r2   c                     g | ]	}|j         
S rC   rc   .0is     rR   
<listcomp>z.test_sparse_classification.<locals>.<listcomp>   s    AAAaQ\AAArT   c                     g | ]}|k    	S rC   rC   r{   tsparse_types     rR   r}   z.test_sparse_classification.<locals>.<listcomp>   s    000Q[ 000rT   rC   )r!   r*   r   r   rD   rE   rF   r   rH   getattrr,   rb   estimators_all)sparse_containerrQ   methodrq   rJ   rK   rL   rM   rN   X_train_sparseX_test_sparsesparse_classifiersparse_resultsdense_classifierdense_resultstypesr   s                   @rR   test_sparse_classificationr   i   s   4    C    Q

C'7di$+C( ( ($GVWf &%g..N$$V,,M) )8UKKK    
c.'""	 
 8W.77FFN ) )8UKKK    
c'7	 
 6G,f55f==Mnm<<<~&&KAA#4#@AAAE0000%0001111111rT   c                     t          d          } t          t          j        d d         t          j        d d         |           \  }}}}t          ddgddgddgddgd          }d t                      t                      t                      t                      fD ]=}|D ]8}t          d
|| d	|                    ||                              |           9>d S )Nr   2   r1   r3   r4   TFr7   rw   rC   )r*   r   diabetesrE   rF   r   r   r)   r   r"   r   rH   rI   )rJ   rK   rL   rM   rN   rO   rA   rQ   s           rR   test_regressionr      s   
Q

C'7crcHOCRC0s( ( ($GVWf : #J#'-		
 	
 D 	 
 
	  	 	FMysMMfMMQQ gfoooo	
 
rT   r   c                    t          d          }t          t          j        d d         t          j        d d         |          \  }}}} G d dt
                    }ddddd	d
dddd	ddddddddg} | |          } | |          }	|D ]}
t          d |            dd|
                    ||          }|                    |	          }t          d |            dd|
                    ||                              |          }t          |          d |j
        D             }t          ||           t          fd|D                       sJ t          ||           d S )Nr   r   r1   c                   "     e Zd ZdZ fdZ xZS ))test_sparse_regression.<locals>.CustomSVRr]   c                 t    t                                          ||           t          |          | _        | S r_   r`   rd   s      rR   rH   z-test_sparse_regression.<locals>.CustomSVR.fit   ri   rT   rj   rp   s   @rR   	CustomSVRr      rr   rT   r   r3   r?   Tr7   r4   r6   FrU   rV   r5   rw   c                     g | ]	}|j         
S rC   ry   rz   s     rR   r}   z*test_sparse_regression.<locals>.<listcomp>   s    EEE!EEErT   c                     g | ]}|k    	S rC   rC   r   s     rR   r}   z*test_sparse_regression.<locals>.<listcomp>   s    444A$444rT   rC   )r*   r   r   rE   rF   r"   r   rH   rI   rb   r   r,   r   )r   rJ   rK   rL   rM   rN   r   parameter_setsr   r   rQ   r   r   r   r   r   s                  @rR   test_sparse_regressionr      s    Q

C'7crcHOCRC0s( ( ($GVWf    C    "&		
 	
 "&		
 	
 dKK$eLLN" &%g..N$$V,,M  A A, 
ikk
 
5;
 

#ng
&
& 	 +22=AA Myy{{MMfMMS'""WV__ 	 >**EE'8'DEEE!.-@@@4444e44455555!.-@@@@'A ArT   c                       e Zd Zd Zd ZdS )DummySizeEstimatorc                 \    |j         d         | _        t          j        |          | _        d S Nr   )shapetraining_size_joblibhashtraining_hash_re   rf   rg   s      rR   rH   zDummySizeEstimator.fit   s&    gaj$k!nnrT   c                 @    t          j        |j        d                   S r   )nponesr   re   rf   s     rR   rI   zDummySizeEstimator.predict  s    wqwqz"""rT   N)rk   rl   rm   rH   rI   rC   rT   rR   r   r      s2        - - -# # # # #rT   r   c                     t          d          } t          t          j        t          j        |           \  }}}}t                                          ||          }t          t                      dd|                               ||          }|                    ||          |                    ||          k    sJ t          t                      dd|                               ||          }|                    ||          |                    ||          k    sJ t          t                      d                              ||          }g }|j
        D ]4}|j        |j        d         k    sJ |                    |j                   5t          t!          |                    t          |          k    sJ d S )Nr   r1   r4   F)rA   r8   r:   r2   T)rA   r:   )r*   r   r   rE   rF   r)   rH   r   scorer   r   r   r   appendr   lenset)rJ   rK   rL   rM   rN   rA   ensembletraining_hashs           rR   test_bootstrap_samplesr     s   
Q

C'7xS( ( ($GVWf &''++GW==I  '))	  
 
c'7  ??7G,,w0P0PPPPP  '))	  
 
c'7  ??7G,,x~~gw/O/OOOOO
  *<*>*>$OOOSS H M) 7 7	'7=+;;;;;Y56666s=!!""c-&8&8888888rT   c                  V   t          d          } t          t          j        t          j        |           \  }}}}t          t                      dd|                               ||          }|j        D ]<}t          j        j	        d         t          j        |          j	        d         k    sJ =t          t                      dd|                               ||          }|j        D ]<}t          j        j	        d         t          j        |          j	        d         k    sJ =d S )Nr   r1   r4   F)rA   r9   r;   r2   r5   T)r*   r   r   rE   rF   r   r)   rH   estimators_features_r   r   unique)rJ   rK   rL   rM   rN   r   featuress          rR   test_bootstrap_featuresr   1  sE   
Q

C'7xS( ( ($GVWf  ')) 	  
 
c'7  1 F F}"1%8)<)<)B1)EEEEEE'))	  
 
c'7  1 E E}"1%	((;(;(A!(DDDDDDE ErT   c            	         t          d          } t          t          j        t          j        |           \  }}}}t          j        dd          5  t          t                      |           	                    ||          }t          t          j        |                    |          d          t          j        t          |                               t          |                    |          t          j        |                    |                               t          t#                      | d	          	                    ||          }t          t          j        |                    |          d          t          j        t          |                               t          |                    |          t          j        |                    |                               d d d            d S # 1 swxY w Y   d S )
Nr   r1   ignore)divideinvalidrw   r5   )axis   )rA   r2   r8   )r*   r   rD   rE   rF   r   errstater   r(   rH   r,   sumrW   r   r   exprX   r   rJ   rK   rL   rM   rN   r   s         rR   test_probabilityr   M  s"   
Q

C'7	4;S( ( ($GVWf 
Hh	7	7	7 
 
$,..S
 
 

#gw

 	 	"F8))&11:::BGCKK<P<P	
 	
 	
 	"""6**BF83M3Mf3U3U,V,V	
 	
 	

 %(**!
 
 

#gw

 	 	"F8))&11:::BGCKK<P<P	
 	
 	
 	"""6**BF83M3Mf3U3U,V,V	
 	
 	
/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   F G>>HHc            	      ,   t          d          } t          t          j        t          j        |           \  }}}}t                      t                      fD ]}t          |ddd|                               ||          }|	                    ||          }t          ||j        z
            dk     sJ d}t          j        t          |          5  t          |d	dd|           }|                    ||           d d d            n# 1 swxY w Y   d S )
Nr   r1   d   TrA   rB   r:   	oob_scorer2   皙?{Some inputs do not have OOB scores. This probably means too few estimators were used to compute any reliable oob estimates.matchr5   )r*   r   rD   rE   rF   r(   r!   r   rH   r   abs
oob_score_pytestwarnsUserWarning)	rJ   rK   rL   rM   rN   rA   clf
test_scorewarn_msgs	            rR   test_oob_score_classificationr   p  s    Q

C'7	4;S( ( ($GVWf -..6 & &	
 
 
 #gw

 	 YYvv..
:.//#5555J 	 \+X666 	& 	&##   C GGGW%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&%& &s   +DD	D	c                      t          d          } t          t          j        t          j        |           \  }}}}t          t                      ddd|                               ||          }|                    ||          }t          ||j
        z
            dk     sJ d}t          j        t          |          5  t          t                      d	dd|           }|                    ||           d d d            d S # 1 swxY w Y   d S )
Nr   r1   r   Tr   r   r   r   r5   )r*   r   r   rE   rF   r   r)   rH   r   r   r   r   r   r   )	rJ   rK   rL   rM   rN   r   r   r   regrs	            rR   test_oob_score_regressionr     sr    Q

C'7xS( ( ($GVWf '))   
c'7  66**JzCN*++c1111	F  
k	2	2	2 # #+--
 
 
 	'"""# # # # # # # # # # # # # # # # # #s   ?7DD
Dc                     t          d          } t          t          j        t          j        |           \  }}}}t          t                      ddd|                               ||          }t                                          ||          }t          |	                    |          |	                    |                     d S )Nr   r1   r5   F)rA   rB   r:   r;   r2   )
r*   r   r   rE   rF   r   r   rH   r,   rI   )rJ   rK   rL   rM   rN   clf1clf2s          rR   test_single_estimatorr     s    
Q

C'7xS( ( ($GVWf %''    
c'7 	   $$Wg66Ddll622DLL4H4HIIIIIrT   c                      t           j        t           j        }} t                      }t	          t          |                              | |          d          rJ d S )NrY   )rD   rE   rF   r(   hasattrr   rH   )rf   rg   bases      rR   
test_errorr     sP    9dkqA!##D(..221a88:MNNNNNNNrT   c                     t          t          j        t          j        d          \  } }}}t	          t                      dd                              | |          }|                    |          }|                    d           |                    |          }t          ||           t	          t                      dd                              | |          }|                    |          }t          ||           t	          t          d          dd                              | |          }|                    |          }|                    d           |                    |          }	t          ||	           t	          t          d          dd                              | |          }|                    |          }
t          ||
           d S )	Nr   r1      n_jobsr2   r5   r   rt   )rv   )r   rD   rE   rF   r   r(   rH   rW   
set_paramsr,   r!   rY   )rK   rL   rM   rN   r   y1y2y3
decisions1
decisions2
decisions3s              rR   test_parallel_classificationr     s   '7	4;Q( ( ($GVWf !    	c'7 
 
			'	'Bq!!!				'	'Bb"%%%     	c'7  
			'	'Bb"%%% !E***11  	c'7  ++F33Jq!!!++F33Jj*555 E***11  	c'7  ++F33Jj*55555rT   c                  X   t          d          } t          t          j        t          j        |           \  }}}}t          t                      dd                              ||          }|                    d           |	                    |          }|                    d           |	                    |          }t          ||           t          t                      dd                              ||          }|	                    |          }t          ||           d S )Nr   r1   r   r   r5   r   r?   )r*   r   r   rE   rF   r   r)   rH   r   rI   r,   )	rJ   rK   rL   rM   rN   r   r   r   r   s	            rR   test_parallel_regressionr     s.    Q

C'7xS( ( ($GVWf   5 7 7PQRRRVV H q!!!			&	!	!Bq!!!			&	!	!Bb"%%% 5 7 7PQRRRVV H 
		&	!	!Bb"%%%%%rT   c                      t           j        t           j        }} d||dk    <   ddd}t          t	          t                                |d                              | |           d S )Nr5   r?   )r5   r?   )rB   estimator__Croc_auc)scoring)rD   rE   rF   r   r   r!   rH   )rf   rg   
parameterss      rR   test_gridsearchr     sf     9dkqAAa1fI #)&AAJ"355)):yIIIMMaQRSSSSSrT   c                  H   t          d          } t          t          j        t          j        |           \  }}}}t          d dd                              ||          }t          |j        t                    sJ t          t                      dd                              ||          }t          |j        t                    sJ t          t                      dd                              ||          }t          |j        t                    sJ t          t          j        t          j        |           \  }}}}t          d dd                              ||          }t          |j        t                    sJ t          t                      dd                              ||          }t          |j        t                    sJ t          t                      dd                              ||          }t          |j        t                    sJ d S )Nr   r1   r   r   )r*   r   rD   rE   rF   r   rH   
isinstance
estimator_r(   r   r   r   r)   r"   r   s         rR   test_estimatorr   (  s    Q

C (8	4;S( ( ($GVWf !aa@@@DDWgVVHh)+ABBBBB     	c'7  h)+ABBBBB aaHHHLL H h):66666 (8xS( ( ($GVWf  QQ???CCGWUUHh)+@AAAAA 5 7 7PQRRRVV H h)+@AAAAAaa@@@DDWgVVHh)3///////rT   c                  2   t          t          t          d          t                                d          } |                     t
          j        t
          j                   t          | d         j	        d         d         j
        t                    sJ d S )Nr5   )kr?   )r9   r   )r   r   r   r(   rH   rD   rE   rF   r   stepsr2   intrA   s    rR   test_bagging_with_pipeliner   U  s    !kA&&&(>(@(@AAPQ  I MM$)T[)))il(,Q/<cBBBBBBBrT   *   c                    t          dd          \  }}d }dD ]X}|t          || d          }n|                    |           |                    ||           t	          |          |k    sJ Yt          d| d	          }|                    ||           t          d
 |D                       t          d |D                       k    sJ d S )Nr<   r5   	n_samplesr2   )r   
   T)rB   r2   
warm_startrB   r  Fc                     g | ]	}|j         
S rC   r1   r{   trees     rR   r}   z#test_warm_start.<locals>.<listcomp>r  s    555d!555rT   c                     g | ]	}|j         
S rC   r1   r  s     rR   r}   z#test_warm_start.<locals>.<listcomp>s  s    111t	111rT   )r	   r   r   rH   r   r   )r2   rf   rg   clf_wsrB   	clf_no_wss         rR   test_warm_startr  ]  s    bq999DAqF + +>&)QU  FF <888

1a6{{l*****!lu  I MM!Q55f55566#11y111; ;      rT   c                  8   t          dd          \  } }t          dd          }|                    | |           |                    d           t	          j        t                    5  |                    | |           d d d            d S # 1 swxY w Y   d S )	Nr<   r5   r   r   T)rB   r  r6   r  )r	   r   rH   r   r   raises
ValueErrorrf   rg   r   s      rR   $test_warm_start_smaller_n_estimatorsr  w  s    bq999DAq
t
<
<
<CGGAqMMMNNN"""	z	"	"  1                 s   +BBBc                     t          dd          \  } }t          | |d          \  }}}}t          ddd	          }|                    ||           |                    |          }|d
z  }d}t          j        t          |          5  |                    ||           d d d            n# 1 swxY w Y   t          ||                    |                     d S )Nr<   r5   r   +   r1   r   TS   rB   r  r2   r4   z;Warm-start fitting without increasing n_estimators does notr   )	r	   r   r   rH   rI   r   r   r   r-   )	rf   rg   rK   rL   rM   rN   r   y_predr   s	            rR   "test_warm_start_equal_n_estimatorsr    s   bq999DAq'712'N'N'N$GVWf
t"
M
M
MCGGGW[[  FsNGLH	k	2	2	2 " "!!!" " " " " " " " " " " " " " "vs{{62233333s   B..B25B2c                     t          dd          \  } }t          | |d          \  }}}}t          ddd	          }|                    ||           |                    d
           |                    ||           |                    |          }t          d
dd	          }|                    ||           |                    |          }	t          ||	           d S )Nr<   r5   r   r  r1   r   TiE  r  r  r  F)r	   r   r   rH   r   rI   r,   )
rf   rg   rK   rL   rM   rN   r	  r   r   r   s
             rR   test_warm_start_equivalencer    s     bq999DAq'712'N'N'N$GVWfA$TRRRF
JJw   
2&&&
JJw   			B
D
Q
Q
QCGGGW	V		Bb"%%%%%rT   c                      t          dd          \  } }t          ddd          }t          j        t                    5  |                    | |           d d d            d S # 1 swxY w Y   d S )Nr<   r5   r   r   T)rB   r  r   )r	   r   r   r  r  rH   r  s      rR   $test_warm_start_with_oob_score_failsr    s    bq999DAq
tt
L
L
LC	z	"	"  1                 s    A$$A(+A(c                  ,   t           j        t           j        }} t          j        |          }t          d          }d}t          j        t          |          5  |	                    | ||           d d d            n# 1 swxY w Y   t          j        t          j        }} t          j        |          }t          d          }d}t          j        t          |          5  |	                    | ||           d d d            d S # 1 swxY w Y   d S )NF)r:   zYWhen fitting BaggingClassifier with sample_weight it is recommended to use bootstrap=Truer   sample_weightzXWhen fitting BaggingRegressor with sample_weight it is recommended to use bootstrap=True)rD   rE   rF   r   	ones_liker   r   r   r   rH   r   r   )rf   rg   r  r   r   regs         rR   $test_warning_bootstrap_sample_weightr     s   9dkqALOOM
e
,
,
,C	2  
k	2	2	2 3 31M2223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 =(/qALOOM
U
+
+
+C	2  
k	2	2	2 3 31M2223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3s$   A??BB#D		DDc                     t           j        t           j        }} t          d          }t	          j        |          dt          |          z  z  }d}t          j        t          |          5  |
                    | ||           d d d            n# 1 swxY w Y   t          dd          }t	          j        |          }d|d	<   t          j        d
          }t          j        t          |          5  t          j        t          d          5  |
                    | ||           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr4   )r8   r?   zUsing the fractional value max_samples=1.0 when the total sum of sample weights is 0.5(\d*) results in a low number \(1\) of bootstrap samples. We recommend passing `max_samples` as an integer.r   r  F)r:   r8   r   zRmax_samples=151 must be <= n_samples=150 to be able to sample without replacement.z1When fitting BaggingClassifier with sample_weight)rD   rE   rF   r   r   r  r   r   r   r   rH   reescaper  r  )rf   rg   r   r  expected_msgs        rR   =test_invalid_sample_weight_max_samples_bootstrap_combinationsr%    s   9dkqA 
,
,
,CLOOq3q66z2M	<  
k	6	6	6 3 31M2223 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 e
=
=
=CLOOMM"9	 L 
z	6	6	6 7 7\R
 
 
 	7 	7 GGAqG666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	77 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7sH   -BBB6ED7+E7D;	;E>D;	?EEEc                        e Zd ZdZddZd ZdS )EstimatorAcceptingSampleWeightz&Fake estimator accepting sample_weightNc                 0    || _         || _        || _        dS zRecord values passed during fitN)X_y_sample_weight_)re   rf   rg   r  s       rR   rH   z"EstimatorAcceptingSampleWeight.fit  s    +rT   c                     d S r_   rC   r   s     rR   rI   z&EstimatorAcceptingSampleWeight.predict      rT   r_   rk   rl   rm   rn   rH   rI   rC   rT   rR   r'  r'    s=        00, , , ,    rT   r'  c                       e Zd ZdZd Zd ZdS )EstimatorRejectingSampleWeightz&Fake estimator rejecting sample_weightc                 "    || _         || _        dS r)  )r*  r+  r   s      rR   rH   z"EstimatorRejectingSampleWeight.fit  s    rT   c                     d S r_   rC   r   s     rR   rI   z&EstimatorRejectingSampleWeight.predict  r.  rT   Nr/  rC   rT   rR   r1  r1    s8        00  
    rT   r1  bagging_classaccept_sample_weightmetadata_routingr8   r  g?c                    t          j        d                              dd          }t          j        ddgd          }t          j        d          }d|d<   d|d<   |rt                      }nt                      }|j        \  }}	t          |t                    r%t          ||                                z            }
n|}
t          |	          5  |r|r|                    d
          } | ||d          }|                    |||           t          |j        |j                  D ]8\  }}t          j        ||          }t          |          t'          |          cxk    r|
k    sn J t          j        |ddg                                          sJ |rg|j        j        ||	fk    sJ |j        j        |fk    sJ t1          |j        |           t1          |j        |           t1          |j        |           |j        j        |
|	fk    sJ |j        j        |
fk    sJ t1          |j        ||                    t1          |j        ||                    :	 d d d            d S # 1 swxY w Y   d S )Nr   r   r5   r   r   r6   r?   r   enable_metadata_routingTr  )r8   rB   )	minlength)r   arangereshaperepeatzerosr'  r1  r   r   floatr   r   r   set_fit_requestrH   rG   r   estimators_samples_bincountr   isinr   r*  r+  r+   r,  )r4  r5  r6  r8   rf   rg   r  base_estimatorr  
n_featuresexpected_integer_max_samplesbaggingrA   samplescountss                  rR   %test_draw_indices_using_sample_weightrJ    s    		#r1%%A
	1a&"AHSMMMM!M! :799799GIz+u%% 3 (+;9J9J9L9L+L'M'M$$'2$	0@	A	A	A : : 	P 4 	P+;;$;OON-KVWXXXAq666"%g&97;V"W"W 	: 	:Iw[I>>>Fv;;#g,,NNNN2NNNNNNN77QF++//11111# : |)i-DDDDD |)i\9999	a000	a000	 8&AAAA !|).JJ-WWWWW |).J-LLLLL	aj999	aj9999#	:: : : : : : : : : : : : : : : : : :s   FI66I:=I:c                     d\  } }}t          | ||          | k    sJ d\  } }}t          | ||          |k    sJ d\  } }}t          | ||          dk    sJ d\  } }}d}t          j        t          |          5  t          | ||          t	          || z            k    sJ 	 d d d            n# 1 swxY w Y   d\  } }}t          j        t          |          5  t          | ||          dk    sJ 	 d d d            n# 1 swxY w Y   d}t
          j                            d	          }d
d|                    d
          }}} t          j        t          |          5  t          | ||          t	          ||	                                z            k    sJ 	 d d d            n# 1 swxY w Y   t          j
        d          }t          j                    5  t          j        d           d\  } }}t          | ||          |k    sJ dd|                    d          }}} t          | ||          t	          ||	                                z            k    sJ 	 d d d            d S # 1 swxY w Y   d S )N)r  Nnot_used)r  r   rL  )r  h㈵>Nr5   )r  gQ?Nz?.+the number of samples.+low number.+max_samples.+as an integerr   zI.+the total sum of sample weights.+low number.+max_samples.+as an integerr   i@B rM  )sizer   error)r      Nr   r3   )r   r   r   r   r   r   randomdefault_rnguniformr   r   warningscatch_warningssimplefilter)r  r8   r  warning_msgwarning_msg_with_weightsrJ   s         rR   test_get_n_samples_bootstraprY  3  s   ,@)I{M#I{MJJiWWWW,=)I{M KGG;VVVV -;)I{M#I{MJJaOOOO,:)I{MSK	k	5	5	5 
 
'	;NNRU)#S
 S
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 

 -;)I{M	k	5	5	5 T T'	;NNRSSSSSST T T T T T T T T T T T T T T 	T  )


"
"C,5tS[[i[=X=XM{I	k)A	B	B	B 
 
'	;NNRU-++---S
 S
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 

 GAJJM		 	"	" 
 
g&&&0=-	;$Y]KK    14S#++3+:O:O;	'	;NNRU-++---S
 S
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
sI   ;(B00B47B4DDD":F))F-0F-BI..I25I2c                  \   t          dd          \  } }t          dd          }|                    | |           |                    ddd	           |                    | |           t	          j        t                    5  t          |d
           d d d            d S # 1 swxY w Y   d S )Nr   r5   r   r   T)rB   r   Fr  )r  r   rB   r   )r	   r   rH   r   r   r  AttributeErrorr   r  s      rR   $test_oob_score_removed_on_warm_startr\  d  s    c:::DAq
d
;
;
;CGGAqMMMNNde"NEEEGGAqMMM	~	&	& # #\"""# # # # # # # # # # # # # # # # # #s   B!!B%(B%c                      t          dd          \  } }t          t                      dddd          }|                    | |          j        |                    | |          j        k    sJ d S )N   r5   r   r3   T)r8   r9   r   r2   )r	   r   r   rH   r   rf   rg   rG  s      rR   test_oob_score_consistencyr`  q  s~     c:::DAq  G ;;q!'7;;q!+<+<+GGGGGGGrT   c                  ^   t          dd          \  } }t          t                      dddd          }|                    | |           |j        }|j        }|j        }t          |          t          |          k    sJ t          |d                   t          |           dz  k    sJ |d         j        j	        d	k    sJ d}||         }||         }||         }	| |         d d |f         }
||         }|	j
        }|	                    |
|           |	j
        }t          ||           d S )
Nr^  r5   r   r3   F)r8   r9   r2   r:   r   r?   r|   )r	   r   r   rH   rA  r   r   r   dtypekindcoef_r,   )rf   rg   rG  estimators_samplesestimators_featuresrP   estimator_indexestimator_samplesestimator_featuresrA   rK   rM   
orig_coefs	new_coefss                 rR   test_estimators_samplesrl    s`    c:::DAq  G KK1 !4!6$J !""c*oo5555!!$%%Q14444a &+s2222 O*?;,_=?+I"#QQQ(:%:;G!"GJMM'7###Ij)44444rT   c                  F   t                      } | j        | j        }}t          t	          d          t                                }t          |dd          }|                    ||           |j        d         j	        d         d         j
                                        }|j        d         }|j        d         }|j        d         }||         d d |f         }	||         }
|                    |	|
           t          |j	        d         d         j
        |           d S )Nr?   )n_componentsr3   r   )rA   r8   r2   r   r5   )r   rE   rF   r   r    r   r   rH   r   r   rd  copyrA  r   r-   )rD   rf   rg   base_pipeliner   pipeline_estimator_coefrA   estimator_sampleestimator_featurerK   rM   s              rR   %test_estimators_samples_deterministicrt    s    ;;D9dkqA!A...0B0D0D M mST
U
U
UCGGAqMMM!oa06r:1=CHHJJ"I.q103!"AAA'8$89G !GMM'7###yr*1-35LMMMMMrT   c                      d} t          d| z  d          \  }}t          t                      | dd          }|                    ||           |j        | k    sJ d S )Nr   r?   r5   r   r3   )r8   r9   r2   )r	   r   r   rH   _max_samples)r8   rf   rg   rG  s       rR   test_max_samples_consistencyrw    sy     Ka+oAFFFDAq	  G KK1;......rT   c                  ^   d} dgdgdggdz  }g ddz  }g ddz  }g ddz  }t          d| 	                              ||          j        }t          d| 	                              ||          j        }t          d| 	                              ||          j        }||g||gk    sJ d S )
Nr   r   r   r5   )ABC)r   r   r5   )r   r5   r?   T)r   r2   )r   rH   r   )r2   rf   Y1Y2Y3x1x2x3s           rR   !test_set_oob_score_label_encodingr    s     L
sQC1A	1	B	aB	QBD|DDD	Q	  	D|DDD	Q	  	D|DDD	Q	 
 8BxrT   c                 d    |                      dd          } d| t          j        |            <   | S )Nr?  T)ro  r   )astyper   isfinite)rf   s    rR   replacer    s0    	t$$AAr{1~~oHrT   c            	         t          j        g dg ddt           j        dgdt           j        dgdt           j         dgg          } t          j        g d          t          j        g dg dg dg dg dg          g}|D ]`}t	                      }t          t          t                    |          }|                    | |          	                    |            t          |          }|                    | |          	                    |           }|j        |j        k    sJ t	                      }t          |          }t          j        t                    5  |                    | |           d d d            n# 1 swxY w Y   t          |          }t          j        t                    5  |                    | |           d d d            n# 1 swxY w Y   bd S )Nr5   r   r   r?   N   r?   r  )r?   r   r   r   r   )r?   r5   	   )r   r     )r   arraynaninfr)   r   r   r  rH   rI   r   r   r   r  r  )rf   y_valuesrg   	regressorpipelinebagging_regressory_hats          rR   *test_bagging_regressor_with_missing_inputsr    s]   
IILLNNO	
	 	A 	!!
											
 	
H  ( ()++	 !4W!=!=yIIQ""1%%%,X66!%%a++33A66w%+%%%% *++	 ++]:&& 	 	LLA	 	 	 	 	 	 	 	 	 	 	 	 	 	 	,X66]:&& 	( 	(!!!Q'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(( (s$   F  F	F	3GG	G	c            	         t          j        g dg ddt           j        dgdt           j        dgdt           j         dgg          } t          j        g d          }t	                      }t          t          t                    |          }|                    | |          	                    |            t          |          }|                    | |           |	                    |           }|j        |j        k    sJ |                    |            |                    |            t	                      }t          |          }t          j        t                     5  |                    | |           d d d            n# 1 swxY w Y   t          |          }t          j        t                     5  |                    | |           d d d            d S # 1 swxY w Y   d S )Nr  r  r?   r  )r   r  r  r  r  )r   r  r  r  r(   r   r   r  rH   rI   r   r   rX   rW   r   r  r  )rf   rg   
classifierr  bagging_classifierr  s         rR   +test_bagging_classifier_with_missing_inputsr    sB   
IILLNNO	
	 	A 	!!A'))J099:FFHLLAq!!!*8441a   &&q))E7ek!!!!((+++$$Q''' ())JZ((H	z	"	"  Q              *844	z	"	" % %q!$$$% % % % % % % % % % % % % % % % % %s$   !FFF7GG"Gc                      t          j        ddgddgg          } t          j        ddg          }t          t                      dd          }|                    | |           d S )Nr5   r?   r   r6   r   g333333?)r9   r2   )r   r  r   r   rH   r_  s      rR   test_bagging_small_max_featuresr  :  si     	1a&1a&!""A
!QA 2 4 43UVWWWGKK1rT   c                 t   t           j                            |           }|                    dd          }t          j        d          } G d dt
                    }t           |            dd          }|                    ||           t          |j	        d         j
        |j        d                    d S )N   r6   c                       e Zd ZdZd ZdS )8test_bagging_get_estimators_indices.<locals>.MyEstimatorz7An estimator which stores y indices information at fit.c                     || _         d S r_   )_sample_indicesr   s      rR   rH   z<test_bagging_get_estimators_indices.<locals>.MyEstimator.fitP  s    #$D   rT   N)rk   rl   rm   rn   rH   rC   rT   rR   MyEstimatorr  M  s)        EE	% 	% 	% 	% 	%rT   r  r5   r   )rA   rB   r2   )r   rQ  RandomStaterandnr;  r)   r   rH   r-   r   r  rA  )global_random_seedrJ   rf   rg   r  r   s         rR   #test_bagging_get_estimators_indicesr  D  s    
 )

 2
3
3C		"aA
	"A% % % % %+ % % % [[]]QR
S
S
SCGGAqMMMsq)93;RST;UVVVVVrT   zbagging, expected_allow_nanr5   r=   c                 N    |                                  j        j        |k    sJ dS )z*Check that bagging inherits allow_nan tag.N)__sklearn_tags__
input_tags	allow_nan)rG  expected_allow_nans     rR   test_bagging_allow_nan_tagr  Y  s.     ##%%0:>PPPPPPPrT   r8  modelr  )rA   rB   c                 Z    |                      t          j        t          j                   dS )zAMake sure that metadata routing works with non-default estimator.NrH   rD   rE   rF   r  s    rR   "test_bagging_with_metadata_routingr  k  s"     
IIdi%%%%%rT   zsub_estimator, caller, calleerI   rX   rW   c                    t          j        ddgddgddgg          }g d}dgd}}t                      } | |          }d	|z   d
z   }	 t          ||	          dd           t	          |          }
|
                    ||            t          |
|          t          j        ddgddgddgg          ||           t          |          sJ |D ]}t          |||||           dS )a  Test that metadata routing works in `BaggingClassifier` with dynamic selection of
    the sub-estimator's methods. Here we test only specific test cases, where
    sub-estimator methods are not present and are not tested with `ConsumingClassifier`
    (which possesses all the methods) in
    sklearn/tests/test_metaestimators_metadata_routing.py: `BaggingClassifier.predict()`
    dynamically routes to `predict` if the sub-estimator doesn't have `predict_proba`
    and `BaggingClassifier.predict_log_proba()` dynamically routes to `predict_proba` if
    the sub-estimator doesn't have `predict_log_proba`, or to `predict`, if it doesn't
    have it.
    r   r?   r5   r6   r  )r5   r?   r   a)registryset__requestT)r  metadatar   r   )rf   r  r  )objr   parentr  r  N)r   r  r&   r   r   rH   r   r'   )sub_estimatorcallercalleerf   rg   r  r  r  rA   set_callee_requestrG  s              rR   3test_metadata_routing_with_dynamic_method_selectionr  |  sI   0 	1a&1a&1a&)**A		A c38M{{Hx000I&:5*GI)**MMMM)444GKK1GGV
(QFQFQF+
,
,#    x== 
 
	'	
 	
 	
 	
 	

 
rT   c                 Z    |                      t          j        t          j                   dS )z^Make sure that we still can use an estimator that does not implement the
    metadata routing.Nr  r  s    rR   -test_bagging_without_support_metadata_routingr    s"     
IIdi%%%%%rT   )r   )~rn   r"  rT  	itertoolsr   r   r   numpyr   r   sklearnr   sklearn.baser   sklearn.datasetsr   r   r	   sklearn.dummyr
   r   sklearn.ensembler   r   r   r   r   r   r   r   sklearn.ensemble._baggingr   sklearn.feature_selectionr   sklearn.linear_modelr   r   sklearn.model_selectionr   r   r   sklearn.neighborsr   r   sklearn.pipeliner   sklearn.preprocessingr   r   sklearn.random_projectionr    sklearn.svmr!   r"   %sklearn.tests.metadata_routing_commonr#   r$   r%   r&   r'   sklearn.treer(   r)   sklearn.utilsr*   sklearn.utils._testingr+   r,   r-   sklearn.utils.fixesr.   r/   rJ   rD   permutationrF   rN  permrE   r   rS   markparametrizer   r   r   r   r   r   r   r   r   r   r   r   thread_unsafer   r   r   r   r  r  r  r  r  r   r%  r'  r1  rJ  rY  r\  r`  rl  rt  rw  r  r  r  r  r  r  r  r  r  r  rC   rT   rR   <module>r     s	    
			  $ $ $ $ $ $ $ $       " " " " " " & & & & & & G G G G G G G G G G 9 9 9 9 9 9 9 9	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ? > > > > > 1 1 1 1 1 1 ? ? ? ? ? ? ? ? Q Q Q Q Q Q Q Q Q Q G G G G G G G G * * * * * * < < < < < < < < < < < < < <                              G F F F F F F F , , , , , ,         
 ? > > > > > > > y{{
t{'((IdO	k$ =??
x+,,d#/$'0 0 0B &G'  # !!&*	   # !!&*	  U$OOd%PP	
  	ONN%  .&2 &2/ .&2R  8 +^n-LMM5A 5A NM5Ap# # # # # # # #'9 '9 '9TE E E8 
  
  
F"& "& "&J!# !# !#HJ J J(O O O&6 &6 &6V & & &4	T 	T 	T )0 )0 )0XC C C   4  4 4 4$& & &&  3 3 3,7 7 7@
 
 
 
 
] 
 
 
	 	 	 	 	] 	 	 	 +;=N*OPP/%??+eT];;S	22/: /: 32 <; @? QP/:d.
 .
 .
b
# 
# 
#H H H%5 %5 %5PN N N6/ / /     4  &( &( &(R% % %@  W W W* !		991EEE	F	FM		77CCC	D	DdK		--//	0	0%8		##%%	 	 %(	 Q Q Q ---,,!<<<1	
 	
 	
 	++;;;!	
 	
 	
	
 
& &
 
 .-&
 #	/IF5	

 
,-@)L  ---#
 #
 .- #
T ((a888	
 	
 	
 	#4#4!#D#D#DSTUUU	 	& &	 	& & &rT   