U
    9%e                     @   s
  U d 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	 ddl
mZmZ ddlmZ ddlZddlZddlZddlmZmZmZ dd	lmZ ddlZdd
lmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@ ddgddgddgddgddgddggZAddddddgZBddgddgddggZCdddgZDejdddddddd\ZEZFeG ZHe@dZIeIJeHjKjLZMeHjNeM eH_NeHjKeM eH_KejOdddd \ZPZQejRd!dd"\ZSZTeSUejVZSejWX d jYZZe"e$d#Z[e#e%d$Z\d%e&iZ]e^ Z_ee`ef ead&< e_be[ e_be\ e_be] e[c Zdee`ef ead'< edbe\ d(d) Zeejfgd*e[d+d, Zhd-d. Ziejfgd*e[ejfgd/d0d1d2 Zjd3d4 Zkejfgd*e\ejfgd/d5d6d7 Zld8d9 Zmejfgd/d:d;d< Znd=d> Zoejfgd*e\d?d@ ZpdAdB Zqejfgd*e[dCdD ZrdEdF ZsejfgdGejtejVfejfgdHeue	e[dIdJge	e\dKdLdMgdNdO ZvdPdQ Zwejfgd*e_dRdS ZxejfgdTe[y ejfgdUdVdWdXgejfgdYejdZddd[dejd]dd^dd_deHjNeHjKd d d`fejzdZdd"dgejfgdbdcee+dddegdfdg Z{ejfgdhe\y ejfgdUdVdWdXgejfgdiejOdddddjdejOdddddjdgejfgdbdce*gdmdn Z|ejfgdoedy dpdq Z}ejfgdoedy ejfgdreHjNeHjKdcddsdtfeHjNeIj~ddueHjNjd dfdvdcdcdsdwfgdxdy Zejfgdbdcdgdzd{ Zd|d} Zejfgd*e[d~d Zdd Zejfgd*eddd Zdd Zejfgd*eddd Zdd Zejfgd*eddd Zejfgd*e[dd Zdd Zejfgd*e[dd Zdd Zdd Ze;dd Zdd Zdd Zdd Zdd Zejfgd*e_dd Zdd Zejfgd*e_dd Zdd Zejfgd*e_dd Zdd Zejfgd*e_dd Zdd Zejfgd*e_ejfgdeeefdd Zdd Zejfgd*edejfgdGejtejVfdd Ze;dd Zejfgd*e_dd Zdd Zejfgd*e[dd Zdd Zejfgd*e[dd ZddĄ Zejfgd*e[ddƄ ZdddɄZejfgd*e_dd˄ Zdd̈́ Zejfgd*e_ddτ Zddф Zejfgd*e_ddӄ ZddՄ Zejfgd*e_ddׄ Zddل Zejfgd*edddۄ Zejfgd*eddd݄ ZdddZdd Zejfgd*eddd Zdd Zdd ZG dd deZZede e<dd Zdd Zdd Zejfgd*eddd Zejfgd*eddd Zejfgd*e\dd Zejfgd*e[dd Zdd Zejfgde$e%gdd Zejfgde\d d Zdd Zejfgd*e_dd ZĐdd Zejfgdd	dgd
d ZdS (  z:
Testing for the forest module (sklearn.ensemble.forest).
    N)defaultdict)partial)combinationsproduct)AnyDict)patch)
coo_matrix
csc_matrix
csr_matrix)comb)datasets)make_classification)TruncatedSVD)DummyRegressor)ExtraTreesClassifierExtraTreesRegressorRandomForestClassifierRandomForestRegressorRandomTreesEmbedding)NotFittedError)explained_variance_scoref1_scoremean_poisson_deviancemean_squared_error)GridSearchCVcross_val_scoretrain_test_split)	LinearSVC)SPARSE_SPLITTERS)_convert_containerassert_almost_equalassert_array_almost_equalassert_array_equalignore_warningsskip_if_no_parallel)Parallel)check_random_state           
   F)	n_samples
