o
    \i                     @   sF  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ZCe ZDeCEeDjFjGZHeDjIeH eD_IeDjFeH eD_Fe ZJeCEeJjFjGZHeJjIeH eJ_IeJjFeH eJ_Fdd ZKejLMdeeBeA dddddddddddddd dddd!gg d"d#d$ ZNd%d& ZOejLMd'eBeA d(d) ZPG d*d+ d+eZQd,d- ZRd.d/ ZSd0d1 ZTd2d3 ZUd4d5 ZVd6d7 ZWd8d9 ZXd:d; ZYd<d= ZZd>d? Z[d@dA Z\dBdC Z]G dDdE dEeZ^dFdG Z_d|dIdJZ`dKdL ZadMdN ZbdOdP ZcdQdR ZddSdT ZedUdV ZfdWdX ZgdYdZ Zhd[d\ Zid]d^ Zjd_d` Zkdadb Zldcdd Zmdedf Zndgdh ZoejLMdieedjdkdfeedjdkdfee dfee1 dfgdldm Zpe	ddnejLMdoeedjdpdjdqeedjdpdjdqgdrds ZqejLMdte5dudufe4dvdwfe3dvdufge	ddndxdy ZrejLMdoeedjdpdjdqeedjdpdjdqgdzd{ ZsdS )}zE
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            	      C   s   t d} ttjtj| d\}}}}tddgddgddgddgd	}d t td
dtddt	 t
 g}t|t|D ]\}}td|| dd|||| q:d S )Nr   random_state      ?      ?      TFmax_samplesmax_features	bootstrapbootstrap_features   max_iter   )	max_depth)	estimatorr.   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=   r?   r?   /var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/lib/python3.10/site-packages/sklearn/ensemble/tests/test_bagging.pytest_classificationA   s8   
	
