a
    �ù»jN¤  ã                
   @   s�  d dl Zd dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZmZmZmZmZ d dlmZmZmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ 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)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z>m?Z?m@Z@mAZAmBZB d dlCmDZD d dlEmFZF dZGejHdd�dd„ ƒZIejJ KdeF¡ejJ Kdd d!g¡ejJ Kd"d#d$g¡d%d&„ ƒƒƒZLd'd(„ ZMejJ Kd"d#d$g¡d)d*„ ƒZNd+d,„ ZOejJ Kdd d!g¡ejJ Kd"d#d$g¡d-d.„ ƒƒZPejJ Kdd d!g¡ejJ Kd"d#d$g¡d/d0„ ƒƒZQejJ Kdd d!g¡ejJ Kd"d#d$g¡ejJ Kd1eRd2ƒ¡d3d4„ ƒƒƒZSd5d6„ ZTeBeUd7�ejJ KdeF¡d8d9„ ƒƒZVejJ Kdd d!g¡d:d;„ ƒZWd<d=„ ZXd>d?„ ZYejJ Kd"d#d$g¡d@dA„ ƒZZejJ Kd"d#d$g¡dBdC„ ƒZ[ejJ Kd"d#d$g¡dDdE„ ƒZ\ejJ KdFej] ^dG¡ _dHdId2¡ej] ^dG¡ _dHdId2dJ¡g¡dKdL„ ƒZ`ejHdMdN„ ƒZaejHdOdP„ ƒZbdQdR„ ZcejJ KdSe de8dTdU�d2¡e de8dTdU�dV¡g¡dWdX„ ƒZedYdZ„ Zfd[d\„ ZgejHdd�d]d^„ ƒZhejHdd�d_d`„ ƒZiejJ KdadIdbg¡ejJ Kdcdddeg¡dfdg„ ƒƒZjdhdi„ ZkejJ Kdjdkdlg¡dmdn„ ƒZldodp„ ZmejJ Kdqdrdsg¡dtdu„ ƒZndvdw„ ZoejJ Kdxepeqg¡dydz„ ƒZrejJ Kdxepeqg¡d{d|„ ƒZsejJ Kd}d~d2dd€œd~d2dd�œg¡d‚dƒ„ ƒZtejJ Kd„g d…¢¡d†d‡„ ƒZuejJ Kdd d!g¡ejJ Kd"d#d$g¡dˆd‰„ ƒƒZvejJ KdŠd‹dŒg¡d�dŽ„ ƒZwejJ Kd�d�geG e xeG¡g¡d‘d’„ ƒZyd“d”„ Zzd•d–„ Z{d—d˜„ Z|d™dš„ Z}d›dœ„ Z~d�dž„ ZdS )Ÿé    N)Úassert_allclose)ÚBaseEstimatorÚClassifierMixinÚclone)ÚCalibratedClassifierCVÚCalibrationDisplayÚ_CalibratedClassifierÚ_sigmoid_calibrationÚ_SigmoidCalibrationÚcalibration_curve)Ú	load_irisÚ
make_blobsÚmake_classification)ÚDummyClassifier)ÚRandomForestClassifierÚVotingClassifier)ÚNotFittedError)ÚDictVectorizer)ÚFrozenEstimator)ÚSimpleImputer)ÚIsotonicRegression)ÚLogisticRegressionÚSGDClassifier)Úbrier_score_loss)ÚKFoldÚLeaveOneOutÚcheck_cvÚcross_val_predictÚcross_val_scoreÚtrain_test_split)ÚMultinomialNB)ÚPipelineÚmake_pipeline)ÚLabelEncoderÚStandardScaler)Ú	LinearSVC)ÚDecisionTreeClassifier)ÚCheckingClassifier)Ú_convert_containerÚassert_almost_equalÚassert_array_almost_equalÚassert_array_equalÚignore_warnings)Úsoftmax)ÚCSR_CONTAINERSéÈ   Úmodule)Úscopec                  C   s   t tddd�\} }| |fS )Né   é*   ©Ú	n_samplesÚ
n_featuresÚrandom_state)r   Ú	N_SAMPLES©ÚXÚy© r<   úDE:\soccer\ml_env\Lib\site-packages\sklearn/tests/test_calibration.pyÚdata9   s    r>   Úcsr_containerÚmethodÚsigmoidÚisotonicÚensembleTFc                 C   s8  t d }| \}}tjjdd�j|jd�}|| ¡ 8 }|d |… |d |… |d |…   }}	}
||d … ||d …  }}tƒ j||	|
d�}| 	|¡d d …df }t
||jd |d�}t t¡� | ||¡ W d   ƒ n1 sÞ0    Y  ||f||ƒ||ƒffD �]0\}}t
||d|d	�}|j||	|
d� | 	|¡d d …df }t||ƒt||ƒk�sZJ ‚|j||	d |
d� | 	|¡d d …df }t||ƒ |j|d|	 d |
d� | 	|¡d d …df }t||ƒ |j||	d d |
d� | 	|¡d d …df }|d
k�rt|d| ƒ n"t||ƒt|d d |ƒk�s J ‚�q d S )Né   r3   ©Úseed©Úsize©Úsample_weighté   ©ÚcvrC   é   ©r@   rM   rC   rA   )r8   ÚnpÚrandomÚRandomStateÚuniformrH   Úminr    ÚfitÚpredict_probar   ÚpytestÚraisesÚ
ValueErrorr   r*   )r>   r@   r?   rC   r5   r:   r;   rJ   ÚX_trainÚy_trainÚsw_trainÚX_testÚy_testÚclfÚprob_pos_clfÚcal_clfZthis_X_trainÚthis_X_testZprob_pos_cal_clfZprob_pos_cal_clf_relabeledr<   r<   r=   Útest_calibration?   sB    (*þ
ÿ



ÿrc   c                 C   s<   | \}}t dd�}| ||¡ |jd j}t|tƒs8J ‚d S )NrD   ©rM   r   )r   rU   Úcalibrated_classifiers_Ú	estimatorÚ
isinstancer%   )r>   r:   r;   Ú	calib_clfZbase_estr<   r<   r=   Ú"test_calibration_default_estimator}   s
    
