U
    ½mœdš  ã                
   @   s|  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 d dlmZmZ d dlmZmZmZ d dlmZ d d	lmZ d d
lmZ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"m#Z# d dl$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z,m-Z- d dlm.Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= d dl>m?Z? d dlm@Z@ dZAe jBdddd„ ƒZCe jD Ed d!d"g¡e jD Ed#d$d%g¡d&d'„ ƒƒZFd(d)„ ZGe jD Ed#d$d%g¡d*d+„ ƒZHe jD Ed d!d"g¡e jD Ed#d$d%g¡d,d-„ ƒƒZIe jD Ed d!d"g¡e jD Ed#d$d%g¡d.d/„ ƒƒZJe jD Ed d!d"g¡e jD Ed#d$d%g¡e jD Ed0eKd1ƒ¡d2d3„ ƒƒƒZLd4d5„ ZMd6d7„ ZNe jD Ed d!d"g¡d8d9„ ƒZOd:d;„ ZPd<d=„ ZQd>d?„ ZRe jD Ed#d$d%g¡d@dA„ ƒZSe jD Ed#d$d%g¡dBdC„ ƒZTe jD Ed#d$d%g¡dDdE„ ƒZUe jD EdFejV WdG¡ XdHdId1¡ejV WdG¡ XdHdId1dJ¡g¡dKdL„ ƒZYe jBdMdN„ ƒZZe jBdOdP„ ƒZ[dQdR„ Z\e jD EdSe  ]e*dTdUd1¡e  ]e*dTdUdV¡g¡dWdX„ ƒZ^dYdZ„ Z_d[d\„ Z`e jBddd]d^„ ƒZae jBddd_d`„ ƒZbdadb„ Zce jD Edcdddeg¡dfdg„ ƒZde jD EdhdIdig¡e jD Edjdkdlg¡dmdn„ ƒƒZedodp„ Zfe jD Edqdrdsg¡dtdu„ ƒZgdvdw„ Zhe jD Edcdddeg¡dxdy„ ƒZidzd{„ Zje jD Ed|ekelg¡d}d~„ ƒZme jD Ed|ekelg¡dd€„ ƒZne jD Edd‚dƒd„g¡d…d†„ ƒZoe jD Ed d!d"g¡e jD Ed#d$d%g¡d‡dˆ„ ƒƒZpe jD Ed‰dŠd‹g¡dŒd„ ƒZqe jD EdŽdgeA e reA¡g¡dd‘„ ƒZsd’d“„ Ztd”d•„ Zue jD Ed d!d"g¡e jD Ed#d$d%g¡d–d—„ ƒƒZvd˜d™„ Zwdšd›„ ZxdS )œé    N)Úassert_allclose)Úsparse)ÚBaseEstimatorÚclone)ÚDummyClassifier)ÚLeaveOneOutÚtrain_test_split)Úassert_array_almost_equalÚassert_almost_equalÚassert_array_equal)Úsoftmax)ÚNotFittedError)Úmake_classificationÚ
make_blobsÚ	load_iris)ÚLabelEncoder)ÚKFoldÚcross_val_predict)ÚMultinomialNB)ÚRandomForestClassifierÚVotingClassifier)ÚLogisticRegressionÚLinearRegression)ÚDecisionTreeClassifier)Ú	LinearSVC)ÚPipelineÚmake_pipeline)ÚStandardScaler)ÚIsotonicRegression)ÚDictVectorizer)ÚSimpleImputer)Úbrier_score_loss)Ú_CalibratedClassifierÚ_SigmoidCalibrationÚ_sigmoid_calibrationÚCalibratedClassifierCVÚCalibrationDisplayÚcalibration_curve)ÚCheckingClassifier)Ú_convert_containeréÈ   Úmodule)Úscopec                  C   s   t tddd\} }| |fS )Né   é*   ©Ú	n_samplesÚ
n_featuresÚrandom_state)r   Ú	N_SAMPLES)ÚXÚy© r6   úW/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/sklearn/tests/test_calibration.pyÚdata4   s    r8   Ú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é   r.   ©Úseed©ÚsizeT©Zforce_alpha©Úsample_weighté   ©Úcvr<   é   ©r9   rG   r<   r:   )r3   ÚnpÚrandomÚRandomStateÚuniformrA   Úminr   ÚfitÚpredict_probar%   ÚpytestÚraisesÚ
ValueErrorr   Ú
csr_matrixr!   ÚAssertionErrorr	   )r8   r9   r<   r0   r4   r5   rD   Ú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_relabeledr6   r6   r7   Útest_calibration:   sF    (þ
 ÿ




 ÿr`   c                 C   s<   | \}}t dd}| ||¡ |jd j}t|tƒs8t‚d S )Nr=   ©rG   r   )r%   rO   Úcalibrated_classifiers_Ú	estimatorÚ
isinstancer   rU   )r8   r4   r5   Ú	calib_clfZbase_estr6   r6   r7   Ú"test_calibration_default_estimatorw   s
    
rf   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 )NrH   )Ún_splitsrF   rE   )	r   r%   rd   rG   rU   rg   rO   Úlenrb   )r8   r<   r4   r5   ZsplitsZkfoldre   Zexpected_n_clfr6   r6   r7   Útest_calibration_cv_splitter   s    
ri   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Ât‚d S )	Nr=   r.   r>   r@   ©r2   )r9   r<   rC   çš™™™™™¹?)r3   rJ   rK   rL   rM   rh   r   r%   rO   rP   ZlinalgZnormrU   )r8   r9   r<   r0   r4   r5   rD   rV   rW   rX   rY   rc   Úcalibrated_clfZprobs_with_swZprobs_without_swÚdiffr6   r6   r7   Útest_sample_weight‘   s    (


rn   c                 C   s‚   | \}}t ||dd\}}}}tdd}	t|	|d|d}
|
 ||¡ |
 |¡}t|	|d|d}| ||¡ | |¡}t||ƒ dS )zTest parallel calibrationr.   rj   r=   )r9   Zn_jobsr<   rE   N)r   r   r%   rO   rP   r   )r8   r9   r<   r4   r5   rV   rY   rW   rZ   rc   Zcal_clf_parallelZprobs_parallelZcal_clf_sequentialZprobs_sequentialr6   r6   r7   Útest_parallel_execution©   s(    
   ÿ
   ÿ
ro   r?   r=   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 t‚| |
|¡d| |
|¡ kst‚||t| |
¡ƒ|d}||||d}|d| k sLt‚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   )rJ   ÚeyeÚsumÚshape)Úy_trueZ
proba_predÚ	n_classesZY_onehotr6   r6   r7   Úmulticlass_brierÇ   s    z5test_calibration_multiclass.<locals>.multiclass_brieré   rj   iô  éd   é
   ç      .@©r0   r1   r2   ZcentersZcluster_stdr=   r   rE   rH   rI   ©ZaxisçÍÌÌÌÌÌä?gffffffî?)rt   gš™™™™™ñ?é   r.   )Ún_estimatorsr2   )r   r   rJ   Úuniquerr   rO   r%   rP   r   rq   Úonesrh   ZscorerU   r   Údecision_functionr   )r9   r<   r?   ru   r[   r4   r5   rt   rV   rW   rY   rZ   r]   ÚprobasZuncalibrated_brierZcalibrated_brierZ	clf_probsZcal_clf_probsr6   r6   r7   Ú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   )rJ   Úzerosrr   ©Úselfr4   r6   r6   r7   Úpredict
  s    zAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predictN)Ú__name__Ú
__module__Ú__qualname__r‡   r6   r6   r6   r7   ÚZeroCalibrator  s   r‹   é2   rx   rv   ry   rz   )rc   ZcalibratorsÚclassesç      ð?)r   r   rO   r"   Úclasses_rP   r   Z
n_classes_)r‹   r4   r5   r[   Ú
calibratorr]   r‚   r6   r6   r7   Ú!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-   r.   r/   r>   r@   Nr=   TrB   Úprefitra   rE   )r;   r:   )r9   rG   rC   r   r{   )r   rJ   rK   rL   rM   rA   rN   r   r%   rQ   rR   r   rO   rP   r   rT   r‡   r   ÚarrayZargmaxr!   rU   )r0   r4   r5   rD   rV   rW   rX   ZX_calibZy_calibZsw_calibrY   rZ   r[   Z	unfit_clfr\   Zthis_X_calibr^   r9   r]   ÚswÚy_probÚy_predr_   r6   r6   r7   Útest_calibration_prefit  s>    (ý
"
þ

"
 ÿ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 )Nrv   rj   r’   FrI   r   )rG   r9   r;   Zclip)Zout_of_boundsrE   )
r   r%   rO   rP   r   r   r#   r   r‡   r   )r8   r9   r4   r5   r[   r]   Z
cal_probasZunbiased_predsr   Zclf_dfZmanual_probasr6   r6   r7   Útest_calibration_ensemble_falseK  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 modelrH   éüÿÿÿrŽ   rE   éÿÿÿÿg¿j˜=ïÉ¿gY90¯(àä?r’   r   r-   N)rJ   r”   r	   r$   Úexpr#   rO   r‡   rQ   rR   rS   Úvstack)ZexFZexYZAB_lin_libsvmZlin_probZsk_probr6   r6   r7   Útest_sigmoid_calibrationd  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   rE   ç        rk   çš™™™™™É?çš™™™™™é?çÍÌÌÌÌÌì?rŽ   r=   ©Ún_binsgš™™™™™¹¿ç      à?Úquantile©r¤   ÚstrategygUUUUUUå?Z
percentile)r¨   N)	rJ   r”   r'   rh   rU   r
   rQ   rR   rS   )rs   r—   Ú	prob_trueÚ	prob_predZy_true2Zy_pred2Zprob_true_quantileZprob_pred_quantiler6   r6   r7   Útest_calibration_curveu  s.       ÿ
r«   c               	   C   sª   t  ddddddg¡} t  ddddddg¡}t t¡ t| |d	d
d W 5 Q R X t t¡D t| |d	 d	dd\}}t| |d	d\}}t||ƒ t||ƒ W 5 Q R X dS )z6Tests the `normalize` parameter of `calibration_curve`r   rE   rŸ   rk   r    r¡   r¢   rŽ   r=   F)r¤   Ú	normalizeTr£   N)rJ   r”   rQ   ÚwarnsÚFutureWarningr'   r
   )rs   r—   Zprob_true_unnormalizedZprob_pred_unnormalizedr©   rª   r6   r6   r7   Ú.test_calibration_curve_with_unnormalized_proba•  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 nanrx   r=   r   r.   )r0   r1   Zn_informativeZn_redundantr2   ©r   r   ZimputerÚrfrE   )r~   r;   )rG   r9   r<   N)	r   rJ   Únanr   r    r   r%   rO   r‡   )r<   r4   r5   r[   Zclf_cr6   r6   r7   Útest_calibration_nan_imputer¨  s        ÿ

ÿr³   c                 C   sl   d}t dd|d\}}tddd}t|dtƒ | d	}| ||¡ | |¡}t|jd
dt 	|j
d ¡ƒ d S )Nr=   rx   rH   )r0   r1   rt   rŽ   rv   ©ÚCr2   r:   rI   rE   r{   r   )r   r   r%   r   rO   rP   r	   rq   rJ   r€   rr   )r<   Znum_classesr4   r5   r[   Zclf_probZprobsr6   r6   r7   Útest_calibration_prob_sum·  s       ÿ
r¶   c                 C   sì   t j dd¡}t  d¡}t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‚qLt  |d	|jd  ¡sLt‚qLd S )
Nrx   rH   rŽ   rv   r´   r:   rI   r   rE   )rJ   rK   ÚrandnZaranger   r%   r   rO   Ú	enumeraterb   rP   r   r„   rh   ÚallrU   Zallcloserr   )r<   r4   r5   r[   r]   ÚiÚcalibrated_classifierZprobar6   r6   r7   Útest_calibration_less_classesÇ  s"    
   ÿ
 "(r¼   r4   r.   é   rH   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 inputrE   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)rJ   r   r   )r†   r4   r5   r6   r6   r7   rO   ð  s    zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitc                 S   s   |  |jd d¡jddS )Nr   r›   rE   r{   )Zreshaperr   rq   r…   r6   r6   r7   r   ô  s    zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionN)rˆ   r‰   rŠ   Ú__doc__rO   r   r6   r6   r6   r7   ÚMockTensorClassifierí  s   r¿   N)r   r%   rO   )r4   r5   r¿   rl   r6   r6   r7   Ú 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ÚchildrE   r   r6   )Ú	dict_dataZtext_labelsr6   r6   r7   rÅ   ý  s    ý
rÅ   c                 C   s,   | \}}t dtƒ fdtƒ fgƒ}| ||¡S )NZ
vectorizerr[   )r   r   r   rO   )rÅ   r4   r5   Zpipeline_prefitr6   r6   r7   Ú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“   ra   Ún_features_in_N)r%   rO   r   r   ÚhasattrrU   r‡   rP   )rÅ   rÆ   r4   r5   r[   re   r6   r6   r7   Útest_calibration_dict_pipeline  s    	
rÉ   zclf, cvrE   ©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 )	Nrx   rH   r=   rv   ©r0   r1   rt   r2   r“   ra   rE   )	r   rO   r%   r   r   rÇ   rU   r   rr   )r[   rG   r4   r5   re   r   r6   r6   r7   Útest_calibration_attributes+  s    	rÌ   c               	   C   sp   t ddddd\} }tdd | |¡}t|dd	}d
}tjt|d" | | d d …d d…f |¡ W 5 Q R X d S )Nrx   rH   r=   rv   rË   rE   rÊ   r“   ra   zAX has 3 features, but LinearSVC is expecting 5 features as input.©Úmatchr’   )r   r   rO   r%   rQ   rR   rS   )r4   r5   r[   re   Úmsgr6   r6   r7   Ú2test_calibration_inconsistent_prefit_n_features_inC  s    rÐ   c                  C   sV   t ddddd\} }tdd„ tdƒD ƒd	d
}| | |¡ t|dd}| | |¡ d S )Nrx   rH   r=   rv   rË   c                 S   s   g | ]}d t |ƒ tƒ f‘qS )Úlr)Ústrr   )Ú.0rº   r6   r6   r7   Ú
<listcomp>U  s     z5test_calibration_votingclassifier.<locals>.<listcomp>r’   Zsoft)Z
estimatorsZvotingr“   )rc   rG   )r   r   ÚrangerO   r%   )r4   r5   Zvotere   r6   r6   r7   Ú!test_calibration_votingclassifierO  s    þrÖ   c                   C   s
   t ddS )NT©Z
return_X_y)r   r6   r6   r6   r7   Ú	iris_data_  s    rØ   c                 C   s    | \}}||dk  ||dk  fS )Nr=   r6   )rØ   r4   r5   r6   r6   r7   Úiris_data_binaryd  s    rÙ   c           
   	   C   s²   |\}}|\}}t ƒ  ||¡}d}tjt|d t |||¡ W 5 Q R X tƒ  ||¡}	d}tjt|d t |	||¡ W 5 Q R X tƒ }	t t	¡ t |	||¡ W 5 Q R X d S )Nz)'estimator' should be a fitted classifierrÍ   z/response method predict_proba is not defined in)
r   rO   rQ   rR   rS   r&   Úfrom_estimatorr   r   r   )
ÚpyplotrØ   rÙ   r4   r5   ZX_binaryZy_binaryÚregrÏ   r[   r6   r6   r7   Ú#test_calibration_display_validationj  s    rÝ   Úconstructor_namerÚ   Úfrom_predictionsc              	   C   sˆ   |\}}t ƒ }| ||¡ | |¡}|dkrZd}tjt|d t |||¡ W 5 Q R X n*d}tjt|d t ||¡ W 5 Q R X d S )NrÚ   z"to be a binary classifier, but gotrÍ   z-y should be a 1d array, got an array of shape)	r   rO   rP   rQ   rR   rS   r&   rÚ   rß   )rÛ   rØ   rÞ   r4   r5   r[   r–   rÏ   r6   r6   r7   Ú#test_calibration_display_non_binary}  s    
rà   r¤   rx   r¨   rM   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¨   ÚalpharE   r§   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)úPerfectly calibrated)r   rO   r&   rÚ   rP   r'   r   r©   rª   r–   Úestimator_namerU   Z
matplotlibrd   Zline_ÚlinesZLine2DZ	get_alphaÚax_ZaxesZAxesZfigure_ZfigureZFigureÚ
get_xlabelÚ
get_ylabelÚ
get_legendÚ	get_textsrh   Úget_text)rÛ   rÙ   r¤   r¨   r4   r5   rÑ   Úvizr–   r©   rª   ZmplÚexpected_legend_labelsÚlegend_labelsÚlabelsr6   r6   r7   Ú test_calibration_display_computeŽ  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   rO   r&   rÚ   rã   rå   rè   ré   rh   rU   rê   )	rÛ   rÙ   r4   r5   r[   rë   rì   rí   rî   r6   r6   r7   Ú$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   rE   r    r¡   çš™™™™™Ù?©rã   râ   )rJ   r”   r&   ÚplotÚappendrå   rè   ré   rh   rU   rê   )
rÛ   ÚnameZexpected_labelr©   rª   r–   rë   rì   rí   rî   r6   r6   r7   Ú'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   rE   r    r¡   rò   zname oneró   zname two©rö   râ   )rJ   r”   r&   rã   rU   rô   rå   rè   ré   rh   rê   )	rÛ   r©   rª   r–   rö   rë   rì   rí   rî   r6   r6   r7   Ú)test_calibration_display_label_class_plotÚ  s    
rù   c                 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 namerE   rÚ   rö   r¹   râ   Zanother_namerø   )r   rO   rP   Úgetattrr&   rã   rU   Úcloserô   rå   rè   ré   rh   rê   )rÞ   rÛ   rÙ   r4   r5   Zclf_namer[   r–   ÚconstructorÚparamsrë   rì   rí   rî   r6   r6   r7   Ú,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axrE   râ   )	r   rO   r   r&   rÚ   rå   Zget_legend_handles_labelsÚcountrU   )	rÛ   rÙ   r4   r5   rÑ   Údtrë   Zviz2rî   r6   r6   r7   Ú!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.r.   Úspamr’   Úeggsr=   ©Údtyper   r@   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)
rJ   rK   rL   r”   ÚrandintrA   rQ   rR   rS   r'   )r  ÚrngÚy1Úy2Úerr_msgr6   r6   r7   Ú*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   rE   r  Úeggr  rk   r    g333333Ó?rò   r|   gffffffæ?r¡   r¢   rŽ   é   r£   r¥   )r¤   Ú	pos_labelN)rJ   r”   r'   r   )r  rs   r   Z
y_true_strr—   r©   Ú_r6   r6   r7   Ú test_calibration_curve_pos_label.  s    r  zpos_label, expected_pos_label)NrE   r°   )rE   rE   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   rO   r&   rÚ   rP   r'   r   r©   rª   r–   rå   ræ   rU   rç   Ú	__class__rˆ   rè   ré   rh   rê   )rÛ   rÙ   r  Zexpected_pos_labelr4   r5   rÑ   rë   r–   r©   rª   rì   rí   rî   r6   r6   r7   Ú"test_calibration_display_pos_labelC  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×   Nrw   r=   r   rE   r  ©r9   r<   rG   rC   )r   r   Úfit_transformrJ   Ú	ones_liker„   rr   r  r   r%   r   rO   Úziprb   r   rc   Úcoef_rP   )r9   r<   r4   r5   rD   ZX_twiceZy_twicerc   Úcalibrated_clf_without_weightsÚcalibrated_clf_with_weightsÚest_with_weightsÚest_without_weightsÚy_pred_with_weightsÚy_pred_without_weightsr6   r6   r7   Ú?test_calibrated_classifier_cv_double_sample_weights_equivalenced  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%   rO   )r!  r8   r4   r5   Ú
fit_paramsr[   Úpc_clfr6   r6   r7   Ú test_calibration_with_fit_params–  s    þr(  rD   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_weightrC   N)r(   r%   rO   )rD   r8   r4   r5   r[   r'  r6   r6   r7   Ú2test_calibration_with_sample_weight_base_estimator©  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 )NrD   )rU   ÚsuperrO   )r†   r4   r5   r&  ©r  r6   r7   rO   Æ  s    zYtest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeight.fit)rˆ   r‰   rŠ   rO   Ú__classcell__r6   r6   r+  r7   ÚClfWithoutSampleWeightÅ  s   r-  rC   N)rJ   r  r(   r%   rQ   r­   ÚUserWarningrO   )r8   r4   r5   rD   r-  r[   r'  r6   r6   r7   Ú5test_calibration_without_sample_weight_base_estimator»  s    
r/  c              	   C   sh   | \}}d|dd… i}t |d}t|ƒ}dttƒ d }tjt|d |j||f|Ž W 5 Q R X dS )z]fit_params having different length than data should raise the
    correct error message.
    r#  NrH   r%  z>Found input variables with inconsistent numbers of samples: \[z, 5\]rÍ   )r(   r%   rÒ   r3   rQ   rR   rS   rO   )r8   r4   r5   r&  r[   r'  rÏ   r6   r6   r7   Ú4test_calibration_with_fit_params_inconsistent_lengthÑ  s    
ÿÿÿr0  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   rE   r=   r  rC   )r   r   r  rJ   r   ZhstackZ
zeros_liker   r%   r   rO   r  rb   r   rc   r  rP   )r9   r<   r4   r5   rD   rc   r  r  r  r  r  r  r6   r6   r7   Ú>test_calibrated_classifier_cv_zeros_sample_weights_equivalenceâ  s6    
ü þþ

r3  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_estimatorrc   z%Both `base_estimator` and `estimator`rÍ   N)r%   r   rQ   rR   rS   rO   )r8   r»   r6   r6   r7   Ú/test_calibrated_classifier_error_base_estimator  s     ÿr5  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`.)r4  z+`base_estimator` was renamed to `estimator`rÍ   N)r%   r   rQ   r­   r®   rO   )r8   r»   Zwarn_msgr6   r6   r7   Ú5test_calibrated_classifier_deprecation_base_estimator  s    r6  )yrQ   ÚnumpyrJ   Znumpy.testingr   Zscipyr   Zsklearn.baser   r   Zsklearn.dummyr   Zsklearn.model_selectionr   r   Zsklearn.utils._testingr	   r
   r   Zsklearn.utils.extmathr   Zsklearn.exceptionsr   Zsklearn.datasetsr   r   r   Zsklearn.preprocessingr   r   r   Zsklearn.naive_bayesr   Zsklearn.ensembler   r   Zsklearn.linear_modelr   r   Zsklearn.treer   Zsklearn.svmr   Zsklearn.pipeliner   r   r   Zsklearn.isotonicr   Zsklearn.feature_extractionr   Zsklearn.imputer    Zsklearn.metricsr!   Zsklearn.calibrationr"   r#   r$   r%   r&   r'   Zsklearn.utils._mockingr(   r)   r3   Zfixturer8   ÚmarkZparametrizer`   rf   ri   rn   ro   rÕ   rƒ   r‘   r˜   r™   rž   r«   r¯   r³   r¶   r¼   rK   rL   r·   rÀ   rÅ   rÆ   rÉ   ÚparamrÌ   rÐ   rÖ   rØ   rÙ   rÝ   rà   rï   rð   r÷   rù   rþ   r  rÒ   Úobjectr  r  r  r   r(  r€   r)  r/  r0  r3  r5  r6  r6   r6   r6   r7   Ú<module>   sø    

;

=/
 


þþ


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