o
    tBh                     @   s
  U d Z ddlZddl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	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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 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# 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, ddl*m-Z- ddl*m.Z. ddl*m/Z/ ddl0m1Z1 ddl0m2Z2 ddl3m4Z4 dd l5m6Z6 dd!lm7Z7 dd"l8m9Z9 d#d$gd$d$gd$d#gd%d%gd%d&gd&d%ggZ:g d'Z;d$d$gd&d&gd(d&ggZ<g d)Z=e%j)d*d+d(ddd,dd-\Z>Z?e%@ ZAe6dZBeBCeAjDjEZFeAjGeF eA_GeAjDeF eA_De%jHd*d+d%d.\ZIZJe%jKd/d%d0\ZLZMeLNejOZLejPQ d jRZSe+e-d1ZTe,e.d2ZUd3e/iZVeW ZXe	eYe
f eZd4< eX[eT eX[eU eX[eV eT\ Z]e	eYe
f eZd5< e][eU d6d7 Z^ej_`d8eTd9d: Zad;d< Zbej_`d8eTej_`d=d>d?d@ ZcdAdB Zdej_`d8eUej_`d=dCdDdE ZedFdG Zfej_`d=dHdIdJ ZgdKdL Zhej_`d8eUdMdN ZidOdP Zjej_`d8eTdQdR ZkdSdT Zlej_`dUejmejOfej_`dVeneeTdWdXgeeUg dYdZd[ Zod\d] Zpej_`d8eXd^d_ Zqej_`d`eTr ej_`dag dbej_`dcg e%j)ddd&ddedfR g e%j)dgd(dhddidjR eAjGeAjDd& d% djfg e%jsdddd0dkR gdldm Ztej_`dneUr ej_`dag dbej_`dog e%jHd*d+d%ddpdqR g e%jHd*d+d&ddpdrR gdsdt Zuej_`due]r dvdw Zvej_`due]r ej_`dxeAjGeAjDdyd,dzd{feAjGeBjwdd|eAjGjxd d&fd}dydydzd~fgdd Zyej_`ddyd,gdd Zzdd Z{ej_`d8eTdd Z|dd Z}ej_`d8e]dd Z~dd Zej_`d8e]dd Zdd Zej_`d8e]dd Zej_`d8eTdd Zdd Zej_`d8eTdd Zdd Zdd Ze dd Zdd Zdd Zdd Zdd Zej_`d8eXdd Zdd Zej_`d8eXdd Zdd Zej_`d8eXdd Zdd Zej_`d8eXdd Zdd Zej_`d8eXej_`deeefdd Zdd Zej_`d8e]ej_`dUejmejOfdd Ze dd Zej_`d8eXdd ZddĄ Zej_`d8eTddƄ ZddȄ Zej_`d8eTddʄ Zdd̄ Zej_`d8eTdd΄ Zd%ddфZej_`d8eXddӄ ZddՄ Zej_`d8eXddׄ Zddل Zej_`d8eXddۄ Zdd݄ Zej_`d8eXdd߄ Zdd Zej_`d8e]dd Zd&ddZdd Zej_`d8e]dd Zdd Zdd ZG dd deSZede e!dd Zdd Zdd Zej_`d8e]dd Zej_`d8e]ej_j`dededfdedfded fejedfejedfdedfed&edfgdd dd	d
 Zej_`d8eUdd Zej_`d8eTdd Zdd Zej_`de-e.gdd Zej_`de+e,e-e.e/gdd Zej_`de+e,e-e.gdd Zej_`ddde.fdde.fgdd Zej_`d eUd!d" ZÐd#d$ ZdS ('  z:
Testing for the forest module (sklearn.ensemble.forest).
    N)defaultdict)combinations)product)DictAny)
csr_matrix)
csc_matrix)
coo_matrix)comb)assert_allclose)DummyRegressor)mean_poisson_deviance)assert_almost_equal)assert_array_almost_equal)assert_array_equal)_convert_container)ignore_warnings)skip_if_no_parallel)NotFittedError)datasets)TruncatedSVD)make_classification)ExtraTreesClassifier)ExtraTreesRegressor)RandomForestClassifier)RandomForestRegressor)RandomTreesEmbedding)train_test_split)GridSearchCV)	LinearSVC)check_random_state)mean_squared_error)SPARSE_SPLITTERS      )r$   r$   r$   r%   r%   r%      )r$   r%   r%     
   F)	n_samples
