
    sg                    $   U d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	 ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl1m9Z: ddl;m<Z< ddl=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZFmGZGmHZH ddlImJZJ dZKdZLe(e*dZMe)e+dZN eO       ZPeOeQd<   ePj                  eM       ePj                  eN       g dZS ej                  g dg dg dg dg dg d g d!g d"g d#g d$g d%g d&g d'g d(g d)g d*g d+g d,g d-g d.g d/g d0g d1g      ZUg d2ZVg d3ZWd4d5gd5d5gd5d4gd6d6gd6d7gd7d6ggZXg d8ZYd5d5gd7d7gd9d7ggZZg d:Z[ ej                         Z]ej                  j                  d6      Z`e`j                  e]j                  j                        Zde]j                  ed   e]_e        e]j                  ed   e]_b         ej                         Zge`j                  egj                  j                        Zdegj                  ed   eg_e        egj                  ed   eg_b         ej                         Zie`j                  eij                  j                        Zdeij                  ed   ei_e        eij                  ed   ei_b         eJd      Zj ej                  dd;d<=      \  ZlZmejj                  d>?      Zod@eoeodAk  <   ejj                  ddBdC?      Zq e&dDd<dEdF      j                         Zse]j                  e]j                  dGegj                  egj                  dGeij                  eij                  dGeXeYdGeUeVdGeUeWdGelemdGeoeqdGeo eqdGeseqdG ej                  dH      eqdGdIZudJ ZvdK ZwdL Zxej                  j                  dMeNj                               ej                  j                  dNeL      dO               Z|dP Z}dQ Z~ej                  j                  dReNj                               ej                  j                  dNeL      dS               ZeCej                  j                  dReNj                               ej                  j                  dTdUdVedWfdXdDedWfdYdVedWfdZdVed;fg      d[                      Zd\ Zd] Zd^ Zd_ Zd` Zda Zdb Zdc Zdd Zde Zdf ZddgZej                  j                  dheP      di        Zej                  j                  dheS      ej                  j                  djeG      dk               Z	 ddlZej                  j                  dheP      dm        Zej                  j                  dheS      ej                  j                  djeG      dn               Zdo Zdp Zdq Zdr Zds Zdt Zdu Zdv Zej                  j                  dheM      dw        Zej                  j                  dheM      dx        Zdy Zdz Zd{ Zd| Zd} Zd~ Zd Zd Zd ZddZej                  j                  deS      ej                  j                  dd      d               Zej                  j                  d e eeS      jU                  eN                  ej                  j                  dddg      d               Zej                  j                  deS      ej                  j                  dg d      ej                  j                  djeG      d                      Zej                  j                  d e e	eSD  cg c]	  } | eNv s|  c} eL             e e	eSD  cg c]	  } | eMv s|  c} eK            z         ej                  j                  dg d      ej                  j                  djeG      d                      Zej                  j                  deS      ej                  j                  d eeGeH            d               Zd Zej                  j                  dheP      d        Zej                  j                  dheP      ej                  j                  ddgeGz         d               Zej                  j                  dheP      d        Zej                  j                  dheS      ej                  j                  deH      d               Zd Zej                  j                  dheP      d        Zej                  j                  dheP      ej                  j                  deH      d               Zd Zd Zej                  j                  ddgeGz         d        Zej                  j                  d e eeujy                               ddhz
              ej                  j                  de(e*g      d               Zej                  j                  deujy                               ej                  j                  de)e+g      d               Zd Zd Zd Zej                  j                  dheP      ej                  j                  dddg      ej                  j                  ddgeGz   eHz         d                      Zej                  j                  dNg d      ej                  j                  dMeNj                               d               Zej                  j                  d ed9            d        Zd Zej                  j                  dMe(e*g      ej                  j                  dd7dBg      d               Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zej                  j                  d e e/j                          e0j                                     d        Zd Zej                  j                  dMePj                               d        Zej                  j                  dNdUdYg      d        Zej                  j                  d ed9            ej                  j                  dNdUdYg      d               Zej                  j                  dNddg      d        Zej                  j                  dNddg      d        Zej                  j                  dNddg      d        Zej                  j                  dNddg      dÄ        Zej                  j                  ddgeHz         ej                  j                  d e)dXū       e+dXū      g      dƄ               Zej                  j                  dMeNj                               dǄ        ZdȄ Zej                  j                  dej                  e)dfej                  e+dfee(dfee*dfg      ej                  j                  dddg      dτ               Zej                  j                  d eeMj                         ddg            dӄ        Zej                  j                  dej                  e)fej                  e(fg      dՄ        Zdք Zej                  j                  dMe)e+g      ej                  j                  d ej                  ej                  d7ej                  dBddg       ej                  ej                  ej                  d9dBddg       ej                  d6d7d9dBej                  ej                  g       ej                  d6d7d9ej                  dej                  g      g      ej                  j                  dNdUdYg      dڄ                      Zdۄ Zd܄ Zd݄ Zdބ Zyc c} w c c} w )z-
Testing for the tree module (sklearn.tree).
    N)chainproduct)NumpyPickler)assert_allclose)clonedatasetstree)DummyRegressor)NotFittedError)SimpleImputer)accuracy_scoremean_poisson_deviancemean_squared_error)cross_val_scoretrain_test_split)make_pipeline)_sparse_random_matrix)DecisionTreeClassifierDecisionTreeRegressorExtraTreeClassifierExtraTreeRegressor)CRITERIA_CLFCRITERIA_REGDENSE_SPLITTERSSPARSE_SPLITTERS)
NODE_DTYPE	TREE_LEAFTREE_UNDEFINED_build_pruned_tree_py_check_n_classes_check_node_ndarray_check_value_ndarray)Tree)compute_sample_weight)assert_almost_equalassert_array_almost_equalassert_array_equalcreate_memmap_backed_dataignore_warningsskip_if_32bit)	_IS_32BITCOO_CONTAINERSCSC_CONTAINERSCSR_CONTAINERS)check_random_state)ginilog_loss)squared_errorabsolute_errorfriedman_msepoisson)r   r   )r   r   	ALL_TREES)r   r      r   r   r      ir   r   r   r   r   )r   r         r   r9   r   r   r8   皙?r   r7   r8   )r>   r   r         r   r    @r8   r   r   r?   r   r8   )r>   r>   r   g333333r   r   r   r   r   r   r=   r   r   r8   )r>   r>   r   r   r   r   r   r;   r   r   r   r   r   r8   )r>   r   r7   
   r7   r   皙	r   r7   r;   r9   r8   )zG @r         r      r   r   rD            ?r   rB   r8   )rE   r   rF   rG   r   rH   r   r   rD   rI   r   r   rA   r8   )rE      rF   rG   r   rH   r   r   rD   rI   r   r   rA   r8   )rE   rK   rF   rG   r   rH   r   r   rD   rI   rJ   r   r>   r   )   rK   r:   r8   rJ   r9   rC   r   r8   r<   r;   r   rL   r   )rL   r   r8   r8   r8   r>   r8   r   r   rA   r;   r   r8   r   )rL   r   r8   rL   r;   r>   rC   rL   r   r>   r8   rL   rL   r   )r8   r8   r   rL   rL   r>   r8   rL   r   r<   r8   rL   r;   r   )r;   r8   r   r;   r   r9   rC   r   r8   r<   r;   r   r;   r8   )rE   rK   rF   rG   r   r8   r   r   rD   rI   rJ   r   rB   r8   )rE   rK   rF   rG   r   r8   r   r   rD   rI         ?r8   r>   r>   )rE   rK   rF   rG   r   rC   r   r   rD   rI   rJ   r   r>   r>   )rL   r   r:   r8   rJ   rA   rC   r   r8   r<   r;   r8   r   r>   )rL   r   r8   r8   r8   rA   r8   r   r   rA   r   r   r   r8   )rL   r8   r8   r8   rL   r>   rC   rL   r   r>   r   rL   r8   r8   )r8   r8   r   r   r8   rB   r8   rL   r   r<   r8   rL   r8   r8   )r;   r8   r   r8   r   r9   r8   r   r8   rA   r   r   r8   r   )r8   r8   r   r   r   r   r8   r8   r8   r8   r8   r8   r   r   r   r8   r   r   r8   r   r   r   r   )      ?r@   333333?皙?rC   g333333@@g)\(?{Gz?gףp=
@rQ   g?        rO   rL   rH   r   r         @g|?5^?g(\??r   rA   r>   r8   rL   )r>   r>   r>   r8   r8   r8   r;   )r>   r8   r8      rC   )random_state	n_samples
n_features)   r:   sizerS   g?r7   )rZ   rZ   g      ?)densityrW   Xy)rZ   r;   )irisdiabetesdigitstoy	clf_small	reg_small
multilabel
sparse-pos
sparse-neg
sparse-mixzerosc                 ~   |j                   | j                   k(  s,J dj                  ||j                   | j                                t        | j                  |j                  |dz          t        | j                  |j                  |dz          | j                  t
        k(  }t        j                  |      }t        | j                  |   |j                  |   |dz          t        | j                  |   |j                  |   |dz          t        | j                  j                         |j                  j                         |dz          t        | j                  |j                  |dz          t        | j                  |j                  |dz   	       t        | j                  |   |j                  |   |d
z   	       y )Nz({0}: inequal number of node ({1} != {2})z: inequal children_rightz: inequal children_leftz: inequal featuresz: inequal thresholdz: inequal sum(n_node_samples)z: inequal n_node_samplesz: inequal impurityerr_msgz: inequal value)
node_countformatr'   children_rightchildren_leftr   nplogical_notfeature	thresholdn_node_samplessumr%   impurityr&   value)dsmessageexternalinternals        O/var/www/html/venv/lib/python3.12/site-packages/sklearn/tree/tests/test_tree.pyassert_tree_equalr      s   	$188q||$
 	!**G6P,P 	'4M*M 9,H~~h'H			(QYYx0'<P2P 	Hq{{84g@U6U 		11
 	!**G6P,P 

AJJBV8VW	1778,g@Q6Q    c                     t         j                         D ]  \  } } |d      }|j                  t        t               t        |j                  t              t        dj                  |               |dd      }|j                  t        t               t        |j                  t              t        dj                  |               y )Nr   rW   Failed with {0}r8   )max_featuresrW   )
	CLF_TREESitemsfitr_   r`   r'   predictTtrue_resultrp   namer#   clfs      r   test_classification_toyr      s    oo' X
d"13;;q>;8I8P8PQU8VW213;;q>;8I8P8PQU8VWXr   c            
         t         j                         D ]  \  } } |d      }|j                  t        t        t        j                  t        t                           t        |j                  t              t        dj                  |              |j                  t        t        t        j                  t        t              d             t        |j                  t              t        dj                  |               y )Nr   r   sample_weightr   rJ   )r   r   r   r_   r`   rs   oneslenr'   r   r   r   rp   fullr   s      r    test_weighted_classification_toyr      s    oo' X
d"1BGGCFO43;;q>;8I8P8PQU8VW1BGGCFC$893;;q>;8I8P8PQU8VWXr   r#   	criterionc                    |dk(  rht        j                  t        j                  t                    dz   }t        j                  t              |z   }t        j                  t
              |z   }nt        }t
        } | |d      }|j                  t        |       t        |j                  t              |        | |dd      }|j                  t        |       t        |j                  t              |       y )Nr5   r8   r   rW   r   r   rW   )rs   absminr`   arrayr   r   r_   r   r   r   )r#   r   ay_trainy_testregr   s          r   test_regression_toyr     s     I FF266!9!((1+/+&*

3CGGAwCKKNF+

CCGGAwCKKNF+r   c                     t        j                  d      } d| d dd df<   d| dd dd f<   t        j                  | j                        \  }}t        j                  |j                         |j                         g      j                  }| j                         } t        j                         D ]  \  }} |d      }|j                  ||        |j                  ||       dk(  sJ dj                  |              |dd      }|j                  ||        |j                  ||       dk(  r~J dj                  |              y )	N)rC   rC   r8   r:   r   r   rN   r   rW   r   )rs   rk   indicesshapevstackravelr   r   r   r   scorerp   )r`   gridxgridyr_   r   r#   r   s          r   test_xorr     s   
AAbqb"1"fIAab!"fI::agg&LE5
		5;;=%++-0133A		Aoo' F
