U
    Ovf                  
   @   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jFGdddgejFGd d!d"gd#d$ ZHd%d& ZIejFGd d!d"gd'd( ZJejFGdddgejFGd d!d"gd)d* ZKejFGdddgejFGd d!d"gd+d, ZLejFGdddgejFGd d!d"gejFGd-eMd.d/d0 ZNd1d2 ZOd3d4 ZPejFGdddgd5d6 ZQd7d8 ZRd9d: ZSejFGd d!d"gd;d< ZTejFGd d!d"gd=d> ZUejFGd d!d"gd?d@ ZVejFGdAejWXdBYdCdDd.ejWXdBYdCdDd.dEgdFdG ZZejDdHdI Z[ejDdJdK Z\dLdM Z]ejFGdNe^e7dOdPdQd.e^e7dOdPdQdRgdSdT Z_dUdV Z`dWdX ZaejDdddYdZ ZbejDddd[d\ ZcejFGd]dDd^gejFGd_d`dagdbdc Zdddde ZeejFGdfdgdhgdidj Zfdkdl ZgejFGdmdndogdpdq Zhdrds ZiejFGdtejekgdudv ZlejFGdtejekgdwdx ZmejFGdydzd{d|gd}d~ ZnejFGdddgejFGd d!d"gdd ZoejFGdddgdd ZpejFGddgeC eqeCgdd Zrdd ZsejFGdddgejFGd d!d"gdd Ztdd Zudd Zvdd Zwdd Zxdd 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)Zscopec                  C   s   t tddd\} }| |fS )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLES)Xy r7   B/tmp/pip-unpacked-wheel-qu3nn_q2/sklearn/tests/test_calibration.pydata7   s    r9   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|  qt||t|d d |kstqd S )N   r/   seedsizeTZforce_alphasample_weight   cvr=      r:   rH   r=   r;   )r4   nprandomRandomStateuniformrB   minr   fitpredict_probar   pytestraises
ValueErrorr   
csr_matrixr   AssertionErrorr)   )r9   r:   r=   r1   r5   r6   rE   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_relabeledr7   r7   r8   test_calibration=   sF    (
 




 ra   c                 C   s<   | \}}t dd}||| |jd j}t|ts8td S )Nr>   rH   r   )r   rP   calibrated_classifiers_	estimator
isinstancer$   rV   )r9   r5   r6   	calib_clfZbase_estr7   r7   r8   "test_calibration_default_estimatorz   s
    
rg   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 )NrI   )n_splitsrG   rF   )	r   r   re   rH   rV   rh   rP   lenrc   )r9   r=   r5   r6   splitsZkfoldrf   Zexpected_n_clfr7   r7   r8   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std S )
Nr>   r/   r?   rA   autodualr3   )r:   r=   rD   皙?)r4   rK   rL   rM   rN   ri   r$   r   rP   rQ   ZlinalgZnormrV   )r9   r:   r=   r1   r5   r6   rE   rW   rX   rY   rZ   rd   calibrated_clfZprobs_with_swZprobs_without_swZdiffr7   r7   r8   test_sample_weight   s    (

rq   c                 C   s   | \}}t ||dd\}}}}tt tddd}	t|	|d|d}
|
|| |
|}t|	|d|d}||| ||}t|| dS )	zTest parallel calibrationr/   )r3   rl   rm   r>   )r:   Zn_jobsr=   rF   N)r   r!   r#   r$   r   rP   rQ   r   )r9   r:   r=   r5   r6   rW   rZ   rX   r[   rd   Zcal_clf_parallelZprobs_parallelZcal_clf_sequentialZprobs_sequentialr7   r7   r8   test_parallel_execution   s(       
   
