U
    9%e|                     @   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   )	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paramsr2    rB   b/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sklearn/ensemble/tests/test_bagging.pytest_classification4   sB      	
 rD   zsparse_format, params, methodr%   r0   Tr)   r&   r(   Fr+   r,   r-   r*   r,   r-   r9   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superr8   type
data_type_selfXy	__class__rB   rC   r8   r   s    
z1test_sparse_classification.<locals>.CustomSVC.fit__name__
__module____qualname____doc__r8   __classcell__rB   rB   rT   rC   	CustomSVCo   s   r\   r   r#   Zlinearovr)Zkerneldecision_function_shaper'   r2   r$   c                 S   s   g | ]
}|j qS rB   rO   .0irB   rB   rC   
<listcomp>   s     z.test_sparse_classification.<locals>.<listcomp>c                    s   g | ]}| kqS rB   rB   rb   tZsparse_typerB   rC   rd      s     )r   r    r   r   r4   r5   r6   r   r8   getattrr!   rN   estimators_allAssertionError)sparse_formatrA   methodr\   r:   r;   r<   r=   r>   X_train_sparseX_test_sparsesparse_classifiersparse_resultsZdense_classifierdense_resultstypesrB   rg   rC   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   diabetesr5   r6   r   r   r   r   r   r   r8   r9   )r:   r;   r<   r=   r>   r?   r2   rA   rB   rB   rC   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>.CustomSVRrJ   c                    s   t  || t|| _| S rK   rL   rP   rT   rB   rC   r8      s    
z-test_sparse_regression.<locals>.CustomSVR.fitrV   rB   rB   rT   rC   	CustomSVR   s   rx   r%   r0   Tr)   r&   r(   FrE   rF   r'   r_   c                 S   s   g | ]
}|j qS rB   r`   ra   rB   rB   rC   rd      s     z*test_sparse_regression.<locals>.<listcomp>c                    s   g | ]}| kqS rB   rB   re   rg   rB   rC   rd      s     )r    r   rv   r5   r6   r   r   r   r   r8   r9   rN   ri   r!   rj   rk   )r:   r;   r<   r=   r>   rx   Zparameter_setsrl   rn   ro   rA   rp   rq   rr   rs   rB   rg   rC   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_rP   rB   rB   rC   r8      s    zDummySizeEstimator.fitc                 C   s   t |jd S r{   )nponesr|   rQ   rR   rB   rB   rC   r9      s    zDummySizeEstimator.predictNrW   rX   rY   r8   r9   rB   rB   rB   rC   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)r2   r*   r,   r$   T)r2   r,   )r    r   rv   r5   r6   r   r8   r   scorerk   rz   ri   r}   r|   appendr   lenset)r:   r;   r<   r=   r>   r2   ensembleZtraining_hashrB   rB   rC   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)r2   r+   r-   r$   r'   T)r    r   rv   r5   r6   r   r   r8   estimators_features_r|   r   uniquerk   )r:   r;   r<   r=   r>   r   featuresrB   rB   rC   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   )r2   r$   r*   )r    r   r4   r5   r6   r   Zerrstater   r   r8   r!   sumrG   r   r   exprH   r   r:   r;   r<   r=   r>   r   rB   rB   rC   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r2   r3   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   r4   r5   r6   r   r   r   r8   r   abs
oob_score_rk   pytestwarnsUserWarning)	r:   r;   r<   r=   r>   r2   clf
test_scorewarn_msgrB   rB   rC   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   r5   r6   r   r   r8   r   r   r   rk   r   r   r   )	r:   r;   r<   r=   r>   r   r   r   ZregrrB   rB   rC   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)r2   r3   r,   r-   r$   )
r    r   rv   r5   r6   r   r   r8   r!   r9   )r:   r;   r<   r=   r>   Zclf1Zclf2rB   rB   rC   test_single_estimator  s$       r   c                  C   s2   t jt j } }t }tt|| |dr.td S )NrI   )r4   r5   r6   r   hasattrr   r8   rk   )rR   rS   baserB   rB   rC   
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   r4   r5   r6   r   r   r8   rG   
set_paramsr!   r   rI   )r;   r<   r=   r>   r   y1y2y3Z
decisions1Z
decisions2Z
decisions3rB   rB   rC   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   r5   r6   r   r   r8   r   r9   r!   )	r:   r;   r<   r=   r>   r   r   r   r   rB   rB   rC   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   )r3   Zestimator__CZroc_auc)Zscoring)r4   r5   r6   r   r   r   r8   )rR   rS   
parametersrB   rB   rC   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   r4   r5   r6   r   r8   
isinstanceZ
estimator_r   rk   r   rv   r   r   r   r   rB   rB   rC   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   r8   r4   r5   r6   r   stepsr$   intrk   )r2   rB   rB   rC   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 rK   )r   r   classes_rP   rB   rB   rC   r8   D  s    zDummyZeroEstimator.fitc                 C   s   | j tj|jd td S )Nr   )dtype)r   r   Zzerosr|   r   r   rB   rB   rC   r9   H  s    zDummyZeroEstimator.predictNr   rB   rB   rB   rC   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    r8   r4   r5   r6   r9   r   raises
ValueErrorrandintr|   )r2   r:   rB   rB   rC   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)r3   r$   
warm_startr3   r   Fc                 S   s   g | ]
}|j qS rB   r#   rb   treerB   rB   rC   rd   n  s     z#test_warm_start.<locals>.<listcomp>c                 S   s   g | ]
}|j qS rB   r#   r   rB   rB   rC   rd   o  s     )r	   r   r   r8   r   rk   r   )r$   rR   rS   clf_wsr3   Z	clf_no_wsrB   rB   rC   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)r3   r   r(   r   )r	   r   r8   r   r   r   r   rR   rS   r   rB   rB   rC   $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   r3   r   r$   r&   z;Warm-start fitting without increasing n_estimators does notr   )	r	   r   r   r8   r9   r   r   r   r"   )	rR   rS   r;   r<   r=   r>   r   Zy_predr   rB   rB   rC   "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   r8   r   r9   r!   )
rR   rS   r;   r<   r=   r>   r   r   r   r   rB   rB   rC   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)r3   r   r   )r	   r   r   r   r   r8   r   rB   rB   rC   $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)r3   r   Fr   )r   r   r3   r   )r	   r   r8   r   r   r   AttributeErrorrh   r   rB   rB   rC   $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   r8   r   rk   rR   rS   baggingrB   rB   rC   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   r8   estimators_samples_r   ri   r   rk   r   kindcoef_r!   )rR   rS   r   Zestimators_samplesZestimators_featuresr@   Zestimator_indexZestimator_samplesZestimator_featuresr2   r;   r=   Z
orig_coefsZ	new_coefsrB   rB   rC   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   )r2   r*   r$   r   r'   )r   r5   r6   r   r   r   r   r8   ri   r   r   copyr   r   r"   )r4   rR   rS   Zbase_pipeliner   Zpipeline_estimator_coefr2   Zestimator_sampleZestimator_featurer;   r=   rB   rB   rC   %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   r8   Z_max_samplesrk   )r*   rR   rS   r   rB   rB   rC   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   r8   r   rk   )r$   rR   ZY1ZY2ZY3x1Zx2Zx3rB   rB   rC   !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)rR   rB   rB   rC   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   r8   r9   r   r|   rk   r   r   r   )rR   Zy_valuesrS   Z	regressorpipelineZbagging_regressory_hatrB   rB   rC   *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   r8   r9   r   r|   rk   rH   rG   r   r   r   )rR   rS   
classifierr   Zbagging_classifierr   rB   rB   rC   +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   r8   r   rB   rB   rC   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 rK   )_sample_indicesrP   rB   rB   rC   r8     s    z<test_bagging_get_estimators_indices.<locals>.MyEstimator.fitN)rW   rX   rY   rZ   r8   rB   rB   rB   rC   MyEstimator  s   r   r'   )r2   r3   r$   )r   randomZRandomStateZrandnZaranger   r   r8   r"   ri   r   r   )r:   rR   rS   r   r   rB   rB   rC   #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_estimatorr3   V`base_estimator` was renamed to `estimator` in version 1.2 and will be removed in 1.4.r   )r   r   r   r   FutureWarningr8   )BaggingZ	EstimatorrR   rS   modelr   rB   rB   rC   '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   r8   r   r   r   Zbase_estimator_)r  rR   rS   r  r   rB   rB   rC   '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   rI   r   r   )   r   N)r   r5   r6   r   r   r   rk   r   r   r   r8   rI   r|   )r4   rR   rS   r   r   Z
y_decisionrB   rB   rC   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_nanrB   rB   rC   test_bagging_allow_nan_tag  s    r  )r   )hrZ   	itertoolsr   r   r~   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"   r:   r4   Zpermutationr6   r   permr5   rv   rD   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  rB   rB   rB   rC   <module>   s   !



)9	*#%$),	

() 


	