d"1yyA#%E'8'?'?'EE%21yyA#%E'8'?'?'EE%Fr   c                     t        t        j                         t              D ]"  \  \  } }} ||d      }|j	                  t
        j                  t
        j                         t        |j                  t
        j                        t
        j                        }|dkD  sJ dj                  | ||              ||dd      }|j	                  t
        j                  t
        j                         t        |j                  t
        j                        t
        j                        }|dkD  rJ dj                  | ||              y )Nr   r   rU   z0Failed with {0}, criterion = {1} and score = {2}rL   r   rJ   )r   r   r   CLF_CRITERIONSr   ra   datatargetr   r   rp   )r   r#   r   r   r   s        r   	test_irisr   2  s    #*9??+<n#M 
tiYQ7		4;;'s{{4995t{{Cs{ 	
NUU)U
 	
{ YQQG		4;;'s{{4995t{{Cs{ 	
NUU)U
 	
{
r   z
name, Treec                 2    ||d      }|j                  t        j                  t        j                         t	        t        j                  |j                  t        j                              }|t        j                  d      k(  sJ d|  d| d|        y )Nr   r   zFailed with z, criterion = z and score = )r   rb   r   r   r   r   pytestapprox)r   r#   r   r   r   s        r   test_diabetes_overfitr   D  s    
 
3CGGHMM8??+xHMM0JKEFMM	  J	dV>)M%IJ r   z&criterion, max_depth, metric, max_lossr2      <   r3   r4   r5   c                      |||dd      }|j                  t        j                  t        j                          |t        j                  |j	                  t        j                              }d|cxk  r|k  sJ  J y )NrI   r   )r   	max_depthr   rW   )r   rb   r   r   r   )r   r#   r   r   metricmax_lossr   losss           r   test_diabetes_underfitr   Q  sa     iaVW
XCGGHMM8??+(//3;;x}}#=>Dthr   c            	          t         j                         D ]v  \  } } |ddd      }|j                  t        j                  t        j
                         |j                  t        j                        }t        t        j                  |d      t        j                  t        j                  j                  d         dj                  |              t        t        j                  |d      |j                  t        j                        dj                  |              t!        |j                  t        j                        t        j"                  |j%                  t        j                              ddj                  |              y y )Nr8   *   r   r   rW   r   r   rm   rK   )r   r   r   ra   r   r   predict_probar&   rs   rx   r   r   rp   r'   argmaxr   r%   exppredict_log_proba)r   r#   r   prob_predicts       r   test_probabilityr   f  s     oo' 

dQQR@		4;;'((3!FF<#GGDIIOOA&'%,,T2	

 	IIlA&KK		"%,,T2	

 	dii(FF3((34%,,T2		

r   c                      t        j                  d      d d t         j                  f   } t        j                  d      }t        j	                         D ]!  \  }} |d d      }|j                  | |       # y )Ni'  r   r   rW   )rs   arangenewaxis	REG_TREESr   r   r_   r`   r   r#   r   s        r   test_arrayreprr     s`     			%BJJ'A
		%Aoo' 
dT21r   c                     ddgddgddgddgddgddgg} g d}t         j                         D ]L  \  }} |d      }|j                  | |       t        |j	                  |       |dj                  |      	       N t        j                         D ]L  \  }} |d      }|j                  | |       t        |j	                  |       |dj                  |      	       N y )
NrA   r>   r8   rL   )r8   r8   r8   r8   r8   r8   r   r   r   rm   )r   r   r   r'   r   rp   r   r%   )r_   r`   r   TreeClassifierr   TreeRegressorr   s          r   test_pure_setr     s    
bB8b"X1v1v1v>AA ) 1 Vn!,13;;q>16G6N6Nt6TUV
  )0 Wm+1CKKNA7H7O7OPT7UVWr   c            
         t        j                  g dg dg dg dg dg dg dg      } t        j                  g d      }t        j                  d	
      5  t        j	                         D ]Z  \  }} |d      }|j                  | |       |j                  | |        |j                  |  |       |j                  |  |        \ 	 d d d        y # 1 sw Y   y xY w)N)gs_c@d	a@籛 `8`@?c@)g_9a@g 8`@g-Vu]@g    @Xd@)gSW j_@r   r   r   )g ً`@4Ta@	lKa@{c@)g|@Y@g~G`a@gwI?lKa@g/"c@)g_@r   r   r   )g:^@r   r   r   )rN   gAw?gtQ?5??rS   g7G?gۺ?gb'?raise)allr   r   )rs   r   errstater   r   r   r   s        r   test_numerical_stabilityr     s    
DDDDDDD	

	A 	WXA		! #//+ 	JD$A&CGGAqMGGArNGGQBNGGQBO	  s   A2CCc            	         t        j                  ddddddd      \  } }t        j                         D ]  \  }} |d      }|j	                  | |       |j
                  }t        j                  |dkD        }|j                  d   dk(  sJ d	j                  |             |dk(  rsJ d	j                  |              t        d      }|j	                  t        j                  t        j                         t        dt        t        j                        
      }|j	                  t        j                  t        j                         t        |j
                  |j
                         y )N  rC   r;   r   FrX   rY   n_informativen_redundant
n_repeatedshufflerW   r   皙?r   rW   max_leaf_nodes)r   make_classificationr   r   r   feature_importances_rs   rx   r   rp   r   ra   r   r   r   r'   )r_   r`   r   r#   r   importancesn_importantclf2s           r   test_importancesr     s)   ''DAq  oo' @
d"1..ff[3./  #r)I+<+C+CD+II)a?!2!9!9$!??@ !a
0CGGDIIt{{#!qTYYPDHHTYY$s//1J1JKr   c                      t               } t        j                  t              5  t	        | d       d d d        y # 1 sw Y   y xY w)Nr   )r   r   raises
ValueErrorgetattr)r   s    r   test_importances_raisesr     s6    
 
"C	z	" -+,- - -s	   :Ac            	         t        j                  ddddddd      \  } }t        ddd	      j                  | |      }t	        d
dd	      j                  | |      }t        |j                  |j                         t        |j                  j                  |j                  j                         t        |j                  j                  |j                  j                         t        |j                  j                  |j                  j                         t        |j                  j                  |j                  j                         y )Ni  rC   r;   r   Fr   r0   r:   )r   r   rW   r2   )r   r   r   r   r   r%   r   r'   tree_ru   rr   rq   rw   )r_   r`   r   r   s       r   )test_importances_gini_equal_squared_errorr     s     ''DAq !6QQ
O
S
S	1C  !QQ	c!Qi  00#2J2JKsyy((#))*;*;<syy..		0G0GHsyy//1I1IJsyy//1I1IJr   c                  V   t         j                         D ]  \  } } |d      }|j                  t        j                  t        j
                         |j                  t        t        j                  t        j                  j                  d               k(  sJ  |d      }|j                  t        j                  t        j
                         |j                  t        t        j                  t        j                  j                  d               k(  sJ  |d      }|j                  t        j                  t        j
                         |j                  dk(  sJ  |d      }|j                  t        j                  t        j
                         |j                  dk(  sJ  |d      }|j                  t        j                  t        j
                         |j                  dk(  sJ  |d      }|j                  t        j                  t        j
                         |j                  t        dt        j                  j                  d   z        k(  sJ  |d      }|j                  t        j                  t        j
                         |j                  t        j                  j                  d   k(  sJ  |d       }|j                  t        j                  t        j
                         |j                  t        j                  j                  d   k(  rJ  y )	Nsqrt)r   r8   log2r;   rR   rJ   rN   )r6   r   r   ra   r   r   max_features_intrs   r   r   r   )r   TreeEstimatorests      r   test_max_featuresr     s$   (0 7m0		4;;'  C		0B(C$DDDD0		4;;'  C		0B(C$DDDD+		4;;'  A%%%+		4;;'  A%%%.		4;;'  A%%%-		4;;'  Cdiiooa.@(@$AAAA-		4;;'  DIIOOA$6666.		4;;'  DIIOOA$6666?7r   c                  J	   t         j                         D ]o  \  } } |       }t        j                  t              5  |j                  t               d d d        |j                  t        t               g dg}t        j                  t              5  |j                  |       d d d         |       }t        d d }t        j                  t              5  |j                  t        |       d d d        t        j                  t              } |       }|j                  |t               t        |j                  t              t                |       }t        j                  t              5  |j                  t               d d d        |j                  t        t               t        j                   t              }t        j                  t              5  |j                  |d d dd f          d d d        t        j"                  t              j                  } |       }|j                  t        j$                  t        |      t               t        j                  t              5  |j                  t               d d d        t        j                  t              5  |j'                  t               d d d         |       }|j                  t        t               t        j                  t              5  |j                  |       d d d        t        j                  t              5  |j'                  |       d d d         |       }t        j                  t              5  |j'                  t               d d d        r t)        d      }t        j                  t        d      5  |j                  g dgg d	       d d d        t        j                  t        d
      5  |j                  g dgg d       d d d        y # 1 sw Y   xY w# 1 sw Y   vxY w# 1 sw Y   ;xY w# 1 sw Y   xY w# 1 sw Y   OxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   \xY w# 1 sw Y   6xY w# 1 sw Y   uxY w# 1 sw Y   xY w# 1 sw Y   y xY w)N)rA   r>   r8   r>   r8   r5   r   zy is not positive.*Poissonmatchr   r8   rL   )r   r   r   zSome.*y are negative.*Poisson)r:   grL   )r   r   r   r   r   r   r_   r   r`   r   rs   asfortranarrayr%   r   r   r   asarrayr   dotapplyr   )	r   r   r   X2y2XftXtr   s	            r   
test_errorr    sH   (0 6mo]]>* 	!a 	! 	1]]]:& 	"b!	" osV]]:& 	GGArN	 q!oACKKNK8 o]]>* 	KKN	 	1JJqM]]:& 	"KK!QR%!	" XXa[]]oq"q!]]:& 	KKN	]]:& 	IIaL	 o1]]:& 	KKO	]]:& 	IIbM	 o]]>* 	IIaL	 	k6r  )
4C	z)E	F (Y'(	z)H	I +\*+ +s	! 	!
	" 	"	 		 		" 	"	 		 	
	 		 	
	 	
( (+ +s   PPP%-P2P?+Q"Q:Q&-Q3'R /R*RP	P"	%P/	2P<	?Q		Q	Q#	&Q0	3Q=	 R
	RR"c                     t        j                  t        j                  t        j
                  j                        } t        j                  }t        dt        j                               D ]  \  }}t        |   } |d|d      }|j                  | |       |j                  j                  |j                  j                  dk7     }t        j                  |      dkD  sJ dj!                  |              |d	|d      }|j                  | |       |j                  j                  |j                  j                  dk7     }t        j                  |      dkD  rJ dj!                  |              y
)z Test min_samples_split parameterdtypeN  rC   r   )min_samples_splitr   rW   r>   	   r   r=   N)rs   r  ra   r   r	   _treeDTYPEr   r   r6   keysr   r   rw   rr   r   rp   )r_   r`   r   r   r   r   node_sampless          r   test_min_samples_splitr  ^  s4   
$))4::+;+;<AA !(inn6F G H!$  a
 	1yy//		0G0G20MNvvl#a'G):)A)A$)GG' !.q
 	1yy//		0G0G20MNvvl#a'G):)A)A$)GG'+Hr   c                     t        j                  t        j                  t        j
                  j                        } t        j                  }t        dt        j                               D ]  \  }}t        |   } |d|d      }|j                  | |       |j                  j                  |       }t        j                  |      }||dk7     }t        j                  |      dkD  sJ dj!                  |              |d|d      }|j                  | |       |j                  j                  |       }t        j                  |      }||dk7     }t        j                  |      dkD  rJ dj!                  |              y )	Nr  r  r:   r   )min_samples_leafr   rW   r7   r   r   )rs   r  ra   r   r	   r  r  r   r   r6   r  r   r   r  bincountr   rp   )	r_   r`   r   r   r   r   outnode_counts
leaf_counts	            r   test_min_samples_leafr  }  sL   
$))4::+;+;<AA !(inn6F G F!$ ~A
 	1iiooa kk#& !12
vvj!A%E'8'?'?'EE%  a
 	1iiooa kk#& !12
vvj!A%E'8'?'?'EE%/Fr   c                    t         |   d   j                  t        j                        }| ||      }t         |   d   }t        j                  |j                  d         }t        j                  |      }t        |    }t        dt        j                  ddd            D ]  \  }}	 ||	|d      }
|
j                  |||	       |*|
j                  j                  |j                               }n|
j                  j                  |      }t        j                  ||
      }||dk7     }t        j                   |      ||
