
    Wh                        d 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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/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z: ddl;m<Z< ddl=m>Z>m?Z? ddl@mAZAmBZB  e<d      ZC e       ZDeCj                  eDj                  j                        ZHeDj                  eH   eD_I        eDj                  eH   eD_F         e       ZJeCj                  eJj                  j                        ZHeJj                  eH   eJ_I        eJj                  eH   eJ_F        d ZKej                  j                  d eeBeAz   dddddddddddddddddd gg d!            d"        ZNd# ZOej                  j                  d$eBeAz         d%        ZP G d& d'e      ZQd( ZRd) ZSd* ZTd+ ZUd, ZVd- ZWd. ZXd/ ZYd0 ZZd1 Z[d2 Z\d3 Z] G d4 d5e      Z^d6 Z_dVd7Z`d8 Zad9 Zbd: Zcd; Zdd< Zed= Zfd> Zgd? Zhd@ ZidA ZjdB ZkdC ZldD ZmdE ZndF Zoej                  j                  dG e edHI            df e edHI            df e e             df e e1             dfg      dJ        Zp e	dK      ej                  j                  dL e edHM      dHN       e edHM      dHN      g      dO               Zqej                  j                  dPe5dQdQfe4dRdSfe3dRdQfg       e	dK      dT               Zrej                  j                  dL e edHM      dHN       e edHM      dHN      g      dU        Zsy)WzE
Testing for the bagging ensemble module (sklearn.ensemble.bagging).
    )cycleproductN)config_context)BaseEstimator)load_diabetes	load_irismake_hastie_10_2)DummyClassifierDummyRegressor)AdaBoostClassifierAdaBoostRegressorBaggingClassifierBaggingRegressorHistGradientBoostingClassifierHistGradientBoostingRegressorRandomForestClassifierRandomForestRegressor)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_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 ]3  \  }}t        d|| dd|j                  ||      j                  |       5 y )Nr   random_state      ?      ?      TFmax_samplesmax_features	bootstrapbootstrap_features   max_iter   )	max_depth)	estimatorr0   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paramsr?   s	            ]/var/www/html/jupyter_env/lib/python3.12/site-packages/sklearn/ensemble/tests/test_bagging.pytest_classificationrQ   A   s    
Q
C'7		4;;S($GVWf :F#'-		
D 	B+J !uZ'89 0	 	
	
 		

 #gw
0    z sparse_container, params, methodr1   r=   Tr5   r2   r4   Fr7   r8   r9   r6   r8   r9   )rG   predict_probapredict_log_probadecision_functionc                 l    G d dt               }t        d      }t        t        t        j
                        t        j                  |      \  }}}} | |      }	 | |      }
t        d
 |dd      dd	|j                  |	|      } t        ||      |
      }t        d
 |dd      dd	|j                  ||      } t        ||      |      }t        ||       t        |	      }|j                  D cg c]  }|j                   }}t        |D cg c]  }||k(  	 c}      sJ y c c}w c c}w )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                 H    t         |   ||       t        |      | _        | S NsuperrF   type
