o
    tBh                  
   @   s	  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlZddlmZ dd	lmZ dd
l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 ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl'm)Z) ddl'm*Z* ddl+m,Z, ddl+m-Z- ddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddlm5Z5 eegZ6ddgddgddgddgdd gd dggZ7g d!Z8ddgd d gd"d ggZ9g d#Z:ed$d%d&d'd(d)\Z;Z<ee<Z<ej=>dZ?e@ ZAe?BeAjCjDZEeAjFeE eA_FeAjCeE eA_CejGHd*d+d,d- ZIejGjHd.g d/dieJd0fd/d1ieKd2fd3dieJd4fd3d5ieKd6fd*d7ieJd8fd9d:ieJd;fd9d<ieJd=fd9d1ieKd>fd?i ieJd@fdAdieJdBfdAd:ieJdCfdAd<ieJdDfdAd7ieJdEfdFdieJdGfdFd1ieKdHfdId1ieKdJfdKd:ieJdLfdKdMieJdNfdKd1ieKdOfdPdieJdQfdPd5ieKdRfdSd:ieJdTfdSd1ieKdUfdVdieJdWfdVd:ieJdXfdVd1ieKdYfdZdieJd[fdZd:ieJd\fdZd<ieJd]fdZd1ieKd^fd_dieJd`fd_daieJdbfd_d1ieKdcfdddieJdefddd5ieKdffdgdieJdhfdgd<ieKdifdjdieJdkfdjd1ieKdlfdmdnieJdofdmd1ieKdpfdqdrieJdsfdtdu dvejGHdwee;e<feeAjFeAjCfgdxdy ZLejGHdzd{d:d|d}fd~d:d|d}fd{dd|dfd~dd|dfgdd ZMejGHdedfedfed~fed{fedfedfgdd ZNejGHd*d+dd ZOejGHd*dejGHd9ddd ZPejGHd9dejGHdddd ZQdd ZRejGHdwee;e<feeAjFeAjCfgdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXejGYddd ZZdd Z[dd Z\ejGHde6dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd Zfdd Zgdd ZhddÄ ZiejGHde6ddƄ ZjejGHde6ddȄ ZkejGHde6ddʄ ZlejGHde6dd̄ ZmejGHde6dd΄ ZnejGHde6ddЄ ZoejGHde6dd҄ ZpejGHde6ddԄ ZqejGHde6ddք ZrejGHde6dd؄ ZsejGHde6ddڄ Ztdd܄ ZuejGHde6ddބ Zvdd Zwdd Zxdd Zydd ZzejGHde6dd Z{ejGHde6dd Z|dd Z}dd Z~dd Zdd Ze*ejGHdeefejGHdeee	fdd Zdd Zdd Zdd Zdd ZejGjHd eee/feee/feee0fgg ddvdd Zdd ZejGHdee ddfee dd	fgd
d Zdd Zdd ZejGHdeegdd ZejGHde6dd ZejGHdddefddefddefgdd ZejGHdeegdd ZdS (  zP
Testing for the gradient boosting module (sklearn.ensemble.gradient_boosting).
    N)assert_allclose)
csr_matrix)
csc_matrix)
coo_matrix)expit)datasets)clone)make_classificationmake_regression)GradientBoostingClassifier)GradientBoostingRegressor)predict_stages)OneHotEncoderscale)	LinearSVC)mean_squared_error)train_test_split)check_random_state
tosequence)NoSampleWeightWrapper)assert_array_almost_equal)assert_array_equal)skip_if_32bit)DataConversionWarning)NotFittedError)DummyClassifierDummyRegressor)make_pipeline)LinearRegression)NuSVR      )r!   r!   r!   r"   r"   r"      )r!   r"   r"   d         
      )	n_samples
n_featuresn_informativenoiserandom_statelosslog_lossexponentialc                 C   s   t | ddd}tt |t W d    n1 sw   Y  |tt t	|tt
 dt|jks8J |jd d |jdd   }t|dksOJ |t}|jdks[J d S )Nr(   r"   r/   n_estimatorsr.   r!           )   r(   r"   )r   pytestraises
