U
    Ovf|                     @   s  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	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 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,m-Z-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 e3dZ7e Z8e79e8j:j;Z<e8j=e< e8_=e8j:e< e8_:e Z>e79e>j:j;Z<e>j=e< e>_=e>j:e< e>_:dd Z?ej@Adee	e
gddddddddddddddddddgd d!d"d#gd$d% ZBd&d' ZCd(d) ZDG d*d+ d+eZEd,d- ZFd.d/ ZGd0d1 ZHd2d3 ZId4d5 ZJd6d7 ZKd8d9 ZLd:d; ZMd<d= ZNd>d? ZOd@dA ZPdBdC ZQG dDdE dEeZRdFdG ZSdvdIdJZTdKdL ZUdMdN ZVdOdP ZWdQdR ZXdSdT ZYdUdV ZZdWdX Z[dYdZ Z\d[d\ Z]d]d^ Z^d_d` Z_dadb Z`dcdd Zadedf Zbdgdh Zcej@Adiee0fee1fgdjdk Zdej@Adleegdmdn Zedodp Zfej@Adqeedrdsdfeedrdsdfee dfee. dfgdtdu ZgdS )wzE
Testing for the bagging ensemble module (sklearn.ensemble.bagging).
    )cycleproductN)
csc_matrix
csr_matrix)BaseEstimator)load_diabetes	load_irismake_hastie_10_2)DummyClassifierDummyRegressor)BaggingClassifierBaggingRegressorHistGradientBoostingClassifierHistGradientBoostingRegressor)SelectKBest)LogisticRegression
Perceptron)GridSearchCVParameterGridtrain_test_split)KNeighborsClassifierKNeighborsRegressor)make_pipeline)FunctionTransformerscale)SparseRandomProjection)SVCSVR)DecisionTreeClassifierDecisionTreeRegressor)check_random_state)assert_array_almost_equalassert_array_equalc            	      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f || dd|||| qtd S )Nr   random_state      ?      ?      TFmax_samplesmax_features	bootstrapbootstrap_features   Zmax_iter   )Z	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paramsr1    rA   G/tmp/pip-unpacked-wheel-qu3nn_q2/sklearn/ensemble/tests/test_bagging.pytest_classification4   sB      	
 rC   zsparse_format, params, methodr%   r0   Tr)   r&   r(   Fr+   r,   r-   r*   r,   r-   r8   predict_probapredict_log_probadecision_functionc                    s   G dd dt }td}tttjtj|d\}}}}| |}	| |}
tf |ddddd	||	|}t	|||
}tf |ddddd	|||}t	|||}t
|| t|	 d
d |jD }t fdd|D std S )Nc                       s    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                    s   t  || t|| _| S Nsuperr7   type
data_type_selfXy	__class__rA   rB   r7   r   s    
z1test_sparse_classification.<locals>.CustomSVC.fit__name__
__module____qualname____doc__r7   __classcell__rA   rA   rS   rB   	CustomSVCo   s   r[   r   r#   Zlinearovr)kerneldecision_function_shaper'   r1   r$   c                 S   s   g | ]
}|j qS rA   rN   .0irA   rA   rB   
<listcomp>   s     z.test_sparse_classification.<locals>.<listcomp>c                    s   g | ]}| kqS rA   rA   rb   tZsparse_typerA   rB   rd      s     )r   r    r   r   r3   r4   r5   r   r7   getattrr!   rM   estimators_allAssertionError)sparse_formatr@   methodr[   r9   r:   r;   r<   r=   X_train_sparseX_test_sparsesparse_classifiersparse_resultsZdense_classifierdense_resultstypesrA   rg   rB   test_sparse_classificationU   sB      
 
 
rt   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 ]0}|D ]&}t
f || d	|||| qrqjd S )
Nr   2   r#   r%   r&   TFr)   r_   )r    r   diabetesr4   r5   r   r   r   r   r   r   r7   r8   )r9   r:   r;   r<   r=   r>   r1   r@   rA   rA   rB   test_regression   s4      
 rw   c                     s2  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}ttfD ]}||}||}	|D ]}