data_type_selfXy	__class__s      rP   rF   z1test_sparse_classification.<locals>.CustomSVC.fit   !    GK1"1gDOKrR   __name__
__module____qualname____doc__rF   __classcell__rf   s   @rP   	CustomSVCrZ   |       E	 	rR   ro   r   r/   linearovr)kerneldecision_function_shaper3   r?   r0   rA   )r    r)   r   r   rB   rC   rD   r   rF   getattrr*   r`   estimators_ra   all)sparse_containerrO   methodro   rH   rI   rJ   rK   rL   X_train_sparseX_test_sparsesparse_classifiersparse_resultsdense_classifierdense_resultssparse_typeitypests                      rP   test_sparse_classificationr   b   sK   4C  Q
C'7dii$++C($GVWf &g.N$V,M) 8UK  
c.'"	 
 8W.7FN ) 8UK  
c'7	 
 6G,f5f=Mnm<~&K#4#@#@AaQ\\AEA%0Q[ 0111 B0s   8D,D1c                  t   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 ]6  }|D ]/  }t        d
|| d	|j                  ||      j                  |       1 8 y )Nr   2   r/   r1   r2   TFr5   ru   rA   )r)   r   diabetesrC   rD   r   r   r(   r   r!   r   rF   rG   )rH   rI   rJ   rK   rL   rM   r?   rO   s           rP   test_regressionr      s    
Q
C'7crHOOCR0s($GVWf : #J#'-		
D 	 
	  	FMysMfMQQgfo	
rR   ry   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|
j                  ||      }|j                  |	      }t        d |       dd|
j                  ||      j                  |      }t        |      }|j                  D cg c]  }|j                   }}t        ||       t        |D cg c]  }||k(  	 c}      sJ t        ||        y c c}w c c}w )Nr   r   r/   c                   "     e Zd ZdZ fdZ xZS ))test_sparse_regression.<locals>.CustomSVRr[   c                 H    t         |   ||       t        |      | _        | S r]   r^   rb   s      rP   rF   z-test_sparse_regression.<locals>.CustomSVR.fit   rg   rR   rh   rn   s   @rP   	CustomSVRr      rp   rR   r   r1   r=   Tr5   r2   r4   FrS   rT   r3   ru   rA   )r)   r   r   rC   rD   r!   r   rF   rG   r`   rw   ra   r*   rx   )ry   rH   rI   rJ   rK   rL   r   parameter_setsr{   r|   rO   r}   r~   r   r   r   r   r   s                     rP   test_sparse_regressionr      s    Q
C'7crHOOCR0s($GVWfC  "&		
 "&		
 dK$eLN" &g.N$V,M  A, 
k
5;

#ng
& 	 +22=A My{MfMS'"WV_ 	 >*'8'D'DE!EE!.-@e4A$4555!.-@'A F 5s   <E
&E
c                       e Zd Zd Zd Zy)DummySizeEstimatorc                 `    |j                   d   | _        t        j                  |      | _        y Nr   )shapetraining_size_joblibhashtraining_hash_rc   rd   re   s      rP   rF   zDummySizeEstimator.fit   s"    ggaj$kk!nrR   c                 F    t        j                  |j                  d         S r   )nponesr   rc   rd   s     rP   rG   zDummySizeEstimator.predict   s    wwqwwqz""rR   Nri   rj   rk   rF   rG   rA   rR   rP   r   r      s    -#rR   r   c                  
   t        d      } t        t        j                  t        j                  |       \  }}}}t               j                  ||      }t        t               dd|       j                  ||      }|j                  ||      |j                  ||      k(  sJ t        t               dd|       j                  ||      }|j                  ||      |j                  ||      kD  sJ t        t               d      j                  ||      }g }|j                  D ];  }|j                  |j                  d   k(  sJ |j                  |j                         = t        t!        |            t        |      k(  sJ y )Nr   r/   r2   F)r?   r6   r8   r0   T)r?   r8   )r)   r   r   rC   rD   r(   rF   r   scorer   rw   r   r   appendr   lenset)rH   rI   rJ   rK   rL   r?   ensembletraining_hashs           rP   test_bootstrap_samplesr      sw   
Q
C'7xS($GVWf &'++GW=I  ')	
 
c'7  ??7G,w0PPPP  ')	
 
c'7  ??7G,x~~gw/OOOO
  *<*>$OSSH M)) 7	''7==+;;;;Y5567 s=!"c-&8888rR   c                  `   t        d      } t        t        j                  t        j                  |       \  }}}}t        t               dd|       j                  ||      }|j                  D ]D  }t        j                  j                  d   t        j                  |      j                  d   k(  rDJ  t        t               dd|       j                  ||      }|j                  D ]D  }t        j                  j                  d   t        j                  |      j                  d   kD  rDJ  y )Nr   r/   r2   F)r?   r7   r9   r0   r3   T)r)   r   r   rC   rD   r   r(   rF   estimators_features_r   r   unique)rH   rI   rJ   rK   rL   r   featuress          rP   test_bootstrap_featuresr   *  s   
Q
C'7xS($GVWf  ') 	
 