ValueErrorpredictTfitXyr   true_resultlenestimators_train_score_npanyapplyshape)r/   clflog_loss_decreaseleaves rJ   /var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/ensemble/tests/test_gradient_boosting.pytest_classification_toy=   s   
rL   zparams, err_type, err_msglearning_ratez!learning_rate == 0, must be > 0.0fooz*learning_rate must be an instance of floatr4   zn_estimators == 0, must be >= 1      ?z(n_estimators must be an instance of int,foobarzLoss 'foobar' not supported	subsampler5   zsubsample == 0.0, must be > 0.0g?z subsample == 1.1, must be <= 1.0z&subsample must be an instance of floatinitz1The init parameter must be an estimator or 'zero'max_featureszmax_features == 0, must be >= 1z"max_features == 0.0, must be > 0.0z#max_features == 1.1, must be <= 1.0zInvalid value for max_features.verbosezverbose == -1, must be >= 0zverbose must be an instance of
warm_startz!warm_start must be an instance ofvalidation_fractionz)validation_fraction == 0.0, must be > 0.0      ?z)validation_fraction == 1.0, must be < 1.0z0validation_fraction must be an instance of floatn_iter_no_changez#n_iter_no_change == 0, must be >= 1z,n_iter_no_change must be an instance of int,tolztol == 0.0, must be > 0.0z!tol must be an instance of float,min_samples_leafz#min_samples_leaf == 0, must be >= 1z$min_samples_leaf == 0.0, must be > 0z-min_samples_leaf must be an instance of floatmin_samples_splitz$min_samples_split == 1, must be >= 2z'min_samples_split == 0.0, must be > 0.0z(min_samples_split == 1.1, must be <= 1.0z.min_samples_split must be an instance of floatmin_weight_fraction_leafz.min_weight_fraction_leaf == -1, must be >= 0.0g333333?z/min_weight_fraction_leaf == 0.6, must be <= 0.5z5min_weight_fraction_leaf must be an instance of floatmax_leaf_nodesz!max_leaf_nodes == 0, must be >= 2z)max_leaf_nodes must be an instance of int	max_depthzmax_depth == -1, must be >= 1z$max_depth must be an instance of intmin_impurity_decreasez+min_impurity_decrease == -1, must be >= 0.0z2min_impurity_decrease must be an instance of float	ccp_alphag      z!ccp_alpha == -1.0, must be >= 0.0z&ccp_alpha must be an instance of float	criterionmaez!criterion='mae' is not supported.c                 C   s"   t | tr| d d ddS | S )Nr(   ] )
isinstancestrreplace)xrJ   rJ   rK   <lambda>   s   " ri   )idszGradientBoosting, X, yc                 C   sN   | di |}t j||d ||| W d    d S 1 s w   Y  d S NmatchrJ   )r7   r8   r<   )GradientBoostingr=   r>   paramserr_typeerr_msgestrJ   rJ   rK   test_gbdt_parameter_checksP   s
    "rs   zparams, err_msghuber)r/   alphazalpha == 0.0, must be > 0.0quantileg333333?zalpha == 1.2, must be < 1.0c                 C   sJ   t jt|d tdi | tt W d    d S 1 sw   Y  d S rk   )r7   r8   r9   r   r<   X_regy_reg)ro   rq   rJ   rJ   rK   test_gbdt_loss_alpha_error   s   "ry   zGradientBoosting, losslsabsolute_errorr1   r2   c                 C   sB   t t | |dtt W d    d S 1 sw   Y  d S )N)r/   r7   r8   r9   r<   r=   r>   )rn   r/   rJ   rJ   rK   test_wrong_type_loss_function   s   "r}   c           	   	   C   s   t jddd\}}|d d |dd  }}|d d |dd  }}tddd| ddd	}||| d||| }|d
