U
    _»{jñž  ã                
   @   sV  d dl Zd dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZ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#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.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@ d dlAmBZB dZCejDdd�dd„ ƒZEejF Gdddg¡ejF Gd d!d"g¡d#d$„ ƒƒZHd%d&„ ZIejF Gd d!d"g¡d'd(„ ƒZJejF Gdddg¡ejF Gd d!d"g¡d)d*„ ƒƒZKejF Gdddg¡ejF Gd d!d"g¡d+d,„ ƒƒZLejF Gdddg¡ejF Gd d!d"g¡ejF Gd-eMd.ƒ¡d/d0„ ƒƒƒZNd1d2„ ZOd3d4„ ZPejF Gdddg¡d5d6„ ƒZQd7d8„ ZRd9d:„ ZSejF Gd d!d"g¡d;d<„ ƒZTejF Gd d!d"g¡d=d>„ ƒZUejF Gd d!d"g¡d?d@„ ƒZVejF GdAejW XdB¡ YdCdDd.¡ejW XdB¡ YdCdDd.dE¡g¡dFdG„ ƒZZejDdHdI„ ƒZ[ejDdJdK„ ƒZ\dLdM„ Z]ejF GdNe ^e7dOdPdQ�d.¡e ^e7dOdPdQ�dR¡g¡dSdT„ ƒZ_dUdV„ Z`dWdX„ ZaejDdd�dYdZ„ ƒZbejDdd�d[d\„ ƒZcejF Gd]dDd^g¡ejF Gd_d`dag¡dbdc„ ƒƒZdddde„ ZeejF Gdfdgdhg¡didj„ ƒZfdkdl„ ZgejF Gdmdndog¡dpdq„ ƒZhdrds„ ZiejF Gdtejekg¡dudv„ ƒZlejF Gdtejekg¡dwdx„ ƒZmejF Gdydzd{d|g¡d}d~„ ƒZnejF Gdddg¡ejF Gd d!d"g¡dd€„ ƒƒZoejF Gd�d‚dƒg¡d„d…„ ƒZpejF Gd†d‡geC e qeC¡g¡dˆd‰„ ƒZrdŠd‹„ ZsejF Gdddg¡ejF Gd d!d"g¡dŒd�„ ƒƒZtdŽd�„ Zud�d‘„ Zvd’d“„ Zwd”d•„ Zxd–d—„ ZydS )˜é    N)Úassert_allclose)Úsparse)ÚBaseEstimatorÚclone)ÚCalibratedClassifierCVÚCalibrationDisplayÚ_CalibratedClassifierÚ_sigmoid_calibrationÚ_SigmoidCalibrationÚcalibration_curve)Ú	load_irisÚ
make_blobsÚmake_classification)ÚDummyClassifier)ÚRandomForestClassifierÚVotingClassifier)ÚNotFittedError)ÚDictVectorizer)Ú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)ÚsoftmaxéÈ   Úmodule)Úscopec                  C   s   t tddd�\} }| |fS )Né   é*   ©Ú	n_samplesÚ
n_featuresÚrandom_state)r   Ú	N_SAMPLES)ÚXÚy© r8   ú]/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sklearn/tests/test_calibration.pyÚdata7   s    r:   ÚmethodÚsigmoidÚisotonicÚensembleTFc              	   C   s(  t d }| \}}tjjdd�j|jd�}|| ¡ 8 }|d |… |d |… |d |…   }}}	||d … ||d …  }
}tdd�j|||	d�}| 	|
¡d d …df }t
||jd |d	�}t t¡� | ||¡ W 5 Q R X ||
ft |¡t |
¡ffD �],\}}t
||d
|d�}|j|||	d� | 	|¡d d …df }t||ƒt||ƒk�sNt‚|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| ƒ qôt||ƒt|d d |ƒksôt‚qôd S )Né   r0   ©Úseed©ÚsizeT©Zforce_alpha©Úsample_weighté   ©Úcvr>   é   ©r;   rI   r>   r<   )r5   ÚnpÚrandomÚRandomStateÚuniformrC   Úminr   ÚfitÚpredict_probar   ÚpytestÚraisesÚ
ValueErrorr   Ú
csr_matrixr   ÚAssertionErrorr)   )r:   r;   r>   r2   r6   r7   rF   ÚX_trainÚy_trainÚsw_trainÚX_testÚy_testÚclfÚprob_pos_clfÚcal_clfZthis_X_trainÚthis_X_testÚprob_pos_cal_clfZprob_pos_cal_clf_relabeledr8   r8   r9   Útest_calibration=   sF    (þ
 ÿ




 ÿrb   c                 C   s<   | \}}t dd�}| ||¡ |jd j}t|tƒs8t‚d S )Nr?   ©rI   r   )r   rQ   Úcalibrated_classifiers_Ú	estimatorÚ