n_featuresn_informativeZn_redundantZ
n_repeatedshufflerandom_stater/   r0   r3      r/   r3   )r   r   )r   r   r   FOREST_ESTIMATORSFOREST_CLASSIFIERS_REGRESSORSc                 C   s   t |  }|ddd}|tt t|tt dt|ks@t	|dddd}|tt t|tt dt|kszt	|
t}|jtt|jfkst	dS )z&Check classification on a toy dataset.r.   r*   n_estimatorsr3   )r:   max_featuresr3   N)FOREST_CLASSIFIERSfitXyr#   predictTtrue_resultlenAssertionErrorapplyshaper:   )nameForestClassifierclfZleaf_indices rJ   a/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sklearn/ensemble/tests/test_forest.pycheck_classification_toyv   s    
rL   rG   c                 C   s   t |  d S N)rL   rG   rJ   rJ   rK   test_classification_toy   s    rO   c                 C   s   t |  }|d|dd}|tjtj |tjtj}|dksNtd||f |d|ddd}|tjtj |tjtj}|dkstd||f d S )	Nr.   r*   r:   	criterionr3   ?z'Failed with criterion %s and score = %fr+   r:   rQ   r;   r3         ?)r<   r=   irisdatatargetscorerD   )rG   rQ   rH   rI   rX   rJ   rJ   rK   check_iris_criterion   s       rY   rQ   )ginilog_lossc                 C   s   t | | d S rM   )rY   rG   rQ   rJ   rJ   rK   	test_iris   s    r]   c                 C   s   t |  }|d|dd}|tt |tt}|dksFtd||f |d|ddd}|tt |tt}|dkstd	||f d S )
N   r*   rP   g(\?z:Failed with max_features=None, criterion %s and score = %f   rS   gq=
ףp?z7Failed with max_features=6, criterion %s and score = %f)FOREST_REGRESSORSr=   X_regy_regrX   rD   )rG   rQ   ForestRegressorregrX   rJ   rJ   rK   check_regression_criterion   s.       re   )squared_errorabsolute_errorfriedman_msec                 C   s   t | | d S rM   )re   r\   rJ   rJ   rK   test_regression   s    ri   c                  C   sF  t jd} d\}}}tj|| || d}| jdd|dt j|dd }| jt || d	}t	|||| d
\}}}	}
t
ddd| d}t
ddd| d}|||	 |||	 tdd||	}||	df||
dffD ]l\}}}t|||}t|t ||dd}t|||}|dkr0||k s0t|d| k stqdS )zTest that random forest with poisson criterion performs better than
    mse for a poisson target.

    There is a similar test for DecisionTreeRegressor.
    *   r-   r-   r.   r4   r(   r+   lowhighsizer   ZaxisZlam	test_sizer3   poissonr.   sqrt)rQ   min_samples_leafr;   r3   rf   mean)Zstrategytraintestgư>Ng?)nprandomRandomStater   make_low_rank_matrixuniformmaxrt   expr   r   r=   r   r   r@   ZcliprD   )rngn_trainn_testr0   r>   coefr?   X_trainX_testy_trainy_testZ
forest_poiZ
forest_msedummyZ	data_nameZ
metric_poiZ
metric_mseZmetric_dummyrJ   rJ   rK   test_poisson_vs_mse   sP    
         
r   )rt   rf   c           	      C   s   t jd}d\}}}tj|| ||d}|jdd|dt j|dd }|jt || d	}t	| d
d|d}|
|| t ||tt |kstdS )z9 "Test that sum(y_pred)==sum(y_true) on the training set.rj   rk   r4   r(   r+   rl   r   rp   rq   r.   F)rQ   r:   	bootstrapr3   N)rz   r{   r|   r   r}   r~   r   rt   r   r   r=   sumr@   pytestapproxrD   )	rQ   r   r   r   r0   r>   r   r?   rd   rJ   rJ   rK   #test_balance_property_random_forest   s"    
     r   c                 C   sj   t |  dd}t|drtt|dr*t|dddgdd	d
ggddg t|drXtt|drftd S )Nr   r3   classes_
n_classes_r*   r+   r,      r^   r_   )r`   hasattrrD   r=   )rG   rrJ   rJ   rK   check_regressor_attributes  s     r   c                 C   s   t |  d S rM   )r   rN   rJ   rJ   rK   test_regressor_attributes  s    r   c              	   C   s   t |  }tjddp |ddddd}|tjtj ttj|	tjddt
tjjd  t|	tjt|tj W 5 Q R X d S )Nignoredivider.   r*   )r:   r3   r;   	max_depthrp   r   )r<   rz   errstater=   rU   rV   rW   r"   r   predict_probaonesrF   r   predict_log_proba)rG   rH   rI   rJ   rJ   rK   check_probability  s"        
 r   c                 C   s   t |  d S rM   )r   rN   rJ   rJ   rK   test_probability/  s    r   c                 C   sH  t j|dd}tj|dd}t|  }|d|dd}||| |j}t|dk}	|jd dksdt	|	dkspt	t
|d d dkst	|j}|jdd	 |j}
t||
 tdd
dt|}|dd|d}|j|||d |j}t
|dkst	dD ]F}|dd|d}|j|||| d |j}t||  |k st	qd S )NFcopyr.   r   rP   皙?r,   r+   n_jobsr*   )r:   r3   rQ   sample_weight        )rT   d   )X_largeastypey_larger7   r=   feature_importances_rz   r   rF   rD   all
set_paramsr"   r'   randintrC   absrw   )rG   rQ   dtype	tolerancer>   r?   ForestEstimatorestimportancesZn_importantZimportances_parallelr   scaleZimportances_bisrJ   rJ   rK   check_importances4  s0    
r   r   zname, criterionrZ   r[   rf   rh   rg   c                 C   s*   d}|t kr|dkrd}t||| | d S )N{Gz?rg   g?)r`   r   )r   rG   rQ   r   rJ   rJ   rK   test_importancesZ  s    	r   c            	         s  dd  dd  fdd} t ddddddddgdddddddd	gdddddddd
gddddddddgddddddddgddddddddgddddddddgddddddddgddddddddgddddddddgg
}t j|d d d df td|d d df  }}|jd }t |}t|D ]}| |||||< q(tddddd||}tdd |j	D |j
 }t|t| t ||  dk std S )Nc                 S   s*   | dk s| |krdS t t|t| ddS )Nr   T)exact)r   int)knrJ   rJ   rK   binomialn  s    z-test_importances_asymptotic.<locals>.binomialc                 S   sF   t | }d}t| D ]*}d| | }|dkr||t| 8 }q|S )Nr         ?r   )rC   rz   bincountlog2)Zsamplesr/   entropycountprJ   rJ   rK   r   q  s    z,test_importances_asymptotic.<locals>.entropyc              
      sf  j \}}tt|}||  fddt|D d}t|D ]}d||||   }t||D ] t fddt|D  D ]}	tj|td}