j"                  z  k\  rJ dj%                  | |
j"                                |j                  d   }t        dt        j                  ddd            D ]  \  }}	 ||	|d      }
|
j                  ||       |*|
j                  j                  |j                               }n|
j                  j                  |      }t        j                  |      }||dk7     }t        j                   |      ||
j"                  z  k\  rJ dj%                  | |
j"                                y)zPTest if leaves contain at least min_weight_fraction_leaf of the
    training setr_   Nr`   r   r  rJ   rI   )min_weight_fraction_leafr   rW   r   )weightsz,Failed with {0} min_weight_fraction_leaf={1})DATASETSastypers   float32rngrandr   rx   r6   r   linspacer   r   r  tocsrr  r   r!  rp   )r   r   sparse_containerr_   r`   r"  total_weightr   r   fracr   r  node_weightsleaf_weightss                 r   check_min_weight_fraction_leafr/    s5    	3&&rzz2A#Q3Ahhqwwqz"G66'?LdOM !(bkk!S!6L M 
%).WX
 	1G,'))//!''),C))//!$C{{38#LA$56FF< L33O3O$OO	
9@@#..
	
O
* 771:L 'bkk!S!6L M 
%).WX
 	1'))//!''),C))//!$C{{3'#LA$56FF< L33O3O$OO	
9@@#..
	
O
r   r   c                     t        | d       y Nra   r/  r   s    r   ,test_min_weight_fraction_leaf_on_dense_inputr4    s    "40r   csc_containerc                      t        | d|       y Nrg   )r*  r2  r   r5  s     r   -test_min_weight_fraction_leaf_on_sparse_inputr9    s     #4Vr   c                    t         |   d   j                  t        j                        }| ||      }t         |   d   }|j                  d   }t
        |    }t        dt        j                  ddd            D ]  \  }} |||dd	      }	|	j                  ||       |*|	j                  j                  |j                               }
n|	j                  j                  |      }
t        j                  |
      }||dk7     }t        j                  |      t        ||	j                  z  d      k\  rJ d
j!                  | |	j                  |	j"                                t        dt        j                  ddd            D ]  \  }} |||dd	      }	|	j                  ||       |*|	j                  j                  |j                               }
n|	j                  j                  |      }
t        j                  |
      }||dk7     }t        j                  |      t        ||	j                  z  ||	j"                  z        k\  rJ d
j!                  | |	j                  |	j"                                y)zzTest the interaction between min_weight_fraction_leaf and
    min_samples_leaf when sample_weights is not provided in fit.r_   Nr`   r   r  rJ   r;   r:   )r!  r   r  rW   zBFailed with {0} min_weight_fraction_leaf={1}, min_samples_leaf={2}r   )r#  r$  rs   r%  r   r6   r   r(  r   r   r  r)  r  r   maxr!  rp   r  )r   r   r*  r_   r`   r+  r   r   r,  r   r  r-  r.  s                r   4check_min_weight_fraction_leaf_with_min_samples_leafr<    sL   
 	3&&rzz2A#Q3A771:LdOM 'bkk!S!6L M 
%))	
 	1'))//!''),C))//!$C{{3'#LA$56vvl#sC8881(
 
 	
OVV#..0D0D
	
 
%
. !(bkk!S!6L M 
%)) 	
 	1'))//!''),C))//!$C{{3'#LA$56vvl#sC888C000(
 
 	
 PVV#..0D0D
	
 
%
r   c                     t        | d       y r1  r<  r3  s    r   Btest_min_weight_fraction_leaf_with_min_samples_leaf_on_dense_inputr?  !  s    8vFr   c                      t        | d|       y r7  r>  r8  s     r   Ctest_min_weight_fraction_leaf_with_min_samples_leaf_on_sparse_inputrA  &  s    
 9l]r   c                    t        j                  d|       \  }}t        dt        j	                               D ]  \  }}t        |   } ||d      } ||dd      } ||dd      } ||d	d      }	|d
f|df|df|	d	ffD ]  \  }
}|
j
                  |k  s!J dj                  |
j
                  |             |
j                  ||       t        |
j                  j                        D ]M  }|
j                  j                  |   t        k7  s%|
j                  j                  |   }|
j                  j                  |   }|
j                  j                  |   }|
j                  j                  |   }|
j                  j                  |   }||z  }|
j                  j                  |   }|
j                  j                  |   }|
j                  j                  |   }||z  }||z   }||z  }|
j                  j                  |   |j                   d   z  }|||z
  z  }||k\  r9J dj                  ||                y )Nd   rX   rW   r  r   r   rW   rP   )r   min_impurity_decreaserW   g-C6?r   gHz>z)Failed, min_impurity_decrease = {0} > {1}z2Failed with {0} expected min_impurity_decrease={1})r   r   r   r6   r  rF  rp   r   ranger   ro   rr   r   ry   weighted_n_node_samplesrq   r   )global_random_seedr_   r`   r   r   r   est1est2est3est4r   expected_decreasenode
imp_parent
wtd_n_nodeleft
wtd_n_leftimp_leftwtd_imp_leftrightwtd_n_right	imp_rightwtd_imp_rightwtd_avg_left_right_impfractional_node_weightactual_decreases                             r   test_min_impurity_decreaser]  0  st    ''#DVWDAq !(inn6F G >!$ NK)TU
 )VW
 )ST

 4L4L6N3K	'
 ,	"C" ))->>:AA))+<> GGAqMcii223   99**40I=!$!3!3D!9J!$!B!B4!HJ992248D!$!B!B4!HJ"yy11$7H#-#8LII44T:E"%))"C"CE"JK #		 2 25 9I$/)$;M-:\-I**j8* 		99$?!''!*L + '="%;;'O
 (+<<KRR'):<; ,	%>r   c            
         t         j                         D ]C  \  } }d| v r!t        j                  t        j                  }}n t
        j                  t
        j                  }} |d      }|j                  ||       |j                  ||      }g d}|D ci c]  }|t        |j                  |       }}t        j                  |      }	t        j                  |	      }
t        |
      |j                  k(  sJ |
j                  ||      }||k(  sJ dj                  |              |D ]-  }t!        t        |
j                  |      ||   d| d|         / F y	c c}w )
z8Test pickling preserves Tree properties and performance.
Classifierr   r   )r   ro   capacity	n_classesrr   rq   n_leavesru   rv   ry   rw   rH  rz   z6Failed to generate same score  after pickling with {0}z"Failed to generate same attribute z after pickling with rm   N)r6   r   ra   r   r   rb   r   r   r   r   pickledumpsloadstype	__class__rp   r'   )r   r   r_   r`   r   r   
attributes	attributefitted_attributeserialized_objectrK  score2s               r   test_picklerm  x  sW   (0 .m499dkkqA==(//qA+1		!Q

  GQ
9BIwsyy)44
 
 #LL-||-.DzS]]***Aq!VO	QCJJ4P	Q) 	I

I. +8 Dv		M.4
s   Ec                     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         j                         D ]  \  }} |d      }|j                  | |      j                  |      }t	        ||       |j
                  dk(  sJ |j                  |      }t        |      dk(  sJ |d   j
                  dk(  sJ |d   j
                  d	k(  sJ |j                  |      }	t        |	      dk(  sJ |	d   j
                  dk(  sJ |	d   j
                  d	k(  rJ  t        j                         D ]L  \  }}
 |
d      }|j                  | |      j                  |      }t        ||       |j
                  dk(  rLJ  y )