n_featuresn_informativen_redundant
n_repeatedshufflerandom_stater*   r+   r0      r*   r0   )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 J |dddd}|tt t|tt dt|ks=J |	t}|j
tt|jfksNJ dS )z&Check classification on a toy dataset.r)   r%   n_estimatorsr0   )r7   max_featuresr0   N)FOREST_CLASSIFIERSfitXyr   predictTtrue_resultlenapplyshaper7   )nameForestClassifierclfleaf_indices rG   y/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/ensemble/tests/test_forest.pycheck_classification_toyv   s   
rI   rC   c                 C      t |  d S N)rI   rC   rG   rG   rH   test_classification_toy      rM   c                 C   s   t |  }|d|dd}|tjtj |tjtj}|dks'J d||f |d|ddd}|tjtj |tjtj}|dksKJ d||f d S )	Nr)   r%   r7   	criterionr0   ?z'Failed with criterion %s and score = %fr&   r7   rP   r8   r0         ?)r9   r:   irisdatatargetscore)rC   rP   rD   rE   rW   rG   rG   rH   check_iris_criterion   s   rX   rP   )ginilog_lossc                 C      t | | d S rK   )rX   rC   rP   rG   rG   rH   	test_iris      r]   c                 C   s   t |  }|d|dd}|tt |tt}|dks#J d||f |d|ddd}|tt |tt}|dksCJ d	||f d S )
N   r%   rO   g(\?z:Failed with max_features=None, criterion %s and score = %f   rR   gq=
ףp?z7Failed with max_features=6, criterion %s and score = %f)FOREST_REGRESSORSr:   X_regy_regrW   )rC   rP   ForestRegressorregrW   rG   rG   rH   check_regression_criterion   s&   
rf   )squared_errorabsolute_errorfriedman_msec                 C   r[   rK   )rf   r\   rG   rG   rH   test_regression   s   rj   c                  C   sB  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 ]4\}}}t|||}t|t ||dd}t|||}|dkr||k sJ |d| k sJ qjd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)   r1   r#   r&   lowhighsizer   axislam	test_sizer0   poissonr)   sqrt)rP   min_samples_leafr8   r0   rg   mean)strategytraintestgư>Ng?)nprandomRandomStater   make_low_rank_matrixuniformmaxrw   expr   r   r:   r   r   r=   clip)rngn_trainn_testr+   r;   coefr<   X_trainX_testy_trainy_test
forest_poi
forest_msedummy	data_name
metric_poi
metric_msemetric_dummyrG   rG   rH   test_poisson_vs_mse   s@   

r   )rw   rg   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sOJ dS )z9 "Test that sum(y_pred)==sum(y_true) on the training set.rk   rl   r1   r#   r&   rm   r   rq   rs   r)   F)rP   r7   	bootstrapr0   N)r~   r   r   r   r   r   r   rw   r   r   r:   sumr=   pytestapprox)	rP   r   r   r   r+   r;   r   r<   re   rG   rG   rH   #test_balance_property_random_forest   s   

(r   c                 C   sf   t |  dd}t|drJ t|drJ |g dg dgddg t|dr*J t|dr1J d S )	Nr   r0   classes_
n_classes_r%   r&   r'      r_   r`   r%   r&   )ra   hasattrr:   )rC   rrG   rG   rH   check_regressor_attributes  s   r   c                 C   rJ   rK   )r   rL   rG   rG   rH   test_regressor_attributes  rN   r   c                 C   s   t |  }tjdd> |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 d    d S 1 sKw   Y  d S )Nignoredivider)   r%   )r7   r0   r8   	max_depthrq   r   )r9   r~   errstater:   rT   rU   rV   r   r   predict_probaonesrB   r   predict_log_proba)rC   rD   rE   rG   rG   rH   check_probability  s   $"r   c                 C   rJ   rK   )r   rL   rG   rG   rH   test_probability/  rN   r   c                 C   sH  t j|dd}tj|dd}t|  }|d|dd}||| |j}t|dk}	|jd dks2J |	dks8J t	|d d dksEJ |j}|j
dd	 |j}
t||
 tdd
dt|}|dd|d}|j|||d |j}t	|dks|J dD ]#}|dd|d}|j|||| d |j}t||  |k sJ q~d S )NF)copyr)   r   rO   皙?r'   r&   n_jobsr%   )r7   r0   rP   sample_weight        )rS   d   )X_largeastypey_larger4   r:   feature_importances_r~   r   rB   all
set_paramsr   r    randintr@   absrz   )rC   rP   dtype	tolerancer;   r<   ForestEstimatorestimportancesn_importantimportances_parallelr   scaleimportances_bisrG   rG   rH   check_importances4  s2   
r   r   zname, criterionrY   rZ   )rg   ri   rh   c                 C   s*   d}|t v r|dkrd}t||| | d S )N{Gz?rh   g?)ra   r   )r   rC   rP   r   rG   rG   rH   test_importancesZ  s   	r   c            	         s(  dd  dd  fdd} t g dg dg d	g d
g dg dg dg dg dg dg
}t j|d d d df td|d d df }}|jd }t |}t|D ]
}| |||||< qXtddddd||}tdd |j	D |j
 }t|t| t ||  dk sJ d S )Nc                 S   s*   | dk s| |kr