t|D ]$}|
d d  | f |	| kM }
q|
d d f ||
  }}t	|dkrg }|  D ](}|d d | f |k}|
||   q||d |  |tfdd|D   7 }qqhqB|S )	Nc                    s"   g | ]}t  d d |f qS rM   )rz   unique).0i)r>   rJ   rK   
<listcomp>  s     zGtest_importances_asymptotic.<locals>.mdi_importance.<locals>.<listcomp>r   r   c                    s   g | ]} |  qS rJ   rJ   )r   j)BvaluesrJ   rK   r     s     r   r   c                    s    g | ]} |t |  qS rJ   )rC   )r   c)r   n_samples_brJ   rK   r     s   )rF   listrangepopr   r   rz   r   boolrC   appendr   )ZX_mr>   r?   r/   r0   featuresimpr   r   bZmask_br   ZX_Zy_childrenxiZmask_xir   r   )r   r>   r   r   rK   mdi_importance|  sB    

 "

z3test_importances_asymptotic.<locals>.mdi_importancer   r*   r+   r,   r   r^   r_         	   r   r-   r[   )r:   r;   rQ   r3   c                 s   s   | ]}|j jd dV  qdS )F)	normalizeN)tree_Zcompute_feature_importancesr   treerJ   rJ   rK   	<genexpr>  s   z.test_importances_asymptotic.<locals>.<genexpr>r   )rz   arrayr   rF   zerosr   r   r=   r   estimators_r:   r!   r   rw   rD   )	r   rV   r>   r?   r0   Ztrue_importancesr   rI   r   rJ   r   rK   test_importances_asymptotici  sL    00

    	r   c              	   C   s8   d | }tjt|d tt|   d W 5 Q R X d S )NzfThis {} instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.matchr   )formatr   raisesr   getattrr7   )rG   err_msgrJ   rJ   rK   !test_unfitted_feature_importances  s    r   rH   X_typer   Z
sparse_csrZ
sparse_csczX, y, lower_bound_accuracyi,  )r/   	n_classesr3   rR     r_   )r/   r   r1   r3   ?
ףp=
?	oob_scoreTmicro)Zaveragec                 C   s2  t ||d}t||ddd\}}}}	| dd|dd}
t|
drBtt|
d	rPt|
|| t|rv||	|
|}n|
||	}|
j|kstt	||
j d
kstt|
dstt|
drtt|
d	st|j
dkr|jd tt|f}n*|jd tt|dddf |jd f}|
jj|ks.tdS )z5Check that OOB score is close to score on a test set.Zconstructor_namerT   r   rr   (   Tr:   r   r   r3   
oob_score_oob_decision_function_r   oob_prediction_r*   N)r    r   r   rD   r=   callabler@   rX   r   r   ndimrF   rC   setr   )rH   r>   r?   r   Zlower_bound_accuracyr   r   r   r   r   
classifier
test_scoreexpected_shaperJ   rJ   rK   test_forest_classifier_oob  s8    
*r  rc   zX, y, lower_bound_r2)r/   r0   	n_targetsr3   ffffff?皙?c                 C   s  t ||d}t||ddd\}}}}	| dd|dd}
t|
drBtt|
d	rPt|
|| t|rv||	|
|}n|
||	}|
j|kstt	||
j d
kstt|
dstt|
d	stt|
drt|j
dkr|jd f}n|jd |j
f}|
jj|ks
tdS )z\Check that forest-based regressor provide an OOB score close to the
    score on a test set.r   rT   r   rr   2   Tr   r   r   r   r   r*   N)r    r   r   rD   r=   r   r@   rX   r   r   r   rF   r   )rc   r>   r?   r   Zlower_bound_r2r   r   r   r   r   Z	regressorr  r  rJ   rJ   rK   test_forest_regressor_oob$  s8    
r  r   c              	   C   s>   | ddddd}t jtdd |tjtj W 5 Q R X dS )zfCheck that a warning is raised when not enough estimator and the OOB
    estimates will be inaccurate.r*   Tr   )r:   r   r   r3   z"Some inputs do not have OOB scoresr   N)r   warnsUserWarningr=   rU   rV   rW   )r   	estimatorrJ   rJ   rK   test_forest_oob_warning`  s    r  zX, y, params, err_msg)r   r   z6Out of bag estimation only available if bootstrap=Truer^   rl   z:The type of target cannot be used to compute OOB estimatesc              	   C   s4   | f |}t jt|d ||| W 5 Q R X d S )Nr   )r   r   
ValueErrorr=   )r   r>   r?   paramsr   r  rJ   rJ   rK   test_forest_oob_errorn  s    
r  c              	   C   sP   t jtdd t| d W 5 Q R X t jtdd t tt W 5 Q R X d S )Nz"got an unexpected keyword argumentr   r   zOOB score not supported)r   r   	TypeErrorr   NotImplementedError_set_oob_score_and_attributesr>   r?   r  rJ   rJ   rK   +test_random_trees_embedding_raise_error_oob  s    r  c                 C   s.   t |   }t|ddd}|tjtj d S )Nr*   r+   )r:   r   )r<   r   r=   rU   rV   rW   )rG   forestrI   rJ   rJ   rK   check_gridsearch  s    
r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   test_gridsearch  s    r  c                 C   sn   t |  }|dddd}||| t|dks2t|jdd ||}|jdd ||}t||d dS )	z-Check parallel computations in classificationr.   r,   r   r:   r   r3   r*   r   r+   N)r7   r=   rC   rD   r   r@   r"   )rG   r>   r?   r   r  y1y2rJ   rJ   rK   check_parallel  s    