k sAJ tddd| dddd}||| d||| }|dk s`J d S )N.  r"   r*   r.     r%   r#   rW   r   )r4   r[   r^   r/   rM   r.   g
ףp=
?         ?)r4   r[   r^   r/   rM   rQ   r.   g{Gz?)r   make_hastie_10_2r   r<   score)	r/   r=   r>   X_trainX_testy_trainy_testgbrt
error_raterJ   rJ   rK   test_classification_synthetic  s4   	r   )squared_errorr{   rt   )rW   r   c           	   
   C   s   t tt}d }d |d| fD ]8}td| d|dddd}|jtt|d |t}|jdks1J |	t}t
t|}|d	k sAJ |d urF	 |}qd S )
Nr#      r&   r"   r   )r4   r/   r^   rQ   r[   r.   rM   sample_weight)r%   r   g{Gz?)rC   onesr@   rx   r   r<   rw   rE   rF   r:   r   )	r/   rQ   r   last_y_predr   regrI   y_predmserJ   rJ   rK   test_regression_dataset*  s,   



r   r   )Nr"   c                 C   sv   |dkrt ttj}tddd| d}|jtjtj|d |tjtj}|dks,J |	tj}|j
dks9J d S )Nr"   r%   r1   r4   r/   r.   rQ   r   ?)   r%   r$   )rC   r   r@   iristargetr   r<   datar   rE   rF   )rQ   r   rG   r   rI   rJ   rJ   rK   	test_irisS  s   r   c            
      C   s  t d} dddddd}tjd| d	d
\}}|d d |d d }}|dd  |dd  }}t }||| t|||}	|	dk sGJ tjd| d\}}|d d |d d }}|dd  |dd  }}tdi |}||| t|||}	|	dk sJ tjd| d\}}|d d |d d }}|dd  |dd  }}tdi |}||| t|||}	|	dk sJ d S )Nr"   r%   r&   r#   皙?r   )r4   r^   r[   rM   r/     rW   r*   r.   r-   r   g      @r   g     @gQ?rJ   )	r   r   make_friedman1r   r<   r   r:   make_friedman2make_friedman3)
r.   regression_paramsr=   r>   r   r   r   r   rG   r   rJ   rJ   rK   test_regression_syntheticd  s8   	r   c                 C   s2   |  }t |dr
J ||| t |dsJ d S )Nfeature_importances_)hasattrr<   )rn   r=   r>   gbdtrJ   rJ   rK   test_feature_importances  s   
r   c                  C   s   t ddd} tt | t W d    n1 sw   Y  | tt t	| 
tt | t}t|dks<J t|dksEJ | jj|jdddd}t	|t d S )Nr%   r"   r4   r.   r5   rW   axisr   )r   r7   r8   r9   predict_probar;   r<   r=   r>   r   r:   r?   rC   allclasses_takeargmax)rG   y_probar   rJ   rJ   rK   test_probability_log  s   
r   c                  C   s\   g d} t ddd}d}tjt|d |jtt| d W d    d S 1 s'w   Y  d S )N)r   r   r   r"   r"   r"   r%   r"   r   zty contains 1 class after sample_weight trimmed classes with zero weights, while a minimum of 2 classes are required.rl   r   )r   r7   r8   r9   r<   r=   r>   )r   rG   msgrJ   rJ   rK   $test_single_class_with_sample_weight  s   "r   c                  C   s   t jddd\} }t| }tddd}|| | t|jdd}d}t	j
t|d t|j||j| W d    n1 s@w   Y  t| }t	j
tdd t|j||j| W d    d S 1 sfw   Y  d S )	Nr%   r"   r   r   r!   z3When X is a sparse matrix, a CSR format is expectedrl   z X should be C-ordered np.ndarray)r   r   r   r   r<   rC   zerosrF   reshaper7   r8   r9   r   rA   rM   asfortranarray)rh   r>   x_sparse_cscrG   r   rq   	x_fortranrJ   rJ   rK    test_check_inputs_predict_stages  s   
"r   c                  C   s   t jddd\} }| d d | dd  }}|d d |dd  }}tddddddd	}||| ||||}|d
k sFJ d| d S )Nr~   r"   r   r   r%      r#   r   )r4   r[   r^   rM   rS   r.   r   zGB failed with deviance %.4f)r   r   r   r<   _lossdecision_function)r=   r>   r   r   r   r   r   r1   rJ   rJ   rK   test_max_feature_regression  s   r   c           
         s   |    j  j}}t||dd\}}}}tdddddd}||| t|jddd	 } fd
d|D }	|	d dks@J t|	dd h dksNJ dS )a  Test that Gini importance is calculated correctly.

    This test follows the example from [1]_ (pg. 373).

    .. [1] Friedman, J., Hastie, T., & Tibshirani, R. (2001). The elements
       of statistical learning. New York: Springer series in statistics.
    r   r.   rt   r   r6   r%   )r/   rM   r]   r4   r.   Nr!   c                    s   g | ]} j | qS rJ   )feature_names).0s
californiarJ   rK   
<listcomp>  s    z6test_feature_importance_regression.<locals>.<listcomp>MedIncr"   r&   >   AveOccupLatitude	Longitude)	r   r   r   r   r<   rC   argsortr   set)
fetch_california_housing_fxtr=   r>   r   r   r   r   r   
sorted_idxsorted_featuresrJ   r   rK   "test_feature_importance_regression  s    r   z7ignore:`max_features='auto'` has been deprecated in 1.1c                  C   sL  t jddd\} }| j\}}| d d }|d d }tddd}||| |jtt|ks2J t	ddd}||| |j|ksEJ t	ddd}||| |jt|d ks\J t	ddd}||| |jtt|kstJ t	dd	d}||| |jtt
|ksJ t	dd
| jd  d}||| |jdksJ d S )Nr~   r"   r   r   auto)r4   rS   333333?sqrtlog2g{Gz?)r   r   rF   r   r<   max_features_intrC   r   r   r   )r=   r>   _r+   r   r   r   rJ   rJ   rK   test_max_feature_auto  s,   
r   c                  C   s   t jdddd\} }| d d |d d }}| dd  }t }tt tj||tj	d W d    n1 s;w   Y  |
|| ||}||D ]
}|j|jksZJ qPt|| d S )Nr   r"   rW   r   r   dtype)r   r   r   r7   r8   r9   rC   fromiterstaged_predictfloat64r<   r:   rF   r   )r=   r>   r   r   r   rG   r   rJ   rJ   rK   test_staged_predict%  s   
r   c            	      C   s  t jddd\} }| d d |d d }}| dd  |dd  }}tdd}tt tj||tj	d W d    n1 sCw   Y  |
|| ||D ]
}|j|jks]J qSt||| ||D ]}|jd |jd ksyJ d	|jd ksJ qkt||| d S )
Nr   r"   r   r      r4   r   r   r#   )r   r   r   r7   r8   r   rC   r   staged_predict_probar   r<   r   rF   r   r:   r   r   )	r=   r>   r   r   r   r   rG   r   staged_probarJ   rJ   rK   test_staged_predict_proba:  s   
r   	Estimatorc              	   C   s   t jd}|jdd}d|d d df  td }|  }||| dD ]>}t|d| d }|d u r5q&tj	dd	 t
||}W d    n1 sLw   Y  d|d d d < t |d dksdJ q&d S )
Nr   )r(   r$   )sizer&   r"   )r:   r   r   staged_T)record)rC   randomRandomStateuniformastyper   r<   getattrwarningscatch_warningslistr   )r   rngr=   r>   	estimatorfuncstaged_funcstaged_resultrJ   rJ   rK   test_staged_functions_defensiveU  s   r   c                  C   s   t ddd} | tt t| tt dt| j	ksJ zdd l
}W n ty0   dd l}Y nw |j| |jd}d } ||} t| tt dt| j	ksQJ d S )Nr%   r"   r   r   )protocol)r   r<   r=   r>   r   r:   r;   r?   r@   rA   cPickleImportErrorpickledumpsHIGHEST_PROTOCOLloads)rG   r   serialized_clfrJ   rJ   rK   test_serializationh  s   
r   c                  C   s   t ddd} tt | tttt W d    n1 s!w   Y  t	ddd} | tttt | 
tdg ttjdtjd| 
tdg d S )Nr%   r"   r   r#   )r"   r   )r   r7   r8   r9   r<   r=   rC   r   r@   r   r:   r   randr   r   rG   rJ   rJ   rK   test_degenerate_targets|  s   (r   c                  C   s`   t dddddd} | tt | t}t ddddd}|tt |t}t||dd	 d S )
Nr%   rv   r&   r   r)   )r4   r/   r^   ru   r.   r{   )r4   r/   r^   r.   decimal)r   r<   rw   rx   r:   r   )clf_quantile
y_quantileclf_aey_aerJ   rJ   rK   test_quantile_loss  s   