dS t t|t| ddS )Nr   T)exact)r
   int)knrG   rG   rH   binomialn  s   *z-test_importances_asymptotic.<locals>.binomialc                 S   sF   t | }d}t| D ]}d| | }|dkr ||t| 8 }q|S )Nr         ?r   )r@   r~   bincountlog2)samplesr*   entropycountprG   rG   rH   r   q  s   z,test_importances_asymptotic.<locals>.entropyc              
      sb  j \}}tt|}||  fddt|D d}t|D ]}d||||   }t||D ]z t fddt|D  D ]i}	tj|td}
t|D ]}|
d d  | f |	| kM }
qP|
d d f ||
 }}t	|dkrg }|  D ]}|d d | f |k}|
||  q~||d |  |tfdd|D   7 }qCq3q!|S )	Nc                    s"   g | ]}t  d d |f qS rK   )r~   unique).0i)r;   rG   rH   
<listcomp>     " zGtest_importances_asymptotic.<locals>.mdi_importance.<locals>.<listcomp>r   r   c                    s   g | ]} |  qS rG   rG   )r   j)BvaluesrG   rH   r         r   r   c                    s    g | ]} |t |  qS rG   )r@   )r   c)r   n_samples_brG   rH   r     s    )rB   listrangepopr   r   r~   r   boolr@   appendr   )X_mr;   r<   r*   r+   featuresimpr   r   bmask_br   X_y_childrenximask_xir   r   )r   r;   r   r   rH   mdi_importance|  sH   

 "
 z3test_importances_asymptotic.<locals>.mdi_importance)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%   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%   r%   	   )r%   r%   r%   r   r%   r%   r%   r   r   r   r%   r(   rZ   r   )r7   r8   rP   r0   c                 s   s    | ]
}|j jd dV  qdS )F)	normalizeN)tree_compute_feature_importancesr   treerG   rG   rH   	<genexpr>  s
    
z.test_importances_asymptotic.<locals>.<genexpr>r   )r~   arrayr   rB   zerosr   r   r:   r   estimators_r7   r   r   rz   )	r   rU   r;   r<   r+   true_importancesr   rE   r   rG   r   rH   test_importances_asymptotici  sD   00

	r  c                 C   sN   d | }tjt|d tt|   d W d    d S 1 s w   Y  d S )NzfThis {} instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.matchr   )formatr   raisesr   getattrr4   )rC   err_msgrG   rG   rH   !test_unfitted_feature_importances  s   "r  rD   X_type)r   