r  c                 C   s6   | t krtj}tj}n| tkr&t}t}t| || d S rM   )r<   rU   rV   rW   r`   ra   rb   r  rG   r>   r?   rJ   rJ   rK   test_parallel  s    r  c           	      C   sl   t |  }|dd}||| |||}t|}t|}t||jksPt|||}||kshtd S )Nr   r   )	r7   r=   rX   pickledumpsloadstype	__class__rD   )	rG   r>   r?   r   objrX   Zpickle_objectobj2Zscore2rJ   rJ   rK   check_pickle  s    


r&  c                 C   sJ   | t krtj}tj}n| tkr&t}t}t| |d d d |d d d  d S )Nr+   )r<   rU   rV   rW   r`   ra   rb   r&  r  rJ   rJ   rK   test_pickle  s    r'  c           	      C   s  ddgddgddgddgddgddgddgddgddgddgddgddgg}ddgddgddgddgddgddgddgddgddgddgddgddgg}ddgddgddgddgg}ddgddgddgddgg}t |  ddd}||||}t|| | tkrtjd	d
 ||}t|dks0t	|d j
dksDt	|d j
dksXt	||}t|dkstt	|d j
dkst	|d j
dkst	W 5 Q R X d S )Nr(   r)   r*   r+   r   r,   Fr3   r   r   r   r   r+   r   r   )r7   r=   r@   r"   r<   rz   r   r   rC   rD   rF   r   	rG   r   r   r   r   r   Zy_predZprobaZ	log_probarJ   rJ   rK   check_multioutput  sR    



r,  c                 C   s   t |  d S rM   )r,  rN   rJ   rJ   rK   test_multioutput  s    r-  c           	      C   s  ddgddgddgddgddgddgddgddgddgddgddgddgg}ddgddgddgddgddgddgddgddgddgdd	gdd	gdd	gg}ddgddgddgddgg}ddgddgddgdd	gg}t |  d
dd}||||}t|| tjdd ||}t|dks&t|d
 j	dks:t|d j	dksNt|
|}t|dksjt|d
 j	dks~t|d j	dkstW 5 Q R X d S )Nr(   r)   r*   r+   redbluegreenpurpleyellowr   Fr(  r   r   r)  r*  )r7   r=   r@   r#   rz   r   r   rC   rD   rF   r   r+  rJ   rJ   rK   test_multioutput_string  sX    


r3  c                 C   s   t |  }|ddtt}|jdks(tt|jddg t	tt
td fj}|ddt|}t|jddg t|jddgddgg d S )Nr   r   r+   r)   r*   r(   )r<   r=   r>   r?   r   rD   r#   r   rz   vstackr   rA   )rG   rH   rI   _yrJ   rJ   rK   check_classes_shape@  s    r6  c                 C   s   t |  d S rM   )r6  rN   rJ   rJ   rK   test_classes_shapeR  s    r7  c                  C   s<   t ddd} tjdd\}}| |}t|tjks8td S )Nr.   F)r:   sparse_outputrT   factor)r   r   make_circlesfit_transformr"  rz   ZndarrayrD   )hasherr>   r?   X_transformedrJ   rJ   rK   test_random_trees_dense_typeW  s    
r?  c                  C   sR   t dddd} t dddd}tjdd\}}| |}||}t| | d S )Nr.   Fr   )r:   r8  r3   TrT   r9  )r   r   r;  r<  r#   toarray)Zhasher_denseZhasher_sparser>   r?   ZX_transformed_denseX_transformed_sparserJ   rJ   rK   test_random_trees_dense_equald  s        

rB  c                  C   s   t ddd} tjdd\}}| |}t ddd} t| || |  |jd |jd ksht	t|j
dd| j tdd	}||}t }||| |||d
kst	d S )N   r*   r9   rT   r9  r   rp   r+   )Zn_componentsr   )r   r   r;  r<  r#   r=   	transformr@  rF   rD   r   r:   r   r   rX   )r=  r>   r?   r>  ZsvdZ	X_reducedZ
linear_clfrJ   rJ   rK   test_random_hasherx  s    


rE  c                  C   sJ   t jdd\} }tddd}|| }|t| }t| |  d S )Nr   r   rC  r*   r9   )r   make_multilabel_classificationr   r<  r
   r#   r@  )r>   r?   r=  r>  rA  rJ   rJ   rK   test_random_hasher_sparse_data  s
    
rG  c                     s   t d} d\}}| ||| dd|fdddD }| ||  fdd|D }t||d	d  D ]\}}t|| qnd S )
N!0  )P   rC  r   r+   c                    s"   g | ]}t d |dd qS )r5   i90  r  )r   r=   )r   r   )r   r   rJ   rK   r     s
    z'test_parallel_train.<locals>.<listcomp>)r*   r+   r,   r          c                    s   g | ]}|  qS rJ   )r   )r   rI   )r   rJ   rK   r     s     r*   )r'   randnr   zipr"   )r   r/   r0   ZclfsZprobasZproba1Zproba2rJ   )r   r   r   rK   test_parallel_train  s    rN  c                     s  t d} | jdddd}| d}d t dd	||}tt}|jD ]6}d