NrA   r>   r8   rL   r   r;   r   r7   rL   )r7   r7   )r   r   r   r   r'   r   r   r   r   r   r%   )r_   r`   r   y_truer   r   r   y_hatproba	log_probar   r   s               r   test_multioutputrt    sR    
R	R	R	
A	
A	
A	Q	Q	Q	
B	
B	
B	A  
Q	Q	Q	
A	
A	
A	Q	Q	Q	
A	
A	
A	A bAq6B7QG,A1g1vAwA/F !* 1 ,n!,1%%a(5&){{f$$$!!!$5zQQx~~'''Qx~~'''))!,	9~"""|!!V+++|!!V+++,"  )0 %m+1%%a(E6*{{f$$$	%r   c                  d   t         j                         D ]  \  } } |d      }|j                  t        t               |j
                  dk(  sJ t        |j                  ddg       t        j                  t        t        j                  t              dz  f      j                  } |d      }|j                  t        |       t        |j
                        dk(  sJ t        |j                        dk(  sJ t        |j
                  ddg       t        |j                  ddgddgg        y )Nr   r   rL   r>   r8   rA   )r   r   r   r_   r`   
n_classes_r'   classes_rs   r   r   r   r   )r   r   r   _ys       r   test_classes_shapery    s     ) 1 =n!,1~~"""3<<"a1 YY288A;?+,..!,23>>"a'''3<< A%%%3>>Aq623<<2q'B7);<=r   c                     t         j                  d d } t         j                  d d }t        d|      }t        j                         D ]=  \  }} |d      }|j                  | ||       t        |j                  |       |       ? y )N}   balancedr   r   r   )	ra   r   r   r$   r   r   r   r%   r   )unbalanced_Xunbalanced_yr   r   r   r   s         r   test_unbalanced_irisr    sy    99Tc?L;;t$L)*lCM ) 1 En!,l-HCKK5|DEr   c                     t        t        j                         t        j                  t        j
                  g      D ]  \  \  } }} |d      }t        j                  t        j                  |      }t        j                  }t        |j                  ||      j                  |      |       t        j                  t        j                  d|      }t        j                  }t        |j                  ||      j                  |      |       t        j                  t        j                  d|      }t        j                  }t        |j                  ||      j                  |      |       t        j                  t        j                  |      }t        j                  }t        |j                  ||      j                  |      |       t        D ]U  } |t        j                  |      }t        j                  }t        |j                  ||      j                  |      |       W t        D ]U  } |t        j                  |      }t        j                  }t        |j                  ||      j                  |      |       W t        j                  t        j                  d d d   |      }t        j                  d d d   }t        |j                  ||      j                  |      |        y )Nr   r   r  C)orderr  Fr;   )r   r6   r   rs   float64r%  r  ra   r   r   r'   r   r   ascontiguousarrayr.   r-   )r   r   r  r   r_   r`   csr_containerr5  s           r   test_memory_layoutr    s%   (/BJJ

3) (8$}u + JJtyy.KK3771a=003Q7 JJtyy59KK3771a=003Q7 JJtyy59KK3771a=003Q7   %8KK3771a=003Q7 , 	<Mdiiu5AAswwq!}44Q7;	< , 	<Mdiiu5AAswwq!}44Q7;	< JJtyy1~U3KK!3771a=003Q7Q(8r   c                  z   t        j                  d      d d t         j                  f   } t        j                  d      }d|d d t        j                  d      }d||dk(  <   t	        d      }|j                  | ||       t        |j                  |       t        j                  d             t        j                  d      d d t         j                  f   } t        j                  d      }d|dd d	|dd d| dddf<   t        j                  d      }d
||d	k(  <   t	        dd      }|j                  | ||       |j                  j                  d   dk(  sJ d||d	k(  <   t	        dd      }|j                  | ||       |j                  j                  d   dk(  sJ t        j                  } t        j                  }t        j                  d| j                   d   d      }t	        d      }|j                  | |   ||          t        j"                  || j                   d         }t	        d      }|j                  | ||       |j                  j$                  t&        j(                  j*                  k7  }t-        |j                  j                  |   |j                  j                  |          y )NrC  rS   2   r   r   r      r8   rL   gRQ?r   g     b@rJ   g     H@)	minlength)rs   r   r   r   r   r   r'   r   rk   r   rv   ra   r   r   r&  randintr   r  rr   r	   r  r   r&   )r_   r`   r   r   
duplicatesr   r   s          r   test_sample_weightr  4  sA    			#q"**}%A
AAcrFGGCLMM!q&
 a
0CGGAqG.s{{1~rwws|4 			#q"**}%A
AAbIAc#JAc#gqjMGGCLM M!q&
 11
=CGGAqG.99q!U***M!q&
 11
=CGGAqG.99q!T))) 			AAQ
C0J
 a
0CGGAjM1Z=)KK
aggajAM!q1DHHQH/yy&&$***>*>>H		H%tzz';';H'Er   c                  D   t        j                  d      d d t         j                  f   } t        j                  d      }d|d d t	        d      }t         j
                  j                  dd      }t        j                  t              5  |j                  | ||       d d d        t        j                  d      }t        j                  d      }t        j                  t        |	      5  |j                  | ||       d d d        y # 1 sw Y   lxY w# 1 sw Y   y xY w)
NrC  rS   r  r   r   r8   r   zgInput should have at least 1 dimension i.e. satisfy `len(x.shape) > 0`, got scalar `array(0.)` instead.r   )rs   r   r   r   r   randomr'  r   r   r   r   r   reescape	TypeError)r_   r`   r   r   expected_errs        r   test_sample_weight_invalidr  h  s    
		#q"**}%A
AAcrF
 a
0CIINN3*M	z	" 31M23 HHQKM99BL 
y	5 31M23 33 33 3s   
D
,D
DDc                    t         |    } |d      }|j                  t        j                  t        j                          |dd      }|j                  t        j                  t        j                         t        |j                  |j                         t        j                  t        j                  t        j                  t        j                  f      j                  } |ddddddddddddgd      }|j                  t        j                  |       t        |j                  |j                          |dd      }|j                  t        j                  |       t        |j                  |j                         t        j                  t        j                  j                        }|t        j                  dk(  xx   d	z  cc<   dd
dd} |d      }|j                  t        j                  t        j                  |        ||d      }|j                  t        j                  t        j                         t        |j                  |j                          |d      }|j                  t        j                  t        j                  |dz          ||d      }|j                  t        j                  t        j                  |       t        |j                  |j                         y )Nr   r   r|  class_weightrW   g       @rN   r  r8   rC  g      Y@rL   )r   r   ra   r   r   r%   r   rs   r   r   r   r   )	r   r   clf1r   
iris_multiclf3clf4r   r  s	            r   test_class_weightsr    s    t_N q)DHHTYY$zBDHHTYY$1143L3LM DKKdkkBCEEJ$$$

 D 	HHTYY
#1143L3LMzBDHHTYY
#1143L3LM GGDKK--.M$++"#s*#u-Lq)DHHTYY]3|!DDHHTYY$1143L3LM q)DHHTYY]A%56|!DDHHTYY]31143L3LMr   c                 @   t         |    }t        j                  t        t        j                  t              dz  f      j
                  } |dddgd      }d}t        j                  t        |      5  |j                  t        |       d d d        y # 1 sw Y   y xY w)	NrL   rJ   rN   r>   r8   r   r  zBnumber of elements in class_weight should match number of outputs.r   )r   rs   r   r`   r   r   r   r   r   r   r_   )r   r   rx  r   rn   s        r   test_class_weight_errorsr    s}     t_N	Arxx{Q'	(	*	*B CC'8&9
JCRG	z	1 2  s   4BBc                      t        j                  dd      \  } }d}t        j                         D ]:  \  }} |d |dz         j	                  | |      }|j                         |dz   k(  r:J  y NrC  r8   rD  r7   )r   r   )r   make_hastie_10_2r6   r   r   get_n_leavesr_   r`   kr   r   r   s         r   test_max_leaf_nodesr    sp    $$sCDAq	A(0 +md1q5AEEaK!QU***+r   c                      t        j                  dd      \  } }d}t        j                         D ]4  \  }} |d|      j	                  | |      }|j                         dk(  r4J  y r  )r   r  r6   r   r   	get_depthr  s         r   test_max_leaf_nodes_max_depthr    se    $$sCDAq	A(0 $ma:>>q!D}}!###$r   c                      dD ]]  } t        t               j                  dgdggddg      j                  |       }d|j                  d   cxk  rdk  rPJ d        J d        y )N)ra  rz   rr   rq   rv   ry   ru   rw   r   r8   rB   r;   z Array points to arbitrary memory)r   r   r   r   flat)attrrz   s     r   test_arrays_persistr    st    	 K .044qcA3Z!QHNNPTUUZZ]&Q&J(JJ&J(JJ&Kr   c                     t        d      } t        j                  d      }| j                  ddd      }t        j                         D ];  \  }} |d      }|j                  ||       |j                  j                  dk(  r;J  y )Nr   )rC   rZ   rL   )rC   r   )	r/   rs   rk   r  r6   r   r   r   r   )rW   r_   r`   r   r   r   s         r   test_only_constant_featuresr    sx    %a(L
AQ5)A(0 (m+1yy""a'''(r   c                  r   t        j                  t        j                  g dgt        j                  d      f            } g d}t        j                         D ]\  \  }}d|vs |dd      }|j                  | |       |j                  j                  dk(  sJ |j                  j                  d	k(  r\J  y )
N)r   r   r   r   r   r8   rL   r7   r:   rI      )r7   rH   )r   r   r   r8   r8   rL   rL   rL   r;   r;   r;   	ExtraTreer   r8   r   rL   r:   )
rs   	transposer   rk   r6   r   r   r   r   ro   r_   r`   r   r   r   s        r   ,test_behaviour_constant_feature_after_splitsr    s    

		568IJK	A 	*A(0 -md"QQ?CGGAqM99&&!+++99''1,,,-r   c                     t        j                  t        j                  dgdgdgdgg      t        j                  d      g      } t        j                  g d      }t        j                         D ]k  \  }} |dd      }|j                  | |       |j                  j                  dk(  sJ t        |j                  |       t        j                  dd	             m t        j                         D ]k  \  }} |dd      }|j                  | |       |j                  j                  dk(  sJ t        |j                  |       t        j                  d
d	             m y )NrN   rS   )r7   r  )rS   rN   rS   rN   r   r8   r   ro  rJ   )r7   )rs   hstackr   rk   r   r   r   r   r   r'   r   r   r   r   r  s        r   (test_with_only_one_non_constant_featuresr    s    
		288cUSEC53%89288I;NOPA
%&A(0 Gm;1yy""a'''3,,Q/1EF	G  )0 ?m;1yy""a'''3;;q>2774+=>	?r   c                  &   t        j                  dd      j                  t         j                        j	                  dd      } t               }t        j                  t        d      5  |j                  | g d       d d d        y # 1 sw Y   y xY w)Ng\)c=Hr7   r>   r8   r%  r   )r   r8   r   r8   )
rs   repeatr$  r  reshaper   r   r   r   r   )r_   r   s     r   test_big_inputr  
  sg    
		(A%%bjj199"a@A
 
"C	z	3 !< ! ! !s   )BBc                  z    ddl m}  t        j                  t              5   |         d d d        y # 1 sw Y   y xY w)Nr   _realloc_test)sklearn.tree._utilsr  r   r   MemoryErrorr  s    r   test_reallocr    s+    1	{	#   s   1:c                     dt        j                  d      z  } t        j                  j	                  dd      }t        j                  j                  ddd      }d| dz   z  }t        d|      }t        j                  t              5  |j                  ||       d d d        d| dz
  z  dz
  }t        d|      }t        j                  t              5  |j                  ||       d d d        y # 1 sw Y   VxY w# 1 sw Y   y xY w)	NrK   PrC   rL   r   r8   best)splitterr   )structcalcsizers   r  randnr  r   r   r   	Exceptionr   r  )n_bitsr_   r`   huger   s        r   test_huge_allocationsr    s    %%F
		AA
		!Q#A !D
 &
FC	y	! 1
 !q D
 &
FC	{	# 1   s   C0C<0C9<Dc                     t         |    }t        |   d   }t        |   d   }|dv r|j                  d   dz  }|d | }|d | }t        t        z   t
        z   D ]5  } ||      } |d|      j                  ||      }	 |d|      j                  ||      }
t        |	j                  |
j                  dj                  |              |	j                  |      }| t        v r"|	j                  |      }|	j                  |      }t        t
        z   t        z   D ]t  } ||t        j                        }t!        |
j                  |      |       | t        v s?t!        |
j                  |             t!        |
j                  |             v 8 y )	Nr_   r`   )rc   rb   r   r:   rW   r   5{0} with dense and sparse format gave different treesr  )r6   r#  r   r,   r-   r.   r   r   r   rp   r   r   r   r   rs   r%  r&   )r	   datasetr   r   r_   r`   rX   r*  X_sparser{   r|   y_predy_probay_log_probasparse_container_testX_sparse_tests                   r   check_sparse_inputr  .  s|   dOM#A#A ((GGAJ!O	jyMjyM*^;nL #A& qI>BB1aHqI>BB8QOGGGGCJJ4P	
 19ooa(G--a0K%3n%D~%U 		!1("**MM%aii&>Gy )!//-*H'R)''6		%r   	tree_typer  )re   rd   rc   rg   rh   ri   rj   rk   c                 0    |dk(  rdnd }t        | ||       y )Nrc   r;   r  )r  r  r   s      r   test_sparse_inputr  W  s     (dIy'95r   rb   rf   c                     t        | |d       y )NrL   r  )r  r  s     r   test_sparse_input_reg_treesr  j  s    
 y'1-r   )rh   ri   rj   rk   c                    t         |    }t        |   d   } ||      }t        |   d   } |ddd      j                  ||      } |ddd      j                  ||      }t        |j                  |j                  dj                  |              t        |j                  |      |j                  |              |ddd	      j                  ||      } |ddd	      j                  ||      }t        |j                  |j                  dj                  |              t        |j                  |      |j                  |              |d|j                  d   dz  
      j                  ||      } |d|j                  d   dz  
      j                  ||      }t        |j                  |j                  dj                  |              t        |j                  |      |j                  |              |dd      j                  ||      } |dd      j                  ||      }t        |j                  |j                  dj                  |              t        |j                  |      |j                  |             y )Nr_   r`   r   r8   rL   )rW   r   r   r  rC   )rW   r   r  )rW   r  r;   r   )	r6   r#  r   r   r   rp   r&   r   r   )	r  r  r5  r   r_   r  r`   r{   r|   s	            r   test_sparse_parametersr  r  s9    i(M#AQH#A 	11BFFq!LA11BFFxQRSA		?FFyQ
 aiilAIIaL9 	11KOOPQSTUA11KOO!	A 		?FFyQ
 aiilAIIaL9 	1x~~a7HA7MNRRSTVWXA1x~~a7HA7MNRR!	A 		?FFyQ
 aiilAIIaL9 	1Q7;;AqAA1Q7;;HaHA		?FFyQ
 aiilAIIaL9r   ztree_type, criterionc                 v   t         |    }t        |   d   } ||      }t        |   d   } |dd|      j                  ||      } |dd|      j                  ||      }	t        |j                  |	j                  dj                  |              t        |	j                  |      |j                  |             y )Nr_   r`   r   r;   rW   r   r   r  )r6   r#  r   r   r   rp   r&   r   )
r  r  r5  r   r   r_   r  r`   r{   r|   s
             r   test_sparse_criteriar    s     i(M#AQH#A1YGKKAqQA1YGKKHVWXA		?FFyQ
 aiilAIIaL9r   zcsc_container,csr_containerc                    t         |    }d}d}|}t        j                  |      }t        d      }g }	g }
d}|g}t	        |      D ]x  }|j                  |d      }|j                  |      d | }|	j                  |       |j                  dd|f      dz
  }|
j                  |       ||z  }|j                  |       z t        j                  |	      j                  t        j                        }	t        j                  |t        j                        }t        j                  t        j                  |
      t        j                        }
 ||