sparse_csr
sparse_csczX, y, lower_bound_accuracyi,  )r*   	n_classesr0   rQ     r`   )r*   r  r,   r0   g?g
ףp=
?c                 C   s  t ||d}t||ddd\}}}}| ddddd}	t|	dr!J t|	d	r(J |	|| |	||}
t|
|	j d
ks?J |	j|ksFJ t|	dsMJ t|	drTJ t|	d	s[J |jdkrl|jd t	t
|f}n|jd t	t
|dddf |jd f}|	jj|ksJ dS )z5Check that OOB score is close to score on a test set.constructor_namerS   r   ru   (   Tr7   r   	oob_scorer0   
oob_score_oob_decision_function_r   oob_prediction_r%   N)r   r   r   r:   rW   r   r  ndimrB   r@   setr  )rD   r;   r<   r	  lower_bound_accuracyr   r   r   r   
classifier
test_scoreexpected_shaperG   rG   rH   test_forest_classifier_oob  s4   
*r  rd   zX, y, lower_bound_r2)r*   r+   	n_targetsr0   ffffff?g?c                 C   s   t ||d}t||ddd\}}}}| ddddd}	t|	dr!J t|	d	r(J |	|| |	||}
t|
|	j d
ks?J |	j|ksFJ t|	dsMJ t|	d	sTJ t|	dr[J |jdkrg|jd f}n|jd |jf}|	j	j|kswJ dS )z\Check that forest-based regressor provide an OOB score close to the
    score on a test set.r  rS   r   ru   2   Tr  r  r  r   r  r%   N)
r   r   r   r:   rW   r   r  r  rB   r  )rd   r;   r<   r	  lower_bound_r2r   r   r   r   	regressorr  r  rG   rG   rH   test_forest_regressor_oob  s4   
r"  r   c                 C   sT   | ddddd}t jtdd |tjtj W d   dS 1 s#w   Y  dS )zfCheck that a warning is raised when not enough estimator and the OOB
    estimates will be inaccurate.r%   Tr   )r7   r  r   r0   z"Some inputs do not have OOB scoresr  N)r   warnsUserWarningr:   rT   rU   rV   )r   	estimatorrG   rG   rH   test_forest_oob_warningV  s   "r&  zX, y, params, err_msgT)r  r   z6Out of bag estimation only available if bootstrap=Truer_   rm   z:The type of target cannot be used to compute OOB estimatesc                 C   sN   | di |}t jt|d ||| W d    d S 1 s w   Y  d S )Nr  rG   )r   r  
ValueErrorr:   )r   r;   r<   paramsr  r%  rG   rG   rH   test_forest_oob_errord  s   "r)  r  c                 C   sz   t jtdd t| d W d    n1 sw   Y  t jtdd t tt W d    d S 1 s6w   Y  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*  rG   rG   rH   +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&   )r7   r   )r9   r   r:   rT   rU   rV   )rC   forestrE   rG   rG   rH   check_gridsearch  s   
r1  c                 C   rJ   rK   )r1  rL   rG   rG   rH   test_gridsearch  s   r2  c                 C   sn   t |  }|dddd}||| t|dksJ |jdd ||}|jdd ||}t||d dS )	z-Check parallel computations in classificationr)   r'   r   r7   r   r0   r%   r   r&   N)r4   r:   r@   r   r=   r   )rC   r;   r<   r   r0  y1y2rG   rG   rH   check_parallel  s   

r6  c                 C   s6   | t v rtj}tj}n| tv rt}t}t| || d S rK   )r9   rT   rU   rV   ra   rb   rc   r6  rC   r;   r<   rG   rG   rH   test_parallel  s   r8  c           	      C   sl   t |  }|dd}||| |||}t|}t|}t||jks(J |||}||ks4J d S )Nr   r   )r4   r:   rW   pickledumpsloadstype	__class__)	rC   r;   r<   r   objrW   pickle_objectobj2score2rG   rG   rH   check_pickle  s   


rB  c                 C   sJ   | t v rtj}tj}n| tv rt}t}t| |d d d |d d d  d S )Nr&   )r9   rT   rU   rV   ra   rb   rc   rB  r7  rG   rG   rH   test_pickle  s   $rC  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v rtjd	d
G ||}t|dksJ |d j	dksJ |d j	dksJ |
|}t|dksJ |d j	dksJ |d j	dksJ W d    d S 1 sw   Y  d S d S )Nr#   r$   r%   r&   r   r'   Fr0   r   r   r   r   r&   r   r   )r4   r:   r=   r   r9   r~   r   r   r@   rB   r   	rC   r   r   r   r   r   y_predproba	log_probarG   rG   rH   check_multioutput  sV   


"rK  c                 C   rJ   rK   )rK  rL   rG   rG   rH   test_multioutput  rN   rL  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G ||}t|dksJ |d
 jdksJ |d jdksJ |	|}t|dksJ |d
 jdksJ |d jdksJ W d    d S 1 sw   Y  d S )Nr#   r$   r%   r&   redbluegreenpurpleyellowr   FrD  r   r   rE  rF  )
r4   r:   r=   r   r~   r   r   r@   rB   r   rG  rG   rG   rH   test_multioutput_string  sZ   


"rR  c                 C   s   t |  }|ddtt}|jdksJ 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#   )r9   r:   r;   r<   r   r   r   r~   vstackr   r>   )rC   rD   rE   _yrG   rG   rH   check_classes_shape6  s   rU  c                 C   rJ   rK   )rU  rL   rG   rG   rH   test_classes_shapeH  rN   rV  c                  C   s<   t ddd} tjdd\}}| |}t|tjksJ d S )Nr)   F)r7   sparse_outputrS   factor)r   r   make_circlesfit_transformr<  r~   ndarray)hasherr;   r<   X_transformedrG   rG   rH   test_random_trees_dense_typeM  s   
r_  c                  C   sR   t dddd} t dddd}tjdd\}}| |}||}t| | d S )Nr)   Fr   )r7   rW  r0   TrS   rX  )r   r   rZ  r[  r   toarray)hasher_densehasher_sparser;   r<   X_transformed_denseX_transformed_sparserG   rG   rH   test_random_trees_dense_equalZ  s   

re  c                  C   s   t ddd} tjdd\}}| |}t ddd} t| || |  |jd |jd ks4J t|j	dd| j
 tdd	}||}t }||| |||d
ks[J d S )N   r%   r6   rS   rX  r   rq   r&   )n_componentsr   )r   r   rZ  r[  r   r:   	transformr`  rB   r   r7   r   r   rW   )r]  r;   r<   r^  svd	X_reduced
linear_clfrG   rG   rH   test_random_hashern  s   