ri   c                 C   sp   | \}}d}t |d�}t||d�}t|jt ƒs2J ‚|jj|ksBJ ‚| ||¡ |rV|nd}t|jƒ|kslJ ‚d S )NrN   ©Ún_splitsrL   rK   )r   r   rg   rM   rk   rU   Úlenre   )r>   rC   r:   r;   ZsplitsÚkfoldrh   Zexpected_n_clfr<   r<   r=   Útest_calibration_cv_splitter‡   s    
rn   c                 C   s¤   | \}}t dd�}t|dd�}tjtdd�� | ||¡ W d   ƒ n1 sN0    Y  ttƒ dd�}tjtdd�� | ||¡ W d   ƒ n1 s–0    Y  d S )Née   rj   TrL   z$Requesting 101-fold cross-validation©Úmatchz!LeaveOneOut cross-validation does)r   r   rW   rX   rY   rU   r   )r>   r:   r;   rm   rh   r<   r<   r=   Útest_calibration_cv_nfold—   s    
*rr   c                 C   sÆ   t d }| \}}tjjdd�jt|ƒd�}|d |… |d |… |d |…   }}}	||d … }
tdd�}t|||d�}|j|||	d� | 	|
¡}| ||¡ | 	|
¡}tj
 || ¡}|dksÂJ ‚d S )	NrD   r3   rE   rG   ©r7   )r@   rC   rI   çš™™™™™¹?)r8   rP   rQ   rR   rS   rl   r%   r   rU   rV   ZlinalgZnorm)r>   r@   rC   r5   r:   r;   rJ   rZ   r[   r\   r]   rf   Úcalibrated_clfZprobs_with_swZprobs_without_swÚdiffr<   r<   r=   Útest_sample_weight¥   s    (


rw   c                 C   sŠ   | \}}t ||dd�\}}}}ttƒ tdd�ƒ}	t|	|d|d�}
|
 ||¡ |
 |¡}t|	|d|d�}| ||¡ | |¡}t||ƒ dS )zTest parallel calibrationr3   rs   rD   )r@   Zn_jobsrC   rK   N)r   r"   r$   r%   r   rU   rV   r   )r>   r@   rC   r:   r;   rZ   r]   r[   r^   rf   Zcal_clf_parallelZprobs_parallelZcal_clf_sequentialZprobs_sequentialr<   r<   r=   Útest_parallel_execution½   s    ÿ
ÿ
rx   rF   rD   c                 C   sÆ  dd„ }t dd�}tdd|ddd	�\}}d
||d
k< t |¡jd }|d d d
… |d d d
…  }}	|dd d
… |dd d
…  }
}| ||	¡ t|| d|d�}| ||	¡ | |
¡}ttj	|dd�t 
t|
ƒ¡ƒ d| |
|¡  k rìdk sòn J ‚| |
|¡d| |
|¡ k�sJ ‚||t| |
¡ƒ|d�}||||d�}|d| k �sLJ ‚tddd�}| ||	¡ | |
¡}||||d�}t|| d|d�}| ||	¡ | |
¡}||||d�}|d| k �sÂJ ‚d S )Nc                 S   s*   t  |¡|  }t  || d ¡|jd  S )NrD   r   )rP   ÚeyeÚsumÚshape)Úy_trueZ
proba_predÚ	n_classesZY_onehotr<   r<   r=   Úmulticlass_brierÛ   s    z5test_calibration_multiclass.<locals>.multiclass_brieré   rs   iô  éd   é
   ç      .@©r5   r6   r7   ZcentersZcluster_stdrD   r   rK   rN   rO   ©ZaxisçÍÌÌÌÌÌä?gffffffî?)r}   gš™™™™™ñ?é   r3   )Ún_estimatorsr7   )r%   r   rP   Úuniquer{   rU   r   rV   r   rz   Úonesrl   Zscorer-   Údecision_functionr   )r@   rC   rF   r~   r_   r:   r;   r}   rZ   r[   r]   r^   ra   ÚprobasZuncalibrated_brierZcalibrated_brierZ	clf_probsZcal_clf_probsr<   r<   r=   Útest_calibration_multiclassÕ   s<    