|	|f||f      }|j                         } ||
|	|f||f      }|j                         }|j                  dd|f      }|j                         }|j                   d	k(  j#                         dkD  sJ |j                   d	k(  j#                         dkD  sJ  |d|
      j%                  ||      } |d|
      j%                  ||      }t'        |j(                  |j(                  dj+                  t,                     ||f}t/        ||      D ]  \  }}t1        |j(                  j3                  |      |j(                  j3                  |             t1        |j3                  |      |j3                  |             t1        |j3                  |      |j(                  j3                  |             t1        |j(                  j5                  |      j                         |j(                  j5                  |      j                                t1        |j5                  |      j                         |j5                  |      j                                t1        |j5                  |      j                         |j(                  j5                  |      j                                t1        |j7                  |      |j7                  |             t,        t8        v st1        |j;                  |      |j;                  |              y )Nr;   rC   r   rJ   r[   r8   r  r   rS   r  r  )r6   rs   r   r/   rG  binomialpermutationappendconcatenater$  int32r   r%  toarrayr  copyr   rx   r   r   r   rp   r	   r   r&   r  decision_pathr   r   r   )r  r5  r  r   r   rY   rX   samplesrW   r   r   offsetindptrin_nonzero_i	indices_idata_ir  r_   r  X_testr`   r{   r|   XsX1r  s                              r   test_explicit_sparse_zerosr    s   
 i(MIJ Iii	"G &a(LGDFXF: "++Is; ,,W5l{C	y!&&q#[N&CaGF+f nnW%,,RXX6GXXfBHH-F88BNN4(

;DdGV4Y
<STHA!	w	:'>M ""$FQ5A "&&(M MMS %%'!+++#%**,q000 	1	:>>q!DA1	:>>xKA		?FFtL -	 B"b/ PB!!''--"3QWW]]25FG!!''"+qwwr{;!!''"+qww}}R/@A!GG!!"%--/1F1Fr1J1R1R1T	
 	"OOB'')1??2+>+F+F+H	
 	"OOB'')177+@+@+D+L+L+N	
 	"!))B-2?9%aoob&91??2;NO%Pr   c                    t         |    }t        j                  d d df   j                         }t        j                  d d df   j	                  d      }t        j
                  }t        j                  t              5   |d      j                  ||       d d d         |d      }|j                  ||       t        j                  t              5  |j                  |g       d d d        y # 1 sw Y   YxY w# 1 sw Y   y xY w)Nr   r  r   )r6   ra   r   r   r  r   r   r   r   r   r   )r   r   r_   X_2dr`   r   s         r   check_raise_error_on_1d_inputr    s    dOM		!Q$A99QT?""7+DA	z	" 01%))!Q/0 Q
'CGGD!	z	" QC 0 0
 s   >C0C<0C9<Dc                 X    t               5  t        |        d d d        y # 1 sw Y   y xY wN)r)   r  r3  s    r   test_1d_inputr  !  s%    		 ,%d+, , ,s    )r*  c                 Z   t         |    }t        j                  dgdgdgdgdgg      }g d}g d}| ||      } |d      }|j                  |||       |j                  j
                  dk(  sJ  |dd      }|j                  |||       |j                  j
                  dk(  sJ y )	Nr   r8   )r   r   r   r   r8   )r=   r=   r=   r=   r=   r   r   g?)rW   r!  )r6   rs   r   r   r   r   )r   r*  r   r_   r`   r   r   s          r    test_min_weight_leaf_split_levelr  '  s     dOM
1#sQC!qc*+AA-M#Q
Q
'CGGAqG.99!###
Q
ECGGAqG.99!###r   c                     t         j                  t        j                  j                  d      }t        |           }|j                  t         t               t        |j                  t               |j                  j                  |             y NFr  X_smallr$  r	   r  r  r6   r   y_smallr'   r  r   )r   	X_small32r   s      r   test_public_apply_all_treesr	  ;  sX    tzz//e<I
D/
CGGGWsyy)399??9+EFr   r  c                 ,    |t         j                  t        j                  j                  d            }t        |           }|j                  t         t               t        |j                  t               |j                  j                  |             y r  r  )r   r  r  r   s       r   test_public_apply_sparse_treesr  D  s_     gnnTZZ-=-=EnJKI
D/
CGGGWsyy)399??9+EFr   c                      t         j                  } t         j                  }t        dd      j	                  | |      }|j                  | d d       j                         }t        |g dg dg       y )Nr   r8   r  rL   )r8   r8   r   r8   r   r8   )ra   r   r   r   r   r  r  r'   )r_   r`   r   node_indicators       r   test_decision_path_hardcodedr  N  s[    		AA
 a1
=
A
A!Q
GC&&q!u-557N~	9'=>r   c                    t         j                  }t         j                  }|j                  d   }t        |    } |dd      }|j                  ||       |j                  |      }|j                         }|j                  ||j                  j                  fk(  sJ |j                  |      }t        |      D 	
cg c]  \  }	}
||	|
f    }}	}
t        |t        j                  |             |j                  j                  t         k(  }t        t        j"                  ||      t        j                  |             |j%                  d      j'                         }|j                  j(                  |k  sJ y c c}
}	w )Nr   rL   r  r  r8   axis)ra   r   r   r   r6   r   r  r  r   ro   r  	enumerater&   rs   r   rr   r   r  rx   r;  r   )r   r_   r`   rX   r   r   node_indicator_csrr  leavesr  jleave_indicator
all_leavesr   s                 r   test_decision_pathr  V  s?   		AA
IdOM
Q!
4CGGAqM**1-'//1NIsyy/C/C#DDDD YYq\F8A&8IJ1~ad+JOJorwwY/GH ((I5J
~z*BGG),D
 """*..0I99)+++ Ks   <E=c                     t          |t              }}t        |    }t        j                  t
              5   |d      j                  ||       d d d        y # 1 sw Y   y xY wNr   r   )X_multilabely_multilabelr6   r   r   r  r   )r   r  r_   r`   r   s        r   test_no_sparse_y_supportr  t  sQ     |4qAdOM	y	! 01%))!Q/0 0 0s   AA!c                     t        ddd      } | j                  dgdgdgdgdggg dg d	
       t        | j                  j                  g d       t        | j                  j                  j                  g d       | j                  dgdgdgdgdggg dt        j                  d      
       t        | j                  j                  g d       t        | j                  j                  j                  g d       | j                  dgdgdgdgdggg d       t        | j                  j                  g d       t        | j                  j                  j                  g d       y)aQ	  Check MAE criterion produces correct results on small toy dataset:

    ------------------
    | X | y | weight |
    ------------------
    | 3 | 3 |  0.1   |
    | 5 | 3 |  0.3   |
    | 8 | 4 |  1.0   |
    | 3 | 6 |  0.6   |
    | 5 | 7 |  0.3   |
    ------------------
    |sum wt:|  2.3   |
    ------------------

    Because we are dealing with sample weights, we cannot find the median by
    simply choosing/averaging the centre value(s), instead we consider the
    median where 50% of the cumulative weight is found (in a y sorted data set)
    . Therefore with regards to this test data, the cumulative weight is >= 50%
    when y = 4.  Therefore:
    Median = 4

    For all the samples, we can get the total error by summing:
    Absolute(Median - y) * weight

    I.e., total error = (Absolute(4 - 3) * 0.1)
                      + (Absolute(4 - 3) * 0.3)
                      + (Absolute(4 - 4) * 1.0)
                      + (Absolute(4 - 6) * 0.6)
                      + (Absolute(4 - 7) * 0.3)
                      = 2.5

    Impurity = Total error / total weight
             = 2.5 / 2.3
             = 1.08695652173913
             ------------------

    From this root node, the next best split is between X values of 3 and 5.
    Thus, we have left and right child nodes:

    LEFT                    RIGHT
    ------------------      ------------------
    | X | y | weight |      | X | y | weight |
    ------------------      ------------------
    | 3 | 3 |  0.1   |      | 5 | 3 |  0.3   |
    | 3 | 6 |  0.6   |      | 8 | 4 |  1.0   |
    ------------------      | 5 | 7 |  0.3   |
    |sum wt:|  0.7   |      ------------------
    ------------------      |sum wt:|  1.6   |
                            ------------------

    Impurity is found in the same way:
    Left node Median = 6
    Total error = (Absolute(6 - 3) * 0.1)
                + (Absolute(6 - 6) * 0.6)
                = 0.3

    Left Impurity = Total error / total weight
            = 0.3 / 0.7
            = 0.428571428571429
            -------------------

    Likewise for Right node:
    Right node Median = 4
    Total error = (Absolute(4 - 3) * 0.3)
                + (Absolute(4 - 4) * 1.0)
                + (Absolute(4 - 7) * 0.3)
                = 1.2

    Right Impurity = Total error / total weight
            = 1.2 / 1.6
            = 0.75
            ------
    r   r3   rL   )rW   r   r   r;   r:   rK   )rI   r  r;   r7   r;   )333333?333333?r   rN   r!  )r_   r`   r   )g,d?gܶm۶m?g?)      @g      @r"  )ffffff?rM   gUUUUUU?)r7   rT   r"  r^   N)
r   r   r   r   ry   r'   rz   r  rs   r   )dt_maes    r   test_maer%  ~  s0   T #"21F
 JJ3aS1#s
#
/  
 FLL))+LMv||))..@ JJ1#sQC!qc*oRWWUVZJXv||,,.CDv||))..>
 JJ1#sQC!qc*oJ>v||,,.CDv||))..>r   c                     d} t        j                  dt         j                        }d}d }t        j                  t        j                  |fD ]  }t        j                         D ]G  \  }} || |      } ||      j                         }|\  }	\  }
}}||	k(  sJ | |
k(  sJ t        ||       I t        j                         D ]B  \  }} || |      } ||      j                         }|\  }	\  }
}}||	k(  sJ | |
k(  sJ ||k(  rBJ   y )Nr;   r  rC  c                 R    t        j                  t        j                  |             S r  )rc  re  rd  )objs    r   _pickle_copyz)test_criterion_copy.<locals>._pickle_copy  s    ||FLL-..r   )
rs   r   intpr  deepcopyr   r   
__reduce__r'   r   )	n_outputsra  rX   r)  	copy_func_typenamecriteriaresult	typename_
n_outputs_rv  
n_samples_s                r   test_criterion_copyr6    s1    I		!277+II/ ii= +	'--/ 	6KAx	95Hx(335F5;2I/
Jy(((
***y*5	6 (--/ 	+KAx	95Hx(335F5;2I/
Jy(((
***
***	++r   c                    t         j                  j                  d      j                  dd      dz  }t        j                  |j                  d            }|d d d df   }|  | |      }|d d df   }t        d      j                  ||      } |j                  |      }t        t        j                  |j                  j                  t        k(        d         }|j                  |      }t        j                  t        j                  |j                  j                                d   }t#        |      dk(  sJ t#        |      dk(  sJ y )Nr   rC  rH   g*Gr%  r>   r   )rs   r  RandomStater  
nan_to_numr$  r   r   r  setwherer   rr   r   
differenceisfiniterv   r   )	r*  r   r_   r`   r	   terminal_regions	left_leaf
empty_leafinfinite_thresholds	            r   "test_empty_leaf_infinite_thresholdrB    s    99  #))#r2T9D==Y/0DQVA#QQUA a044Q:D!tzz!}BHHTZZ55BCAFGI%%&67J2;;tzz/C/C#D"DEaH!"a'''z?ar   tree_clsc                 b   t         |    } | d   | d   }} |dd      }|j                  ||      }|j                  }|j                  }t	        j
                  t	        j                  |      dk\        sJ t	        j
                  t	        j                  |      dk\        sJ t        ||||       y Nr_   r`   rZ   r   rE  r#  cost_complexity_pruning_path
ccp_alphas
impuritiesrs   r   diffassert_pruning_creates_subtreer  rC  r_   r`   r   infopruning_pathrI  s           r   'test_prune_tree_classifier_are_subtreesrO    s    
 wG3<qA
"1
5C++Aq1D??LJ66"'','1,---66"''*%*+++"8Q<@r   c                 b   t         |    } | d   | d   }} |dd      }|j                  ||      }|j                  }|j                  }t	        j
                  t	        j                  |      dk\        sJ t	        j
                  t	        j                  |      dk\        sJ t        ||||       y rE  rF  rL  s           r   'test_prune_tree_regression_are_subtreesrQ  #  s     wG3<qA
"1
5C++Aq1D??LJ66"'','1,---66"''*%*+++"8Q<@r   c                      t        d      } | j                  dgdggddg       t        dd      }|j                  dgdggddg       t        | j                  |j                         y )Nr   r   r8   rC   )rW   	ccp_alpha)r   r   assert_is_subtreer   )r  r   s     r   test_prune_single_node_treerU  4  s`    !q1DHHqcA3Z!Q  "qB?DHHqcA3Z!Q djj$**-r   c                     g }|D ].  } | d|d      j                  ||      }|j                  |       0 t        ||dd        D ]%  \  }}t        |j                  |j                         ' y )NrZ   r   )r   rS  rW   r8   )r   r  ziprT  r   )	estimator_clsr_   r`   rN  
estimatorsrS  r   prev_estnext_ests	            r   rK  rK  @  sz    J! 	2QRSWWq
 	#	 "*jn= :((..(..9:r   c                 >   | j                   |j                   k\  sJ | j                  |j                  k\  sJ | j                  }| j                  }|j                  }|j                  }dg}|r1|j	                         \  }}t        | j                  |   |j                  |          t        | j                  |   |j                  |          t        | j                  |   |j                  |          t        | j                  |   |j                  |          ||   ||   k(  rt        t        |j                  |          nXt        | j                  |   |j                  |          |j                  ||   ||   f       |j                  ||   ||   f       |r0y y )N)r   r   )ro   r   rr   rq   popr&   rz   r%   ry   rw   rH  r   rv   r  )	r	   subtreetree_c_lefttree_c_rightsubtree_c_leftsubtree_c_rightstacktree_node_idxsubtree_node_idxs	            r   rT  rT  O  s   ??g00000>>W.....$$K&&L**N,,OHE