dd t	|j
j|j
jD }||  d7  < qHt fdd| D }t|dkstd|d d kstd|d d kstd|d d kstd|d d kst|d d dkst|d d dks&ttd}tjddd|d d df< tjddd|d d df< | d}tddd||}tt}|jD ]8}d
dd t	|j
j|j
jD }||  d7  < qdd | D }t|dkstd S )NrH  r   r   )r   r*   )ro   r   r-   rj   r9    c                 s   s.   | ]&\}}|d kr"d|t |f ndV  qdS r   z%d,%d/-Nr   r   ftrJ   rJ   rK   r     s   z$test_distribution.<locals>.<genexpr>r*   c                    s    g | ]\}}d |   |fqS )r   rJ   r   r   r   Zn_treesrJ   rK   r     s     z%test_distribution.<locals>.<listcomp>r^   g?r+   r,   333333?z0,1/0,0/--0,2/--)r   r+   )r;   r3   c                 s   s.   | ]&\}}|d kr"d|t |f ndV  qdS rP  rR  rS  rJ   rJ   rK   r     s   c                 S   s   g | ]\}}||fqS rJ   rJ   rV  rJ   rJ   rK   r     s     r   )r'   r   randr   r=   r   r   r   joinrM  r   feature	thresholdsorteditemsrC   rD   rz   emptyr{   )r   r>   r?   rd   Zuniquesr   rJ   rW  rK   test_distribution  s@    