ÿ

""ÿ

rŒ   c                  C   sh   G dd„ dƒ} t dddddd�\}}tƒ  ||¡}| ƒ }t||g|jd�}| |¡}t|d	|j ƒ d S )
Nc                   @   s   e Zd Zdd„ ZdS )z9test_calibration_zero_probability.<locals>.ZeroCalibratorc                 S   s   t  |jd ¡S )Nr   )rP   Úzerosr{   ©Úselfr:   r<   r<   r=   Úpredict  s    zAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predictN)Ú__name__Ú
__module__Ú__qualname__r�   r<   r<   r<   r=   ÚZeroCalibrator  s   r”   é2   r�   r   r‚   rƒ   )rf   ZcalibratorsÚclassesç      ð?)r   r   rU   r   Úclasses_rV   r   Z
n_classes_)r”   r:   r;   r_   Ú
calibratorra   r‹   r<   r<   r=   Ú!test_calibration_zero_probability  s    
ÿ

ÿ
rš   )Úcategoryc              
   C   s8  d}t d| ddd�\}}tjjdd�j|jd�}|| ¡ 8 }|d|… |d|… |d|…   }}}||d	| … ||d	| … ||d	| …   }}	}
|d	| d… |d	| d…  }}tƒ }t|d
d�}t	 
t¡� | ||	¡ W d  ƒ n1 sú0    Y  | |||¡ | |¡dd…df }||f| |ƒ| |ƒffD ]ò\}}dD ]â}t||d
d�}tt|ƒ|d�}|
dfD ]´}|j||	|d� |j||	|d� | |¡}| |¡}| |¡}| |¡}|dd…df }|dd…df }t||ƒ t|t ddg¡tj|dd� ƒ t||ƒt||ƒk�svJ ‚�qv�qL�q@dS )z*Test calibration for prefitted classifiersr•   é   r2   r3   r4   rE   rG   NrD   Úprefitrd   rK   )rB   rA   )r@   rM   ©r@   rI   r   r„   )r   rP   rQ   rR   rS   rH   rT   r    r   rW   rX   r   rU   rV   r   r�   r+   ÚarrayZargmaxr   )r?   r5   r:   r;   rJ   rZ   r[   r\   ZX_calibZy_calibZsw_calibr]   r^   r_   Z	unfit_clfr`   Zthis_X_calibrb   r@   Zcal_clf_prefitZcal_clf_frozenÚswZy_prob_prefitZy_prob_frozenZy_pred_prefitZy_pred_frozenZprob_pos_cal_clf_prefitZprob_pos_cal_clf_frozenr<   r<   r=   Útest_calibration_prefit0  sL    (ý
"*þ




ÿ
ÿr¡   c                 C   sª   | \}}t dd�}t||ddd�}| ||¡ | |¡}t|||ddd�}|dkr^td	d
�}ntƒ }| ||¡ | ||¡ | |¡}	| |	¡}
t	|d d …df |
ƒ d S )Nr   rs   rœ   FrO   rŠ   )rM   r@   rB   Zclip)Zout_of_boundsrK   )
r%   r   rU   rV   r   r   r
   rŠ   r�   r   )r>   r@   r:   r;   r_   ra   Z
cal_probasZunbiased_predsr™   Zclf_dfZmanual_probasr<   r<   r=   Útest_calibration_ensemble_falsej  s    



r¢   c                  C   sÄ   t  g d¢¡} t  g d¢¡}t  ddg¡}t|t| |ƒdƒ ddt  |d |  |d  ¡  }tƒ  | |¡ | ¡}t||d	ƒ t 	t
¡�( tƒ  t  | | f¡|¡ W d
  ƒ n1 s¶0    Y  d
S )z0Test calibration values with Platt sigmoid model)rN   éüÿÿÿr—   )rK   éÿÿÿÿr¤   g¿j˜=ïÉ¿gY90¯(àä?rœ   r—   r   rK   r2   N)rP   rŸ   r*   r	   Úexpr
   rU   r�   rW   rX   rY   Zvstack)ZexFZexYZAB_lin_libsvmZlin_probZsk_probr<   r<   r=   Útest_sigmoid_calibrationƒ  s    "r¦   c                  C   sT  t  g d¢¡} t  g d¢¡}t| |dd�\}}t|ƒt|ƒksBJ ‚t|ƒdksRJ ‚t|ddgƒ t|ddgƒ t t¡� tdgd	gƒ W d
  ƒ n1 sœ0    Y  t  g d¢¡}t  g d¢¡}t||ddd�\}}t|ƒt|ƒksêJ ‚t|ƒdksúJ ‚t|ddgƒ t|ddgƒ t t¡� t||dd� W d
  ƒ n1 �sF0    Y  d
S )z Check calibration_curve function)r   r   r   rK   rK   rK   )ç        rt   çš™™™™™É?çš™™™™™é?çÍÌÌÌÌÌì?r—   rD   ©Ún_binsr   rK   rt   rª   gš™™™™™¹¿N)r   r   r   r   rK   rK   )r§   rt   r¨   ç      à?rª   r—   Úquantile©r¬   ÚstrategygUUUUUUå?r©   Z
percentile)r°   )rP   rŸ   r   rl   r)   rW   rX   rY   )r|   Úy_predÚ	prob_trueÚ	prob_predZy_true2Zy_pred2Zprob_true_quantileZprob_pred_quantiler<   r<   r=   Útest_calibration_curve”  s(    ,ÿ
r´   c                 C   sf   t dddddd�\}}tj|d< tdtƒ fdtd	d
�fgƒ}t|dd| d�}| ||¡ | |¡ dS )z$Test that calibration can accept nanr�   rD   r   r3   )r5   r6   Zn_informativeZn_redundantr7   ©r   r   ZimputerÚrfrK   )r‡   rB   )rM   r@   rC   N)	r   rP   Únanr!   r   r   r   rU   r�   )rC   r:   r;   r_   Zclf_cr<   r<   r=   Útest_calibration_nan_imputer³  s    
ÿ

ÿr¸   c                 C   sd   t dddd�\}}g d¢}tddd�}t|d	td
d�| d�}| ||¡ t| |¡jdd�dƒ d S )Nr�   rN   rD   )r5   r6   r}   )
rK   rK   rK   rK   rK   r   r   r   r   r   r—   r   )ÚCr7   rA   rœ   rj   rO   rK   r„   )r   r%   r   r   rU   r   rV   rz   )rC   r:   Ú_r;   r_   Zclf_probr<   r<   r=   Útest_calibration_prob_sumÂ  s    ÿr»   c           	      C   s   t j dd¡}g d¢g d¢ g d¢ }tdd�}t|dtd	ƒ| d
�}| ||¡ | rÎt  d¡}tddgdd	gƒD ]Z\}}|j	|  
|¡}t|d d …|f t  t|ƒ¡ƒ t  |d d …||kf dk¡spJ ‚qpn.|j	d  
|¡}t|jdd�t  |jd ¡ƒ d S )Né   rN   )r   r   r   rK   )rK   rK   rD   rD   )rD   rœ   rœ   rœ   r   rs   rA   rœ   rO   é   r   rD   rK   r„   )rP   rQ   Úrandnr&   r   r   rU   ZarangeÚzipre   rV   r+   r�   rl   Úallr*   rz   r‰   r{   )	rC   r:   r;   r_   ra   r–   Zcalib_iZclass_iZprobar<   r<   r=   Útest_calibration_less_classesÑ  s    
ÿ
 &rÁ   r:   r3   é   rN   r2   c                 C   s4   g d¢}G dd„ dt tƒ}t|ƒ ƒ}| | |¡ dS )z;Test that calibration accepts n-dimensional arrays as input)rK   r   r   rK   rK   r   rK   rK   r   r   rK   r   r   rK   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )z>test_calibration_accepts_ndarray.<locals>.MockTensorClassifierz*A toy estimator that accepts tensor inputsc                 S   s   t  |¡| _| S ©N)rP   rˆ   r˜   )r�   r:   r;   r<   r<   r=   rU   ý  s    zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitc                 S   s   |  |jd d¡jdd�S )Nr   r¤   rK   r„   )Úreshaper{   rz   rŽ   r<   r<   r=   rŠ     s    zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionN)r‘   r’   r“   Ú__doc__rU   rŠ   r<   r<   r<   r=   ÚMockTensorClassifierú  s   rÆ   N)r   r   r   rU   )r:   r;   rÆ   ru   r<   r<   r=   Ú test_calibration_accepts_ndarrayï  s    	
rÇ   c                  C   s<   dddœdddœdddœdddœdddœg} g d	¢}| |fS )
NZNYÚadult)ÚstateZageZTXÚVTÚchildÚCTZBR)rK   r   rK   rK   r   r<   )Ú	dict_dataZtext_labelsr<   r<   r=   rÍ   
  s    ûrÍ   c                 C   s,   | \}}t dtƒ fdtƒ fgƒ}| ||¡S )NZ
vectorizerr_   )r!   r   r   rU   )rÍ   r:   r;   Zpipeline_prefitr<   r<   r=   Údict_data_pipeline  s
    ÿrÎ   c                 C   sj   | \}}|}t t|ƒdd�}| ||¡ t|j|jƒ t|dƒrDJ ‚t|dƒrRJ ‚| |¡ | |¡ dS )aR  Test that calibration works in prefit pipeline with transformer

    `X` is not array-like, sparse matrix or dataframe at the start.
    See https://github.com/scikit-learn/scikit-learn/issues/8710

    Also test it can predict without running into validation errors.
    See https://github.com/scikit-learn/scikit-learn/issues/19637
    rD   rd   Ún_features_in_N)r   r   rU   r+   r˜   Úhasattrr�   rV   )rÍ   rÎ   r:   r;   r_   rh   r<   r<   r=   Útest_calibration_dict_pipeline   s    	
rÑ   zclf, cvrK   ©r¹   r�   c                 C   sš   t ddddd�\}}|dkr(|  ||¡} t| |d�}| ||¡ |dkrht|j| jƒ |j| jks–J ‚n.tƒ  |¡j}t|j|ƒ |j|jd ks–J ‚d S )	Nr�   rN   rD   r   ©r5   r6   r}   r7   r�   rd   rK   )r   rU   r   r+   r˜   rÏ   r#   r{   )r_   rM   r:   r;   rh   r–   r<   r<   r=   Útest_calibration_attributes:  s    	rÔ   c                  C   s„   t ddddd�\} }tdd� | |¡}tt|ƒƒ}d}tjt|d	��, | | d d …d d
…f |¡ W d   ƒ n1 sv0    Y  d S )Nr�   rN   rD   r   rÓ   rK   rÒ   zAX has 3 features, but LinearSVC is expecting 5 features as input.rp   rœ   )r   r%   rU   r   r   rW   rX   rY   )r:   r;   r_   rh   Úmsgr<   r<   r=   Ú2test_calibration_inconsistent_prefit_n_features_inR  s    rÖ   c                  C   sX   t ddddd�\} }tdd„ tdƒD ƒd	d
�}| | |¡ tt|ƒd�}| | |¡ d S )Nr�   rN   rD   r   rÓ   c                 S   s   g | ]}d t |ƒ tƒ f‘qS )Úlr)Ústrr   )Ú.0Úir<   r<   r=   Ú
<listcomp>d  ó    z5test_calibration_votingclassifier.<locals>.<listcomp>rœ   Zsoft)Z
estimatorsZvoting©rf   )r   r   ÚrangerU   r   r   )r:   r;   Zvoterh   r<   r<   r=   Ú!test_calibration_votingclassifier^  s    þrß   c                   C   s
   t dd�S )NT©Z
return_X_y)r   r<   r<   r<   r=   Ú	iris_datan  s    rá   c                 C   s    | \}}||dk  ||dk  fS )NrD   r<   )rá   r:   r;   r<   r<   r=   Úiris_data_binarys  s    râ   r¬   r�   r°   rS   r®   c                 C   sR  |\}}t ƒ  ||¡}tj|||||dd�}| |¡d d …df }t||||d�\}	}
t|j|	ƒ t|j|
ƒ t|j	|ƒ |j
dksˆJ ‚dd l}t|j|jjƒs¤J ‚|j ¡ dks¶J ‚t|j|jjƒsÊJ ‚t|j|jjƒsÞJ ‚|j ¡ dksðJ ‚|j ¡ dk�sJ ‚dd	g}|j ¡  ¡ }t|ƒt|ƒk�s0J ‚|D ]}| ¡ |v �s4J ‚�q4d S )
Nr©   )r¬   r°   ÚalpharK   r¯   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)úPerfectly calibrated)r   rU   r   Úfrom_estimatorrV   r   r   r²   r³   Úy_probÚestimator_nameZ
matplotlibrg   Úline_ÚlinesZLine2DZ	get_alphaÚax_ZaxesZAxesZfigure_ZfigureZFigureÚ
get_xlabelÚ
get_ylabelÚ
get_legendÚ	get_textsrl   Úget_text)Úpyplotrâ   r¬   r°   r:   r;   r×   Úvizræ   r²   r³   ZmplÚexpected_legend_labelsÚlegend_labelsÚlabelsr<   r<   r=   Ú test_calibration_display_computey  s2    ÿÿ
rõ   c           	      C   sz   |\}}t tƒ tƒ ƒ}| ||¡ t |||¡}|jdg}|j ¡  	¡ }t
|ƒt
|ƒks\J ‚|D ]}| ¡ |v s`J ‚q`d S )Nrä   )r"   r$   r   rU   r   rå   rç   rê   rí   rî   rl   rï   )	rð   râ   r:   r;   r_   rñ   rò   ró   rô   r<   r<   r=   Ú$test_plot_calibration_curve_pipeline¤  s    
rö   zname, expected_label)NZ_line1)Úmy_estr÷   c           
      C   sš   t  g d¢¡}t  g d¢¡}t  g ¡}t||||d�}| ¡  |d u rJg n|g}| d¡ |j ¡  ¡ }t|ƒt|ƒks|J ‚|D ]}	|	 	¡ |v s€J ‚q€d S )N©r   rK   rK   r   ©r¨   r©   r©   çš™™™™™Ù?©rç   rä   )
rP   rŸ   r   ÚplotÚappendrê   rí   rî   rl   rï   )
rð   ÚnameZexpected_labelr²   r³   ræ   rñ   rò   ró   rô   r<   r<   r=   Ú'test_calibration_display_default_labels²  s    

rÿ   c           	      C   s    t  g d¢¡}t  g d¢¡}t  g ¡}d}t||||d�}|j|ksHJ ‚d}|j|d� |dg}|j ¡  ¡ }t|ƒt|ƒks‚J ‚|D ]}| 	¡ |v s†J ‚q†d S )Nrø   rù   zname onerû   zname two©rþ   rä   )
rP   rŸ   r   rç   rü   rê   rí   rî   rl   rï   )	rð   r²   r³   ræ   rþ   rñ   rò   ró   rô   r<   r<   r=   Ú)test_calibration_display_label_class_plotÅ  s    
r  Úconstructor_namerå   Zfrom_predictionsc                 C   s  |\}}d}t ƒ  ||¡}| |¡d d …df }tt| ƒ}| dkrL|||fn||f}	||	d|iŽ}
|
j|kspJ ‚| d¡ |
 ¡  |dg}|
j 	¡  
¡ }t|ƒt|ƒks¬J ‚|D ]}| ¡ |v s°J ‚q°| d¡ d}|
j|d� t|ƒt|ƒksôJ ‚|D ]}| ¡ |v søJ ‚qød S )	Nzmy hand-crafted namerK   rå   rþ   rÀ   rä   Zanother_namer   )r   rU   rV   Úgetattrr   rç   Úcloserü   rê   rí   rî   rl   rï   )r  rð   râ   r:   r;   Zclf_namer_   ræ   ÚconstructorÚparamsrñ   rò   ró   rô   r<   r<   r=   Ú,test_calibration_display_name_multiple_callsÙ  s*    


r  c           	      C   sj   |\}}t ƒ  ||¡}tƒ  ||¡}t |||¡}tj||||jd�}|j ¡ d }| d¡dksfJ ‚d S )N)ÚaxrK   rä   )r   rU   r&   r   rå   rê   Zget_legend_handles_labelsÚcount)	rð   râ   r:   r;   r×   Údtrñ   Zviz2rô   r<   r<   r=   Ú!test_calibration_display_ref_lineü  s    r  Údtype_y_strc                 C   s|   t j d¡}t jdgd dgd  | d�}|jdd|jd�}d	}tjt|d
�� t	||ƒ W d  ƒ n1 sn0    Y  dS )zKCheck error message when a `pos_label` is not specified with `str` targets.r3   Úspamrœ   ÚeggsrD   ©Údtyper   rG   z–y_true takes value in {'eggs', 'spam'} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitlyrp   N)
rP   rQ   rR   rŸ   ÚrandintrH   rW   rX   rY   r   )r  ÚrngÚy1Úy2Úerr_msgr<   r<   r=   Ú*test_calibration_curve_pos_label_error_str	  s    ÿr  c                 C   sÈ   t  g d¢¡}t jddg| d�}|| }t  g d¢¡}t||dd�\}}t|g d¢ƒ t||ddd	�\}}t|g d¢ƒ t|d
| ddd	�\}}t|g d¢ƒ t|d
| ddd	�\}}t|g d¢ƒ dS )z8Check the behaviour when passing explicitly `pos_label`.)	r   r   r   rK   rK   rK   rK   rK   rK   r  Úeggr  )	rt   r¨   g333333Ó?rú   r…   gffffffæ?r©   rª   r—   r½   r«   )r   r­   rK   rK   )r¬   Ú	pos_labelrK   r   )r   r   r­   rK   N)rP   rŸ   r   r   )r  r|   r–   Z
y_true_strr±   r²   rº   r<   r<   r=   Ú test_calibration_curve_pos_label  s    r  ÚkwargsÚredú-.)ÚcZlwZls)ÚcolorZ	linewidthZ	linestylec                 C   sf   |\}}t ƒ  ||¡}tj|||fi |¤Ž}|j ¡ dks>J ‚|j ¡ dksPJ ‚|j ¡ dksbJ ‚dS )z*Check that matplotlib aliases are handled.r  rD   r  N)r   rU   r   rå   rè   Ú	get_colorZget_linewidthZget_linestyle)rð   râ   r  r:   r;   r×   rñ   r<   r<   r=   Útest_calibration_display_kwargs.  s    	r   zpos_label, expected_pos_label))NrK   rµ   )rK   rK   c                 C   sô   |\}}t ƒ  ||¡}tj||||d�}| |¡dd…|f }t|||d�\}	}
t|j|	ƒ t|j|
ƒ t|j	|ƒ |j
 ¡ d|› d�ksŽJ ‚|j
 ¡ d|› d�ks¨J ‚|jjdg}|j
 ¡  ¡ }t|ƒt|ƒksÖJ ‚|D ]}| ¡ |v sÚJ ‚qÚdS )z?Check the behaviour of `pos_label` in the `CalibrationDisplay`.)r  Nz,Mean predicted probability (Positive class: ú)z'Fraction of positives (Positive class: rä   )r   rU   r   rå   rV   r   r   r²   r³   ræ   rê   rë   rì   Ú	__class__r‘   rí   rî   rl   rï   )rð   râ   r  Zexpected_pos_labelr:   r;   r×   rñ   ræ   r²   r³   rò   ró   rô   r<   r<   r=   Ú"test_calibration_display_pos_labelA  s*    
ÿÿ
ÿÿr#  c                 C   sP  t dd�\}}tƒ  |¡}|dd… |dd…  }}t |¡d }tj|jd d |jd f|jd�}||ddd…dd…f< ||ddd…dd…f< tj|jd d |jd�}||ddd…< ||ddd…< tƒ }t	|| |dd	�}t
|ƒ}	|	j|||d
� | ||¡ t|	j|jƒD ]\}
}t|
jj|jjƒ �q|	 |¡}| |¡}t||ƒ dS )zrCheck that passing repeating twice the dataset `X` is equivalent to
    passing a `sample_weight` with a factor 2.Trà   Nr€   rD   r   rK   r  )r@   rC   rM   rI   )r   r$   Zfit_transformrP   Ú	ones_liker�   r{   r  r   r   r   rU   r¿   re   r   rf   Zcoef_rV   )r@   rC   r:   r;   rJ   ZX_twiceZy_twicerf   Zcalibrated_clf_without_weightsZcalibrated_clf_with_weightsZest_with_weightsZest_without_weightsZy_pred_with_weightsZy_pred_without_weightsr<   r<   r=   Ú?test_calibrated_classifier_cv_double_sample_weights_equivalenceb  s>    $üþþ

r%  Úfit_params_typeÚlistrŸ   c                 C   sL   |\}}t || ƒt || ƒdœ}tddgd�}t|ƒ}|j||fi |¤Ž dS )z£Tests that fit_params are passed to the underlying base estimator.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/12384
    )ÚaÚbr(  r)  )Zexpected_fit_paramsN)r(   r'   r   rU   )r&  r>   r:   r;   Ú
fit_paramsr_   Úpc_clfr<   r<   r=   Ú test_calibration_with_fit_params”  s    þr,  rJ   r—   c                 C   s.   |\}}t dd�}t|ƒ}|j||| d� dS )zMTests that sample_weight is passed to the underlying base
    estimator.
    T)Zexpected_sample_weightrI   N)r'   r   rU   )rJ   r>   r:   r;   r_   r+  r<   r<   r=   Ú-test_calibration_with_sample_weight_estimator§  s    
r-  c                 C   sn   | \}}t  |¡}G dd„ dtƒ}|ƒ }t|ƒ}t t¡�  |j|||d� W d  ƒ n1 s`0    Y  dS )zÏCheck that even if the estimator doesn't support
    sample_weight, fitting with sample_weight still works.

    There should be a warning, since the sample_weight is not passed
    on to the estimator.
    c                       s   e Zd Z‡ fdd„Z‡  ZS )zPtest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeightc                    s"   d|vsJ ‚t ƒ j||fi |¤ŽS )NrJ   ©ÚsuperrU   )r�   r:   r;   r*  ©r"  r<   r=   rU   Ä  s    zTtest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeight.fit©r‘   r’   r“   rU   Ú__classcell__r<   r<   r0  r=   ÚClfWithoutSampleWeightÃ  s   r3  rI   N)rP   r$  r'   r   rW   ZwarnsÚUserWarningrU   )r>   r:   r;   rJ   r3  r_   r+  r<   r<   r=   Ú0test_calibration_without_sample_weight_estimator¹  s    