*/))+''!JJ}%w}}5E'F	
 	MM-('*:*:;K*L	
 	.0F0FGW0X	
 	((7++,<=	

 *+?O/PP0A0ABR0ST  }-w/@/@AQ/R LL+m4nEU6VWXLLm,o>N.OP3 r   r  r  r  c                 8   t         d   }|d   j                  t        j                  j                  d      }|t        |      }n ||d         }t        j                  |j                  t        j                  j                        |_        t        |j                  |j                  |j                  f      \  |_        |_	        |_
        t        t        j                  t        t        j                  j                              }t        |    |      }|j                  ||       t        |j                  |      |j                  |             t        |j!                  |      j#                         |j!                  |      j#                                y )Nre   r_   Fr  r  )r  )r#  r$  r	   r  r  r(   rs   r   r   r   r  r  r6   r   r'   r   r  todense)r   r  r*  r  r  
X_readonly
y_readonlyr   s           r   "test_apply_path_readonly_all_treesrj  w  s7    {#Gcl!!$**"2"2!?G.w7
%gcl3
((:??$**:J:JK

 &__j00*2C2CD
		
O
 +288G4::CSCS+TUJ
D/8
,CGGJ
#s{{:.G0DE*%--/1B1B71K1S1S1Ur   )r2   r4   r5   c                    t         j                  t         j                  }} ||       }|j                  ||       t	        j
                  |j                  |            t        j                  t	        j
                  |            k(  sJ y )Nr   )	rb   r   r   r   rs   rx   r   r   r   )r   r#   r_   r`   r   s        r   test_balance_propertyrl    s\     ==(//qA

#CGGAqM66#++a.!V]]266!9%====r   seedc           	         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}g d}t        d|       }|j                  ||       t        j                  |j	                  |            dk(  sJ t        d|       }|j                  ||       t        j
                  |j	                  |      dkD        sJ d	}t        j                  |dz  dz  d
d||dz  dz  |       \  }}d|d|k  |dk  z  <   t        j                  |      }t        d|       }|j                  ||       t        j
                  |j	                  |      dkD        sJ y )Nr   r8   rL   r;   )r   r   r   r   r8   rL   r;   r7   r2   r   r5   rC   r   r  )effective_ranktail_strengthrX   rY   r   rW   r>   )	r   r   rs   aminr   r   r   make_regressionr   )rm  r_   r`   r   rY   s        r   test_poisson_zero_nodesrs    sN    Q!Q!Q!Q!Q!Q!Q!QHA A  /
MCGGAqM773;;q>"a'''
)$
GCGGAqM66#++a.1$%%% J##!A~* 1n)DAq ArAv!a%
q	A
)$
GCGGAqM66#++a.1$%%%r   c            	      0   t         j                  j                  d      } d\  }}}t        j                  ||z   ||       }| j                  dd|      t        j                  |d      z  }| j                  t        j                  ||z        	      }t        |||| 
      \  }}}	}
t        dd|       }t        dd|       }|j                  ||	       |j                  ||	       t        d      j                  ||	      }||	df||
dffD ]  \  }}}t        ||j                  |            }t        |t        j                  |j                  |      dd             }t        ||j                  |            }|dk(  r
|d|z  k  sJ |d|z  k  rJ  y )Nr   )  ru  rC   rX   rY   rW   rA   rL   )lowhighr\   r   r  )lam)	test_sizerW   r5   rC   )r   r  rW   r2   mean)strategytraintestgV瞯<rJ   g      ?)rs   r  r8  r   make_low_rank_matrixuniformr;  r5   r   r   r   r   r
   r   r   clip)r&  n_trainn_testrY   r_   coefr`   X_trainr  r   r   tree_poitree_msedummyval
metric_poi
metric_msemetric_dummys                     r   test_poisson_vs_mser    s   
 ))


#C".GVZ%%F"z	A
 ;;2AJ;7"&&:KKDq4x()A'7	1S($GVWf %rH %!RcH LL'"LL'"F+//AE1FFF3KL 
0	1c*1h.>.>q.AB
*1bggh6F6Fq6I5RV.WX
,Qa0@A &=j 0000D<////
0r   ra  c                 N   d\  }}t        j                  ||||dd      \  }} | dd      j                  ||      } | dd      j                  ||      }t        |j                  |j                  | d	       t        |j                  |      |j                  |             y
)z3Test that criterion=entropy gives same as log_loss.)r  r:   r   r   )ra  rX   rY   r   r   rW   r1   +   r   entropyz> with criterion 'entropy' and 'log_loss' gave different trees.N)r   r   r   r   r   r   r   )r#   ra  rX   rY   r_   r`   tree_log_losstree_entropys           r   'test_criterion_entropy_same_as_log_lossr    s     "Iz'' DAq :B?CCAqIM)"=AA!QGL(PQ
 M))!,l.B.B1.EFr   c                  6   t        j                  d      \  } }t        dd      j                  | |       j	                  | |      }d fd}t        j                   |             }|j	                  | |      }t        j                  ||      sJ y )Nr   r   r;   r  c                     | j                         j                  | j                  j                               j	                         S r  )byteswapviewr  newbyteorderr,  )arrs    r   reduce_ndarrayz8test_different_endianness_pickle.<locals>.reduce_ndarray  s/    ||~""399#9#9#;<GGIIr   c                     t        j                         } t        j                  |       }t        j
                  j                         |_        |j
                  t        j                  <   |j                         | j                  d       | S Nr   )ioBytesIOrc  Picklercopyregdispatch_tabler  rs   ndarraydumpseek)fpr   r  s     r    get_pickle_non_native_endiannesszJtest_different_endianness_pickle.<locals>.get_pickle_non_native_endianness  sb    JJLNN1"11668'5$	s	q	r   )	r   r   r   r   r   rc  loadrs   isclose)r_   r`   r   r  new_clf	new_scorer   r  s         @@r    test_different_endianness_pickler    s    ''Q7DAq
 a1
=CGGAqMIIaOEJ kk:<=Ga#I::eY'''r   c                  N   t        j                  d      \  } }t        dd      j                  | |       j	                  | |      } G d dt
              fd}t        j                   |             }|j	                  | |      }t        j                  ||      sJ y )Nr   r   r;   r  c                        e Zd Z fdZ xZS )Ptest_different_endianness_joblib_pickle.<locals>.NonNativeEndiannessNumpyPicklerc                     t        |t        j                        r7|j                         j	                  |j
                  j                               }t        | !  |       y r  )	
isinstancers   r  r  r  r  r  supersave)selfr(  rg  s     r   r  zUtest_different_endianness_joblib_pickle.<locals>.NonNativeEndiannessNumpyPickler.save'  s@    #rzz*lln))#))*@*@*BCGLr   )__name__
__module____qualname__r  __classcell__)rg  s   @r   NonNativeEndiannessNumpyPicklerr  &  s    	 	r   r  c                      t        j                         }  |       }|j                         | j                  d       | S r  )r  r  r  r  )r  r  r  r   s     r   'get_joblib_pickle_non_native_endiannesszXtest_different_endianness_joblib_pickle.<locals>.get_joblib_pickle_non_native_endianness,  s3    JJL+A.	s	q	r   )
r   r   r   r   r   r   joblibr  rs   r  )r_   r`   r   r  r  r  r  r   s         @@r   'test_different_endianness_joblib_pickler    s    ''Q7DAq
 a1
=CGGAqMIIaOE,  kkACDGa#I::eY'''r   c                    t         rt        j                  nt        j                  }g d}| j                  j
                  j                         D ci c]  \  }\  }}|| }}}}|D ]  }|||<   	 t        j                  t        |j                               t        |j                               d      }| j                  |d      S c c}}}w )N)
left_childright_childru   rw   namesformats	same_kindcasting)r+   rs   int64r  r  fieldsr   listr  valuesr$  )node_ndarraynew_dtype_for_indexing_fieldsindexing_field_namesr   r  r/  new_dtype_dict	new_dtypes           r   "get_different_bitness_node_ndarrayr  9  s    09BHHrxx! V -9,>,>,E,E,K,K,M (juaeN  % =<t= ~**,-$~?T?T?V:WXI y+>>s   Cc                    | j                   j                  j                         D ci c]  \  }\  }}|| }}}}| j                   j                  j                         D cg c]  \  }}|	 }}}|D cg c]  }d|z   	 }}t	        j                   t        |j                               t        |j                               |d      }| j                  |d      S c c}}}w c c}}w c c}w )NrK   )r  r  offsetsr  r  )r  r  r   r  rs   r  r  r$  )	r  r   r  r/  r  r  r  shifted_offsetsr  s	            r   $get_different_alignment_node_ndarrayr  K  s    ,8,>,>,E,E,K,K,M (juaeN  ,8+=+=+D+D+K+K+MN-%vNGN078fq6z8O8.--/0N1134&	
I y+>> O8s   C$C&7C,c                     t         rt        j                  nt        j                  } | j                         \  }\  }}}}|j                  |d      }|j                         }t        |d         |d<   ||||f|fS )Nr  r  nodes)r+   rs   r  r  r,  r$  r  r  )	r	   r  rC  rY   ra  r-  statenew_n_classes	new_states	            r   "reduce_tree_with_different_bitnessr  \  sw    %288I:I$//:K7H0z9i%$$Y$DM

I;Ig<NOIgz=)<iHHr   c                  0   t        j                  d      \  } }t        dd      j                  | |       j	                  | |      }fd}t        j                   |             }|j	                  | |      }|t        j                  |      k(  sJ y )Nr   r   r;   r  c                     t        j                         } t        j                  |       }t        j
                  j                         |_        t        |j
                  t        <   |j                         | j                  d       | S r  )r  r  rc  r  r  r  r  r  
CythonTreer  r  r  r  r   s     r   "pickle_dump_with_different_bitnesszItest_different_bitness_pickle.<locals>.pickle_dump_with_different_bitnessn  s^    JJLNN1"11668'I$	s	q	r   )	r   r   r   r   r   rc  r  r   r   )r_   r`   r   r  r  r  r   s         @r   test_different_bitness_pickler  g  s    ''Q7DAq
 a1
=CGGAqMIIaOE kk<>?Ga#IFMM),,,,r   c                  0   t        j                  d      \  } }t        dd      j                  | |       j	                  | |      }fd}t        j                   |             }|j	                  | |      }|t        j                  |      k(  sJ y )Nr   r   r;   r  c                      t        j                         } t        |       }t        j                  j                         |_        t        |j                  t        <   |j                         | j                  d       | S r  )
r  r  r   r  r  r  r  r  r  r  r  s     r   "joblib_dump_with_different_bitnesszPtest_different_bitness_joblib_pickle.<locals>.joblib_dump_with_different_bitness  sY    JJLO"11668'I$	s	q	r   )	r   r   r   r   r   r  r  r   r   )r_   r`   r   r  r  r  r   s         @r   $test_different_bitness_joblib_pickler  }  s     ''Q7DAq
 a1