r`  c                 C   sp   t t }}t|  }|ddddd||}|jd  dks@t|dddd||}|jd  dksltd S )Nr*   r   r   )r   Zmax_leaf_nodesr:   r3   )r   r:   r3   )hastie_Xhastie_yr7   r=   r   Z	get_depthrD   rG   r>   r?   r   r   rJ   rJ   rK   check_max_leaf_nodes_max_depth  s    
    rd  c                 C   s   t |  d S rM   )rd  rN   rJ   rJ   rK   test_max_leaf_nodes_max_depth  s    re  c                 C   s   t t }}t|  }|dddd}||| |jd jjdk}|jd jj| }t	|t
|d d ksxtd| |dddd}||| |jd jjdk}|jd jj| }t	|t
|d d kstd| d S )Nr.   r*   r   )Zmin_samples_splitr:   r3   r)   rT   Failed with {0})ra  rb  r7   r=   r   r   Zchildren_leftZn_node_samplesrz   minrC   rD   r   )rG   r>   r?   r   r   Znode_idxZnode_samplesrJ   rJ   rK   check_min_samples_split  s    
(rh  c                 C   s   t |  d S rM   )rh  rN   rJ   rJ   rK   test_min_samples_split  s    ri  c                 C   s   t t }}t|  }|dddd}||| |jd j|}t|}||dk }t	|dkspt
d| |dddd}||| |jd j|}t|}||dk }t	|t|d d kst
d| d S )Nr^   r*   r   )rv   r:   r3   r   rf  g      ?)ra  rb  r7   r=   r   r   rE   rz   r   rg  rD   r   rC   )rG   r>   r?   r   r   outZnode_countsZ
leaf_countrJ   rJ   rK   check_min_samples_leaf  s    


rk  c                 C   s   t |  d S rM   )rk  rN   rJ   rJ   rK   test_min_samples_leaf#  s    rl  c                 C   s   t t }}t|  }tjd}||jd }t|}t	dddD ]}||ddd}d| krfd|_
|j|||d |jd j|}	tj|	|d	}
|
|
dk }t|||j ksFtd
| |jqFd S )Nr   rT   r_   r*   )min_weight_fraction_leafr:   r3   ZRandomForestFr   )weightsz,Failed with {0} min_weight_fraction_leaf={1})ra  rb  r7   rz   r{   r|   rY  rF   r   Zlinspacer   r=   r   r   rE   r   rg  rm  rD   r   )rG   r>   r?   r   r   rn  Ztotal_weightfracr   rj  Znode_weightsZleaf_weightsrJ   rJ   rK   check_min_weight_fraction_leaf(  s0    

   rp  c                 C   s   t |  d S rM   )rp  rN   rJ   rJ   rK   test_min_weight_fraction_leafG  s    rq  c                 C   s   t |  }|ddd||}|ddd||}t|||| | tksV| tkrzt|||| t|j|j | tkrt|||| t|	||	| | t
krt|| ||  t|| ||  d S )Nr   r+   )r3   r   )r7   r=   r"   rE   r<   r`   r@   r   r   r   FOREST_TRANSFORMERSrD  r@  r<  )rG   r>   ZX_sparser?   r   ZdensesparserJ   rJ   rK   check_sparse_inputL  s2        rt  sparse_matrixc                 C   s(   t jddd\}}t| |||| d S )Nr   r  )r3   r/   )r   rF  rt  )rG   ru  r>   r?   rJ   rJ   rK   test_sparse_inputi  s    rv  c                 C   s  t |  ddd}tjtj|d}tj}t||||| tjtjd|d}tj}t||||| tjtjd|d}tj}t||||| tj	tj|d}tj}t||||| |j
jtkr^ttj|d}tj}t||||| ttj|d}tj}t||||| ttj|d}tj}t||||| tjtjd d d |d}tjd d d }t||||| d S )	Nr   Fr(  r   C)orderr   Fr,   )r7   rz   ZasarrayrU   rV   rW   r"   r=   r@   Zascontiguousarrayr  Zsplitterr   r   r
   r	   )rG   r   r   r>   r?   rJ   rJ   rK   check_memory_layoutq  s4    rz  c                 C   s   t | | d S rM   )rz  )rG   r   rJ   rJ   rK   test_memory_layout  s    r{  c              	   C   s|   t |  }tt |ddd|| W 5 Q R X |dd}||| | tksX| tkrxtt || W 5 Q R X d S )Nr*   r   r9   r   )r7   r   r   r  r=   r<   r`   r@   )rG   r>   X_2dr?   r   r   rJ   rJ   rK   check_1d_input  s    
r}  c              	   C   sT   t jd d df }t jd d df d}t j}t  t| ||| W 5 Q R X d S )Nr   r)   r*   )rU   rV   reshaperW   r$   r}  )rG   r>   r|  r?   rJ   rJ   rK   test_1d_input  s
    r  c           	      C   s  t |  }|dd}|tjtj |ddd}|tjtj t|j|j ttjtjtjfj	}|ddddddddddddgdd}|tj| t|j|j |ddd}|tj| t|j|j t
tjj}|tjdk  d	9  < dd
dd}|dd}|tjtj| ||dd}|tjtj t|j|j |dd}|tjtj|d  ||dd}|tjtj| t|j|j d S )Nr   r   balancedclass_weightr3   g       @r   )r   r*   r+   r*   r   g      Y@r+   )r<   r=   rU   rV   rW   r!   r   rz   r4  rA   r   rF   )	rG   rH   Zclf1Zclf2Z
iris_multiZclf3Zclf4r   r  rJ   rJ   rK   check_class_weights  s@    





r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   test_class_weights  s    r  c                 C   s~   t |  }ttttd fj}|ddd}|t| |ddddddgdd}|t| |d	dd}|t| d S )
Nr+   r  r   r  rT   r   r~  )r(   r+   balanced_subsample)r<   rz   r4  r?   r   rA   r=   r>   )rG   rH   r5  rI   rJ   rJ   rK   6check_class_weight_balanced_and_bootstrap_multi_output  s     r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   5test_class_weight_balanced_and_bootstrap_multi_output  s    r  c              	   C   s   t |  }ttttd fj}|dddd}|tt d}tj	t
|d |t| W 5 Q R X |dd	d
gdd}tt |t| W 5 Q R X d S )Nr+   r  Tr   )r  
warm_startr3   JWarm-start fitting without increasing n_estimators does not fit new trees.r   rT   r   r~  r  )r<   rz   r4  r?   r   rA   r=   r>   r   r	  r
  r   r  )rG   rH   r5  rI   warn_msgrJ   rJ   rK   check_class_weight_errors  s    r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   test_class_weight_errors  s    r  rj   c                 C   s   t t }}t|  }d }dD ]D}|d kr6|||dd}n|j|d ||| t||kstq|d|dd}||| tdd |D td	d |D kstt|	||	|d

| d d S )N)r^   r.   T)r:   r3   r  r:   r.   Fc                 S   s   g | ]
}|j qS rJ   r   r   rJ   rJ   rK   r   5  s     z$check_warm_start.<locals>.<listcomp>c                 S   s   g | ]
}|j qS rJ   r   r   rJ   rJ   rK   r   6  s     rf  )r   )ra  rb  r7   r   r=   rC   rD   r   r#   rE   r   )rG   r3   r>   r?   r   Zest_wsr:   Z	est_no_wsrJ   rJ   rK   check_warm_start   s6    
    
  r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   test_warm_start>  s    r  c                 C   s~   t t }}t|  }|ddddd}||| |ddddd}||| |jddd ||| t|||| d S )Nr^   r*   Fr:   r   r  r3   Tr+   )r  r3   )ra  rb  r7   r=   r   r"   rE   )rG   r>   r?   r   r   est_2rJ   rJ   rK   check_warm_start_clearC  s    
   r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   test_warm_start_clearT  s    r  c              	   C   s^   t t }}t|  }|dddd}||| |jdd tt ||| W 5 Q R X d S )Nr^   r*   T)r:   r   r  r   r  )ra  rb  r7   r=   r   r   r   r  rc  rJ   rJ   rK   %check_warm_start_smaller_n_estimatorsY  s    
r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   $test_warm_start_smaller_n_estimatorsd  s    r  c              	   C   s   t t }}t|  }|ddddd}||| |ddddd}||| |jdd d}tjt|d	 ||| W 5 Q R X t|	||	| d S )
Nr^   r,   Tr*   r  r+   r   r  r   )
ra  rb  r7   r=   r   r   r	  r
  r#   rE   )rG   r>   r?   r   r   r  r  rJ   rJ   rK   #check_warm_start_equal_n_estimatorsi  s"    
   r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   "test_warm_start_equal_n_estimators  s    r  c                 C   s   t t }}t|  }|ddddddd}||| |ddddddd}||| |jdddd ||| t|d	s|t|j|jkst|ddddddd}||| t|d	rt|jdd
 t|j|| |j|jkstd S )N   r,   Fr*   T)r:   r   r  r3   r   r   r^   )r  r   r:   r   r  )	ra  rb  r7   r=   r   r   rD   r   r$   )rG   r>   r?   r   r   r  Zest_3rJ   rJ   rK   check_warm_start_oob  sJ    
r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   test_warm_start_oob  s    r  c              
   C   s~   t t }}t|  }|ddddd}tj|d|jd@}||| tjt	dd ||| W 5 Q R X |
  W 5 Q R X d S )Nr.   T)r:   r  r   r   r  )wrapsz%Warm-start fitting without increasingr   )ra  rb  r7   r   objectr  r=   r   r	  r
  Zassert_called_once)rG   r>   r?   r   r   Z!mock_set_oob_score_and_attributesrJ   rJ   rK   test_oob_not_computed_twice  s$    
     r  r  c                 C   sX   t ddd}t| }dd dd |  D }||||}t|j| t|| d S )Nr   Fr(  c                 S   s   g | ]}|qS rJ   rJ   )r   chrJ   rJ   rK   r     s     z&test_dtype_convert.<locals>.<listcomp>ZABCDEFGHIJKLMNOPQRSTU)r   rz   eyer=   r@   r#   r   )r   r   r>   r?   resultrJ   rJ   rK   test_dtype_convert  s    
r  c                    s   t t }}|jd }t|  }|ddddd}||| ||\jd d ks\tjd |ksnttt	dd |j
D  ||}t|jd D ]<  fd	dt|d d  f D }t|tj|d
 qd S )Nr   r^   r*   Fr  r)   c                 S   s   g | ]}|j jqS rJ   )r   
node_count)r   erJ   rJ   rK   r     s     z'check_decision_path.<locals>.<listcomp>c                    s$   g | ]\}}|  | f qS rJ   rJ   )r   r   r   Zest_idZ	indicatorZn_nodes_ptrrJ   rK   r     s   )rF   )ra  rb  rF   r7   r=   Zdecision_pathrD   r#   rz   diffr   rE   r   	enumerater"   r   )rG   r>   r?   r/   r   r   leavesZleave_indicatorrJ   r  rK   check_decision_path  s$    

 
r  c                 C   s   t |  d S rM   )r  rN   rJ   rJ   rK   test_decision_path  s    r  c                  C   s\   t jddd\} }ttttg}|D ]4}|dd}|| | |jD ]}|jdksBt	qBq"d S )Nr   r*   r6   r   )min_impurity_decrease)
r   make_hastie_10_2r   r   r   r   r=   r   r  rD   )r>   r?   Zall_estimatorsZ	Estimatorr   r   rJ   rJ   rK   test_min_impurity_decrease  s    

r  c               	   C   s   t dd} td}dddg}d}tjt|d | || W 5 Q R X d	d	d	g}d
}tjt|d | || W 5 Q R X d S )Nrt   )rQ   )r,   r,   r)   r*   r,   zNSome value\(s\) of y are negative which is not allowed for Poisson regression.r   r   zLSum of y is not strictly positive which is necessary for Poisson regression.)r   rz   r   r   r   r  r=   )r   r>   r?   r   rJ   rJ   rK   test_poisson_y_positive_check  s    



r  c                       s(   e Zd Z fddZ fddZ  ZS )	MyBackendc                    s   d| _ t j|| d S )Nr   )r   super__init__)selfargskwargsr#  rJ   rK   r  $  s    zMyBackend.__init__c                    s   |  j d7  _ t  S )Nr*   )r   r  
start_call)r  r  rJ   rK   r  (  s    zMyBackend.start_call)__name__
__module____qualname__r  r  __classcell__rJ   rJ   r  rK   r  #  s   r  testingc               	   C   sv   t ddd} td\}}| tt W 5 Q R X |jdks@ttd\}}| t W 5 Q R X |jdksrtd S )Nr.   r+   )r:   r   r  r   )	r   joblibZparallel_backendr=   r>   r?   r   rD   r   )rI   bar   _rJ   rJ   rK   test_backend_respected0  s    r  c                  C   sH   t ddddd\} }tdddd| |}tjd|j d	d
sDtd S )Nr  r,   r*   )r/   r1   r3   r   r^   rj      )rv   r3   r:   gHz>)Zabs_tol)r   r   r=   mathiscloser   r   rD   )r>   r?   rI   rJ   rJ   rK   #test_forest_feature_importances_sum@  s       
   r  c                  C   sB   t d} t d}tdd| |}t|jt jdt jd d S )N)r.   r.   )r.   r.   r  r   )rz   r   r   r   r=   r#   r   float64)r>   r?   ZgbrrJ   rJ   rK   *test_forest_degenerate_feature_importancesJ  s    

r  c              	   C   s>   t |  ddd}d}tjt|d |tt W 5 Q R X d S )NFrT   r   max_sampleszl`max_sample` cannot be set if `bootstrap=False`. Either switch to `bootstrap=True` or set `max_sample=None`.r   )r8   r   r   r  r=   r>   r?   )rG   r   r   rJ   rJ   rK   test_max_samples_bootstrapR  s
    r  c              	   C   sB   t |  dtdd}d}tjt|d |tt W 5 Q R X d S )NTg    eAr  z=`max_samples` must be <= n_samples=6 but got value 1000000000r   )r8   r   r   r   r  r=   r>   r?   )rG   r   r   rJ   rJ   rK    test_large_max_samples_exception_  s    r  c                 C   s   t ttdddd\}}}}t|  dddd}||||}t|  dd dd}||||}t||}	t||}
|	t|
kst	d S )Nr  rX  r   )Z
train_sizers   r3   Tr   r   r  r3   )
r   ra   rb   r`   r=   r@   r   r   r   rD   )rG   r   r   r   r   
ms_1_modelZms_1_predictms_None_modelZms_None_predictZms_1_msZ
ms_None_msrJ   rJ   rK   $test_max_samples_boundary_regressorsh  s,            