c'7  11 F}}""1%8)<)B)B1)EEEEF  ')	
 
c'7  11 E}}""1%		((;(A(A!(DDDDErR   c            	         t        d      } t        t        j                  t        j                  |       \  }}}}t        j                  dd      5  t        t               |       j                  ||      }t        t        j                  |j                  |      d      t        j                  t        |                   t        |j                  |      t        j                  |j!                  |                   t        t#               | d	      j                  ||      }t        t        j                  |j                  |      d      t        j                  t        |                   t        |j                  |      t        j                  |j!                  |                   d d d        y # 1 sw Y   y xY w)
Nr   r/   ignore)divideinvalidru   r3   )axis   )r?   r0   r6   )r)   r   rB   rC   rD   r   errstater   r'   rF   r*   sumrU   r   r   exprV   r   rH   rI   rJ   rK   rL   r   s         rP   test_probabilityr   F  sY   
Q
C'7		4;;S($GVWf 
Hh	7 
$,.S

#gw
 	 	"FF8))&1:BGGCK<P	
 	"""6*BFF83M3Mf3U,V	

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

#gw
 	 	"FF8))&1:BGGCK<P	
 	"""6*BFF83M3Mf3U,V	
/
 
 
s   EF99Gc            	         t        d      } t        t        j                  t        j                  |       \  }}}}t               t               fD ]  }t        |ddd|       j                  ||      }|j                  ||      }t        ||j                  z
        dk  sJ d}t        j                  t        |      5  t        |d	dd|       }|j                  ||       d d d         y # 1 sw Y   xY w)
Nr   r/   d   Tr?   r@   r8   	oob_scorer0   皙?{Some inputs do not have OOB scores. This probably means too few estimators were used to compute any reliable oob estimates.matchr3   )r)   r   rB   rC   rD   r'   r    r   rF   r   abs
oob_score_pytestwarnsUserWarning)	rH   rI   rJ   rK   rL   r?   clf
test_scorewarn_msgs	            rP   test_oob_score_classificationr   i  s    Q
C'7		4;;S($GVWf -.6 &	
 #gw
 	 YYvv.
:./#555J 	 \\+X6 	&## C GGGW%	& 	&%&$	& 	&s   >#C,,C5	c                     t        d      } t        t        j                  t        j                  |       \  }}}}t        t               ddd|       j                  ||      }|j                  ||      }t        ||j                  z
        dk  sJ d}t        j                  t        |      5  t        t               d	dd|       }|j                  ||       d d d        y # 1 sw Y   y xY w)
Nr   r/   r   Tr   r   r   r   r3   )r)   r   r   rC   rD   r   r(   rF   r   r   r   r   r   r   )	rH   rI   rJ   rK   rL   r   r   r   regrs	            rP   test_oob_score_regressionr     s     Q
C'7xS($GVWf ') 
c'7  66*JzCNN*+c111	F  
k	2 #+-
 	'"# # #s   /+C##C,c                  P   t        d      } t        t        j                  t        j                  |       \  }}}}t        t               ddd|       j                  ||      }t               j                  ||      }t        |j                  |      |j                  |             y )Nr   r/   r3   F)r?   r@   r8   r9   r0   )
r)   r   r   rC   rD   r   r   rF   r*   rG   )rH   rI   rJ   rK   rL   clf1clf2s          rP   test_single_estimatorr     s    
Q
C'7xS($GVWf %'  
c'7 	  $$Wg6Ddll62DLL4HIrR   c                      t         j                  t         j                  }} t               }t	        t        |      j                  | |      d      rJ y )NrW   )rB   rC   rD   r'   hasattrr   rF   )rd   re   bases      rP   
test_errorr     sB    99dkkqA!#D(.221a8:MNNNNrR   c                     t        t        j                  t        j                  d      \  } }}}t	        t               dd      j                  | |      }|j                  |      }|j                  d       |j                  |      }t        ||       t	        t               dd      j                  | |      }|j                  |      }t        ||       t	        t        d      dd      j                  | |      }|j                  |      }|j                  d       |j                  |      }	t        ||	       t	        t        d      dd      j                  | |      }|j                  |      }
t        ||
       y )	Nr   r/      n_jobsr0   r3   r   rr   )rt   )r   rB   rC   rD   r   r'   rF   rU   