=CGGAqMIIaOE kk<>?Ga#IFMM),,,,r   c                  L   t         r#t        j                  t        j                        n"t        j                  t        j                        } t        j                  t        j                        t        j                  t        j                        g}||D cg c]  }|j                          c}z  }t        j                  ddg|       }|D ]  }t        |j                  |      |         t        j                  t        d      5  t        j                  ddgg|       }t        ||        d d d        t        j                  t        d      5  |j                  t        j                        }t        ||        d d d        y c c}w # 1 sw Y   ^xY w# 1 sw Y   y xY w)Nr   r8   r  zWrong dimensions.+n_classesr   zn_classes.+incompatible dtype)r+   rs   r  r  r  r  r   r    r$  r   r   r   r  )expected_dtypeallowed_dtypesdtra  wrong_dim_n_classeswrong_dtype_n_classess         r   test_check_n_classesr    s;   +4RXXbhh'"((288:LNhhrxx("((288*<=N>BRr(BBN!Q~6I ?))"-~>? 
z)F	G > hhAx~F,n=> 
z)H	I @ ) 0 0 <.?@ @ C> >@ @s   F	
'F,FFF#c                     t        j                  t         j                        } d}t        j                  ||       }| | j	                         g}|D ]  }t        |||        t        j                  t        d      5  t        || d       d d d        |d d d d d df   t        j                  |      fD ]>  }t        j                  t        d      5  t        || |j                         d d d        @ t        j                  t        d	      5  t        |j                  t         j                        | |       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)
N)r:   r8   rL   r  )r  expected_shapezWrong shape.+value arrayr   )r8   rL   r8   zvalue array.+C-contiguouszvalue array.+incompatible dtype)rs   r  r  rk   r  r"   r   r   r   r  r   r$  r%  )r  r  value_ndarrayr  r  problematic_arrs         r   test_check_value_ndarrayr    sJ   XXbjj)NNHH^>BM$n&A&A&CDN 
"^	


 
z)C	D 
.	


 *!Q(3R5F5F}5UV ]]:-HI 	 -.44	 	 
z)J	K 
  ,))	

 

 
	 	
 
s$   ?E
E,E"
EE	"E+c                     t         } t        j                  d|       }|t        |      t	        |      g}||D cg c]+  }|j                  |j                  j                               - c}z  }|D ]  }t        ||         t        j                  t        d      5  t        j                  d|       }t        ||        d d d        t        j                  t        d      5  |d d d   }t        ||        d d d        |j                  j                  j                         D ci c]  \  }\  }}|| }}}}|j                         }	t        j                  |	d	<   t        j                  t!        |	j#                               t!        |	j%                               d
      }
|j                  |
      }t        j                  t        d      5  t        ||        d d d        |j                         }	t        j&                  |	d<   t        j                  t!        |	j#                               t!        |	j%                               d
      }
|j                  |
      }t        j                  t        d      5  t        ||        d d d        y c c}w # 1 sw Y   xY w# 1 sw Y   xY wc c}}}w # 1 sw Y   xY w# 1 sw Y   y xY w)N)r:   r  )r  zWrong dimensions.+node arrayr   )r:   rL   znode array.+C-contiguousrL   rv   r  znode array.+incompatible dtyper  )r   rs   rk   r  r  r$  r  r  r!   r   r   r   r  r   r  r  r  r  r  r  )r  r  valid_node_ndarraysr  problematic_node_ndarrayr   r  r/  
dtype_dictr  r  s              r   test_check_node_ndarrayr    s   N88D7L 	*<8,\:
 8K14

399))+,  # ILHI 
z)G	H U#%88F.#I 4^TU 
z)C	D U#/!#4 4^TU 7C6H6H6O6O6U6U6WXX"2$
$+XJX  __&N"$((N;~**,-$~?T?T?V:WXI  ,229=	z)I	J U4^TU  __&N#%::N< ~**,-$~?T?T?V:WXI  ,229=	z)I	J U4^TU UMU UU U YU UU Us;   0J%J&J),J6J= K	J&)J3=K	KSplitterc                 d   t         j                  j                  d      }d}dt        j                  ddgt         j                        }}t        d   ||      } | ||dd|d	
      }t        j                  |      }t        j                  |      }|j                  |k(  sJ t        ||       sJ y	)z&Check that splitters are serializable.r   rC   rL   r;   r  r0   r:   rJ   N)monotonic_cst)rs   r  r8  r   r*  r   rc  rd  re  r   r  )	r  r&  r   r-  ra  r   r  splitter_serializesplitter_backs	            r   test_splitter_serializabler    s    
 ))


#CLbhh1vRWW=yIV$Y	:I	<CDQHh/LL!34M%%555mX...r   c                     t        | j                  d            }t        d      }|j                  t        t
               t        j                  ||       t        j                  |d      }t        |j                  |j                  d       y)zhCheck that Trees can be deserialized with read only buffers.

    Non-regression test for gh-25584.
    z
clf.joblibr   r   r)	mmap_modez?The trees of the original and loaded classifiers are not equal.N)strjoinr   r   r  r  r  r  r  r   r   )tmpdirpickle_pathr   
loaded_clfs       r   /test_tree_deserialization_from_read_only_bufferr  	  sh    
 fkk,/0K
 a
0CGGGW
KK[![C8J		Ir   c                 6   t        j                  ddgddgg      }t        j                  ddg      } | d      j                  ||        | d      }d}t        j                  t
        |      5   |j                  ||       ddd       y# 1 sw Y   yxY w)zhCheck that an error is raised when min_sample_split=1.

    non-regression test for issue gh-25481.
    r   r8   rN   )r  zb'min_samples_split' .* must be an int in the range \[2, inf\) or a float in the range \(0.0, 1.0\]r   N)rs   r   r   r   r   r   )r#   r_   r`   r	   msgs        r   test_min_sample_split_1_errorr  $	  s     	1a&1a&!"A
!QA 	3##Aq) !$D	0  
z	- A  s   2BBc                    t        j                  g dg      j                  }t        j                  g d      }t        dd|       }|j	                  ||       |j                  t         j                  gg      }t        |t        j                  |dd       g       |dd }|dd }t        dd|       }|j	                  ||       |j                  t         j                  gg      }t        |t        j                  |d	d       g       y)
z=Check missing values goes to correct node during predictions.	r   r8   rL   r;   rK   r  rH      r   	r   r=   r!  r=   r#  r#  rM   g?g@r   r8   r  r<   Nr>   r9   )	rs   r   r   r   r   r   nanr   r{  )r   r_   r`   dtcr  X_equaly_equals          r   ;test_missing_values_best_splitter_on_equal_nodes_no_missingr  :	  s     	01244A
>?A
R1	
RCGGAqM [[266($FFRWWQrsV_-. fGfG
R1	
RCGGGW [[266($FFRWWWRS\234r   c                 p   t        j                  g dg      j                  }t        j                  g d      }t        |d|       }|j	                  ||       |j
                  j                  d   }|j
                  j                  d   }|j
                  j                  |   }|j
                  j                  |   }||kD  }	|j
                  j                  |   d   }
|j
                  j                  |   d   }|j                  t         j                  gg      }|	rt        |
|       yt        ||       y)zCheck missing values go to the correct node during predictions for ExtraTree.

    Since ETC use random splits, we use different seeds to verify that the
    left/right node is chosen correctly when the splits occur.
    r	  r  r8   r  r   N)rs   r   r   r   r   r   rr   rq   rH  rz   r   r  r   )r   rm  r_   r`   etrr  r  left_samplesright_samples	went_lefty_pred_lefty_pred_rightr  s                r   =test_missing_values_random_splitter_on_equal_nodes_no_missingr  T	  s    	01244A
>?A
$!y
QCGGAqM ((+J))**1-K 9944Z@LII55kBM},I ))//*-a0K99??;/2L [[266($FV,f-r   r  r0   c                    d}t        j                  t         j                  gdz  g dz   g      j                  }t        j                  |gdz  dgdz  z   dgdz  z         }t	        dd|       }|j                  ||       t        j                  t         j                  dd	gg      j                  }|j                  |      }t        ||ddg       y
)zITest when missing values are uniquely present in a class among 3 classes.r   r7   )r   r8   rL   r;   rK   r  rH   r
  r8   rL   r   r  r;   r
  Nrs   r   r  r   r   r   r   r'   )r   missing_values_classr_   r`   r  r  
y_nan_preds          r   /test_missing_values_best_splitter_three_classesr  w	  s     
266(Q,!;;<=??A
&'!+qcAg5a?@A
 bA
SCGGAqMXX2'(**FV$Jz$8!Q#?@r   c                    t        j                  t         j                  gdz  g dz   g      j                  }t        j                  dgdz  dgdz  z         }t	        dd|       }|j                  ||       t        j                  t         j                  d	t         j                  gg      j                  }|j                  |      }t        |g d
       y)zMissing values spanning only one class at fit-time must make missing
    values at predict-time be classified has belonging to this class.r7   r   r8   rL   r;   r7   r:   r   r8   rI   r   rL   r  r:   )r   r8   r   Nr  r   r_   r`   r  r  r  s         r   )test_missing_values_best_splitter_to_leftr!  	  s     	266(Q,!334577A
!qA37"#A
 bA
SCGGAqMXX266*+,..F[[ Fvy)r   c                    t        j                  t         j                  gdz  g dz   g      j                  }t        j                  dgdz  dgdz  z   dgdz  z         }t	        dd|       }|j                  ||       t        j                  t         j                  dd	gg      j                  }|j                  |      }t        |g d
       y)zMissing values and non-missing values sharing one class at fit-time
    must make missing values at predict-time be classified has belonging
    to this class.r7   r  r8   r   rL   r   r  rO   g333333@r  Nr  r   s         r   *test_missing_values_best_splitter_to_rightr#  	  s    
 	266(Q,!334577A
!qA37"aS1W,-A
 bA
SCGGAqMXXS)*+--F[[ Fvy)r   c                    t        j                  ddddt         j                  ddddt         j                  g
g      j                  }t        j                  d	gdz  dgdz  z         }t	        d
d|       }|j                  ||       t        j                  t         j                  ddgg      j                  }|j                  |      }t        |g d       y)zNCheck behavior of missing value when there is one missing value in each class.r8   rL   r;   r:   rC   rZ   rV   r   r   r   r  gffffff@gA@r  Nr  r   s         r   >test_missing_values_best_splitter_missing_both_classes_has_nanr%  	  s     	1aArvvr2r2rvv>?@BBA
!qA37"#A
 bA
SCGGAqMXXT*+,..F[[ F vy)r   r	   r   c                 j   t        j                  ddddt         j                  ddddt         j                  g
g      j                  }t        j                  d	gdz  dgdz  z         }|  | |      }t	        j
                  t        d      5   |j                  ||       d
d
d
       y
# 1 sw Y   y
xY w)z4Check unsupported configurations for missing values.r8   rL   r;   r:   rC   rZ   rV   r   r   NzInput X contains NaNr   )rs   r   r  r   r   r   r   r   )r*  r	   r_   r`   s       r   test_missing_value_errorsr'  	  s     	1aArvvr2r2rvv>?@BBA
!qA37"#A#Q	z)?	@ A  s   B))B2c                 D   t         j                  j                         t         j                  }}t        j
                  |ddddf<   t        j
                  |ddddf<    | dd      }|j                  ||       |j                  |      }|d	k\  j                         sJ y)
z5Smoke test for poisson regression and missing values.Nr:   r   rI   r>   r5   r   r   rS   )	rb   r   r  r   rs   r  r   r   r   )r#   r_   r`   r   r  s        r   test_missing_values_poissonr)  	  s     ==qA Acc1fIAcc2gJ

4CGGAqM[[^FcM   r   c                  D    t        j                  | i |\  }}|dkD  }||fS )N   )r   make_friedman1)argskwargsr_   r`   s       r   make_friedman1_classificationr/  	  s-    ""D3F3DAq	BAa4Kr   zmake_data, Tree, tolerancegQ?gQ?gQ?sample_weight_trainr   c                 ~   d\  }} | ||d|      \  }}|j                         }	t        j                  j                  |      }
t        j                  |	|
j                  ddg|j                  ddg      <   t        |	||	      \  }}}}|d
k(  r#t        j                  |j                  d         }nd}d} |||      }|j                  |||       |j                  ||      }t        t                |||            }|j                  ||       |j                  ||      }||z   |kD  sJ d|d| d|        y)zFCheck that trees can deal with missing values have decent performance.)r   rC   rN   )rX   rY   noiserW   FTrU   r   r\   r  r   r   r   NrC   r   r   zscore_native_tree=z + z! should be strictly greater than )r  rs   r  r8  r  choicer   r   r   r   r   r   r   )	make_datar#   r0  rI  	tolerancerX   rY   r_   r`   	X_missingr&  X_missing_trainX_missing_testr   r   r   r   native_treescore_native_treetree_with_imputerscore_tree_with_imputers                        r   !test_missing_values_is_resiliencer>  	  sh   ( &Iz'	DAq I
))

 2