isinstancer$   rW   )r:   r6   r7   Ú	calib_clfZbase_estr8   r8   r9   Ú"test_calibration_default_estimatorz   s
    
rh   c                 C   sp   | \}}d}t |d�}t||d�}t|jt ƒs2t‚|jj|ksBt‚| ||¡ |rV|nd}t|jƒ|kslt‚d S )NrJ   )Ún_splitsrH   rG   )	r   r   rf   rI   rW   ri   rQ   Úlenrd   )r:   r>   r6   r7   ZsplitsZkfoldrg   Zexpected_n_clfr8   r8   r9   Útest_calibration_cv_splitter„   s    
rk   c                 C   sÈ   t d }| \}}tjjdd�jt|ƒd�}|d |… |d |… |d |…   }}}	||d … }
tddd�}t|||d�}|j|||	d� | 	|
¡}| ||¡ | 	|
¡}tj
 || ¡}|d	ksÄt‚d S )
Nr?   r0   r@   rB   Úauto©Údualr4   )r;   r>   rE   çš™™™™™¹?)r5   rL   rM   rN   rO   rj   r$   r   rQ   rR   ZlinalgZnormrW   )r:   r;   r>   r2   r6   r7   rF   rX   rY   rZ   r[   re   Úcalibrated_clfZprobs_with_swZprobs_without_swÚdiffr8   r8   r9   Útest_sample_weight”   s    (

rr   c                 C   sŒ   | \}}t ||dd�\}}}}ttƒ tddd�ƒ}	t|	|d|d�}
|
 ||¡ |
 |¡}t|	|d|d�}| ||¡ | |¡}t||ƒ dS )	zTest parallel calibrationr0   )r4   rl   rm   r?   )r;   Zn_jobsr>   rG   N)r   r!   r#   r$   r   rQ   rR   r   )r:   r;   r>   r6   r7   rX   r[   rY   r\   re   Zcal_clf_parallelZprobs_parallelZcal_clf_sequentialZprobs_sequentialr8   r8   r9   Útest_parallel_execution¬   s(       ÿ
   ÿ
rs   rA   r?   c                 C   sÈ  dd„ }t d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 t‚| |
|¡d| |
|¡ k�st‚||t| |
¡ƒ|d�}||||d�}|d| k �sNt‚tddd�}| ||	¡ | |
¡}||||d�}t|| d|d�}| ||	¡ | |
¡}||||d�}|d| k �sÄt‚d S )Nc                 S   s*   t  |¡|  }t  || d ¡|jd  S )Nr?   r   )rL   ÚeyeÚsumÚshape)Úy_trueZ
proba_predÚ	n_classesZY_onehotr8   r8   r9   Úmulticlass_brierÊ   s    z5test_calibration_multiclass.<locals>.multiclass_brierrl   é   rm   iô  éd   é
   ç      .@©r2   r3   r4   ZcentersZcluster_stdr?   r   rG   rJ   rK   ©ZaxisçÍÌÌÌÌÌä?gffffffî?)rx   gš™™™™™ñ?é   r0   )Ún_estimatorsr4   )r$   r   rL   Úuniquerv   rQ   r   rR   r   ru   Úonesrj   ZscorerW   r+   Údecision_functionr   )r;   r>   rA   ry   r]   r6   r7   rx   rX   rY   r[   r\   r_   ÚprobasZuncalibrated_brierZcalibrated_brierZ	clf_probsZcal_clf_probsr8   r8   r9   Útest_calibration_multiclassÄ   sH        ÿ

""  ÿ

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   )rL   Úzerosrv   ©Úselfr6   r8   r8   r9   Úpredict  s    zAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predictN)Ú__name__Ú
__module__Ú__qualname__r‹   r8   r8   r8   r9   ÚZeroCalibrator  s   r�   é2   r|   rz   r}   r~   )re   ZcalibratorsÚclassesç      ð?)r   r   rQ   r   Úclasses_rR   r   Z
n_classes_)r�   r6   r7   r]   Ú
calibratorr_   r†   r8   r8   r9   Ú!test_calibration_zero_probability  s"        ÿ
  ÿ