r  c                  C   sV   t ddd} tttt}| t| t| t	tttt
 dt| jks)J d S )Nr%   r"   r   )r   r   maprf   r>   r<   r=   r   r:   r;   r?   r@   rA   )rG   symbol_yrJ   rJ   rK   test_symbol_labels  s
   r
  c                  C   sZ   t ddd} tjttjd}| t| t| t	tjt
tjd dt| jks+J d S Nr%   r"   r   r   )r   rC   asarrayr>   float32r<   r=   r   r:   r;   r?   r@   rA   )rG   float_yrJ   rJ   rK   test_float_class_labels  s
   r  c                  C   s   t ddd} tjttjd}|d d tjf }d}tjt|d | 	t
| W d    n1 s1w   Y  t| tt dt| jksGJ d S )Nr%   r"   r   r   zA column-vector y was passed when a 1d array was expected. Please change the shape of y to \(n_samples, \), for example using ravel().rl   )r   rC   r  r>   int32newaxisr7   warnsr   r<   r=   r   r:   r;   r?   r@   rA   )rG   y_warn_msgrJ   rJ   rK   test_shape_y  s   r  c                  C   s4  t t} tddd}|| t t|tt	 dt
|jks"J t t} tddd}|| t t|tt	 dt
|jksDJ t jtt jd}t |}tddd}|t| t|tt	 dt
|jksnJ t jtt jd}t |}tddd}|t| t|tt	 dt
|jksJ d S r  )rC   r   r=   r   r<   r>   r   r:   r;   r?   r@   rA   ascontiguousarrayr  r  )X_rG   r  rJ   rJ   rK   test_mem_layout  s,   