tf | dd|
	||}|
|	}tf | dd|
	||
|}t| dd |jD }t|| t fdd|D s tt|| qqzd S )Nr   ru   r#   c                       s    e Zd ZdZ fddZ  ZS )z)test_sparse_regression.<locals>.CustomSVRrI   c                    s   t  || t|| _| S rJ   rK   rO   rS   rA   rB   r7      s    
z-test_sparse_regression.<locals>.CustomSVR.fitrU   rA   rA   rS   rB   	CustomSVR   s   rx   r%   r0   Tr)   r&   r(   FrD   rE   r'   r_   c                 S   s   g | ]
}|j qS rA   r`   ra   rA   rA   rB   rd      s     z*test_sparse_regression.<locals>.<listcomp>c                    s   g | ]}| kqS rA   rA   re   rg   rA   rB   rd      s     )r    r   rv   r4   r5   r   r   r   r   r7   r8   rM   ri   r!   rj   rk   )r9   r:   r;   r<   r=   rx   Zparameter_setsrl   rn   ro   r@   rp   rq   rr   rs   rA   rg   rB   test_sparse_regression   s\      


  
 
ry   c                   @   s   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_rO   rA   rA   rB   r7      s    zDummySizeEstimator.fitc                 C   s   t |jd S r{   )nponesr|   rP   rQ   rA   rA   rB   r8      s    zDummySizeEstimator.predictNrV   rW   rX   r7   r8   rA   rA   rA   rB   rz      s   rz   c                  C   s   t d} ttjtj| d\}}}}t ||}tt dd| d||}||||||ksft	tt dd| d||}||||||kst	tt
 dd||}g }|jD ]$}|j|jd kst	||j qtt|t|kst	d S )Nr   r#   r&   F)r1   r*   r,   r$   T)r1   r,   )r    r   rv   r4   r5   r   r7   r   scorerk   rz   ri   r}   r|   appendr   lenset)r9   r:   r;   r<   r=   r1   ensembleZtraining_hashrA   rA   rB   test_bootstrap_samples   sH         
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sBtqBtt dd| d||}|jD ]$}tjj	d t
|j	d kstqd S )Nr   r#   r&   F)r1   r+   r-   r$   r'   T)r    r   rv   r4   r5   r   r   r7   estimators_features_r|   r   uniquerk   )r9   r:   r;   r<   r=   r   featuresrA   rA   rB   test_bootstrap_features  s8       
" 
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 5 Q R X d S )
Nr   r#   ignore)divideinvalidr_   r'   )Zaxis   )r1   r$   r*   )r    r   r3   r4   r5   r   Zerrstater   r   r7   r!   sumrF   r   r   exprG   r   r9   r:   r;   r<   r=   r   rA   rA   rB   test_probability9  sL               r   c            	   
   C   s   t d} ttjtj| d\}}}}t t fD ]|}t|ddd| d||}|	||}t
||j dk sntd}tjt|d$ t|d	dd| d}||| W 5 Q R X q.d S )
Nr   r#   d   Tr1   r2   r,   	oob_scorer$   皙?{Some inputs do not have OOB scores. This probably means too few estimators were used to compute any reliable oob estimates.matchr'   )r    r   r3   r4   r5   r   r   r   r7   r   abs
oob_score_rk   pytestwarnsUserWarning)	r9   r:   r;   r<   r=   r1   clf
test_scorewarn_msgrA   rA   rB   test_oob_score_classification\  s<       r   c            	   	   C   s   t d} ttjtj| d\}}}}tt ddd| d||}|||}t	||j
 dk s`td}tjt|d& tt d	dd| d}||| W 5 Q R X d S )