set_paramsr*   r    rW   )rI   rJ   rK   rL   r   y1y2y3
decisions1
decisions2
decisions3s              rP   test_parallel_classificationr     sj   '7		4;;Q($GVWf ! 	c'7 
 
			'Bq!				'Bb"%  	c'7  
			'Bb"% !E*11	c'7  ++F3Jq!++F3Jj*5 E*11	c'7  ++F3Jj*5rR   c                     t        d      } t        t        j                  t        j                  |       \  }}}}t        t               dd      j                  ||      }|j                  d       |j                  |      }|j                  d       |j                  |      }t        ||       t        t               dd      j                  ||      }|j                  |      }t        ||       y )Nr   r/   r   r   r3   r   r=   )r)   r   r   rC   rD   r   r(   rF   r   rG   r*   )	rH   rI   rJ   rK   rL   r   r   r   r   s	            rP   test_parallel_regressionr     s    
Q
C'7xS($GVWf   5 7PQRVVH q!			&	!Bq!			&	!Bb"% 5 7PQRVVH 
		&	!Bb"%rR   c                      t         j                  t         j                  }} d||dk(  <   ddd}t        t	        t                     |d      j                  | |       y )Nr3   r=   )r3   r=   )r@   estimator__Croc_auc)scoring)rB   rC   rD   r   r   r    rF   )rd   re   
parameterss      rP   test_gridsearchr     sP     99dkkqAAa1fI #)&AJ"35):yIMMaQRSrR   c                     t        d      } t        t        j                  t        j                  |       \  }}}}t        d dd      j                  ||      }t        |j                  t              sJ t        t               dd      j                  ||      }t        |j                  t              sJ t        t               dd      j                  ||      }t        |j                  t              sJ t        t        j                  t        j                  |       \  }}}}t        d dd      j                  ||      }t        |j                  t              sJ t        t               dd      j                  ||      }t        |j                  t              sJ t        t               dd      j                  ||      }t        |j                  t              sJ y )Nr   r/   r   r   )r)   r   rB   rC   rD   r   rF   
isinstance
estimator_r'   r   r   r   r(   r!   r   s         rP   test_estimatorr     s   
Q
C (8		4;;S($GVWf !aa@DDWgVHh))+ABBB  	c'7  h))+ABBB aaHLLH h)):666 (8xS($GVWf  QQ?CCGWUHh))+@AAA 5 7PQRVVH h))+@AAAaa@DDWgVHh))3///rR   c                     t        t        t        d      t                     d      } | j	                  t
        j                  t
        j                         t        | d   j                  d   d   j                  t              sJ y )Nr3   )kr=   )r7   r   )r   r   r   r'   rF   rB   rC   rD   r   stepsr0   intr?   s    rP   test_bagging_with_pipeliner   H  sd    !kA&(>(@APQI MM$))T[[)il((,Q/<<cBBBrR   c                       e Zd Zd Zd Zy)DummyZeroEstimatorc                 :    t        j                  |      | _        | S r]   )r   r   classes_r   s      rP   rF   zDummyZeroEstimator.fitQ  s    		!rR   c                 l    | j                   t        j                  |j                  d   t                 S )Nr   )dtype)r   r   zerosr   r   r   s     rP   rG   zDummyZeroEstimator.predictU  s$    }}RXXaggaj<==rR   Nr   rA   rR   rP   r   r   P  s    >rR   r   c            
         t        t                     } t        d      }| j                  t        j
                  t        j                        j                  t        j
                         t        j                  t              5  | j                  t        j
                  t        j                  |j                  dt        j
                  j                  d                d d d        y # 1 sw Y   y xY w)Nr   
   )size)sample_weight)r   r   r)   rF   rB   rC   rD   rG   r   raises
ValueErrorrandintr   )r?   rH   s     rP   1test_bagging_sample_weight_unsupported_but_passedr  Y  s    !"4"67I
Q
CMM$))T[[)11$))<	z	" 
IIKK++b		0B+D 	 	

 
 