rl  c                  C   sJ   t jdd\} }tddd}|| }|t| }t| |  d S )Nr   r   rf  r%   r6   )r   make_multilabel_classificationr   r[  r   r   r`  )r;   r<   r]  r^  rd  rG   rG   rH   test_random_hasher_sparse_data  s
   
rn  c                     s   t d} d\}}| ||| dd|fdddD }| ||  fdd|D }t||d	d  D ]	\}}t|| q7d S )
N!0  )P   rf  r   r&   c                    s"   g | ]}t d |dd qS )r2   i90  r3  )r   r:   )r   r   )r   r   rG   rH   r     s    z'test_parallel_train.<locals>.<listcomp>)r%   r&   r'   r          c                    s   g | ]}|  qS rG   )r   )r   rE   )r   rG   rH   r     s    r%   )r    randnr   zipr   )r   r*   r+   clfsprobasproba1proba2rG   )r   r   r   rH   test_parallel_train  s   ry  c                     s  t d} | jdddd}| d}d t dd	||}tt}|jD ]}d
dd t	|j
j|j
jD }||  d7  < q$t fdd| D }t|dksUJ d|d d ks_J d|d d ksiJ d|d d kssJ d|d d ks}J |d d dksJ |d d dksJ 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 ]}d
dd t	|j
j|j
jD }||  d7  < qdd | D }t|dksJ d S )Nro  r   r   )r  r%   )rp   r  r(   rk   r6    c                 s   0    | ]\}}|d krd|t |f ndV  qdS r   z%d,%d/-Nr   r   ftrG   rG   rH   r     
    
z$test_distribution.<locals>.<genexpr>r%   c                    s    g | ]\}}d |   |fqS )r   rG   r   r   r   n_treesrG   rH   r     s     z%test_distribution.<locals>.<listcomp>r_   g?r&   r'   333333?z0,1/0,0/--0,2/--)r  r&   )r8   r0   c                 s   r{  r|  r~  r  rG   rG   rH   r     r  c                 S   s   g | ]\}}||fqS rG   rG   r  rG   rG   rH   r     r   r   )r    r   randr   r:   r   r   r   joinrt  r   feature	thresholdsorteditemsr@   r~   emptyr   )r   r;   r<   re   uniquesr   rG   r  rH   test_distribution  s@   






r  c                 C   sp   t t}}t|  }|ddddd||}|jd  dks J |dddd||}|jd  dks6J d S )Nr%   r   r   )r   max_leaf_nodesr7   r0   )r   r7   r0   )hastie_Xhastie_yr4   r:   r   	get_depthrC   r;   r<   r   r   rG   rG   rH   check_max_leaf_nodes_max_depth  s   
r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_max_leaf_nodes_max_depth  rN   r  c                 C   s  t t}}t|  }tt |dd|| W d    n1 s"w   Y  tt |dd|| W d    n1 s@w   Y  tt |dd|| W d    n1 s^w   Y  |dddd}||| |jd jj	dk}|jd jj
| }t|t|d d ksJ d	| |dddd}||| |jd jj	dk}|jd jj
| }t|t|d d ksJ d	| d S )
Nr$   )min_samples_splitr   g?r)   r%   )r  r7   r0   rS   Failed with {0})r  r  r4   r   r  r'  r:   r   r   children_leftn_node_samplesr~   minr@   r  )rC   r;   r<   r   r   node_idxnode_samplesrG   rG   rH   check_min_samples_split  s*   
(,r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_min_samples_split  rN   r  c                 C   sV  t t}}t|  }tt |dd|| W d    n1 s"w   Y  tt |dd|| W d    n1 s@w   Y  |dddd}||| |jd j	|}t
|}||dk }t
|dkstJ d| |d	ddd}||| |jd j	|}t
|}||dk }t
|t|d	 d ksJ d| d S )
Nr$   )ry   r   r_   r%   )ry   r7   r0   r   r  g      ?)r  r  r4   r   r  r'  r:   r   r   rA   r~   r   r  r  r@   )rC   r;   r<   r   r   outnode_counts
leaf_countrG   rG   rH   check_min_samples_leaf
  s(   


,r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_min_samples_leaf'  rN   r  c                 C   s   t t}}t|  }tjd}||jd }t|}t	dddD ]A}||ddd}d| v r3d|_
|j|||d |jd j|}	tj|	|d	}
|
|
dk }t|||j ksdJ d
| |jq#d S )Nr   rS   r`   r%   )min_weight_fraction_leafr7   r0   RandomForestFr   )weightsz,Failed with {0} min_weight_fraction_leaf={1})r  r  r4   r~   r   r   r  rB   r   linspacer   r:   r   r   rA   r   r  r  r  )rC   r;   r<   r   r   r  total_weightfracr   r  node_weightsleaf_weightsrG   rG   rH   check_min_weight_fraction_leaf,  s*   