r•   c               
   C   sÞ  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d
d�}t|dd�}t	 
t¡� | ||¡ W 5 Q R X | |||¡ | |
¡dd…df }||
ft |¡t |
¡ffD ]¤\}}dD ]”}t||dd�}|	dfD ]v}|j|||d� | |¡}| |¡}|dd…df }t|t ddg¡tj|dd� ƒ t||ƒt||ƒk�sZt‚�qZ�q@�q4dS )z*Test calibration for prefitted classifiersr�   é   r/   r0   r1   r@   rB   Nr?   TrD   Úprefitrc   rG   )r=   r<   )r;   rI   rE   r   r   )r   rL   rM   rN   rO   rC   rP   r   r   rS   rT   r   rQ   rR   r   rV   r‹   r*   ÚarrayZargmaxr   rW   )r2   r6   r7   rF   rX   rY   rZ   ZX_calibZy_calibZsw_calibr[   r\   r]   Z	unfit_clfr^   Zthis_X_calibr`   r;   r_   ÚswÚy_probÚy_predra   r8   r8   r9   Útest_calibration_prefit  s>    (ý
"
þ

"
 ÿrœ   c                 C   s¬   | \}}t d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 )Nrl   rz   rm   r–   FrK   r…   )rI   r;   r=   Zclip)Zout_of_boundsrG   )
r$   r   rQ   rR   r   r   r
   r…   r‹   r   )r:   r;   r6   r7   r]   r_   Z
cal_probasZunbiased_predsr”   Zclf_dfZmanual_probasr8   r8   r9   Útest_calibration_ensemble_falseN  s    


r�   c               	   C   s´   t  dddg¡} t  dddg¡}t  ddg¡}t|t| |ƒdƒ ddt  |d	 |  |d  ¡  }tƒ  | |¡ | ¡}t||d
ƒ t 	t
¡� tƒ  t  | | f¡|¡ W 5 Q R X dS )z0Test calibration values with Platt sigmoid modelrJ   éüÿÿÿr’   rG   éÿÿÿÿg¿j˜=ïÉ¿gY90¯(àä?r–   r   r/   N)rL   r˜   r)   r	   Úexpr
   rQ   r‹   rS   rT   rU   Úvstack)ZexFZexYZAB_lin_libsvmZlin_probZsk_probr8   r8   r9   Útest_sigmoid_calibrationg  s    "r¢   c               	   C   sL  t  ddddddg¡} t  ddddddg¡}t| |d	d
�\}}t|ƒt|ƒksRt‚t|ƒd	ksbt‚t|ddgƒ t|ddgƒ t t¡� tdgdgƒ W 5 Q R X t  ddddddg¡}t  ddddddg¡}t||d	dd�\}}t|ƒt|ƒksöt‚t|ƒd	k�st‚t|ddgƒ t|ddgƒ t t¡� t||dd� W 5 Q R X dS )z Check calibration_curve functionr   rG   g        ro   çš™™™™™É?çš™™™™™é?çÍÌÌÌÌÌì?r’   r?   ©Ún_binsgš™™™™™¹¿ç      à?Úquantile©r§   ÚstrategygUUUUUUå?Z
percentile)r«   N)	rL   r˜   r   rj   rW   r(   rS   rT   rU   )rw   r›   Ú	prob_trueÚ	prob_predZy_true2Zy_pred2Zprob_true_quantileZprob_pred_quantiler8   r8   r9   Útest_calibration_curvex  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|   r?   r   r0   )r2   r3   Zn_informativeZn_redundantr4   ©r   r   ZimputerÚrfrG   )r‚   r=   )rI   r;   r>   N)	r   rL   Únanr    r   r   r   rQ   r‹   )r>   r6   r7   r]   Zclf_cr8   r8   r9   Útest_calibration_nan_imputer—  s        ÿ

ÿr²   c                 C   sn   d}t dd|d�\}}tdddd�}t|d	tƒ | d
�}| ||¡ | |¡}t|jdd�t 	|j
d ¡ƒ d S )Nr?   r|   rJ   )r2   r3   rx   rl   r’   rz   ©rn   ÚCr4   r<   rK   rG   r   r   )r   r$   r   r   rQ   rR   r)   ru   rL   r„   rv   )r>   Znum_classesr6   r7   r]   Zclf_probZprobsr8   r8   r9   Útest_calibration_prob_sum¦  s       ÿ
rµ   c                 C   sî   t j dd¡}t  d¡}tdddd�}t|dtƒ | d�}| ||¡ t|j	ƒD ]š\}}| 
|¡}| rÎt|d d …|f t  t|ƒ¡ƒ t  |d d …d |…f d	k¡s¦t‚t  |d d …|d
 d …f d	k¡sèt‚qNt  |d
|jd	  ¡sNt‚qNd S )Nr|   rJ   rl   r’   rz   r³   r<   rK   r   rG   )rL   rM   ÚrandnZaranger$   r   r   rQ   Ú	enumeraterd   rR   r*   rˆ   rj   ÚallrW   Zallcloserv   )r>   r6   r7   r]   r_   ÚiÚcalibrated_classifierZprobar8   r8   r9   Útest_calibration_less_classes¶  s"    
   ÿ
 "(r»   r6   r0   é   rJ   r/   c                 C   sL   dddddddddddddddg}G dd„ dt ƒ}t|ƒ ƒ}| | |¡ dS )z;Test that calibration accepts n-dimensional arrays as inputrG   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)rL   rƒ   r“   )rŠ   r6   r7   r8   r8   r9   rQ   ß  s    zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitc                 S   s   |  |jd d¡jdd�S )Nr   rŸ   rG   r   )Úreshaperv   ru   r‰   r8   r8   r9   r…   ã  s    zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionN)rŒ   r�   rŽ   Ú__doc__rQ   r…   r8   r8   r8   r9   ÚMockTensorClassifierÜ  s   r¿   N)r   r   rQ   )r6   r7   r¿   rp   r8   r8   r9   Ú test_calibration_accepts_ndarrayÑ  s    	"
rÀ   c                  C   s.   dddœdddœdddœg} dddg}| |fS )	NZNYÚadult)ÚstateZageZTXÚVTÚchildrG   r   r8   )Ú	dict_dataZtext_labelsr8   r8   r9   rÅ   ì  s    ý
rÅ   c                 C   s,   | \}}t dtƒ fdtƒ fgƒ}| ||¡S )NZ
vectorizerr]   )r    r   r   rQ   )rÅ   r6   r7   Zpipeline_prefitr8   r8   r9   Údict_data_pipeline÷  s
    ÿrÆ   c                 C   sf   | \}}|}t |dd�}| ||¡ t|j|jƒ t|dƒr@t‚t|dƒrNt‚| |¡ | |¡ 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
    r—   rc   Ún_features_in_N)r   rQ   r*   r“   ÚhasattrrW   r‹   rR   )rÅ   rÆ   r6   r7   r]   rg   r8   r8   r9   Útest_calibration_dict_pipeline   s    	
rÉ   zclf, cvrl   rG   ©rn   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–t‚n.tƒ  |¡j}t|j|ƒ |j|jd ks–t‚d S )	Nr|   rJ   r?   rz   ©r2   r3   rx   r4   r—   rc   rG   )	r   rQ   r   r*   r“   rÇ   rW   r"   rv   )r]   rI   r6   r7   rg   r‘   r8   r8   r9   Útest_calibration_attributes  s    	rÌ   c               	   C   sr   t ddddd�\} }tddd� | |¡}t|d	d
�}d}tjt|d��" | | d d …d d…f |¡ W 5 Q R X d S )Nr|   rJ   r?   rz   rË   rl   rG   rÊ   r—   rc   zAX has 3 features, but LinearSVC is expecting 5 features as input.©Úmatchr–   )r   r$   rQ   r   rS   rT   rU   )r6   r7   r]   rg   Úmsgr8   r8   r9   Ú2test_calibration_inconsistent_prefit_n_features_in2  s    rÐ   c                  C   sV   t ddddd�\} }tdd„ tdƒD ƒd	d
�}| | |¡ t|dd�}| | |¡ d S )Nr|   rJ   r?   rz   rË   c                 S   s   g | ]}d t |ƒ tƒ f‘qS )Úlr)Ústrr   )Ú.0r¹   r8   r8   r9   Ú
<listcomp>D  s     z5test_calibration_votingclassifier.<locals>.<listcomp>r–   Zsoft)Z
estimatorsZvotingr—   )re   rI   )r   r   ÚrangerQ   r   )r6   r7   Zvoterg   r8   r8   r9   Ú!test_calibration_votingclassifier>  s    þrÖ   c                   C   s
   t dd�S )NT©Z
return_X_y)r   r8   r8   r8   r9   Ú	iris_dataN  s    rØ   c                 C   s    | \}}||dk  ||dk  fS )Nr?   r8   )rØ   r6   r7   r8   r8   r9   Úiris_data_binaryS  s    rÙ   r§   r|   r«   rO   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ˆt‚dd l}t|j|jjƒs¤t‚|j ¡ dks¶t‚t|j|jjƒsÊt‚t|j|jjƒsÞt‚|j ¡ dksðt‚|j ¡ dk�st‚dd	g}|j ¡  ¡ }t|ƒt|ƒk�s0t‚|D ]}| ¡ |k�s4t‚�q4d S )
Nr¤   )r§   r«   ÚalpharG   rª   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)úPerfectly calibrated)r   rQ   r   Úfrom_estimatorrR   r   r   r¬   r­   rš   Úestimator_namerW   Z
matplotlibrf   Zline_ÚlinesZLine2DZ	get_alphaÚax_ZaxesZAxesZfigure_ZfigureZFigureÚ
get_xlabelÚ
get_ylabelÚ
get_legendÚ	get_textsrj   Úget_text)ÚpyplotrÙ   r§   r«   r6   r7   rÑ   Úvizrš   r¬   r­   ZmplÚexpected_legend_labelsÚlegend_labelsÚlabelsr8   r8   r9   Ú test_calibration_display_computeY  sB         ÿ   ÿ
rê   c           	      C   sz   |\}}t tƒ tƒ ƒ}| ||¡ t |||¡}|jdg}|j ¡  	¡ }t
|ƒt
|ƒks\t‚|D ]}| ¡ |ks`t‚q`d S )NrÛ   )r!   r#   r   rQ   r   rÜ   rÝ   rß   râ   rã   rj   rW   rä   )	rå   rÙ   r6   r7   r]   ræ   rç   rè   ré   r8   r8   r9   Ú$test_plot_calibration_curve_pipeline„  s    
rë   zname, expected_label)NZ_line1)Úmy_estrì   c           
      C   s¢   t  ddddg¡}t  ddddg¡}t  g ¡}t||||d�}| ¡  |d krRg n|g}| d¡ |j ¡  ¡ }t|ƒt|ƒks„t	‚|D ]}	|	 
¡ |ksˆt	‚qˆd S )Nr   rG   r£   r¤   çš™™™™™Ù?©rÝ   rÛ   )rL   r˜   r   ÚplotÚappendrß   râ   rã   rj   rW   rä   )
rå   ÚnameZexpected_labelr¬   r­   rš   ræ   rç   rè   ré   r8   r8   r9   Ú'test_calibration_display_default_labels’  s    

rò   c           	      C   s¨   t  ddddg¡}t  ddddg¡}t  g ¡}d}t||||d�}|j|ksPt‚d}|j|d	� |d
g}|j ¡  ¡ }t	|ƒt	|ƒksŠt‚|D ]}| 
¡ |ksŽt‚qŽd S )Nr   rG   r£   r¤   rí   zname onerî   zname two©rñ   rÛ   )rL   r˜   r   rÝ   rW   rï   rß   râ   rã   rj   rä   )	rå   r¬   r­   rš   rñ   ræ   rç   rè   ré   r8   r8   r9   Ú)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spt‚| d¡ |
 ¡  |dg}|
j	 
¡  ¡ }t|ƒt|ƒks¬t‚|D ]}| ¡ |ks°t‚q°| d¡ d}|
j|d� t|ƒt|ƒksôt‚|D ]}| ¡ |ksøt‚qød S )	Nzmy hand-crafted namerG   rÜ   rñ   r¸   rÛ   Zanother_nameró   )r   rQ   rR   Úgetattrr   rÝ   rW   Úcloserï   rß   râ   rã   rj   rä   )rõ   rå   rÙ   r6   r7   Zclf_namer]   rš   ÚconstructorÚparamsræ   rç   rè   ré   r8   r8   r9   Ú,test_calibration_display_name_multiple_calls¹  s*    


rú   c           	      C   sj   |\}}t ƒ  ||¡}tƒ  ||¡}t |||¡}tj||||jd�}|j ¡ d }| d¡dksft‚d S )N)ZaxrG   rÛ   )	r   rQ   r%   r   rÜ   rß   Zget_legend_handles_labelsÚcountrW   )	rå   rÙ   r6   r7   rÑ   Údtræ   Zviz2ré   r8   r8   r9   Ú!test_calibration_display_ref_lineÜ  s    rý   Údtype_y_strc              	   C   sh   t j d¡}t jdgd dgd  | d�}|jdd|jd�}d	}tjt|d
�� t	||ƒ W 5 Q R X dS )zKCheck error message when a `pos_label` is not specified with `str` targets.r0   Úspamr–   Úeggsr?   ©Údtyper   rB   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 explicitlyrÍ   N)
rL   rM   rN   r˜   ÚrandintrC   rS   rT   rU   r   )rþ   ÚrngÚy1Úy2Úerr_msgr8   r8   r9   Ú*test_calibration_curve_pos_label_error_stré  s    ÿr  c                 C   sô   t  dddddddddg	¡}t jddg| d�}|| }t  dddd	d
ddddg	¡}t||dd�\}}t|ddddgƒ t||ddd�\}}t|ddddgƒ t|d| ddd�\}}t|ddddgƒ t|d| ddd�\}}t|ddddgƒ dS )z8Check the behaviour when passing explicitly `pos_label`.r   rG   rÿ   Úeggr  ro   r£   g333333Ó?rí   r€   gffffffæ?r¤   r¥   r’   é   r¦   r¨   )r§   Ú	pos_labelN)rL   r˜   r   r   )rþ   rw   r‘   Z
y_true_strr›   r¬   Ú_r8   r8   r9   Ú test_calibration_curve_pos_labelù  s    r  zpos_label, expected_pos_label)NrG   r¯   )rG   rG   c                 C   sô   |\}}t ƒ  ||¡}tj||||d�}| |¡dd…|f }t|||d�\}	}
t|j|	ƒ t|j|
ƒ t|j	|ƒ |j
 ¡ d|› d�ksŽt‚|j
 ¡ d|› d�ks¨t‚|jjdg}|j
 ¡  ¡ }t|ƒt|ƒksÖt‚|D ]}| ¡ |ksÚt‚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   rQ   r   rÜ   rR   r   r   r¬   r­   rš   rß   rà   rW   rá   Ú	__class__rŒ   râ   rã   rj   rä   )rå   rÙ   r  Zexpected_pos_labelr6   r7   rÑ   ræ   rš   r¬   r­   rç   rè   ré   r8   r8   r9   Ú"test_calibration_display_pos_label  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{   r?   r   rG   r  ©r;   r>   rI   rE   )r   r#   Úfit_transformrL   Ú	ones_likerˆ   rv   r  r   r   r   rQ   Úziprd   r   re   Úcoef_rR   )r;   r>   r6   r7   rF   ZX_twiceZy_twicere   Úcalibrated_clf_without_weightsÚcalibrated_clf_with_weightsÚest_with_weightsÚest_without_weightsÚy_pred_with_weightsÚy_pred_without_weightsr8   r8   r9   Ú?test_calibrated_classifier_cv_double_sample_weights_equivalence/  s>    $üþþ

r  Úfit_params_typeÚlistr˜   c                 C   sH   |\}}t || ƒt || ƒdœ}tddgd�}t|ƒ}|j||f|Ž 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   rQ   )r  r:   r6   r7   Ú
fit_paramsr]   Úpc_clfr8   r8   r9   Ú test_calibration_with_fit_paramsa  s    þr#  rF   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_weightrE   N)r&   r   rQ   )rF   r:   r6   r7   r]   r"  r8   r8   r9   Ú2test_calibration_with_sample_weight_base_estimatort  s    
r$  c              	   C   sZ   | \}}t  |¡}G dd„ dtƒ}|ƒ }t|ƒ}t t¡� |j|||d� W 5 Q R X 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 )zUtest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeightc                    s   d|kst ‚tƒ j||f|ŽS )NrF   ©rW   ÚsuperrQ   )rŠ   r6   r7   r!  ©r  r8   r9   rQ   ‘  s    zYtest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeight.fit©rŒ   r�   rŽ   rQ   Ú__classcell__r8   r8   r'  r9   ÚClfWithoutSampleWeight�  s   r*  rE   N)rL   r  r&   r   rS   ÚwarnsÚUserWarningrQ   )r:   r6   r7   rF   r*  r]   r"  r8   r8   r9   Ú5test_calibration_without_sample_weight_base_estimator†  s    
r-  c                 C   s  t dd�\}}tƒ  |¡}t |dd… |dd… f¡}t |dd… |dd… f¡}t |¡}d|ddd…< tƒ }t|| |dd	�}t	|ƒ}|j
|||d
� | 
|ddd… |ddd… ¡ t|j|jƒD ]\}}	t|jj|	jjƒ qÊ| |¡}
| |¡}t|
|ƒ dS )z|Check that passing removing some sample from the dataset `X` is
    equivalent to passing a `sample_weight` with a factor 0.Tr×   Né(   r�   éZ   rG   r?   r  rE   )r   r#   r  rL   r¡   ZhstackZ
zeros_liker   r   r   rQ   r  rd   r   re   r  rR   )r;   r>   r6   r7   rF   re   r  r  r  r  r  r  r8   r8   r9   Ú>test_calibrated_classifier_cv_zeros_sample_weights_equivalenceœ  s6    
ü þþ

r0  c              	   C   s8   t tƒ tƒ d�}tjtdd�� |j| Ž  W 5 Q R X dS )zUCheck that we raise an error is a user set both `base_estimator` and
    `estimator`.)Úbase_estimatorre   z%Both `base_estimator` and `estimator`rÍ   N)r   r   rS   rT   rU   rQ   )r:   rº   r8   r8   r9   Ú/test_calibrated_classifier_error_base_estimatorÉ  s     ÿr2  c              	   C   s8   t tƒ d�}d}tjt|d�� |j| Ž  W 5 Q R X dS )zPCheck that we raise a warning regarding the deprecation of
    `base_estimator`.)r1  z+`base_estimator` was renamed to `estimator`rÍ   N)r   r   rS   r+  ÚFutureWarningrQ   )r:   rº   Zwarn_msgr8   r8   r9   Ú5test_calibrated_classifier_deprecation_base_estimatorÔ  s    r4  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 k	st ‚tƒ j|||d�S )NrE   r%  )rŠ   r6   r7   rF   Ú	fit_paramr'  r8   r9   rQ   â  s    zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit)NNr(  r8   r8   r'  r9   ÚTestClassifierá  s   r6  )re   r5  rG   N)r   r   rQ   rL   r„   rj   )r:   r6  r8   r8   r9   Ú2test_calibration_with_non_sample_aligned_fit_paramÝ  s    ÿÿr7  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 ¡ szt‚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è  rB   rG   r   g     jø@)rŸ   rG   NT)rI   r7   Ú
classifierZsquared_hinge)Zlossr4   g     ˆÃ@r<   )r;   Zroc_auc)Zscoringr=   )rL   rM   Zdefault_rngÚnormalr˜   Úintr½   r   Úsplitr   rQ   r…   ÚanyrW   r   r   r   )Úglobal_random_seedZprobÚnZrandom_noiser7   r6   rI   ÚindicesÚtrainÚtestrX   rY   r[   Zsgd_clfÚpredictionsZclf_sigmoidZscore_sigmoidZclf_isotonicZscore_isotonicr8   r8   r9   Ú@test_calibrated_classifier_cv_works_with_large_confidence_scoresë  s2    	.

þ
þrC  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 )Nr@   r{   r   r?   rB   éþÿÿÿ)ÚlowÚhighrC   ro   )rB  r7   Zmax_abs_prediction_thresholdr|   )rB  r7   g�íµ ÷Æ°>)Úatol)rL   rM   rN   r  rO   r	   r   )r=  r4   r>  r7   Zpredictions_smallZthreshold_1Za1Úb1Zthreshold_2Za2Úb2Úa3Zb3rG  r8   r8   r9   Ú5test_sigmoid_calibration_max_abs_prediction_threshold  s2    ý
ý
þ
rK  )zÚnumpyrL   rS   Znumpy.testingr   Zscipyr   Zsklearn.baser   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.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*   Zsklearn.utils.extmathr+   r5   Zfixturer:   ÚmarkZparametrizerb   rh   rk   rr   rs   rÕ   r‡   r•   rœ   r�   r¢   r®   r²   rµ   r»   rM   rN   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-  r0  r2  r4  r7  rC  rK  r8   r8   r8   r9   Ú<module>   sî     

;

=/



þþ



þþ




) ÿ

"


 0
þþ
+	2