rO   z sparse_container, params, methodr/   r;   Tr3   r0   r2   Fr5   r6   r7   r4   r6   r7   )rE   predict_probapredict_log_probadecision_functionc                    s   G dd dt }td}tttjtj|d\}}}}| |}	| |}
td|ddddd	||	|}t	|||
}td|ddddd	|||}t	|||}t
|| t|	 d
d |jD }t fdd|D ssJ d S )Nc                           e Zd ZdZ fddZ  ZS )z-test_sparse_classification.<locals>.CustomSVC7SVC variant that records the nature of the training setc                       t  || t|| _| S NsuperrD   type
data_type_selfXy	__class__r?   rN   rD         
z1test_sparse_classification.<locals>.CustomSVC.fit__name__
__module____qualname____doc__rD   __classcell__r?   r?   ra   rN   	CustomSVC|       rj   r   r-   linearovr)kerneldecision_function_shaper1   r=   r.   c                 S      g | ]}|j qS r?   r\   .0ir?   r?   rN   
<listcomp>       z.test_sparse_classification.<locals>.<listcomp>c                       g | ]}| kqS r?   r?   rt   tsparse_typer?   rN   rv          r?   )r   r(   r   r   r@   rA   rB   r   rD   getattrr)   r[   estimators_all)sparse_containerrM   methodrj   rF   rG   rH   rI   rJ   X_train_sparseX_test_sparsesparse_classifiersparse_resultsdense_classifierdense_resultstypesr?   r{   rN   test_sparse_classificationb   s:   


r   c                  C   s   t d} ttjd d tjd d | d\}}}}tddgddgddgddgd}d t t t t	 fD ]}|D ]}t
d
|| d	|||| q9q5d S )Nr   2   r-   r/   r0   TFr3   rp   r?   )r(   r   diabetesrA   rB   r   r
   r'   r   r    r   rD   rE   )rF   rG   rH   rI   rJ   rK   r=   rM   r?   r?   rN   test_regression   s0   

r   r   c                    s"  t d}ttjd d tjd d |d\}}}}G dd dt}ddddd	d
dddd	ddddddddg}| |}| |}	|D ]K}
td| dd|
||}||	}td| dd|
|||}t	| dd |j
D }t|| t fdd|D sJ t|| qCd S )Nr   r   r-   c                       rU   )z)test_sparse_regression.<locals>.CustomSVRrV   c                    rW   rX   rY   r]   ra   r?   rN   rD      rc   z-test_sparse_regression.<locals>.CustomSVR.fitrd   r?   r?   ra   rN   	CustomSVR   rk   r   r/   r;   Tr3   r0   r2   FrP   rQ   r1   rp   c                 S   rq   r?   rr   rs   r?   r?   rN   rv      rw   z*test_sparse_regression.<locals>.<listcomp>c                    rx   r?   r?   ry   r{   r?   rN   rv      r}   r?   )r(   r   r   rA   rB   r    r   rD   rE   r[   r   r)   r   )r   rF   rG   rH   rI   rJ   r   parameter_setsr   r   rM   r   r   r   r   r?   r{   rN   test_sparse_regression   sN   




r   c                   @      e Zd Zdd Zdd ZdS )DummySizeEstimatorc                 C   s   |j d | _t|| _d S Nr   )shapetraining_size_joblibhashtraining_hash_r]   r?   r?   rN   rD      s   zDummySizeEstimator.fitc                 C   s   t |jd S r   )nponesr   r^   r_   r?   r?   rN   rE      s   zDummySizeEstimator.predictNre   rf   rg   rD   rE   r?   r?   r?   rN   r          r   c                  C   s   t d} ttjtj| d\}}}}t ||}tt dd| d||}||||||ks3J tt dd| d||}||||||ksNJ tt	 dd||}g }|j
D ]}|j|jd ksjJ ||j q^tt|t|ks}J d S )Nr   r-   r0   F)r=   r4   r6   r.   T)r=   r6   )r(   r   r   rA   rB   r'   rD   r   scorer   r   r   r   appendr   lenset)rF   rG   rH   rI   rJ   r=   ensembletraining_hashr?   r?   rN   test_bootstrap_samples   s>   

r   c                  C   s   t d} ttjtj| d\}}}}tt dd| d||}|jD ]}tjj	d t
|j	d ks3J q!tt dd| d||}|jD ]}tjj	d t
|j	d ksVJ qDd S )Nr   r-   r0   F)r=   r5   r7   r.   r1   T)r(   r   r   rA   rB   r   r'   rD   estimators_features_r   r   unique)rF   rG   rH   rI   rJ   r   featuresr?   r?   rN   test_bootstrap_features*  s2   

"
"r   c                  C   s  t d} ttjtj| d\}}}}tjddd` tt | d	||}t
tj||ddtt| t
||t|| tt | dd		||}t
tj||ddtt| t
||t|| W d    d S 1 s{w   Y  d S )
Nr   r-   ignore)divideinvalidrp   r1   )axis   )r=   r.   r4   )r(   r   r@   rA   rB   r   errstater   r&   rD   r)   sumrR   r   r   exprS   r   rF   rG   rH   rI   rJ   r   r?   r?   rN   test_probabilityF  s8   
"r   c            	   	   C   s   t d} ttjtj| d\}}}}t t fD ]H}t|ddd| d||}|	||}t
||j dk s7J d}tjt|d t|d	dd| d}||| W d    n1 sZw   Y  qd S )
Nr   r-   d   Tr=   r>   r6   	oob_scorer.   皙?{Some inputs do not have OOB scores. This probably means too few estimators were used to compute any reliable oob estimates.matchr1   )r(   r   r@   rA   rB   r&   r   r   rD   r   abs
oob_score_pytestwarnsUserWarning)	rF   rG   rH   rI   rJ   r=   clf
test_scorewarn_msgr?   r?   rN   test_oob_score_classificationi  s<   
r   c            	      C   s   t d} ttjtj| d\}}}}tt ddd| d||}|||}t	||j
 dk s0J d}tjt|d tt d	dd| d}||| W d    d S 1 sUw   Y  d S )