r5  c                 C   s>   G dd„ dt ƒ}t|ƒ d�j| dt t| d ƒd ¡iŽ dS )z[Check that CalibratedClassifierCV does not enforce sample alignment
    for fit parameters.c                       s   e Zd Zd‡ fdd„	Z‡  ZS )zJtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifierNc                    s   |d usJ ‚t ƒ j|||d�S )NrI   r.  )r�   r:   r;   rJ   Ú	fit_paramr0  r<   r=   rU   Ô  s    zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit)NNr1  r<   r<   r0  r=   ÚTestClassifierÓ  s   r7  rÝ   r6  rK   N)r   r   rU   rP   r‰   rl   )r>   r7  r<   r<   r=   Ú2test_calibration_with_non_sample_aligned_fit_paramÏ  s    ÿÿr8  c                 C   s&  d}d}t j | ¡j|d�}t  dgt|| ƒ dg|t|| ƒ   ¡}d| d¡ | }td|d	d
�}| ||¡}|D ]T\}}	|| ||  }
}||	 }t	d| d�}| 
|
|¡ | |¡}|dk ¡ szJ ‚qztt	d| d�dd�}t|||dd�}tt	d| d�dd�}t|||dd�}t||ƒ dS )zÓTest that :class:`CalibratedClassifierCV` works with large confidence
    scores when using the `sigmoid` method, particularly with the
    :class:`SGDClassifier`.

    Non-regression test for issue #26766.
    gq=