r  c                  C   sT   t dddd} | tt | jjd dksJ t| jd d tg ddd	 d S )
Nr%   r"   r   r4   r.   rQ   r   r   )gRQ?g333333?gQ?gQg)\(r#   r  )	r   r<   r=   r>   oob_improvement_rF   r   rC   arrayr   rJ   rJ   rK   test_oob_improvement  s   
r  c                  C   sP   t dddd} | tt tt | j W d    d S 1 s!w   Y  d S )Nr%   r"   rW   r  )r   r<   r=   r>   r7   r8   AttributeErrorr  r   rJ   rJ   rK   test_oob_improvement_raise  s
   "r  c                  C   sV   t ddddd} | tjtj | tjtj}|dksJ | jjd | jks)J d S )Nr%   r1   r"   r   r   r   r   )	r   r<   r   r   r   r   r  rF   r4   )rG   r   rJ   rJ   rK   test_oob_multilcass_iris  s   r  c                  C   s   ddl m}  dd l}|j}|  |_tddddd}|tt |j}||_|d |	 
 }ddgd	gd
  d }||ksCJ tdd | D }d|ksTJ d S )Nr   StringIOr%   r"   皙?)r4   r.   rT   rQ    %10s%16sr$   )Iter
Train LosszOOB ImproveRemaining Timec                 s       | ]}d V  qdS r"   NrJ   r   lrJ   rJ   rK   	<genexpr>"      z&test_verbose_output.<locals>.<genexpr>   ior!  sysstdoutr   r<   r=   r>   seekreadlinerstripjoinsum	readlinesr!  r2  
old_stdoutrG   verbose_outputheadertrue_headern_linesrJ   rJ   rK   test_verbose_output  s    
r@  c                  C   s   ddl m}  dd l}|j}|  |_tdddd}|tt |j}||_|d |	 
 }ddgd	gd  d
 }||ksBJ tdd | D }d|ksSJ d S )Nr   r   r%   r"   r#   )r4   r.   rT   r#  r$  r%  )r&  r'  r(  c                 s   r)  r*  rJ   r+  rJ   rJ   rK   r-  >  r.  z+test_more_verbose_output.<locals>.<genexpr>r0  r:  rJ   rJ   rK   test_more_verbose_output'  s   
rA  Clsc                 C   s   t jddd\}}| ddd}||| | dddd}||| |jdd ||| | tu r?t|||| d S t|||| t|||| d S )	Nr%   r"   r   r   r4   r^   Tr4   r^   rU   r   )	r   r   r<   
set_paramsr   r   r:   r   r   rB  r=   r>   rr   est_wsrJ   rJ   rK   test_warm_startC  s   rH  c                 C   sv   t jddd\}}| ddd}||| | dddd}||| |jdd ||| t|||| d S )	Nr%   r"   r   i,  rC  TrD  r   r   r   r<   rE  r   r:   rF  rJ   rJ   rK   test_warm_start_n_estimatorsX     rJ  c                 C   s   t jddd\}}| dddd}||| |jddd ||| |jd	 jdks-J tdd
D ]}|j| df jdksAJ q2d S )Nr%   r"   r   TrD  n   r#   rC  r   r      r   )r   r   r<   rE  rA   r^   range)rB  r=   r>   rr   irJ   rJ   rK   test_warm_start_max_depthg  s   rQ  c                 C   sv   t jddd\}}| ddd}||| | dddd}||| |jdd ||| t|||| d S )	Nr%   r"   r   rC  TrD  F)rU   rI  )rB  r=   r>   rr   est_2rJ   rJ   rK   test_warm_start_clearv  rK  rS  c                 C   t   t jddd\}}| dddd}||| |jdd tt ||| W d    d S 1 s3w   Y  d S )Nr%   r"   r   TrD  r   r   r   r   r<   rE  r7   r8   r9   rB  r=   r>   rr   rJ   rJ   rK   !test_warm_start_zero_n_estimators     "rW  c                 C   rT  )Nr%   r"   r   TrD  c   r   rU  rV  rJ   rJ   rK   $test_warm_start_smaller_n_estimators  rX  rZ  c                 C   sh   t jddd\}}| ddd}||| t|}|j|jdd ||| t|||| d S )Nr%   r"   r   rC  Tr4   rU   )r   r   r<   r   rE  r4   r   r:   )rB  r=   r>   rr   est2rJ   rJ   rK   "test_warm_start_equal_n_estimators  s   r]  c                 C   s   t jddd\}}| dddd}||| |jddd ||| t|jd d td t|jd	d  d
ktjdtd d S )Nr%   r"   r   TrD  rL  r   )r4   rQ   ir5   r(   r   )	r   r   r<   rE  r   r  rC   r   boolrV  rJ   rJ   rK   test_warm_start_oob_switch  s   &r_  c                 C   s   t jddd\}}| ddddd}||| | dddddd}||| |jdd	 ||| t|jd d |jd d  d S )
Nr%   r"   r   r   r   )r4   r^   rQ   r.   Tr4   r^   rQ   r.   rU   r   )r   r   r<   rE  r   r  rF  rJ   rJ   rK   test_warm_start_oob  s   
"ra  c           
      C   s   t jddd\}}tttg}| dddddd}||| || |jdd ||| ||}|D ]?}||}| dddddd}||| || |jdd ||| ||}	t|j	d d |j	d d  t||	 q5d S )	Nr%   r"   r   r   Tr`  r   r   )