Nr   r-   r   Tr   r   r   r   r1   )r(   r   r   rA   rB   r   r'   rD   r   r   r   r   r   r   )	rF   rG   rH   rI   rJ   r   r   r   regrr?   r?   rN   test_oob_score_regression  s6   
"r   c                  C   sf   t d} ttjtj| d\}}}}tt ddd| d||}t ||}t|	||	| d S )Nr   r-   r1   F)r=   r>   r6   r7   r.   )
r(   r   r   rA   rB   r   r   rD   r)   rE   )rF   rG   rH   rI   rJ   clf1clf2r?   r?   rN   test_single_estimator  s   
r   c                  C   s2   t jt j} }t }tt|| |drJ d S )NrT   )r@   rA   rB   r&   hasattrr   rD   )r_   r`   baser?   r?   rN   
test_error  s   r   c                  C   s  t tjtjdd\} }}}tt ddd| |}||}|jdd ||}t	|| tt ddd| |}||}t	|| tt
ddddd| |}||}|jdd ||}	t	||	 tt
ddddd| |}||}
t	||
 d S )	Nr   r-      n_jobsr.   r1   r   rm   )ro   )r   r@   rA   rB   r   r&   rD   rR   
set_paramsr)   r   rT   )rG   rH   rI   rJ   r   y1y2y3
decisions1
decisions2
decisions3r?   r?   rN   test_parallel_classification  sF   









r   c            	      C   s   t d} ttjtj| d\}}}}tt ddd||}|jdd |	|}|jdd |	|}t
|| tt ddd||}|	|}t
|| d S )Nr   r-   r   r   r1   r   r;   )r(   r   r   rA   rB   r   r'   rD   r   rE   r)   )	rF   rG   rH   rI   rJ   r   r   r   r   r?   r?   rN   test_parallel_regression  s"   




r   c                  C   sD   t jt j} }d||dk< ddd}ttt |dd| | d S )Nr1   r;   )r1   r;   )r>   estimator__Croc_auc)scoring)r@   rA   rB   r   r   r   rD   )r_   r`   
parametersr?   r?   rN   test_gridsearch  s   
 r   c                  C   s,  t d} ttjtj| d\}}}}td ddd||}t|jt	s$J tt	 ddd||}t|jt	s8J tt
 ddd||}t|jt
sLJ ttjtj| d\}}}}td ddd||}t|jtslJ tt ddd||}t|jtsJ tt ddd||}t|jtsJ d S )Nr   r-   r   r   )r(   r   r@   rA   rB   r   rD   
isinstance
estimator_r&   r   r   r   r'   r    r   r?   r?   rN   test_estimator  s6   

r   c                  C   sL   t ttddt dd} | tjtj t| d j	d d j
ts$J d S )Nr1   )kr;   )r5   r   )r   r   r   r&   rD   r@   rA   rB   r   stepsr.   intr=   r?   r?   rN   test_bagging_with_pipelineH  s
   "r   c                   @   r   )DummyZeroEstimatorc                 C   s   t || _| S rX   )r   r   classes_r]   r?   r?   rN   rD   Q  s   zDummyZeroEstimator.fitc                 C   s   | j tj|jd td S )Nr   )dtype)r   r   zerosr   r   r   r?   r?   rN   rE   U  s   zDummyZeroEstimator.predictNr   r?   r?   r?   rN   r   P  r   r   c                  C   s   t t } td}| tjtjtj t	t
 | jtjtj|jdtjjd dd W d    d S 1 s9w   Y  d S )Nr   
   )size)sample_weight)r   r   r(   rD   r@   rA   rB   rE   r   raises
ValueErrorrandintr   )r=   rF   r?   r?   rN   1test_bagging_sample_weight_unsupported_but_passedY  s   
"r   *   c                 C   s   t ddd\}}d }dD ]"}|d u rt|| dd}n|j|d ||| t||ks.J qtd| d	d}||| td
d |D tdd |D ksPJ d S )Nr8   r1   	n_samplesr.   )r   r   T)r>   r.   
warm_startr>   r   Fc                 S   rq   r?   r-   rt   treer?   r?   rN   rv   {  rw   z#test_warm_start.<locals>.<listcomp>c                 S   rq   r?   r-   r   r?   r?   rN   rv   |  rw   )r   r   r   rD   r   r   )r.   r_   r`   clf_wsr>   	clf_no_wsr?   r?   rN   test_warm_startf  s"   r   c                  C   sp   t ddd\} }tddd}|| | |jdd tt || | W d    d S 1 s1w   Y  d S )	Nr8   r1   r   r   T)r>   r   r2   r   )r   r   rD   r   r   r   r   r_   r`   r   r?   r?   rN   $test_warm_start_smaller_n_estimators  s   "r   c            	      C   s   t ddd\} }t| |dd\}}}}tdddd	}||| ||}|d
7 }d}tjt|d ||| W d    n1 sCw   Y  t||| d S )Nr8   r1   r   +   r-   r   TS   r>   r   r.   r0   z;Warm-start fitting without increasing n_estimators does notr   )	r   r   r   rD   rE   r   r   r   r*   )	r_   r`   rG   rH   rI   rJ   r   y_predr   r?   r?   rN   "test_warm_start_equal_n_estimators  s   
r  c            
      C   s   t ddd\} }t| |dd\}}}}tdddd	}||| |jd
d ||| ||}td
ddd	}||| ||}	t||	 d S )Nr8   r1   r   r   r-   r   TiE  r   r   r   F)r   r   r   rD   r   rE   r)   )
r_   r`   rG   rH   rI   rJ   r   r   r   r   r?   r?   rN   test_warm_start_equivalence  s   