Nr   r#   ru   Tr   r   r   r   r'   )r    r   rv   r4   r5   r   r   r7   r   r   r   rk   r   r   r   )	r9   r:   r;   r<   r=   r   r   r   ZregrrA   rA   rB   test_oob_score_regression  s:       r   c                  C   sf   t d} ttjtj| d\}}}}tt ddd| d||}t ||}t|	||	| d S )Nr   r#   r'   F)r1   r2   r,   r-   r$   )
r    r   rv   r4   r5   r   r   r7   r!   r8   )r9   r:   r;   r<   r=   Zclf1Zclf2rA   rA   rB   test_single_estimator  s$       r   c                  C   s2   t jt j } }t }tt|| |dr.td S )NrH   )r3   r4   r5   r   hasattrr   r7   rk   )rQ   rR   baserA   rA   rB   
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$   r'   r   r\   )r^   )r   r3   r4   r5   r   r   r7   rF   
set_paramsr!   r   rH   )r:   r;   r<   r=   r   y1y2y3Z
decisions1Z
decisions2Z
decisions3rA   rA   rB   test_parallel_classification  sb         


   

   


   
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   r'   r   r0   )r    r   rv   r4   r5   r   r   r7   r   r8   r!   )	r9   r:   r;   r<   r=   r   r   r   r   rA   rA   rB   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 )Nr'   r0   )r'   r0   )r2   Zestimator__CZroc_auc)Zscoring)r3   r4   r5   r   r   r   r7   )rQ   rR   
parametersrA   rA   rB   test_gridsearch  s    
r   c                  C   s0  t d} ttjtj| d\}}}}td ddd||}t|jt	sHt
tt	 ddd||}t|jt	spt
tt ddd||}t|jtst
ttjtj| d\}}}}td ddd||}t|jtst
tt ddd||}t|jtst
tt ddd||}t|jts,t
d S )Nr   r#   r   r   )r    r   r3   r4   r5   r   r7   
isinstanceZ
estimator_r   rk   r   rv   r   r   r   r   rA   rA   rB   test_estimator  sH             r   c                  C   sL   t ttddt dd} | tjtj t| d j	d d j
tsHtd S )Nr'   )kr0   )r+   r   )r   r   r   r   r7   r3   r4   r5   r   stepsr$   intrk   )r1   rA   rA   rB   test_bagging_with_pipeline;  s     r   c                   @   s   e Zd Zdd Zdd ZdS )DummyZeroEstimatorc                 C   s   t || _| S rJ   )r   r   classes_rO   rA   rA   rB   r7   D  s    zDummyZeroEstimator.fitc                 C   s   | j tj|jd td S )Nr   )dtype)r   r   zerosr|   r   r   rA   rA   rB   r8   H  s    zDummyZeroEstimator.predictNr   rA   rA   rA   rB   r   C  s   r   c               	   C   sj   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 5 Q R X d S )Nr   
   )size)Zsample_weight)r   r   r    r7   r3   r4   r5   r8   r   raises
ValueErrorrandintr|   )r1   r9   rA   rA   rB   1test_bagging_sample_weight_unsupported_but_passedL  s    
r   *   c                 C   s   t ddd\}}d }dD ]D}|d kr4t|| dd}n|j|d ||| t||kstqtd| d	d}||| td
d |D tdd |D kstd S )Nr.   r'   Z	n_samplesr$   )r   r   T)r2   r$   
warm_startr2   r   Fc                 S   s   g | ]
}|j qS rA   r#   rb   treerA   rA   rB   rd   n  s     z#test_warm_start.<locals>.<listcomp>c                 S   s   g | ]
}|j qS rA   r#   r   rA   rA   rB   rd   o  s     )r	   r   r   r7   r   rk   r   )r$   rQ   rR   clf_wsr2   Z	clf_no_wsrA   rA   rB   test_warm_startY  s*        r   c               	   C   sZ   t ddd\} }tddd}|| | |jdd tt || | W 5 Q R X d S )	Nr.   r'   r   r   T)r2   r   r(   r   )r	   r   r7   r   r   r   r   rQ   rR   r   rA   rA   rB   $test_warm_start_smaller_n_estimatorss  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 5 Q R X t||| d S )Nr.   r'   r   +   r#   r   TS   r2   r   r$   r&   z;Warm-start fitting without increasing n_estimators does notr   )	r	   r   r   r7   r8   r   r   r   r"   )	rQ   rR   r:   r;   r<   r=   r   Zy_predr   rA   rA   rB   "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 )Nr.   r'   r   r   r#   r   TiE  r   r   r   F)r	   r   r   r7   r   r8   r!   )
rQ   rR   r:   r;   r<   r=   r   r   r   r   rA   rA   rB   test_warm_start_equivalence  s    