r   r   r   r   r   r<   r:   rE  r   r  )
rB  r=   r>   sparse_matrix_type	est_densey_pred_densesparse_constructorX_sparse
est_sparsey_pred_sparserJ   rJ   rK   test_warm_start_sparse  s:   





ri  c                 C   s   t jddd\}}| dddd}| dddd}||| |jdd ||| t|}||| |jdd ||| t|||| d S )Nr%   r"   r   T)r4   r.   rU   rN  r   )r   r   r<   rE  rC   r   r   r:   )rB  r=   r>   est_cest_fortran	X_fortranrJ   rJ   rK   test_warm_start_fortran  s   
rm  c                 C   s   | dkrdS dS )z#Returns True on the 10th iteration.	   TFrJ   )rP  rr   localsrJ   rJ   rK   early_stopping_monitor   s   rp  c                 C   s  t jddd\}}| ddddd}|j||td |jdks J |jjd d	ks*J |jjd d	ks4J |jjd d	ks>J |j	d
d ||| |jd
ksQJ |jjd d
ks[J |jjd d
kseJ | dddddd}|j||td |jdks}J |jjd d	ksJ |jjd d	ksJ |jjd d	ksJ |j	d
dd ||| |jd
ksJ |jjd d
ksJ |jjd d
ksJ |jjd d
ksJ d S )Nr%   r"   r   r   r   )r4   r^   r.   rQ   )monitorr   r(   r   r   T)r4   r^   r.   rQ   rU   Fr[  )
r   r   r<   rp  r4   rA   rF   rB   r  rE  rV  rJ   rJ   rK   test_monitor_early_stopping  s4   
rr  c                  C   s   ddl m}  tjddd\}}d}tdd d|d d}||| |jd	 j}|j|ks.J |j	|j	| k j
d |d ks?J d S )
Nr   	TREE_LEAFr%   r"   r   r&   r   r4   r^   r.   r]   rM  )sklearn.tree._treert  r   r   r   r<   rA   tree_r^   children_leftrF   )rt  r=   r>   krr   treerJ   rJ   rK   test_complete_classification-  s   &r{  c                  C   sb   ddl m}  d}tdd d|d d}|tt |jd j}|j|j| k j	d |d ks/J d S )Nr   rs  r&   r   r"   ru  )r!   r   )
rv  rt  r   r<   rw   rx   rA   rw  rx  rF   )rt  ry  rr   rz  rJ   rJ   rK   test_complete_regression>  s   &r|  c                  C   s   t ddtt} t| tt}tdddddd}|tt |t}tt|}||k s0J tdddd	d
}tt	 |tt W d    d S 1 sOw   Y  d S )Nmean)strategyr   r"   zeror   )r4   r^   r.   rR   rM   r   rP   r4   r^   r.   rR   )
r   r<   rw   rx   r   r:   r   r7   r8   r9   )baselinemse_baselinerr   r   mse_gbdtrJ   rJ   rK   test_zero_estimator_regM  s   


"r  c                  C   s   t j} tt j}tddddd}|| | || |dks!J |dk}d||< d|| < tddddd}|| | || |dksFJ tddddd}t	t
 || | W d    d S 1 sew   Y  d S )Nr   r"   r  r  gQ?r   rP   )r   r   rC   r  r   r   r<   r   r7   r8   r9   )r=   r>   rr   maskrJ   rJ   rK   test_zero_estimator_clfb  s*   