r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_min_weight_fraction_leafK  rN   r  c                 C   s   t |  }|ddd||}|ddd||}t|||| | tv s+| tv r=t|||| t|j|j | tv rWt|||| t|	||	| | t
v r{t|| ||  t|| ||  d S d S )Nr   r&   )r0   r   )r4   r:   r   rA   r9   ra   r=   r   r   r   FOREST_TRANSFORMERSrh  r`  r[  )rC   r;   X_sparser<   r   densesparserG   rG   rH   check_sparse_inputP  s,   r  sparse_matrixc                 C   s(   t jddd\}}t| |||| d S )Nr   r  )r0   r*   )r   rm  r  )rC   r  r;   r<   rG   rG   rH   test_sparse_inputm  s   r  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v rt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   FrD  r   C)orderr   Fr'   )r4   r~   asarrayrT   rU   rV   r   r:   r=   ascontiguousarraybase_estimatorsplitterr"   r   r   r	   )rC   r   r   r;   r<   rG   rG   rH   check_memory_layoutu  s4   r  c                 C   r[   rK   )r  )rC   r   rG   rG   rH   test_memory_layout  r^   r  c                 C   s   t |  }tt |ddd|| W d    n1 sw   Y  |dd}||| | tv s6| tv rStt || W d    d S 1 sLw   Y  d S d S )Nr%   r   r6   r   )r4   r   r  r'  r:   r9   ra   r=   )rC   r;   X_2dr<   r   r   rG   rG   rH   check_1d_input  s   
"r  c                 C   sj   t jd d df }t jd d df d}t j}t  t| ||| W d    d S 1 s.w   Y  d S )Nr   r$   r%   )rT   rU   reshaperV   r   r  )rC   r;   r  r<   rG   rG   rH   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_weightr0          @r   )r   r%   r&   r%   r   g      Y@r&   )r9   r:   rT   rU   rV   r   r   r~   rS  r>   r   rB   )	rC   rD   clf1clf2
iris_multiclf3clf4r   r  rG   rG   rH   check_class_weights  s@   





r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_class_weights  rN   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  rS   r   r  )r#   r&   balanced_subsample)r9   r~   rS  r<   r   r>   r:   r;   )rC   rD   rT  rE   rG   rG   rH   6check_class_weight_balanced_and_bootstrap_multi_output  s   r  c                 C   rJ   rK   )r  rL   rG   rG   rH   5test_class_weight_balanced_and_bootstrap_multi_output  rN   r  c                 C   s  t |  }ttttd fj}|ddd}tt |	t
t W d    n1 s-w   Y  tt |	t
| W d    n1 sHw   Y  |dddd}|	t
t d}tjt|d	 |	t
| W d    n1 stw   Y  |d
dd}tt |	t
| W d    n1 sw   Y  |dddgdd}tt |	t
| W d    d S 1 sw   Y  d S )Nr&   z	the larchr   r  r  T)r  
warm_startr0   JWarm-start fitting without increasing n_estimators does not fit new trees.r  r%   rS   r   r  )r9   r~   rS  r<   r   r>   r   r  r'  r:   r;   r#  r$  )rC   rD   rT  rE   warn_msgrG   rG   rH   check_class_weight_errors
  s0   "r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_class_weight_errors+  rN   r  rk   c                 C   s   t t}}t|  }d }dD ]"}|d u r|||dd}n|j|d ||| t||ks/J q|d|dd}||| tdd |D td	d |D ksQJ t||||d
	| d d S )N)r_   r)   T)r7   r0   r  r7   r)   Fc                 S      g | ]}|j qS rG   r   r   rG   rG   rH   r   E      z$check_warm_start.<locals>.<listcomp>c                 S   r  rG   r   r   rG   rG   rH   r   F  r  r  )r  )
r  r  r4   r   r:   r@   r  r   rA   r  )rC   r0   r;   r<   r   est_wsr7   	est_no_wsrG   rG   rH   check_warm_start0  s*   


r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_warm_startN  rN   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r7   r   r  r0   Tr&   )r  r0   )r  r  r4   r:   r   r   rA   )rC   r;   r<   r   r   est_2rG   rG   rH   check_warm_start_clearS  s   
r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_warm_start_cleard  rN   r  c                 C   st   t t}}t|  }|dddd}||| |jdd tt ||| W d    d S 1 s3w   Y  d S )Nr_   r%   T)r7   r   r  r   r  )r  r  r4   r:   r   r   r  r'  r  rG   rG   rH   %check_warm_start_smaller_n_estimatorsi  s   
"r  c                 C   rJ   rK   )r  rL   rG   rG   rH   $test_warm_start_smaller_n_estimatorst  rN   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 d    n1 sEw   Y  t|	||	| d S )
Nr_   r'   Tr%   r  r&   r   r  r  )
r  r  r4   r:   r   r   r#  r$  r   rA   )rC   r;   r<   r   r   r  r  rG   rG   rH   #check_warm_start_equal_n_estimatorsy  s   
r  c                 C   rJ   rK   )r  rL   rG   rG   rH   "test_warm_start_equal_n_estimators  rN   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>J |j|jksFJ |ddddddd}||| t|d	r]J |jdd
 t|j|| |j|jkssJ d S )N   r'   Fr%   T)r7   r   r  r0   r   r  r_   )r  r  r7   r  r*  )r  r  r4   r:   r   r   r  r   )rC   r;   r<   r   r   r  est_3rG   rG   rH   check_warm_start_oob  sJ   
r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_warm_start_oob  rN   r  r  c                 C   sX   t ddd}t| }dd dd |  D }||||}t|j| t|| d S )Nr   FrD  c                 S   s   g | ]}|qS rG   rG   )r   chrG   rG   rH   r     s    z&test_dtype_convert.<locals>.<listcomp>ABCDEFGHIJKLMNOPQRSTU)r   r~   eyer:   r=   r   r   )r  r  r;   r<   resultrG   rG   rH   test_dtype_convert  s   
r  c                    s   t t}}|jd }t|  }|ddddd}||| ||\jd d ks.J jd |ks7J ttdd |j	D  |
|}t|jd D ]  fd	dt|d d  f D }t|tj|d
 qQd S )Nr   r_   r%   Fr  r$   c                 S   s   g | ]}|j jqS rG   )r   
node_count)r   erG   rG   rH   r     s    z'check_decision_path.<locals>.<listcomp>c                    s$   g | ]\}}|  | f qS rG   rG   )r   r   r   est_id	indicatorn_nodes_ptrrG   rH   r     s    )rB   )r  r  rB   r4   r:   decision_pathr   r~   diffr   rA   r   	enumerater   r   )rC   r;   r<   r*   r   r   leavesleave_indicatorrG   r  rH   check_decision_path  s$   


r  c                 C   rJ   rK   )r  rL   rG   rG   rH   test_decision_path  rN   r  c                  C   s\   t jddd\} }ttttg}|D ]}|dd}|| | |jD ]	}|jdks*J q!qd S )Nr   r%   r3   r   )min_impurity_decrease)	r   make_hastie_10_2r   r   r   r   r:   r   r  )r;   r<   all_estimators	Estimatorr   r   rG   rG   rH   test_min_impurity_decrease  s   

r   c                  C   s   t dd} td}g d}d}tjt|d | || W d    n1 s(w   Y  g d}d}tjt|d | || W d    d S 1 sLw   Y  d S )	Nrw   )rP   )r'   r'   )r$   r%   r'   zNSome value\(s\) of y are negative which is not allowed for Poisson regression.r  )r   r   r   zLSum of y is not strictly positive which is necessary for Poisson regression.)r   r~   r   r   r  r'  r:   )r   r;   r<   r  rG   rG   rH   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|i | d S )Nr   )r   super__init__)selfargskwargsr=  rG   rH   r    s   zMyBackend.__init__c                    s   |  j d7  _ t  S )Nr%   )r   r  
start_call)r  r  rG   rH   r	  #  s   
zMyBackend.start_call)__name__
__module____qualname__r  r	  __classcell__rG   rG   r  rH   r    s    r  testingc                  C   s   t ddd} td\}}| tt W d    n1 sw   Y  |jdks*J td\}}| t W d    n1 sAw   Y  |jdksMJ d S )Nr)   r&   )r7   r   r  r   )r   joblibparallel_backendr:   r;   r<   r   r   )rE   bar   _rG   rG   rH   test_backend_respected+  s   r  c                  C   sH   t ddddd\} }tdddd| |}tjd|j d	d
s"J d S )Nr  r'   r%   )r*   r,   r0   r  r_   rk      )ry   r0   r7   gHz>)abs_tol)r   r   r:   mathiscloser   r   )r;   r<   rE   rG   rG   rH   #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   )r~   r   r   r   r:   r   r   float64)r;   r<   gbrrG   rG   rH   *test_forest_degenerate_feature_importancesE  s   

r  c                 C   sT   t |  ddd}d}tjt|d |tt W d    d S 1 s#w   Y  d S )NFrS   r   max_sampleszl`max_sample` cannot be set if `bootstrap=False`. Either switch to `bootstrap=True` or set `max_sample=None`.r  )r5   r   r  r'  r:   r;   r<   )rC   r   r  rG   rG   rH   test_max_samples_bootstrapM  s   "r  zmax_samples, exc_type, exc_msgg    eAz>`max_samples` must be in range 1 to 6 but got value 1000000000r  z=`max_samples` must be in range \(0.0, 1.0\] but got value 2.0r   z=`max_samples` must be in range \(0.0, 1.0\] but got value 0.0z=`max_samples` must be in range \(0.0, 1.0\] but got value nanz=`max_samples` must be in range \(0.0, 1.0\] but got value infzstr max_samples?!zD`max_samples` should be int or float, but got type '\<class 'str'\>'zN`max_samples` should be int or float, but got type '\<class 'numpy.ndarray'\>'c                 C   s"   t | tr| d d ddS | S )Nr)   ]rz  )
isinstancestrreplace)xrG   rG   rH   <lambda>  r   r$  )idsc                 C   sP   t |  d|d}tj||d |tt W d    d S 1 s!w   Y  d S )NTr  r  )r5   r   r  r:   r;   r<   )rC   r  exc_typeexc_msgr   rG   rG   rH   test_max_samples_exceptionsZ  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sDJ d S )Nr  r  r   )
train_sizerv   r0   Tr   r   r  r0   )	r   rb   rc   ra   r:   r=   r!   r   r   )rC   r   r   r   r   
ms_1_modelms_1_predictms_None_modelms_None_predictms_1_ms
ms_None_msrG   rG   rH   $test_max_samples_boundary_regressors  s   


r1  c           	      C   sr   t ttdtd\}}}}t|  dddd}||||}t|  dd dd}||||}tj|| d S )Nr   )r0   stratifyTr   r*  )	r   r   r   r9   r:   r   r~   r  r   )	rC   r   r   r   r  r+  
ms_1_probar-  ms_None_probarG   rG   rH   %test_max_samples_boundary_classifiers  s   r5  c                  C   s`   g dg} t g d}t }d}tjt|d || | W d    d S 1 s)w   Y  d S )Nr   r   z3sparse multilabel-indicator for y is not supported.r  )r   r   r   r  r'  r:   )r;   r<   r   msgrG   rG   rH   test_forest_y_sparse  s   
"r7  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sEJ |d S )Nr%   i'  r&   r   )r7   r0   r  z=Tree without `max_samples` restriction should have more nodes)r~   r   r   rs  r:   r   r   r  )	r8  r   r;   r<   est1est2tree1tree2r6  rG   rG   rH   'test_little_tree_with_small_max_samples  s&   r=  r  c                 C   sl   t ddgddgg}t ddg}|  ||}tjtdd |j W d    d S 1 s/w   Y  d S )Nr%   r&   r'   r   r   z`n_features_` was deprecatedr  )r~   r   r:   r   r#  FutureWarningn_features_)r  r;   r<   r   rG   rG   rH   test_n_features_deprecation  s   "r@  c                 C   sr   t ddgddgg}t ddg}| dd}d}tjt|d	 ||| W d
   d