rr   r@   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 rdk sn t||
|d||
| kst||t||
|d}||||d}|d| k sNttddd}|||	 ||
}||||d}t|| d|d}|||	 ||
}||||d}|d| k std S )Nc                 S   s*   t ||  }t || d |jd  S )Nr>   r   )rK   Zeyesumshape)y_trueZ
proba_pred	n_classesZY_onehotr7   r7   r8   multiclass_brier   s    z5test_calibration_multiclass.<locals>.multiclass_brierrl      rm   i  d   
         .@r1   r2   r3   ZcentersZcluster_stdr>   r   rF   rI   rJ   Zaxis?gffffff?)rv   g?   r/   )n_estimatorsr3   )r$   r   rK   uniquert   rP   r   rQ   r   rs   onesri   ZscorerV   r+   decision_functionr   )r:   r=   r@   rw   r\   r5   r6   rv   rW   rX   rZ   r[   r^   probasZuncalibrated_brierZcalibrated_brierZ	clf_probsZcal_clf_probsr7   r7   r8   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   )rK   zerosrt   selfr5   r7   r7   r8   predict  s    zAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predictN)__name__
__module____qualname__r   r7   r7   r7   r8   ZeroCalibrator  s   r   2   rz   rx   r{   r|   )rd   Zcalibratorsclasses      ?)r   r   rP   r   classes_rQ   r   Z
n_classes_)r   r5   r6   r\   
calibratorr^   r   r7   r7   r8   !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/   r0   r?   rA   Nr>   TrC   prefitrb   rF   )r<   r;   )r:   rH   rD   r   r}   )r   rK   rL   rM   rN   rB   rO   r   r   rR   rS   r   rP   rQ   r   rU   r   r*   arrayZargmaxr   rV   )r1   r5   r6   rE   rW   rX   rY   ZX_calibZy_calibZsw_calibrZ   r[   r\   Z	unfit_clfr]   Zthis_X_calibr_   r:   r^   swy_proby_predr`   r7   r7   r8   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   rx   rm   r   FrJ   r   )rH   r:   r<   Zclip)Zout_of_boundsrF   )
r$   r   rP   rQ   r   r   r
   r   r   r   )r9   r:   r5   r6   r\   r^   Z
cal_probasZunbiased_predsr   Zclf_dfZmanual_probasr7   r7   r8   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 modelrI   r   rF   gj=ɿgY90(?r   r   r.   N)rK   r   r)   r	   expr
   rP   r   rR   rS   rT   vstack)ZexFZexYZAB_lin_libsvmZlin_probZsk_probr7   r7   r8   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st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   rF   g        ro   皙?皙??r   r>   n_binsg      ?quantiler   strategygUUUUUU?Z
percentile)r   N)	rK   r   r   ri   rV   r(   rR   rS   rT   )ru   r   	prob_true	prob_predZy_true2Zy_pred2Zprob_true_quantileZprob_pred_quantiler7   r7   r8   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 nanrz   r>   r   r/   )r1   r2   Zn_informativeZn_redundantr3   r   r   ZimputerrfrF   )r   r<   )rH   r:   r=   N)	r   rK   nanr    r   r   r   rP   r   )r=   r5   r6   r\   Zclf_cr7   r7   r8   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>   rz   rI   )r1   r2   rv   rl   r   rx   rn   Cr3   r;   rJ   rF   r}   r   )r   r$   r   r   rP   rQ   r)   rs   rK   r   rt   )r=   Znum_classesr5   r6   r\   Zclf_probZprobsr7   r7   r8   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 ]\}}|
|}| rt|d d |f t t| t |d d d |f d	kstt |d d |d
 d f d	kstqNt |d
|jd	  sNtqNd S )Nrz   rI   rl   r   rx   r   r;   rJ   r   rF   )rK   rL   randnZaranger$   r   r   rP   	enumeraterc   rQ   r*   r   ri   allrV   Zallclosert   )r=   r5   r6   r\   r^   icalibrated_classifierZprobar7   r7   r8   test_calibration_less_classes  s"    
   
 "(r   r5   r/      rI   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 inputrF   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)rK   r   r   )r   r5   r6   r7   r7   r8   rP     s    zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitc                 S   s   | |jd djddS )Nr   r   rF   r}   )reshapert   rs   r   r7   r7   r8   r     s    zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionN)r   r   r   __doc__rP   r   r7   r7   r7   r8   MockTensorClassifier  s   r   N)r   r   rP   )r5   r6   r   rp   r7   r7   r8    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NYZadult)stateZageZTXVTchildrF   r   r7   )	dict_dataZtext_labelsr7   r7   r8   r     s    
r   c                 C   s,   | \}}t dt fdt fg}|||S )NZ
vectorizerr\   )r    r   r   rP   )r   r5   r6   Zpipeline_prefitr7   r7   r8   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   rb   n_features_in_N)r   rP   r*   r   hasattrrV   r   rQ   )r   r   r5   r6   r\   rf   r7   r7   r8   test_calibration_dict_pipeline   s    	
r   zclf, cvrl   rF   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stn.t |j}t|j| |j|jd kstd S )	Nrz   rI   r>   rx   r1   r2   rv   r3   r   rb   rF   )	r   rP   r   r*   r   r   rV   r"   rt   )r\   rH   r5   r6   rf   r   r7   r7   r8   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 )Nrz   rI   r>   rx   r   rl   rF   r   r   rb   zAX has 3 features, but LinearSVC is expecting 5 features as input.matchr   )r   r$   rP   r   rR   rS   rT   )r5   r6   r\   rf   msgr7   r7   r8   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 )Nrz   rI   r>   rx   r   c                 S   s   g | ]}d t | t fqS )lr)strr   ).0r   r7   r7   r8   
<listcomp>D  s     z5test_calibration_votingclassifier.<locals>.<listcomp>r   Zsoft)Z
estimatorsZvotingr   )rd   rH   )r   r   rangerP   r   )r5   r6   Zvoterf   r7   r7   r8   !test_calibration_votingclassifier>  s    r   c                   C   s
   t ddS )NTZ
return_X_y)r   r7   r7   r7   r8   	iris_dataN  s    r   c                 C   s    | \}}||dk  ||dk  fS )Nr>   r7   )r   r5   r6   r7   r7   r8   iris_data_binaryS  s    r   r   rz   r   rN   r   c                 C   sR  |\}}t  ||}tj|||||dd}||d d df }t||||d\}	}
t|j|	 t|j|
 t|j	| |j
dkstdd l}t|j|jjst|j dkstt|j|jjstt|j|jjst|j dkst|j dkstdd	g}|j  }t|t|ks0t|D ]}| |ks4tq4d S )
Nr   )r   r   alpharF   r   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rP   r   from_estimatorrQ   r   r   r   r   r   estimator_namerV   Z
matplotlibre   Zline_linesZLine2DZ	get_alphaax_ZaxesZAxesZfigure_figureZFigure
get_xlabel
get_ylabel
get_legend	get_textsri   get_text)pyplotr   r   r   r5   r6   r   vizr   r   r   Zmplexpected_legend_labelslegend_labelslabelsr7   r7   r8    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   rP   r   r   r   r   r   r   ri   rV   r   )	r   r   r5   r6   r\   r   r   r   r   r7   r7   r8   $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st	|D ]}	|	
 |kst	qd S )Nr   rF   r   r   皙?r   r   )rK   r   r   plotappendr   r   r   ri   rV   r   )
r   nameZexpected_labelr   r   r   r   r   r   r   r7   r7   r8   '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st|D ]}|
 |kstqd S )Nr   rF   r   r   r   zname oner   zname twor   r   )rK   r   r   r   rV   r   r   r   r   ri   r   )	r   r   r   r   r   r   r   r   r   r7   r7   r8   )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st|D ]}| |kstq|d d}|
j|d t|t|kst|D ]}| |kstqd S )	Nzmy hand-crafted namerF   r   r   r   r   Zanother_namer   )r   rP   rQ   getattrr   r   rV   closer   r   r   r   ri   r   )r   r   r   r5   r6   Zclf_namer\   r   constructorparamsr   r   r   r   r7   r7   r8   ,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axrF   r   )	r   rP   r%   r   r   r   Zget_legend_handles_labelscountrV   )	r   r   r5   r6   r   dtr   Zviz2r   r7   r7   r8   !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   rA   zy_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)
rK   rL   rM   r   randintrB   rR   rS   rT   r   )r   rngy1y2err_msgr7   r7   r8   *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   rF   r   Zeggr   ro   r   g333333?r   r~   gffffff?r   r   r      r   r   )r   	pos_labelN)rK   r   r   r   )r   ru   r   Z
y_true_strr   r   _r7   r7   r8    test_calibration_curve_pos_label  s    r
  zpos_label, expected_pos_label)NrF   r   )rF   rF   c                 C   s   |\}}t  ||}tj||||d}||dd|f }t|||d\}	}
t|j|	 t|j|
 t|j	| |j
 d| dkst|j
 d| dkst|jjdg}|j
  }t|t|kst|D ]}| |kstqd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   rP   r   r   rQ   r   r   r   r   r   r   r   rV   r   	__class__r   r   r   ri   r   )r   r   r  Zexpected_pos_labelr5   r6   r   r   r   r   r   r   r   r   r7   r7   r8   "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   Nry   r>   r   rF   r   r:   r=   rH   rD   )r   r#   fit_transformrK   	ones_liker   rt   r   r   r   r   rP   ziprc   r   rd   coef_rQ   )r:   r=   r5   r6   rE   ZX_twiceZy_twicerd   calibrated_clf_without_weightscalibrated_clf_with_weightsest_with_weightsest_without_weightsy_pred_with_weightsy_pred_without_weightsr7   r7   r8   ?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 )zTests 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   rP   )r  r9   r5   r6   
fit_paramsr\   pc_clfr7   r7   r8    test_calibration_with_fit_paramsa  s    r   rE   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_weightrD   N)r&   r   rP   )rE   r9   r5   r6   r\   r  r7   r7   r8   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 )zCheck 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 )NrE   rV   superrP   )r   r5   r6   r  r  r7   r8   rP     s    zYtest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeight.fitr   r   r   rP   __classcell__r7   r7   r$  r8   ClfWithoutSampleWeight  s   r'  rD   N)rK   r  r&   r   rR   warnsUserWarningrP   )r9   r5   r6   rE   r'  r\   r  r7   r7   r8   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   rF   r>   r  rD   )r   r#   r  rK   r   ZhstackZ
zeros_liker   r   r   rP   r  rc   r   rd   r  rQ   )r:   r=   r5   r6   rE   rd   r  r  r  r  r  r  r7   r7   r8   >test_calibrated_classifier_cv_zeros_sample_weights_equivalence  s6    
 

r-  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_estimatorrd   z%Both `base_estimator` and `estimator`r   N)r   r   rR   rS   rT   rP   )r9   r   r7   r7   r8   /test_calibrated_classifier_error_base_estimator  s     r/  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`.)r.  z+`base_estimator` was renamed to `estimator`r   N)r   r   rR   r(  FutureWarningrP   )r9   r   Zwarn_msgr7   r7   r8   5test_calibrated_classifier_deprecation_base_estimator  s    r1  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 )NrD   r"  )r   r5   r6   rE   	fit_paramr$  r7   r8   rP     s    zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit)NNr%  r7   r7   r$  r8   TestClassifier  s   r3  )rd   r2  rF   N)r   r   rP   rK   r   ri   )r9   r3  r7   r7   r8   2test_calibration_with_non_sample_aligned_fit_param  s    r4  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 )zTest 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  rA   rF   r   g     j@)r   rF   NT)rH   r6   
classifierZsquared_hinge)Zlossr3   g     @r;   )r:   Zroc_auc)Zscoringr<   )rK   rL   Zdefault_rngnormalr   intr   r   splitr   rP   r   anyrV   r   r   r   )global_random_seedZprobnZrandom_noiser6   r5   rH   indicesZtraintestrW   rX   rZ   Zsgd_clfpredictionsZclf_sigmoidZscore_sigmoidZclf_isotonicZscore_isotonicr7   r7   r8   @test_calibrated_classifier_cv_works_with_large_confidence_scores  s2    	.


r?  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?   ry   r   r>   rA   )lowhighrB   ro   )r>  r6   Zmax_abs_prediction_thresholdrz   )r>  r6   gư>)atol)rK   rL   rM   r  rN   r	   r   )r:  r3   r;  r6   Zpredictions_smallZthreshold_1Za1Zb1Zthreshold_2Za2Zb2a3Zb3rC  r7   r7   r8   5test_sigmoid_calibration_max_abs_prediction_threshold  s2    


rE  )zZnumpyrK   rR   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+   r4   Zfixturer9   markZparametrizera   rg   rk   rq   rr   r   r   r   r   r   r   r   r   r   r   rL   rM   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*  r-  r/  r1  r4  r?  rE  r7   r7   r7   r8   <module>   s     

;

=/






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