r  c           	      C   sr   t ttdtd\}}}}t|  dddd}||||}t|  dd dd}||||}tj|| d S )Nr   )r3   ZstratifyTr   r  )	r   r   r   r<   r=   r   rz   r  Zassert_allclose)	rG   r   r   r   r  r  Z
ms_1_probar  Zms_None_probarJ   rJ   rK   %test_max_samples_boundary_classifiers~  s&           r  c               	   C   sN   dddgg} t dddg}t }d}tjt|d || | W 5 Q R X d S )	Nr*   r+   r,   r   r^   r_   z3sparse multilabel-indicator for y is not supported.r   )r   r   r   r   r  r=   )r>   r?   r   msgrJ   rJ   rK   test_forest_y_sparse  s    r  ForestClassc           	      C   s   t jd}|dd}|ddk}| d|d d}| d|dd}||| ||| |jd j}|jd j}d}|j|jkst|d S )Nr*   i'  r+   r   )r:   r3   r  z=Tree without `max_samples` restriction should have more nodes)	rz   r{   r|   rL  r=   r   r   r  rD   )	r  r   r>   r?   Zest1Zest2Ztree1Ztree2r  rJ   rJ   rK   'test_little_tree_with_small_max_samples  s&    r  Forestc                 C   sN   ddl m} tdd}|j\}}|||}t|  dd|d}|t| d S )Nr   )MSEr)   r*   r+   )r:   r   rQ   )Zsklearn.tree._criterionr  rb   r  rF   r`   r=   ra   )r  r  r?   r/   Z	n_outputsZmse_criterionr   rJ   rJ   rK   -test_mse_criterion_object_segfault_smoke_test  s    

r  c                  C   sX   t jd} t | dd}tddddd|}| }dd d	D }t|| d
S )z3Check feature names out for Random Trees Embedding.r   r   r   r+   F)r:   r   r8  r3   c                 S   s    g | ]\}}d | d| qS )Zrandomtreesembedding_r  rJ   )r   r   leafrJ   rJ   rK   r     s   zAtest_random_trees_embedding_feature_names_out.<locals>.<listcomp>))r   r+   )r   r,   )r   r^   )r   r_   r  )r*   r,   )r*   r^   )r*   r_   N)	rz   r{   r|   r   rL  r   r=   Zget_feature_names_outr#   )r3   r>   r=  namesZexpected_namesrJ   rJ   rK   -test_random_trees_embedding_feature_names_out  s       r  c              	   C   sb   t ddgddgg}t ddg}t|   }||| d}tjt|d |j W 5 Q R X d S )Nr*   r+   r,   r   r   zoAttribute `base_estimator_` was deprecated in version 1.2 and will be removed in 1.4. Use `estimator_` instead.r   )rz   r   r7   r=   r   r	  FutureWarningZbase_estimator_)rG   r>   r?   modelr  rJ   rJ   rK   'test_base_estimator_property_deprecated  s    
r  c                 C   sf   |  tjjdttdd tjjdd}t	dd|d\}}t
|dd	}td
|d}t|||d
d dS )zRandomForestClassifier must work on readonly sparse data.

    Non-regression test for: https://github.com/scikit-learn/scikit-learn/issues/25333
    r&   r   )Z
max_nbytesr   )seedr  r4   Tr   r+   )r   r3   )ZcvN)setattrsklearnZensembleZ_forestr   r&   rz   r{   r|   r   r   r   r   )Zmonkeypatchr   r>   r?   rI   rJ   rJ   rK   test_read_only_buffer  s    
r  r  r  c                 C   s0   t jdd\}}tdd| dd}||| dS )z^Check low max_samples works and is rounded to one.

    Non-regression test for gh-24037.
    T)Z
return_X_yr.   g-C6?r   )r:   r  r  r3   N)r   Z	load_winer   r=   )r  r>   r?   r  rJ   rJ   rK   .test_round_samples_to_one_when_samples_too_low	  s       r  )rR   )r   )r   )r  )r  )rj   )r  )__doc__	itertoolsr  r  collectionsr   	functoolsr   r   r   typingr   r   Zunittest.mockr   r  numpyrz   r   Zscipy.sparser	   r
   r   Zscipy.specialr   r  r   Zsklearn.datasetsr   Zsklearn.decompositionr   Zsklearn.dummyr   Zsklearn.ensembler   r   r   r   r   Zsklearn.exceptionsr   Zsklearn.metricsr   r   r   r   Zsklearn.model_selectionr   r   r   Zsklearn.svmr   Zsklearn.tree._classesr   Zsklearn.utils._testingr    r!   r"   r#   r$   r%   Zsklearn.utils.parallelr&   Zsklearn.utils.validationr'   r>   r?   rA   rB   r   r   Z	load_irisrU   r   ZpermutationrW   ro   permrV   Zmake_regressionra   rb   r  ra  rb  r   Zfloat32parallelZget_active_backendr#  ZDEFAULT_JOBLIB_BACKENDr<   r`   rr  dictr7   str__annotations__updater   r8   rL   markZparametrizerO   rY   r]   re   ri   r   r   r   r   r   r   r   r  chainr   r   r   r   rF  r  r  r  r   rF   r  r  r  r  r  r  r&  r'  r,  r-  r3  r6  r7  r?  rB  rE  rG  rN  r`  rd  re  rh  ri  rk  rl  rp  rq  rt  rv  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  Zregister_parallel_backendr  r  r  r  r  r  r  r  r  r  r  r  r  r  rJ   rJ   rJ   rK   <module>   s,  
 (

 




 5


&m
	   )      (




3

7

6



/

	/






2









	