s   AC))C2c                    t        dd      \  }}d }dD ]G  }|t        || d      }n|j                  |       |j                  ||       t	        |      |k(  rGJ  t        d| d	      }|j                  ||       t        |D cg c]  }|j                   c}      t        |D cg c]  }|j                   c}      k(  sJ y c c}w c c}w )
Nr:   r3   	n_samplesr0   )r   r   T)r@   r0   
warm_startr@   r   F)r	   r   r   rF   r   r   r0   )r0   rd   re   clf_wsr@   	clf_no_wstrees          rP   test_warm_startr  f  s     bq9DAqF +>&)QUF <8

1a6{l***+ "luI MM!Qf5d!!56#'01t		1;   51s   C	*C
c                     t        dd      \  } }t        dd      }|j                  | |       |j                  d       t	        j
                  t              5  |j                  | |       d d d        y # 1 sw Y   y xY w)	Nr:   r3   r  r   T)r@   r  r4   r  )r	   r   rF   r   r   r   r   rd   re   r   s      rP   $test_warm_start_smaller_n_estimatorsr    sg    bq9DAq
t
<CGGAqMNNN"	z	" 1  s   A77B c                  t   t        dd      \  } }t        | |d      \  }}}}t        ddd	      }|j                  ||       |j	                  |      }|d
z  }d}t        j                  t        |      5  |j                  ||       d d d        t        ||j	                  |             y # 1 sw Y   %xY w)Nr:   r3   r  +   r/   r   TS   r@   r  r0   r2   z;Warm-start fitting without increasing n_estimators does notr   )	r	   r   r   rF   rG   r   r   r   r+   )	rd   re   rI   rJ   rK   rL   r   y_predr   s	            rP   "test_warm_start_equal_n_estimatorsr    s    bq9DAq'712'N$GVWf
t"
MCGGGW[[ FsNGLH	k	2 "!"vs{{623" "s   7B..B7c                  n   t        dd      \  } }t        | |d      \  }}}}t        ddd	      }|j                  ||       |j	                  d
       |j                  ||       |j                  |      }t        d
dd	      }|j                  ||       |j                  |      }	t        ||	       y )Nr:   r3   r  r  r/   r   TiE  r  r   r  F)r	   r   r   rF   r   rG   r*   )
rd   re   rI   rJ   rK   rL   r  r   r   r   s
             rP   test_warm_start_equivalencer    s     bq9DAq'712'N$GVWfA$TRF
JJw 
2&
JJw 		B
D
QCGGGW	V	Bb"%rR   c                      t        dd      \  } }t        ddd      }t        j                  t              5  |j                  | |       d d d        y # 1 sw Y   y xY w)Nr:   r3   r  r   T)r@   r  r   )r	   r   r   r   r   rF   r  s      rP   $test_warm_start_with_oob_score_failsr    sN    bq9DAq
tt
LC	z	" 1  s   AAc                  "   t        dd      \  } }t        dd      }|j                  | |       |j                  ddd	       |j                  | |       t	        j
                  t              5  t        |d
       d d d        y # 1 sw Y   y xY w)Nr   r3   r  r   T)r@   r   Fr   )r  r   r@   r   )r	   r   rF   r   r   r   AttributeErrorrv   r  s      rP   $test_oob_score_removed_on_warm_startr    su    c:DAq