S 1 s2w   Y  d
S )z9Check warning raised for max_features="auto" deprecation.r%   r&   r'   r   r   auto)r8   a  `max_features='auto'` has been deprecated in 1.1 and will be removed in 1.3. To keep the past behaviour, explicitly set `max_features=(1.0|'sqrt')` or remove this parameter as it is also the default value for RandomForest(Regressors|Classifiers) and ExtraTrees(Regressors|Classifiers)\.r  N)r~   r   r   r#  r>  r:   )r  r;   r<   r   r  rG   rG   rH   test_max_features_deprecation  s   
	"rB  z'old_criterion, new_criterion, Estimatormserg   maerh   c                 C   s   || dd}t jtd|  dd |tt W d    n1 s"w   Y  ||dd}|tt t|t|t d S )Nr   )rP   r0   zCriterion 'z' was deprecatedr  )r   r#  r>  r:   r;   r<   r   r=   )old_criterionnew_criterionr  r9  r:  rG   rG   rH   test_criterion_deprecated  s   	rG  Forestc                 C   sN   ddl m} tdd}|j\}}|||}t|  dd|d}|t| d S )Nr   )MSEr$   r%   r&   )r7   r   rP   )sklearn.tree._criterionrI  rc   r  rB   ra   r:   rb   )rH  rI  r<   r*   	n_outputsmse_criterionr   rG   rG   rH   -test_mse_criterion_object_segfault_smoke_test'  s   