r  c                  C   sZ   t ddd\} }tdddd}tt || | W d    d S 1 s&w   Y  d S )Nr8   r1   r   r   T)r>   r   r   )r   r   r   r   r   rD   r   r?   r?   rN   $test_warm_start_with_oob_score_fails  s
   "r  c                  C   s~   t ddd\} }tddd}|| | |jdddd	 || | tt t|d
 W d    d S 1 s8w   Y  d S )Nr   r1   r   r   T)r>   r   Fr   )r   r   r>   r   )r   r   rD   r   r   r   AttributeErrorr~   r   r?   r?   rN   $test_oob_score_removed_on_warm_start  s   "r  c                  C   sH   t ddd\} }tt ddddd}|| |j|| |jks"J d S )N   r1   r   r/   T)r4   r5   r   r.   )r   r   r   rD   r   r_   r`   baggingr?   r?   rN   test_oob_score_consistency  s   $r	  c                  C   s   t ddd\} }tt ddddd}|| | |j}|j}|j}t|t|ks+J t|d t| d ks9J |d jj	d	ksCJ d}|| }|| }|| }	| | d d |f }
|| }|	j
}|	|
| |	j
}t|| d S )
Nr  r1   r   r/   F)r4   r5   r.   r6   r   r;   ru   )r   r   r   rD   estimators_samples_r   r   r   r   kindcoef_r)   )r_   r`   r  estimators_samplesestimators_featuresrL   estimator_indexestimator_samplesestimator_featuresr=   rG   rI   
orig_coefs	new_coefsr?   r?   rN   test_estimators_samples  s2   r  c                  C   s   t  } | j| j}}ttddt }t|d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_componentsr/   r   )r=   r4   r.   r   r1   )r   rA   rB   r   r   r   r   rD   r   r   r  copyr
  r   r*   )r@   r_   r`   base_pipeliner   pipeline_estimator_coefr=   estimator_sampleestimator_featurerG   rI   r?   r?   rN   %test_estimators_samples_deterministic  s   


r  c                  C   sH   d} t d|  dd\}}tt | ddd}||| |j| ks"J d S )Nr   r;   r1   r   r/   )r4   r5   r.   )r   r   r   rD   _max_samples)r4   r_   r`   r  r?   r?   rN   test_max_samples_consistency  s   r  c                  C   s   d} dgdgdggd }g dd }g dd }g dd }t d| d	||j}t d| d	||j}t d| d	||j}||g||gksIJ d S )
Nr   r   r   r1   )ABC)r   r   r1   )r   r1   r;   T)r   r.   )r   rD   r   )r.   r_   Y1Y2Y3x1x2x3r?   r?   rN   !test_set_oob_score_label_encoding$  s$   