d
;CGGAqMNNde"NEGGAqM	~	& #\"# # #s   /BBc                      t        dd      \  } }t        t               dddd      }|j                  | |      j                  |j                  | |      j                  k(  sJ y )N   r3   r  r1   T)r6   r7   r   r0   )r	   r   r   rF   r   rd   re   baggings      rP   test_oob_score_consistencyr     sb     c:DAqG ;;q!''7;;q!+<+G+GGGGrR   c                     t        dd      \  } }t        t               dddd      }|j                  | |       |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                  |
|       |	j                  }t        ||       y )
Nr  r3   r  r1   F)r6   r7   r0   r8   r   r=   r   )r	   r   r   rF   estimators_samples_r   rw   r   r   kindcoef_r*   )rd   re   r  estimators_samplesestimators_featuresrN   estimator_indexestimator_samplesestimator_featuresr?   rI   rK   
orig_coefs	new_coefss                 rP   test_estimators_samplesr,    s1    c:DAqG KK1 !44!66$$J !"c*o555!!$%Q1444a &&++s222 O*?;,_=?+I"#Q(:%:;G!"GJMM'7#Ij)4rR   c                     t               } | j                  | j                  }}t        t	        d      t                     }t        |dd      }|j                  ||       |j                  d   j                  d   d   j                  j                         }|j                  d   }|j                  d   }|j                  d   }||   d d |f   }	||   }
|j                  |	|
       t        |j                  d   d   j                  |       y )Nr=   )n_componentsr1   r   )r?   r6   r0   r   r3   )r   rC   rD   r   r   r   r   rF   rw   r   r$  copyr"  r   r+   )rB   rd   re   base_pipeliner   pipeline_estimator_coefr?   estimator_sampleestimator_featurerI   rK   s              rP   %test_estimators_samples_deterministicr4    s     ;D99dkkqA!A.0B0DM mST
UCGGAqM!ooa066r:1=CCHHJ"I..q1003!"A'8$89G !GMM'7#yr*1-335LMrR   c                      d} t        d| z  d      \  }}t        t               | dd      }|j                  ||       |j                  | k(  sJ y )Nr   r=   r3   r  r1   )r6   r7   r0   )r	   r   r   rF   _max_samples)r6   rd   re   r  s       rP   test_max_samples_consistencyr7    sZ     Ka+oAFDAq	G KK1;...rR   c                  H   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                  ||      j                  }t        d| 	      j                  ||      j                  }t        d| 	      j                  ||      j                  }||g||gk(  sJ y )
Nr   r   r   r3   )ABC)r   r   r3   )r   r3   r=   T)r   r0   )r   rF   r   )r0   rd   Y1Y2Y3x1x2x3s           rP   !test_set_oob_score_label_encodingrB  $  s     L
sQC1A	1	B	aB	QBD|D	Q	  	D|D	Q	  	D|D	Q	 
 8BxrR   c                 ^    | j                  dd      } d| t        j                  |        <   | S )NfloatT)r/  r   )astyper   isfinite)rd   s    rP   replacerG  >  s-    	t$AAr{{1~oHrR   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              |      }|j                  | |      j                  |        t        |      }|j                  | |      j                  |       }|j                  |j                  k(  sJ t	               }t        |      }t        j                  t              5  |j                  | |       d d d        t        |      }t        j                  t              5  |j                  | |       d d d         y # 1 sw Y   LxY w# 1 sw Y   1xY w)Nr3   r   r   r=   N   r=   rK  )r=   r   r   r   r   )r=   r3   	   )r   rK     )r   arraynaninfr(   r   r   rG  rF   rG   r   r   r   r   r   )rd   y_valuesre   	regressorpipelinebagging_regressory_hats          rP   *test_bagging_regressor_with_missing_inputsrV  D  s   
NNO	
	A 	!
	
H  ()+	 !4W!=yIQ""1%,X6!%%a+33A6ww%++%%% *+	 +]]:& 	LLA	,X6]]:& 	(!!!Q'	( 	((	 		( 	(s   F+F7+F4	7G	c            	      t   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              |      }|j                  | |      j                  |        t        |      }|j                  | |       |j                  |       }|j                  |j                  k(  sJ |j                  |        |j                  |        t	               }t        |      }t        j                  t               5  |j                  | |       d d d        t        |      }t        j                  t               5  |j                  | |       d d d        y # 1 sw Y   IxY w# 1 sw Y   y xY w)NrI  rJ  r=   rK  )r   rK  rK  rK  rK  )r   rN  rO  rP  r'   r   r   rG  rF   rG   r   r   rV   rU   r   r   r   )rd   re   
classifierrS  bagging_classifierrU  s         rP   +test_bagging_classifier_with_missing_inputsrZ  m  sh   
NNO	
	A 	!A')J09:FHLLAq!*841a &&q)E77ekk!!!((+$$Q' ()JZ(H	z	" Q*84	z	" %q!$% % % %s   F"F."F+.F7c                      t        j                  ddgddgg      } t        j                  ddg      }t        t               dd      }|j	                  | |       y )Nr3   r=   r   r4   r   g333333?)r7   r0   )r   rN  r   r   rF   r  s      rP   test_bagging_small_max_featuresr\    sR     	1a&1a&!"A