r   c               	   C   sD   t ddd\} }tdddd}tt || | W 5 Q R X d S )Nr.   r'   r   r   T)r2   r   r   )r	   r   r   r   r   r7   r   rA   rA   rB   $test_warm_start_with_oob_score_fails  s    r   c               	   C   sh   t ddd\} }tddd}|| | |jdddd	 || | tt t|d
 W 5 Q R X d S )Nr   r'   r   r   T)r2   r   Fr   )r   r   r2   r   )r	   r   r7   r   r   r   AttributeErrorrh   r   rA   rA   rB   $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sDtd S )N   r'   r   r%   T)r*   r+   r   r$   )r	   r   r   r7   r   rk   rQ   rR   baggingrA   rA   rB   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sVtt|d t| d ksrt|d j	j
d	kstd}|| }|| }|| }	| | d d |f }
|| }|	j}|	|
| |	j}t|| d S )
Nr   r'   r   r%   F)r*   r+   r$   r,   r   r0   rc   )r	   r   r   r7   estimators_samples_r   ri   r   rk   r   kindcoef_r!   )rQ   rR   r   Zestimators_samplesZestimators_featuresr?   Zestimator_indexZestimator_samplesZestimator_featuresr1   r:   r<   Z
orig_coefsZ	new_coefsrA   rA   rB   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 )Nr0   )Zn_componentsr%   r   )r1   r*   r$   r   r'   )r   r4   r5   r   r   r   r   r7   ri   r   r   copyr   r   r"   )r3   rQ   rR   Zbase_pipeliner   Zpipeline_estimator_coefr1   Zestimator_sampleZestimator_featurer:   r<   rA   rA   rB   %test_estimators_samples_deterministic  s      


r   c                  C   sH   d} t d|  dd\}}tt | ddd}||| |j| ksDtd S )Nr   r0   r'   r   r%   )r*   r+   r$   )r	   r   r   r7   Z_max_samplesrk   )r*   rQ   rR   r   rA   rA   rB   test_max_samples_consistency  s    r   c                  C   s   d} dgdgdggd }dddgd }dddgd }dddgd }t d	| d
||j}t d	| d
||j}t d	| d
||j}||g||gkstd S )Nr   r   r   r'   ABCr0   T)r   r$   )r   r7   r   rk   )r$   rQ   ZY1ZY2ZY3x1Zx2Zx3rA   rA   rB   !test_set_oob_score_label_encoding  s*       r   c                 C   s"   | j ddd} d| t|  < | S )NfloatT)r   r   )Zastyper   isfinite)rQ   rA   rA   rB   replace1  s    r   c               
   C   s8  t dddgdd dgdt jdgdt jdgdt j dgg} t dddddgt dddgdddgdddgdddgdddggg}|D ]}t }ttt|}|| |	|  t
|}|| |	| }|j|jkstt }t|}tt || | W 5 Q R X t
|}tt || | W 5 Q R X qd S )Nr'   r   r   r0      	      )r   arraynaninfr   r   r   r   r7   r8   r   r|   rk   r   r   r   )rQ   Zy_valuesrR   Z	regressorpipelineZbagging_regressory_hatrA   rA   rB   *test_bagging_regressor_with_missing_inputs7  s@    