3CGIvvIcjj%QWWc
jCD7G1#584O^Wf f$ 5 5a 89 I9KLKOOOWMOJ#)).&A%	@RS /73/55nfMy(+BB 
c) -#$	&Br   zTree, expected_scoreg333333?g(\?c                 H   t         j                  j                  d      }d}|j                  |df      }t        j                  t        j
                  |dz        t        j                  |dz        g      }|j                  ddg|dd	g
      }|j                         j                  t              }||    ||<   |j                  |      }	t         j                  |	|<   |	|dddf<    | |      }
t        |
||d      j                         }||k\  sJ d| d|        y)z@Check the tree learns when only the missing value is predictive.r   ru  rZ   r[   rL   FTgffffff?rP   r3  Nr:   r   )cvzExpected CV score: z	 but got )rs   r  r8  standard_normalr  rk   r   r4  r  r$  boolr  r   r{  )r#   expected_scorerI  r&  rX   r_   r`   X_random_masky_maskX_predictiver	   tree_cv_scores               r    test_missing_value_is_predictiverH  
  s!    ))


"CI)R1A
a0"'')q.2IJKA JJt}9tJMMVVX__T"F#M22F=&&I&6L66LAadG/0D $D!Q15::<M'F	^,Im_EF'r   zmake_data, Treec                    t         j                  j                  d      }d\  }} | |||      \  }}t         j                  ||j	                  ddg|j
                  ddg      <   t        j                  |j
                  d         }d	|d
d
d<    |d      }|j                  |||        |d      }	|	j                  |dd
dd
d
f   |dd
d          t        |	j                  |      |j                  |             y
)z=Check sample weight is correctly handled with missing values.r   )r  rC   rv  FTrU   r   r3  rS   NrL   r   r   r8   )
rs   r  r8  r  r4  r   r   r   r   r   )
r5  r#   r&  rX   rY   r_   r`   r   tree_with_swtree_samples_removeds
             r   test_sample_weight_non_uniformrL  =
  s     ))


"C$IzyZcRDAq @BvvAcjj%QWWc
j;< GGAGGAJ'MM#A#Q'LQ7Q/Qqt!tQwZ14a41(003\5I5I!5LMr   c                  F   t        d      j                  t        j                  t        j                        } t        d      j                  t        j                  t        j                        }t        j                  |       }t        j                  |      }||k(  sJ y r  )r   r   ra   r   r   rc  rd  )tree1tree2pickle1pickle2s       r   test_deterministic_picklerR  Z
  sl     #266tyy$++NE"266tyy$++NEll5!Gll5!Ggr   r_   r:   rI   c                    |j                  dd      }t        j                  d      } | |d      j                  ||      }t	        |      j                  |j                  dd      |      }|j
                  j                  }t        |dk\        sJ |j                                t        |j
                  j                  dd |j
                  j                  dd        t        j                  |j
                  j                  dk(  |j
                  j                  dk(  z        }t        |j
                  j                  |   d       y)	a'  Check that we properly handle missing values in regression trees using a toy
    dataset.

    The regression targeted by this test was that we were not reinitializing the
    criterion when it comes to the number of missing values. Therefore, the value
    of the critetion (i.e. MSE) was completely wrong.

    This test check that the MSE is null when there is a single sample in the leaf.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/28254
    https://github.com/scikit-learn/scikit-learn/issues/28316
    r>   r8   rI   r   r   NrL   rS   )r  rs   r   r   r   r   ry   r   r   r   flatnonzerorr   rw   )r#   r_   r   r`   r	   tree_refry   
leaves_idxs           r   'test_regression_tree_missing_values_toyrW  g
  s   6 	
		"aA
		!A)!488A>DT{qyyQ/3Hzz""Hx1}-x||~- DJJ''+X^^-D-DRa-HI 		!	!R	'DJJ,E,E,JKJ DJJ''
3S9r   c                    t         j                  j                  |       }d}t        j                  |t         j                        j                  dd      }t         j                  |dd d d f<   |j                  |       t        j                  |      }t        | d      j                  ||      }|j                  j                  }t        |dk\        sJ |       y )	NrC  r  r>   r8   ir:   r  r   )rs   r  r8  r   r  r  r  r   r   r   r   ry   r   )rI  r&  rX   r_   r`   r	   ry   s          r   -test_regression_extra_tree_missing_values_toyrY  
  s    
))

 2
3CI
		)2::.66r1=AAcdAgJKKN
		)A+=KOOPQSTUDzz""Hx1}'x'r   c                  >   t        j                  d      \  } }t        j                  j	                  d      }| j                         }|j                  t        j                  dt        j                        | dddgf   dz  	      j                  t              }t        j                  ||<   t        ||d
      \  }}}}t        j                  g dt        j                        }t        ddd      }	 |	j                  ||   ||          t!        |	j"                  j$                  dk\        sJ t        j&                  |	j"                  j(                  dk(  |	j"                  j*                  dk(  z        }
t-        |	j"                  j$                  |
   d       y)a  Check that we properly handle missing values in clasification trees using a toy
    dataset.

    The test is more involved because we use a case where we detected a regression
    in a random forest. We therefore define the seed and bootstrap indices to detect
    one of the non-frequent regression.

    Here, we check that the impurity is null or positive in the leaves.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/28254
    T)
return_X_yr   )r8   r7   )r   r  NrL   rK   )nr     r   )prL   Q   '   a   [   &   .      e   r]  Y   R   rC  r   E      r^     I   J   3   /   k      K   n   rZ   r   h   9      r   rq  O   #   M   Z   rm  rc  r]  ^   ra     rK   ]   r|  rk  rx  r
  r]  rl  m   rr     rC   r{  rs  ri  \   4   rZ   r}  rK   rK      ri  rw  r
  r
  r  r  r   rV   rd  N   r
  r~  i   r  r   rk  r
  f   r  r]  rd  r8   rh  rH       rq  ry  j   rz  r   8   rw  rp  >   U   r^  r_  P   rj  ?   rI   r  T   r;   r;   L   r  r  r;   r   iHnr   r   r>   r8   rS   )r   	load_irisrs   r  r8  r  r  r   r  r$  rB  r  r   r   r   r   r   r   ry   rT  rr   rw   r   )r_   r`   r&  r7  maskr  r/  r   r   r	   rV  s              r   +test_classification_tree_missing_values_toyr  
  sW    .DAq
))


#CI<<
''bhh
/1QV9q=  fTl 	 ffIdO-iLGQ hh  XXG "&zD DHHWWww/0tzz""a'(((		!	!R	'DJJ,E,E,JKJ DJJ''
3S9r   c                     t        dd      }  | j                  t        j                  t        j                         t        j                  | j                        }t        | j                  || j                        }t        j                  | j                  j                  t
        j                        }d|d<   t        || j                  |       | j                  j                  dk(  sJ |j                  dk(  sJ t!        j"                  t$              5  t'        | j                  j(                  |j(                         ddd       t'        | j                  j(                  d   |j(                  d          t        | j                  || j                        }t        j                  | j                  j                  t
        j                        }d|dd t        || j                  |       | j                  j                  dk(  sJ |j                  dk(  sJ |j                         t'        | j                  j(                  |j(                         y# 1 sw Y   xY w)zHTest pruning a tree with the Python caller of the Cythonized prune tree.r   r8   r  r  r;   N)r   r   ra   r   r   rs   
atleast_1drv  r  n_features_in_r4  rk   r   ro   uint8r   r   r   AssertionErrorr'   rz   r	   ra  pruned_treeleave_in_subtrees       r   test_build_pruned_tree_pyr  
  s   !qA>DDHHTYY$doo.IT00)T__MK xx

 5 5RXXFQ+tzz3CD::  A%%%!!Q&&&	~	& @4::++[->->?@tzz''*K,=,=a,@A T00)T__MKxx

 5 5RXXFQR +tzz3CD::  A%%%!!Q&>(>(>>&tzz''):):;@ @s   +II c                     t        dd      }  | j                  t        j                  t        j                         t        j                  | j                        }t        | j                  || j                        }t        j                  | j                  j                  t
        j                        }d|d<   t        j                   t"        d      5  t%        || j                  |       ddd       y# 1 sw Y   yxY w)z8Test pruning a tree does not result in an infinite loop.r   r8   r  r  z,Node has reached a leaf in the original treer   N)r   r   ra   r   r   rs   r  rv  r  r  r4  rk   r   ro   r  r   r   r   r   r  s       r   $test_build_pruned_tree_infinite_loopr  
  s     "qA>DDHHTYY$doo.IT00)T__MK xx

 5 5RXXFQ	H
 I 	k4::7GHI I Is   C77D r  )__doc__r  r  r  rc  r  r  	itertoolsr   r   r  numpyrs   r   joblib.numpy_pickler   numpy.testingr   sklearnr   r   r	   sklearn.dummyr
   sklearn.exceptionsr   sklearn.imputer   sklearn.metricsr   r   r   sklearn.model_selectionr   r   sklearn.pipeliner   sklearn.random_projectionr   sklearn.treer   r   r   r   sklearn.tree._classesr   r   r   r   sklearn.tree._treer   r   r   r   r    r!   r"   r#   r  sklearn.utilsr$   sklearn.utils._testingr%   r&   r'   r(   r)   r*   sklearn.utils.fixesr+   r,   r-   r.   sklearn.utils.validationr/   r   REG_CRITERIONSr   r   dictr6   __annotations__updateSPARSE_TREESr   r  r  y_small_regr_   r`   r   r   r  ra   r  r8  r&  r  r   r\   permr   load_diabetesrb   load_digitsrc   rW   make_multilabel_classificationr  r  r  X_sparse_posr  y_randomr  X_sparse_mixrk   r#  r   r   r   markparametrizer  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r/  r4  r9  r<  r?  rA  r]  rm  rt  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  sortedr:  intersectionr  r  r  r  rW  r  r  r  r  r	  r  r  r  r  r%  r6  rB  r  rO  rQ  rU  rK  rT  rj  rl  rG  rs  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>  rH  rr  r   rL  rR  r  rW  rY  r  r  r  )r	   s   0r   <module>r     s|     	  	  $    , ) ) ) ( - ( U U E * ;     2 /   8%O 5.	 3,	
 &	4  	    	    "((56<94:@>>@74545?@A84544/8 P6 	"XBx"bAq6Aq6Aq6:"X1v1v xiiA
t{{''(IIdO	kk$ "8!!#
x++,d#//$'				
v}}))*kk$d#!!$DXDDbR l
 ###1$'\S  !151$RTJRRT ))$++.mm(//:KKfmm4W-[1$<8$84%H5$84288G$84$N	X	X !1!1!34n5, 6 5,*F*
$ y'89n5J 6 :J y'89,	"0"5	2126	/4	B-r2	 : 
4W 2L>-K<!7H?+DH>FB8
v +1 ,1 ..9W : /W
 &*:
z +G ,G ..9 : /EP0f9%x=(	E*8Z1h30 +,N ,,N^ +	 ,	+$K$(-?"!*&R l3	6 46
 fS->-K-KI-V&WXZ$=>. ? Y. l3$WX.90: : Y 40:f <E4493D$E~	VW
,D$$)2CDnU $WX.9: : Y:" l3!3~~#FHP 4HPV  +, ,,
 ++dVn-DE$ F ,$$ +G ,G ..9G : /G? +, ,,: +.90 : ,0a?H+8 +dVn-DE  F $ vc(--/*k:-FFG &<>Q%RSA TA HMMO4&;=O%PQA R 5A	.:%P +fh%78+dVn-D~-UV W 9 ,4 &RS!1!1!34	> 5 T	> q*& +&B'0T "8:M!NOq!f-G . PG,(2(4?$?"I-,-6@$
B1Uh ,o,,.0G0@0G0G0IJ//& !1!1!34 5* &GH5 I52 q*&GH. I +.B y&&9:A ;A y&&9:* ;* y&&9:* ;*  y&&9:* ;* +dVn-DE
(89%56
 F
 !1!1!34! 5!   
	 	 "7;		 	 "4d;	&(>E	&(;TB .v?' @ 'Z /Y5E5E5G$PT1VWF XF: 		!	!#89		%	%'=>NN,
 "79K!LM 	"&&!RVVQ1-."&&"&&!Q1-.!Q1bffbff-.!Q2661bff-.
 &GH: I
 N:B(,:^<>IO* FDs   	~~.	~8~