!QA 2 43UVWGKK1rR   c                 X   t         j                  j                  |       }|j                  dd      }t        j                  d      } G d dt
              }t         |       dd      }|j                  ||       t        |j                  d   j                  |j                  d          y )N   r4   c                       e Zd ZdZd Zy)8test_bagging_get_estimators_indices.<locals>.MyEstimatorz7An estimator which stores y indices information at fit.c                     || _         y r]   )_sample_indicesr   s      rP   rF   z<test_bagging_get_estimators_indices.<locals>.MyEstimator.fit  s
    #$D rR   N)ri   rj   rk   rl   rF   rA   rR   rP   MyEstimatorr`    s
    E	%rR   rc  r3   r   )r?   r@   r0   )r   randomRandomStaterandnaranger(   r   rF   r+   rw   rb  r"  )global_random_seedrH   rd   re   rc  r   s         rP   #test_bagging_get_estimators_indicesri    s    
 ))

 2
3C		"aA
		"A%+ % []QR
SCGGAqMsq)993;R;RST;UVrR   zbagging, expected_allow_nanr3   r;   c                 V    | j                         j                  j                  |k(  sJ y)z*Check that bagging inherits allow_nan tag.N)__sklearn_tags__
input_tags	allow_nan)r  expected_allow_nans     rP   test_bagging_allow_nan_tagro    s(     ##%00::>PPPPrR   )enable_metadata_routingmodelr  )r?   r@   c                 `    | j                  t        j                  t        j                         y)zAMake sure that metadata routing works with non-default estimator.NrF   rB   rC   rD   rq  s    rP   "test_bagging_with_metadata_routingru    s     
IIdii%rR   zsub_estimator, caller, calleerG   rV   rU   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	        |      }
|
j                  ||        t        |
|      t        j                  ddgddgddgg      ||       t        |      sJ |D ]  }t        |||||        y)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=   r3   r4   rK  )r3   r=   r   a)registryset__requestT)r   metadatar   r   )rd   r   r{  )objrz   parentr   r{  N)r   rN  r%   rv   r   rF   r   r&   )sub_estimatorcallercalleerd   re   r   r{  rx  r?   set_callee_requestr  s              rP   3test_metadata_routing_with_dynamic_method_selectionr    s    0 	1a&1a&1a&)*AA c38M{Hx0I&:5*GI)*M)4GKK1GGV
((QFQFQF+
,# x== 
	'	

rR   c                 `    | j                  t        j                  t        j                         y)z^Make sure that we still can use an estimator that does not implement the
    metadata routing.Nrs  rt  s    rP   -test_bagging_without_support_metadata_routingr    s     
IIdii%rR   )*   )trl   	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.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+   sklearn.utils.fixesr,   r-   rH   rB   permutationrD   r   permrC   r   rQ   markparametrizer   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,  r4  r7  rB  rG  rV  rZ  r\  ri  ro  ru  r  r  rA   rR   rP   <module>r     s   %    " & G G 9	 	 	 2 ? Q Q G * < <    G , P > {
t{{''(IIdO	kk$ ?
x++,d#//$'0B &'  # !!&*	  # !!&*	 U$Od%P	
  	O%.&2/.&2R8 +^n-LM5A N5Ap# #'9TE8 
F"&J!#HJ(O&6R&4	T)0XC> >

44$&&
#H%5PN6/ 4&(R%@W* !	91E	FM	7C	DdK	-/	0%8	#%	 %(	QQ -,!<1	
 	+;!	
	
&
 .&
 #	/IF5	

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