r   c               	   C   s  t dddgdd dgdt jdgdt jdgdt j dgg} t dddddg}t }ttt|}|| |	|  t
|}|| | |	| }|j|jkst||  ||  t }t|}tt || | W 5 Q R X t
|}tt || | W 5 Q R X d S )Nr'   r   r   r0   r   )r   r   r   r   r   r   r   r   r7   r8   r   r|   rk   rG   rF   r   r   r   )rQ   rR   
classifierr   Zbagging_classifierr   rA   rA   rB   +test_bagging_classifier_with_missing_inputs`  s2    

	


r   c                  C   sD   t ddgddgg} t ddg}tt ddd}|| | d S )Nr'   r0   r   r(   r   g333333?)r+   r$   )r   r   r   r   r7   r   rA   rA   rB   test_bagging_small_max_features  s    r   c                  C   sj   t jd} | dd}t d}G dd dt}t| ddd}||| t|j	d j
|jd  d S )Nr      r(   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 rJ   )_sample_indicesrO   rA   rA   rB   r7     s    z<test_bagging_get_estimators_indices.<locals>.MyEstimator.fitN)rV   rW   rX   rY   r7   rA   rA   rA   rB   MyEstimator  s   r   r'   )r1   r2   r$   )r   randomZRandomStateZrandnZaranger   r   r7   r"   ri   r   r   )r9   rQ   rR   r   r   rA   rA   rB   #test_bagging_get_estimators_indices  s    
r   zBagging, Estimatorc              	   C   s`   t ddgddgg}t ddg}| | dd}d}tjt|d	 ||| W 5 Q R X d S )
Nr'   r0   r   r(   r   r   )base_estimatorr2   V`base_estimator` was renamed to `estimator` in version 1.2 and will be removed in 1.4.r   )r   r   r   r   FutureWarningr7   )BaggingZ	EstimatorrQ   rR   modelr   rA   rA   rB   'test_base_estimator_argument_deprecated  s    r  r  c              	   C   s^   t ddgddgg}t ddg}|  }||| d}tjt|d |j W 5 Q R X d S )Nr'   r0   r   r(   r   zoAttribute `base_estimator_` was deprecated in version 1.2 and will be removed in 1.4. Use `estimator_` instead.r   )r   r   r7   r   r   r  Zbase_estimator_)r  rQ   rR   r  r   rA   rA   rB   'test_base_estimator_property_deprecated  s    r  c               	   C   sp   t  } | j| j }}tt d}t|ds.td}tjt	|d |
|||}W 5 Q R X |jdksltdS )zSCheck that `BaggingClassifier` delegate to classifier with
    `decision_function`.)r   rH   r   r   )   r   N)r   r4   r5   r   r   r   rk   r   r   r  r7   rH   r|   )r3   rQ   rR   r   r   Z
y_decisionrA   rA   rB   4test_deprecated_base_estimator_has_decision_function  s    r  zbagging, expected_allow_nanr'   r/   c                 C   s   |   d |kstdS )z*Check that bagging inherits allow_nan tag.	allow_nanN)Z	_get_tagsrk   )r   Zexpected_allow_nanrA   rA   rB   test_bagging_allow_nan_tag  s    r	  )r   )hrY   	itertoolsr   r   r~   Znumpyr   r   Zscipy.sparser   r   Zsklearn.baser   Zsklearn.datasetsr   r   r	   Zsklearn.dummyr
   r   Zsklearn.ensembler   r   r   r   Zsklearn.feature_selectionr   Zsklearn.linear_modelr   r   Zsklearn.model_selectionr   r   r   Zsklearn.neighborsr   r   Zsklearn.pipeliner   Zsklearn.preprocessingr   r   Zsklearn.random_projectionr   Zsklearn.svmr   r   Zsklearn.treer   r   Zsklearn.utilsr    Zsklearn.utils._testingr!   r"   r9   r3   Zpermutationr5   r   permr4   rv   rC   markZparametrizert   rw   ry   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r	  rA   rA   rA   rB   <module>   s   !



)9	*#%$),	

() 


	