"r  GBEstimatorc                 C   st   t jddd\}}d}| d|d||}|jd j}|jdks"J | dd||}|jd j}|jdks8J d S )Nr%   r"   r   r&   )r^   r]   rM  )r^   )r   r   r<   rA   rw  r^   )r  r=   r>   ry  rr   rz  rJ   rJ   rK   test_max_leaf_nodes_max_depth  s   r  c                 C   sH   t jddd\}}| dd}||| |jjD ]	}|jdks!J qd S )Nr%   r"   r   r   )r_   )r   r   r<   rA   flatr_   )r  r=   r>   rr   rz  rJ   rJ   rK   test_min_impurity_decrease  s   
r  c                  C   sp   t ddd} | ddgddggddg | jjd dksJ | ddgddggddg | jjd dks6J d S )Nr(   Tr[  r   r"   r#   r$   )r   r<   rA   rF   r   rJ   rJ   rK   %test_warm_start_wo_nestimators_change  s
   r  c                  C   s   t dddd} tt | t W d    n1 sw   Y  | tt t	| 
tt | t}t|dks=J t|dksFJ | t }t|d d df td|  | jj|jddd	d}t	|t d S )
Nr2   r%   r"   r3   r5   rW   r#   r   r   )r   r7   r8   r9   r   r;   r<   r=   r>   r   r:   r?   rC   r   r   ravelr   r   r   r   r   )rG   r   r   r   rJ   rJ   rK   test_probability_exponential  s   
r  c                  C   st   ddgddgddgddgg} g d}g d}dD ]}t dd|d}|j| ||d	 |ddggd d
ks7J qd S )Nr"   r   r   r   r"   r   r   r   r"   r"   )rt   r   r{   rv   rW   r#   )rM   r4   r/   r   r   )r   r<   r:   r=   r>   r   r/   gbrJ   rJ   rK   *test_non_uniform_weights_toy_edge_case_reg  s   r  c                  C   sn   ddgddgddgddgg} g d}g d}dD ]}t d|d}|j| ||d t|ddggdg qd S )	Nr"   r   r  r  r0   r   )r4   r/   r   )r   r<   r   r:   r  rJ   rJ   rK   *test_non_uniform_weights_toy_edge_case_clf  s   r  EstimatorClasssparse_matrixc           	      C   sX  t jddddd\}}|d d df }||}| ddddd	||}| ddddd	||}t|||| t|||| t|j|j t|||| t|||| t| trt|	||	| t|
||
| t|||| t|||| t||||D ]\}}t|| qd S d S )
Nr   2   r"   r   )r.   r*   r+   	n_classesr(   r#   gHz>)r4   r.   r^   r_   )r   make_multilabel_classificationr<   r   rE   r:   r   
issubclassr   r   predict_log_probar   zipstaged_decision_function)	r  r  r>   r=   rf  densesparse
res_sparseresrJ   rJ   rK   test_sparse_input  sL   

r  c                  C   s  t ddd\} }tdddddd}tdddddd}t| |dd	\}}}}|dd
f|ddf|ddf|dd
ffD ]"\}}	}
|j|	d ||| |j|
ksOJ |||dksYJ q7tddddd}|| | tddddd}|| | |jdks}J |jdksJ d S )N  r   r   r(   r   r$   *   )r4   rX   rM   r^   r.   r         gMbP?F   )rY   gffffff?r  )r4   rM   r^   r.   r   )r	   r   r   r   rE  r<   n_estimators_r   )r=   r>   gbcgbrr   r   r   r   rr   rY   early_stop_n_estimatorsrJ   rJ   rK   %test_gradient_boosting_early_stopping   sF   r  c                  C   s  t ddd\} }tddddddd	}t|jd
d}t|jdd}tddddddd}t|jd
d}t|jdd}t| |dd\}}	}
}|||
 |||
 |j|jks[J |||
 |||
 |j|jksoJ |||
 |||
 |j|jk sJ |j|jk sJ d S )Nr  r   r   r%   r(   r   r$   r  )r4   rX   rV   rM   r^   r.   r   )rV   r   rX   )r4   rX   rM   r^   rV   r.   r   )r	   r   r   rE  r   r   r<   r  )r=   r>   r  gbc2gbc3r  gbr2gbr3r   r   r   r   rJ   rJ   rK   *test_gradient_boosting_validation_fraction.  s@   r  c                  C   sn   ddgddgddgddgg} g d}t dd}tjtdd	 || | W d    d S 1 s0w   Y  d S )
Nr"   r#   r$   r&   r   )r   r   r   r"   r  z0The least populated class in y has only 1 memberrl   r   r7   r8   r9   r<   )r=   r>   r  rJ   rJ   rK   test_early_stopping_stratifiedY  s   
"r  c                   C   s   t dddS )Nr$   r"   )r  n_clusters_per_class)r	   rJ   rJ   rJ   rK   _make_multiclasse  s   r  z!gb, dataset_maker, init_estimator)zbinary classificationzmulticlass classification
regressionc                 C   s   | \}}t jdd}| }| |dj|||d t| }| |d|| tjtdd | |dj|||d W d    d S 1 sHw   Y  d S )Nr  r%   rR   r   z*estimator.*does not support sample weightsrl   )	rC   r   r   r   r<   r   r7   r8   r9   )r  dataset_makerinit_estimatorr=   r>   r   init_estrJ   rJ   rK    test_gradient_boosting_with_initi  s   