r'  c                 C   s"   | j ddd} d| t|  < | S )NfloatT)r  r   )astyper   isfinite)r_   r?   r?   rN   replace>  s   r+  c               	   C   sL  t g dg ddt jdgdt jdgdt j dgg} t g dt g dg dg dg dg dgg}|D ]k}t }ttt|}|| |	|  t
|}|| |	| }|j|jksbJ t }t|}tt || | W d    n1 sw   Y  t
|}tt || | W d    n1 sw   Y  q8d S )Nr1   r   r   r;   N   r;   r.  )r;   r   r   r   r   )r;   r1   	   )r   r.     )r   arraynaninfr'   r   r   r+  rD   rE   r   r   r   r   r   )r_   y_valuesr`   	regressorpipelinebagging_regressory_hatr?   r?   rN   *test_bagging_regressor_with_missing_inputsD  sH   


r9  c               	   C   s4  t g dg ddt jdgdt jdgdt j dgg} t g d}t }ttt|}|| |	|  t
|}|| | |	| }|j|jksLJ ||  ||  t }t|}tt || | W d    n1 ssw   Y  t
|}tt || | W d    d S 1 sw   Y  d S )Nr,  r-  r;   r.  )r   r.  r.  r.  r.  )r   r1  r2  r3  r&   r   r   r+  rD   rE   r   r   rS   rR   r   r   r   )r_   r`   
classifierr6  bagging_classifierr8  r?   r?   rN   +test_bagging_classifier_with_missing_inputsm  s6   

	


"r<  c                  C   sD   t ddgddgg} t ddg}tt ddd}|| | d S )Nr1   r;   r   r2   r   g333333?)r5   r.   )r   r1  r   r   rD   r  r?   r?   rN   test_bagging_small_max_features  s   r=  c                 C   sj   t j| }|dd}t d}G dd dt}t| ddd}||| t|j	d j
|jd  d S )N   r2   c                   @   s   e Zd ZdZdd ZdS )z8test_bagging_get_estimators_indices.<locals>.MyEstimatorz7An estimator which stores y indices information at fit.c                 S   s
   || _ d S rX   )_sample_indicesr]   r?   r?   rN   rD     s   
z<test_bagging_get_estimators_indices.<locals>.MyEstimator.fitN)re   rf   rg   rh   rD   r?   r?   r?   rN   MyEstimator  s    r@  r1   r   )r=   r>   r.   )r   randomRandomStaterandnaranger'   r   rD   r*   r   r?  r
  )global_random_seedrF   r_   r`   r@  r   r?   r?   rN   #test_bagging_get_estimators_indices  s   
rF  zbagging, expected_allow_nanr1   r9   c                 C   s   |   jj|ks
J dS )z*Check that bagging inherits allow_nan tag.N)__sklearn_tags__
input_tags	allow_nan)r  expected_allow_nanr?   r?   rN   test_bagging_allow_nan_tag  s   rK  )enable_metadata_routingmodelr   )r=   r>   c                 C      |  tjtj dS )zAMake sure that metadata routing works with non-default estimator.NrD   r@   rA   rB   rM  r?   r?   rN   "test_bagging_with_metadata_routing  s   rQ  zsub_estimator, caller, calleerE   rS   rR   c                 C   s   t ddgddgddgg}g d}dgd}}t }| |d}d	| d
 }	t||	ddd t|d}
|
|| t|
|t ddgddgddgg||d t|sVJ |D ]}t|||||d qXd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;   r1   r2   r.  )r1   r;   r   a)registryset__requestT)r   metadatar   r   )r_   r   rV  )objr   parentr   rV  N)r   r1  r$   r~   r   rD   r   r%   )sub_estimatorcallercalleer_   r`   r   rV  rS  r=   set_callee_requestr  r?   r?   rN   3test_metadata_routing_with_dynamic_method_selection  s0   

r]  c                 C   rN  )z^Make sure that we still can use an estimator that does not implement the
    metadata routing.NrO  rP  r?   r?   rN   -test_bagging_without_support_metadata_routing  s   r^  )r   )trh   	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,   rF   r@   permutationrB   r   permrA   r   rO   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  r  r  r'  r+  r9  r<  r=  rF  rK  rQ  r]  r^  r?   r?   r?   rN   <module>   s   (
!


)
8	*#%$),	

() 

		

*