×£på?iè  rG   rK   r   g     jø@)r¤   rK   NT)rM   r;   Ú
classifierZsquared_hinge)Zlossr7   g     ˆÃ@rA   rž   Zroc_auc)ZscoringrB   )rP   rQ   Zdefault_rngÚnormalrŸ   ÚintrÄ   r   Úsplitr   rU   rŠ   Úanyr   r   r   )Úglobal_random_seedZprobÚnZrandom_noiser;   r:   rM   ÚindicesÚtrainÚtestrZ   r[   r]   Zsgd_clfÚpredictionsZclf_sigmoidZscore_sigmoidZclf_isotonicZscore_isotonicr<   r<   r=   Ú@test_calibrated_classifier_cv_works_with_large_confidence_scoresÝ  s2    	.

þ
þrD  c                 C   s®   t jj| d�}d}|jdd|d�}|jdddd�}d}t|||d	�\}}d
}t|||d	�\}	}
t||d�\}}d}t||	|d� t|	||d� t||
|d� t|
||d� d S )NrE   r€   r   rD   rG   éþÿÿÿ)ÚlowÚhighrH   rt   )rC  r;   Zmax_abs_prediction_thresholdr�   )rC  r;   g�íµ ÷Æ°>)Úatol)rP   rQ   rR   r  rS   r	   r   )r>  r7   r?  r;   Zpredictions_smallZthreshold_1Za1Úb1Zthreshold_2Za2Úb2Úa3Zb3rH  r<   r<   r=   Ú5test_sigmoid_calibration_max_abs_prediction_threshold  s2    ý
ý
þ
rL  c                 C   s,   G dd„ dt ƒ}|ƒ }t|ƒ}|j| Ž  dS )zoCheck that CalibratedClassifierCV works with float32 predict proba.

    Non-regression test for gh-28245.
    c                       s   e Zd Z‡ fdd„Z‡  ZS )z4test_float32_predict_proba.<locals>.DummyClassifer32c                    s   t ƒ  |¡ tj¡S rÃ   )r/  rV   ZastyperP   Zfloat32rŽ   r0  r<   r=   rV   ?  s    zBtest_float32_predict_proba.<locals>.DummyClassifer32.predict_proba)r‘   r’   r“   rV   r2  r<   r<   r0  r=   ÚDummyClassifer32>  s   rM  N)r   r   rU   )r>   rM  Úmodelr™   r<   r<   r=   Útest_float32_predict_proba8  s    rO  c                  C   s8   t jjdd�} dgd dgd  }tdd� | |¡ dS )	zlCheck that CalibratedClassifierCV works with string targets.

    non-regression test for issue #28841.
    )é   rœ   rG   r(  r�   r)  rœ   rd   N)rP   rQ   r:  r   rU   r9   r<   r<   r=   Ú(test_error_less_class_samples_than_foldsH  s    rQ  )€ÚnumpyrP   rW   Znumpy.testingr   Zsklearn.baser   r   r   Zsklearn.calibrationr   r   r   r	   r
   r   Zsklearn.datasetsr   r   r   Zsklearn.dummyr   Zsklearn.ensembler   r   Zsklearn.exceptionsr   Zsklearn.feature_extractionr   Zsklearn.frozenr   Zsklearn.imputer   Zsklearn.isotonicr   Zsklearn.linear_modelr   r   Zsklearn.metricsr   Zsklearn.model_selectionr   r   r   r   r   r   Zsklearn.naive_bayesr    Zsklearn.pipeliner!   r"   Zsklearn.preprocessingr#   r$   Zsklearn.svmr%   Zsklearn.treer&   Zsklearn.utils._mockingr'   Zsklearn.utils._testingr(   r)   r*   r+   r,   Zsklearn.utils.extmathr-   Zsklearn.utils.fixesr.   r8   Zfixturer>   ÚmarkZparametrizerc   ri   rn   rr   rw   rx   rÞ   rŒ   rš   ÚFutureWarningr¡   r¢   r¦   r´   r¸   r»   rÁ   rQ   rR   r¾   rÇ   rÍ   rÎ   rÑ   ÚparamrÔ   rÖ   rß   rá   râ   rõ   rö   rÿ   r  r  r  rØ   Úobjectr  r  r   r#  r%  r,  r‰   r-  r5  r8  rD  rL  rO  rQ  r<   r<   r<   r=   Ú<module>   sþ     

;

=8



þþ


þþ




)ÿ

"



þþ

 0
þþ
2)