"r  c                  C   s   t dd\} }tt }t|d}|| | tjtdd |j| |t	| j
d d W d    n1 s7w   Y  tjtdd" tdd	d
}t|d}|j| |t	| j
d d W d    d S 1 shw   Y  d S )Nr   r   r  z>The initial estimator Pipeline does not support sample weightsrl   r   znu <= 0 or nu > 1r   rO   )gammanu)r
   r   r   r   r<   r7   r8   r9   rC   r   rF   r   )r=   r>   rR   r  rJ   rJ   rK   )test_gradient_boosting_with_init_pipeline  s   



"r  zestimator, missing_methodr  r   r:   c                 C   sH   d| }t jt|d | tt W d    d S 1 sw   Y  d S )NzFThe init parameter must be a valid estimator and support both fit and rl   r|   )r   missing_methodmessagerJ   rJ   rK   )test_gradient_boosting_init_wrong_methods  s   "r  c                  C   sx   dggd } ddgdgd  }t dddd}tjtdd	 || | W d    n1 s.w   Y  t ddd
d}d S )Nr"   r(   r   r'   r   r"  )rX   r.   rV   z0The training data after the early stopping splitrl   g?r  )r=   r>   r  rJ   rJ   rK   test_early_stopping_n_classes  s   
r  c                  C   s>   t d} t d}t | |}t|jt jdt jd d S )N)r(   r(   )r(   r(   r   )rC   r   r   r   r<   r   r   r   )r=   r>   r  rJ   rJ   rK   'test_gbr_degenerate_feature_importances  s   

r  c                 C   l   t ddgddgg}t ddg}|  ||}tjtdd |j W d    d S 1 s/w   Y  d S )Nr"   r#   r$   r&   r   z`n_features_` was deprecatedrl   )rC   r  r<   r7   r  FutureWarningn_features_r   r=   r>   rr   rJ   rJ   rK   test_n_features_deprecation     "r  c                 C   s   | ddd}t jtdd |tt W d    n1 sw   Y  | ddd}|tt t|drAt|t|t d S t|	t|	t d S )Nr   r   )ra   r.   zCriterion 'mse' was deprecatedrl   r   r   )
r7   r  r  r<   r=   r>   r   r   r   r:   )r   est1r\  rJ   rJ   rK   test_criterion_mse_deprecated  s   
r  zold_loss, new_loss, Estimatorr   laddeviancec                 C   s   || dd}t jtd|  dd |tt W d    n1 s"w   Y  ||dd}|tt t|t|t d S )Nr   )r/   r.   zThe loss.* 'z' was deprecatedrl   )r7   r  r  r<   r=   r>   r   r:   )old_lossnew_lossr   r  r\  rJ   rJ   rK   test_loss_deprecated  s   r  c                 C   r  )Nr"   r#   r$   r&   r   z`loss_` was deprecatedrl   )rC   r  r<   r7   r  r  loss_r  rJ   rJ   rK   test_loss_attribute_deprecation  r  r  )__doc__r   numpyrC   numpy.testingr   scipy.sparser   r   r   scipy.specialr   r7   sklearnr   sklearn.baser   sklearn.datasetsr	   r
   sklearn.ensembler   r   #sklearn.ensemble._gradient_boostingr   sklearn.preprocessingr   r   sklearn.svmr   sklearn.metricsr   sklearn.model_selectionr   sklearn.utilsr   r   sklearn.utils._mockingr   sklearn.utils._testingr   r   r   sklearn.exceptionsr   r   sklearn.dummyr   r   sklearn.pipeliner   sklearn.linear_modelr   r   GRADIENT_BOOSTING_ESTIMATORSr=   r>   r;   r?   rw   rx   r   r   r   	load_irisr   permutationr   r   permr   markparametrizerL   r9   	TypeErrorrs   ry   r}   r   r   r   r   r   r   r   r   r   r   filterwarningsr   r   r   r   r   r   r  r
  r  r  r  r  r  r  r@  rA  rH  rJ  rQ  rS  rW  rZ  r]  r_  ra  ri  rm  rp  rr  r{  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rJ   rJ   rJ   rK   <module>   s   (


	 "',0268=>@DFKPUZ_ceikpuy{   
	

"'+
	
!
!
 











#

$


(.+		



