U
    9%e                  
   @   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)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/Darija-Ai-API/env/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|  qt||t|d d |kstqd 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st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 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   )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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   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 ]\}}|
|}| 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 )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stn.t |j}t|j| |j|jd kst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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   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st	|D ]}	|	
 |kst	qd 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st|D ]}|
 |kstqd 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st|D ]}| |kstq|d d}|
j|d t|t|kst|D ]}| |kstqd 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   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)
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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   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 )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   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 )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 )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 )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  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