rM  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)r7   r   rW  r0   c                 S   s    g | ]\}}d | d| qS )randomtreesembedding_r  rG   )r   r   leafrG   rG   rH   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)	r~   r   r   r   rs  r   r:   get_feature_names_outr   )r0   r;   r]  namesexpected_namesrG   rG   rH   -test_random_trees_embedding_feature_names_out7  s   rS  )rk   )r  )__doc__r9  r  collectionsr   	itertoolsr   r   typingr   r   numpyr~   scipy.sparser   r   r	   scipy.specialr
   r   r  numpy.testingr   sklearn.dummyr   sklearn.metricsr   sklearn.utils._testingr   r   r   r   r   r   sklearn.exceptionsr   sklearnr   sklearn.decompositionr   sklearn.datasetsr   sklearn.ensembler   r   r   r   r   sklearn.model_selectionr   r   sklearn.svmr   sklearn.utils.validationr    r!   sklearn.tree._classesr"   r;   r<   r>   r?   r   r   	load_irisrT   r   permutationrV   rp   permrU   make_regressionrb   rc   r  r  r  r   float32parallelget_active_backendr=  DEFAULT_JOBLIB_BACKENDr9   ra   r  dictr4   r!  __annotations__updater   r5   rI   markparametrizerM   rX   r]   rf   rj   r   r   r   r   r   r   r   r  chainr   r  r  r   rm  r  r"  r&  r   rB   r)  r.  r1  r2  r6  r8  rB  rC  rK  rL  rR  rU  rV  r_  re  rl  rn  ry  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  r   r  r  register_parallel_backendr  r  r  r  r   r'  naninfr+  r   r(  r1  r5  r7  r=  r@  rB  rG  rM  rS  rG   rG   rG   rH   <module>   s   
(





5


&m
	$%




3

7

6



/

	/

!




2






(,	
	