
    -iϦ              
          S SK rS SKrS SKJr  S SKJrJrJr  S SK	J
r
JrJrJrJrJr  S SKJrJrJr  S SKJr  S SKJrJr  S SKJr  S S	KJr  S S
KJr  S SKJ r   S SK!J"r"  S SK#J$r$J%r%  S SK&J'r'  S SK(J)r)J*r*J+r+J,r,J-r-J.r.  S SK/J0r0  S SK1J2r2J3r3  S SK4J5r5J6r6  S SK7J8r8  S SK9J:r:  S SK;J<r<  S SK=J>r>J?r?J@r@JArAJBrB  S SKCJDrD  S SKEJFrF  SrG\R                  " SS9S 5       rI\R                  R                  S\F5      \R                  R                  SSS /5      \R                  R                  S!S"S#/5      S$ 5       5       5       rLS% rM\R                  R                  S!S"S#/5      S& 5       rNS' rO\R                  R                  SSS /5      \R                  R                  S!S"S#/5      S( 5       5       rP\R                  R                  SSS /5      \R                  R                  S!S"S#/5      S) 5       5       rQ\R                  R                  SSS /5      \R                  R                  S!S"S#/5      \R                  R                  S*\R" S+5      5      S, 5       5       5       rSS- rT\B" \US.9\R                  R                  S\F5      S/ 5       5       rV\R                  R                  SSS /5      S0 5       rWS1 rXS2 rY\R                  R                  S!S"S#/5      S3 5       rZ\R                  R                  S!S"S#/5      S4 5       r[\R                  R                  S!S"S#/5      S5 5       r\\R                  R                  S6\R                  R                  S75      R                  S8S9S+5      \R                  R                  S75      R                  S8S9S+S:5      /5      S; 5       r`\R                  S< 5       ra\R                  S= 5       rbS> rc\R                  R                  S?\R                  " \8" S@SA9S+5      \R                  " \8" S@SA9SB5      /5      SC 5       reSD rfSE rg\R                  " SS9SF 5       rh\R                  " SS9SG 5       ri\R                  R                  SHS9SI/5      \R                  R                  SJSKSL/5      SM 5       5       rjSN rk\R                  R                  SOSPSQ/5      SR 5       rlSS rm\R                  R                  STSUSV/5      SW 5       rnSX ro\R                  R                  SY\p\q/5      SZ 5       rr\R                  R                  SY\p\q/5      S[ 5       rs\R                  R                  S\S]S+S^S_.S]S+S^S`./5      Sa 5       rt\R                  R                  Sb/ ScQ5      Sd 5       ru\R                  R                  SSS /5      \R                  R                  S!S"S#/5      Se 5       5       rv\R                  R                  SfSgSh/5      Si 5       rw\R                  R                  SjSk/\G-  \R                  " \G5      /5      Sl 5       rySm rzSn r{So r|Sp r}\R                  R                  SqS"S#/5      \R                  R                  SSS /5      Sr 5       5       r~Ss rg)t    N)assert_allclose)BaseEstimatorClassifierMixinclone)CalibratedClassifierCVCalibrationDisplay_CalibratedClassifier_sigmoid_calibration_SigmoidCalibrationcalibration_curve)	load_iris
make_blobsmake_classification)DummyClassifier)RandomForestClassifierVotingClassifier)NotFittedError)DictVectorizer)FrozenEstimator)SimpleImputer)IsotonicRegression)LogisticRegressionSGDClassifier)brier_score_loss)KFoldLeaveOneOutcheck_cvcross_val_predictcross_val_scoretrain_test_split)MultinomialNB)Pipelinemake_pipeline)LabelEncoderStandardScaler)	LinearSVC)DecisionTreeClassifier)CheckingClassifier)_convert_containerassert_almost_equalassert_array_almost_equalassert_array_equalignore_warnings)softmax)CSR_CONTAINERS   module)scopec                  *    [        [        SSS9u  pX4$ )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLESXys     Q/var/www/html/venv/lib/python3.13/site-packages/sklearn/tests/test_calibration.pydatar?   9   s    qrRDA4K    csr_containermethodsigmoidisotonicensembleTFc                 ~   [         S-  nU u  pV[        R                  R                  SS9R	                  UR
                  S9nXUR                  5       -  nUS U US U US U pnXTS  XdS  p[        5       R                  XU
S9nUR                  U5      S S 2S4   n[        XR
                  S-   US9n[        R                  " [        5         UR                  XV5        S S S 5        X4U" U5      U" U5      44 GH0  u  nn[        XSUS	9nUR                  UXS9  UR                  U5      S S 2S4   n[        X5      [        UU5      :  d   eUR                  UU	S-   U
S9  UR                  U5      S S 2S4   n[        UU5        UR                  USU	-  S-
  U
S9  UR                  U5      S S 2S4   n[        UU5        UR                  UU	S-   S-  U
S9  UR                  U5      S S 2S4   nUS
:X  a  [        USU-
  5        GM  [        X5      [        US-   S-  U5      :  a  GM1   e   g ! , (       d  f       GNX= f)N   r5   seedsizesample_weight   cvrE      rB   rP   rE   rC   )r:   nprandomRandomStateuniformrK   minr!   fitpredict_probar   pytestraises
ValueErrorr   r+   )r?   rB   rA   rE   r7   r<   r=   rM   X_trainy_trainsw_trainX_testy_testclfprob_pos_clfcal_clfthis_X_trainthis_X_testprob_pos_cal_clfprob_pos_cal_clf_relabeleds                       r>   test_calibrationri   ?   st   
 QIDAII))r)2:::GMLA "#:I*9}Zi?XhGz]AjMF /

gh

GC$$V,QT2L$SVVaZ(KG	z	"A 
#
 
	w	v!67&!k )HU 	L'B"00=adC  58H$9
 
 	
 

 	L'A+XF%,%:%:;%G1%M"!"24NO 	L!g+/J%,%:%:;%G1%M"!"24NO 	L7Q;!"38L%,%:%:;%G1%M"Y%&6<V8VW $F9<L!q "<=   C&	 
#	"s   
H--
H<c                     U u  p[        SS9nUR                  X5        UR                  S   R                  n[	        U[
        5      (       d   eg )NrG   rP   r   )r   rX   calibrated_classifiers_	estimator
isinstancer&   )r?   r<   r=   	calib_clfbase_ests        r>   "test_calibration_default_estimatorrq   }   sH    DA&!,IMM!003==Hh	****r@   c                    U u  p#Sn[        US9n[        XQS9n[        UR                  [         5      (       d   eUR                  R                  U:X  d   eUR                  X#5        U(       a  UOSn[        UR                  5      U:X  d   eg )NrQ   n_splitsrO   rN   )r   r   rn   rP   rt   rX   lenrl   )r?   rE   r<   r=   splitskfoldro   expected_n_clfs           r>   test_calibration_cv_splitterry      s     DAF6"E&%CIillE****<<  F***MM!'VQNy001^CCCr@   c                 f   U u  p[        SS9n[        USS9n[        R                  " [        SS9   UR                  X5        S S S 5        [        [        5       SS9n[        R                  " [        SS9   UR                  X5        S S S 5        g ! , (       d  f       NT= f! , (       d  f       g = f)Ne   rs   TrO   z$Requesting 101-fold cross-validationmatchz!LeaveOneOut cross-validation does)r   r   rZ   r[   r\   rX   r   )r?   r<   r=   rw   ro   s        r>   test_calibration_cv_nfoldr~      s    DA3E&%$?I	z)O	Pa 
Q '+-$GI	z)L	Ma 
N	M	 
Q	P 
N	Ms   B6B"
B"
B0c                    [         S-  nU u  pE[        R                  R                  SS9R	                  [        U5      S9nUS U US U US U pnXCS  n
[        SS9n[        XUS9nUR                  XxU	S9  UR                  U
5      nUR                  Xx5        UR                  U
5      n[        R                  R                  X-
  5      nUS:  d   eg )	NrG   r5   rH   rJ   r9   )rB   rE   rL   皙?)r:   rS   rT   rU   rV   ru   r&   r   rX   rY   linalgnorm)r?   rB   rE   r7   r<   r=   rM   r]   r^   r_   r`   rm   calibrated_clfprobs_with_swprobs_without_swdiffs                   r>   test_sample_weightr      s     QIDAII))r)2::A:GM!":I*9}Zi?XhGz]Fr*I+IxXNwx@"008M w(%33F;99>>-:;D#::r@   c                 &   U u  p4[        X4SS9u  pVpx[        [        5       [        SS95      n	[	        XSUS9n
U
R                  XW5        U
R                  U5      n[	        XSUS9nUR                  XW5        UR                  U5      n[        X5        g)zTest parallel calibrationr5   r   rG   )rB   n_jobsrE   rN   N)r    r#   r%   r&   r   rX   rY   r   )r?   rB   rE   r<   r=   r]   r`   r^   ra   rm   cal_clf_parallelprobs_parallelcal_clf_sequentialprobs_sequentials                 r>   test_parallel_executionr      s     DA'72'N$GWn.	r0JKI-X *%33F;N/X 7,)77?N5r@   rI   rG   c                    S n[        SS9n[        SSUSSS9u  pVS	XfS	:  '   [        R                  " U5      R                  S
   nUS S S	2   US S S	2   pUSS S	2   USS S	2   pUR                  X5        [        X@SUS9nUR                  X5        UR                  U
5      n[        [        R                  " USS9[        R                  " [        U
5      5      5        SUR                  X5      s=:  a  S:  d   e   eUR                  X5      SUR                  X5      -  :  d   eU" U[        UR                  U
5      5      US9nU" XUS9nUSU-  :  d   e[        SSS9nUR                  X5        UR                  U
5      nU" UUUS9n[        X@SUS9nUR                  X5        UR                  U
5      nU" UUUS9nUSU-  :  d   eg )Nc                     [         R                  " U5      U    n[         R                  " X1-
  S-  5      UR                  S   -  $ )NrG   r   )rS   eyesumshape)y_true
proba_pred	n_classesY_onehots       r>   multiclass_brier5test_calibration_multiclass.<locals>.multiclass_brier   s:    66)$V,vvx,23hnnQ6GGGr@      r   i  d   
         .@r7   r8   r9   centerscluster_stdrG   r   rN   rQ   rR   axis?gffffff?)r   g?   r5   )n_estimatorsr9   )r&   r   rS   uniquer   rX   r   rY   r   r   onesru   scorer.   decision_functionr   )rB   rE   rI   r   rb   r<   r=   r   r]   r^   r`   ra   rd   probasuncalibrated_briercalibrated_brier	clf_probscal_clf_probss                     r>   test_calibration_multiclassr      s   H 
#C#D"RVDA A!eH		!""1%I1vq1vWqt!tWa1gFGGG$SAQGKK!""6*FBFF6*BGGCK,@A
 #))F+2d22222 ==(4#))F2K+KKKK
 *--f56) ()Lc$66666 !br
BCGGG!!&)I)&)yQ$SAQGKK!))&1M'Sc$66666r@   c                       " S S5      n [        SSSSSS9u  p[        5       R                  X5      nU " 5       n[        X4/UR                  S9nUR                  U5      n[        US	UR                  -  5        g )
Nc                       \ rS rSrS rSrg)9test_calibration_zero_probability.<locals>.ZeroCalibratori  c                 H    [         R                  " UR                  S   5      $ )Nr   )rS   zerosr   selfr<   s     r>   predictAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predict  s    88AGGAJ''r@    N)__name__
__module____qualname____firstlineno__r   __static_attributes__r   r@   r>   ZeroCalibratorr     s    	(r@   r   2   r   r   r   r   )rm   calibratorsclasses      ?)r   r   rX   r	   classes_rY   r   
n_classes_)r   r<   r=   rb   
calibratorrd   r   s          r>   !test_calibration_zero_probabilityr     s~    
( (
 !RTDA 



%C!J#<G ""1%F FC#..01r@   )categoryc                 r   Sn[        SU-  SSS9u  p#[        R                  R                  SS9R	                  UR
                  S9nX"R                  5       -  nUSU USU USU pvnX!S	U-   X1S	U-   XAS	U-   pnUS	U-  S US	U-  S p[        5       n[        US
S9n[        R                  " [        5         UR                  X5        SSS5        UR                  XVU5        UR                  U5      SS2S4   nX4U " U5      U " U5      44 GH  u  nnS GH  n[        UUS
S9n[        [        U5      US9nU
S4 H  nUR                  UU	US9  UR                  UU	US9  UR                  U5      nUR                  U5      nUR                  U5      nUR                  U5      nUSS2S4   nUSS2S4   n[!        UU5        [!        U[        R"                  " SS/5      [        R$                  " USS9   5        ['        X5      ['        UU5      :  a  M   e   GM     GM     g! , (       d  f       GNd= f)z*Test calibration for prefitted classifiersr      r4   r5   r6   rH   rJ   NrG   prefitrk   rN   )rD   rC   rB   rP   rB   rL   r   r   )r   rS   rT   rU   rV   rK   rW   r!   r   rZ   r[   r   rX   rY   r   r   r,   arrayargmaxr   )rA   r7   r<   r=   rM   r]   r^   r_   X_caliby_calibsw_calibr`   ra   rb   	unfit_clfrc   this_X_calibrf   rB   cal_clf_prefitcal_clf_frozenswy_prob_prefity_prob_frozeny_pred_prefity_pred_frozenprob_pos_cal_clf_prefitprob_pos_cal_clf_frozens                               r>   test_calibration_prefitr   0  sm   
 IY1SUVDAII))r)2:::GMLA "#:I*9}Zi?XhG	a)m$	a)m$!i-0 G
 q9}'1y=?);F /C&sx8I	~	&g' 
' GGGh'$$V,QT2L 
	w	v!67&!k .F3C8TN3OC4HQWXN&""<"K""<"K . < <[ I . < <[ I . 6 6{ C . 6 6{ C*71*='*71*='"=-@"!288QF#3BIImRS4T#U (=@P3A    '	 .	& 
'	&s   8H''
H6c                 r   U u  p#[        SS9n[        XASSS9nUR                  X#5        UR                  U5      n[	        XBUSSS9nUS:X  a
  [        S	S
9nO
[        5       nUR                  Xs5        UR                  X#5        UR                  U5      n	UR                  U	5      n
[        US S 2S4   U
5        g )Nr   r   r   FrR   r   )rP   rB   rD   clip)out_of_boundsrN   )
r&   r   rX   rY   r   r   r   r   r   r   )r?   rB   r<   r=   rb   rd   
cal_probasunbiased_predsr   clf_dfmanual_probass              r>   test_calibration_ensemble_falser   j  s     DA

#C$SANGKK&&q)J 'sqQ?RSN'f=
(*
NN>%GGAM""1%F&&v.MJq!t$m4r@   c                  >   [         R                  " / SQ5      n [         R                  " / SQ5      n[         R                  " SS/5      n[        U[        X5      S5        SS[         R                  " US   U -  US   -   5      -   -  n[        5       R                  X5      R                  U 5      n[        X4S	5        [        R                  " [        5         [        5       R                  [         R                  " X 45      U5        S
S
S
5        g
! , (       d  f       g
= f)z0Test calibration values with Platt sigmoid model)rQ   r   )rN   r   gj=ɿgY90(?r   r   r   rN   r4   N)rS   r   r+   r
   expr   rX   r   rZ   r[   r\   vstack)exFexYAB_lin_libsvmlin_probsk_probs        r>   test_sigmoid_calibrationr     s    
((<
 C
((;
CHH24GHIMm-A#-KQOcBFF=#3c#9M!<L#LMMNH!#''199#>Gh3 
z	"!!"))SJ"7= 
#	"	"s   0D
Dc                     [         R                  " / SQ5      n [         R                  " / SQ5      n[        XSS9u  p#[        U5      [        U5      :X  d   e[        U5      S:X  d   e[	        USS/5        [	        USS/5        [
        R                  " [        5         [        S/S	/5        S
S
S
5        [         R                  " / SQ5      n[         R                  " / SQ5      n[        XESSS9u  pg[        U5      [        U5      :X  d   e[        U5      S:X  d   e[	        USS/5        [	        USS/5        [
        R                  " [        5         [        XESS9  S
S
S
5        g
! , (       d  f       N= f! , (       d  f       g
= f)z Check calibration_curve function)r   r   r   rN   rN   rN   )        r   皙?皙??r   rG   n_binsr   rN   r   r   gN)r   r   r   r   rN   rN   )r   r   r         ?r   r   quantiler   strategygUUUUUU?r   
percentile)r   )rS   r   r   ru   r*   rZ   r[   r\   )r   y_pred	prob_true	prob_predy_true2y_pred2prob_true_quantileprob_pred_quantiles           r>   test_calibration_curver    sI   XX()FXX45F,VAFIy>S^+++y>Q	Aq6*	C:. 
z	"1#v& 
# hh)*Ghh56G->Z.* !"c*<&====!"a'''*QJ7*S#J7 
z	"'\B 
#	"! 
#	"  
#	"s   E'E8'
E58
Fc                     [        SSSSSS9u  p[        R                  US'   [        S[	        5       4S[        S	S
94/5      n[        USSU S9nUR                  X5        UR                  U5        g)z$Test that calibration can accept nanr   rG   r   r5   )r7   r8   n_informativen_redundantr9   r   r   imputerrfrN   )r   rD   )rP   rB   rE   N)	r   rS   nanr"   r   r   r   rX   r   )rE   r<   r=   rb   clf_cs        r>   test_calibration_nan_imputerr    sy     !QSDA ffAdG

]_	%.DRS.T'UVC #31Z(SE	IIaO	MM!r@   c                     [        SSSS9u  p/ SQn[        SSS9n[        US	[        S
S9U S9nUR	                  X5        [        UR                  U5      R                  SS9S5        g )Nr   rQ   rG   )r7   r8   r   )
rN   rN   rN   rN   rN   r   r   r   r   r   r   r   )Cr9   rC   r   rs   rR   rN   r   )r   r&   r   r   rX   r   rY   r   )rE   r<   _r=   rb   clf_probs         r>   test_calibration_prob_sumr    sn     QGDA&A
c
*C%I%"3hH LLH**1-11q193?r@   c           	         [         R                  R                  SS5      n/ SQ/ SQ-   / SQ-   n[        SS9n[	        US[        S	5      U S
9nUR                  X5        U (       a  [         R                  " S5      n[        SS/SS	/5       H|  u  pgUR                  U   R                  U5      n[        US S 2U4   [         R                  " [        U5      5      5        [         R                  " US S 2XW:g  4   S:  5      (       a  M|   e   g UR                  S   R                  U5      n[        UR!                  SS9[         R"                  " UR$                  S   5      5        g )N   rQ   )r   r   r   rN   )rN   rN   rG   rG   )rG   r   r   r   r   r   rC   r   rR      r   rG   rN   r   )rS   rT   randnr'   r   r   rX   arangeziprl   rY   r,   r   ru   allr+   r   r   r   )	rE   r<   r=   rb   rd   r   calib_iclass_iprobas	            r>   test_calibration_less_classesr    s'    			AA|#l2A
 a
0C$I%(XG KK))A, #QFQF 3G33G<JJ1MEuQZ0"((3q62BC66%7#5 56:;;;; !4 //2@@C!%)))"3RWWU[[^5LMr@   r<   r5      rQ   r4   c                 v    / SQn " S S[         [        5      n[        U" 5       5      nUR                  X5        g)z;Test that calibration accepts n-dimensional arrays as input)rN   r   r   rN   rN   r   rN   rN   r   r   rN   r   r   rN   r   c                   $    \ rS rSrSrS rS rSrg)>test_calibration_accepts_ndarray.<locals>.MockTensorClassifieri  z*A toy estimator that accepts tensor inputsc                 <    [         R                  " U5      U l        U $ N)rS   r   r   )r   r<   r=   s      r>   rX   Btest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fit  s    IIaLDMKr@   c                 Z    UR                  UR                  S   S5      R                  SS9$ )Nr   r   rN   r   )reshaper   r   r   s     r>   r   Ptest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_function  s)    99QWWQZ,00a088r@   )r   N)r   r   r   r   __doc__rX   r   r   r   r@   r>   MockTensorClassifierr#    s    8		9r@   r+  N)r   r   r   rX   )r<   r=   r+  r   s       r>    test_calibration_accepts_ndarrayr,    s5     	6A	9 	9 ,,@,BCNqr@   c                  <    SSS.SSS.SSS.SSS.SSS./n / S	QnX4$ )
NNYadult)stateageTXVTchildCTBR)rN   r   rN   rN   r   r   )	dict_datatext_labelss     r>   r7  r7  
  sC     w'w'w'w'w'I "K!!r@   c                 n    U u  p[        S[        5       4S[        5       4/5      nUR                  X5      $ )N
vectorizerrb   )r"   r   r   rX   )r7  r<   r=   pipeline_prefits       r>   dict_data_pipeliner<    s?    DA
(	)E3I3K+LMO q$$r@   c                 (   U u  p#Un[        [        U5      SS9nUR                  X#5        [        UR                  UR                  5        [        US5      (       a   e[        US5      (       a   eUR                  U5        UR                  U5        g)a>  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
rG   rk   n_features_in_N)r   r   rX   r,   r   hasattrr   rY   )r7  r<  r<   r=   rb   ro   s         r>   test_calibration_dict_pipeliner@     s     DA
C&s';BIMM!y))3<<8 s,----y"23333 aAr@   zclf, cvrN   r  r   c                 V   [        SSSSS9u  p#US:X  aO  U R                  X#5      n [        XS9n[        R                  " [
        5         UR                  X#5        S S S 5        O[        XS9nUR                  X#5        US:X  a=  [        UR                  U R                  5        UR                  U R                  :X  d   eg [        5       R                  U5      R                  n[        UR                  U5        UR                  UR                  S   :X  d   eg ! , (       d  f       N= f)	Nr   rQ   rG   r   r7   r8   r   r9   r   rk   rN   )r   rX   r   rZ   warnsFutureWarningr,   r   r>  r$   r   )rb   rP   r<   r=   ro   r   s         r>   test_calibration_attributesrF  :  s     QUVWDA	X~ggam*36	\\-(MM! )( +36	a	X~9--s||<''3+=+====.$$Q'009--w7''1771:555 )(s   	D
D(c                     [        SSSSS9u  p[        SS9R                  X5      n[        [	        U5      5      nSn[
        R                  " [        US	9   UR                  U S S 2S S
24   U5        S S S 5        g ! , (       d  f       g = f)Nr   rQ   rG   r   rC  rN   rA  zAX has 3 features, but LinearSVC is expecting 5 features as input.r|   r   )r   r&   rX   r   r   rZ   r[   r\   )r<   r=   rb   ro   msgs        r>   2test_calibration_inconsistent_prefit_n_features_inrI  V  ss     QUVWDA
a.

Q
"C&s';<I
MC	z	-a2A2h" 
.	-	-s   A;;
B	c            	         [        SSSSS9u  p[        [        S5       Vs/ s H  nS[        U5      -   [	        5       4PM     snSS	9nUR                  X5        [        [        U5      S
9nUR                  X5        g s  snf )Nr   rQ   rG   r   rC  r   lrsoft)
estimatorsvotingrm   )r   r   rangestrr   rX   r   r   )r<   r=   ivotero   s        r>   !test_calibration_votingclassifierrT  b  sy     QUVWDACH8L8aTCF]$6$898LD 	HHQN&1FGIMM! Ms   "A>c                      [        SS9$ )NT
return_X_y)r   r   r@   r>   	iris_datarX  r  s    %%r@   c                 &    U u  pXS:     X"S:     4$ )NrG   r   )rX  r<   r=   s      r>   iris_data_binaryrZ  w  s     DAU8Q1uXr@   r   r   r   rV   r   c           	         Uu  pE[        5       R                  XE5      n[        R                  " XdXRUSS9nUR	                  U5      S S 2S4   n[        XXX#S9u  p[        UR                  U	5        [        UR                  U
5        [        UR                  U5        UR                  S:X  d   eSS Kn[        UR                  UR                  R                  5      (       d   eUR                  R!                  5       S:X  d   e[        UR"                  UR$                  R&                  5      (       d   e[        UR(                  UR*                  R,                  5      (       d   eUR"                  R/                  5       S:X  d   eUR"                  R1                  5       S:X  d   eSS	/nUR"                  R3                  5       R5                  5       n[7        U5      [7        U5      :X  d   eU H  nUR9                  5       U;   a  M   e   g )
Nr   )r   r   alpharN   r   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rX   r   from_estimatorrY   r   r   r   r  y_probestimator_name
matplotlibrn   line_linesLine2D	get_alphaax_axesAxesfigure_figureFigure
get_xlabel
get_ylabel
get_legend	get_textsru   get_text)pyplotrZ  r   r   r<   r=   rK  vizr_  r   r  mplexpected_legend_labelslegend_labelslabelss                  r>    test_calibration_display_computerw  }  s    DA			!	!!	'B

+
+
q(#C a A&F,	&I CMM9-CMM9-CJJ'!5555 cii!1!1222299 C'''cggsxx}}----ckk3::#4#4555577#SSSS77#NNNN24JKGG&&(224M}%;!<<<< $::::  r@   c                 r   Uu  p#[        [        5       [        5       5      nUR                  X#5        [        R
                  " XBU5      nUR                  S/nUR                  R                  5       R                  5       n[        U5      [        U5      :X  d   eU H  nUR                  5       U;   a  M   e   g )Nr]  )r#   r%   r   rX   r   r^  r`  rf  rn  ro  ru   rp  )	rq  rZ  r<   r=   rb   rr  rt  ru  rv  s	            r>   $test_plot_calibration_curve_pipelinery    s    DA
(*<*>
?CGGAM

+
+CA
6C!002HIGG&&(224M}%;!<<<< $::::  r@   zname, expected_label)N_line1)my_estr{  c                    [         R                  " / SQ5      n[         R                  " / SQ5      n[         R                  " / 5      n[        X4XQS9nUR                  5         Uc  / OU/nUR	                  S5        UR
                  R                  5       R                  5       n[        U5      [        U5      :X  d   eU H  n	U	R                  5       U;   a  M   e   g )Nr   rN   rN   r   r   r   r   皙?r`  r]  )
rS   r   r   plotappendrf  rn  ro  ru   rp  )
rq  nameexpected_labelr   r  r_  rr  rt  ru  rv  s
             r>   'test_calibration_display_default_labelsr    s     &I-.IXXb\F
Y6
OCHHJ#'<RdV!!"89GG&&(224M}%;!<<<< $::::  r@   c                    [         R                  " / SQ5      n[         R                  " / SQ5      n[         R                  " / 5      nSn[        XX4S9nUR                  U:X  d   eSnUR	                  US9  US/nUR
                  R                  5       R                  5       n[        U5      [        U5      :X  d   eU H  nUR                  5       U;   a  M   e   g )Nr}  r~  zname oner  zname twor  r]  )
rS   r   r   r`  r  rf  rn  ro  ru   rp  )	rq  r   r  r_  r  rr  rt  ru  rv  s	            r>   )test_calibration_display_label_class_plotr    s     &I-.IXXb\FD
Y6
OC%%%DHH$H"$:;GG&&(224M}%;!<<<< $::::  r@   constructor_namer^  from_predictionsc                    Uu  p4Sn[        5       R                  X45      nUR                  U5      S S 2S4   n[        [        U 5      nU S:X  a  XcU4OXG4n	U" U	SU06n
U
R
                  U:X  d   eUR                  S5        U
R                  5         US/nU
R                  R                  5       R                  5       n[        U5      [        U5      :X  d   eU H  nUR                  5       U;   a  M   e   UR                  S5        SnU
R                  US9  [        U5      [        U5      :X  d   eU H  nUR                  5       U;   a  M   e   g )	Nzmy hand-crafted namerN   r^  r  r  r]  another_namer  )r   rX   rY   getattrr   r`  closer  rf  rn  ro  ru   rp  )r  rq  rZ  r<   r=   clf_namerb   r_  constructorparamsrr  rt  ru  rv  s                 r>   ,test_calibration_display_name_multiple_callsr    sQ    DA%H


"
"1
(Cq!!Q$'F,.>?K,0@@ca[qkF
v
-H
-C)))
LLHHJ&(>?GG&&(224M}%;!<<<< $::::   LLHHH(H}%;!<<<< $::::  r@   c                 D   Uu  p#[        5       R                  X#5      n[        5       R                  X#5      n[        R                  " XBU5      n[        R                  " XRX6R
                  S9nUR
                  R                  5       S   nUR                  S5      S:X  d   eg )N)axrN   r]  )r   rX   r'   r   r^  rf  get_legend_handles_labelscount)	rq  rZ  r<   r=   rK  dtrr  viz2rv  s	            r>   !test_calibration_display_ref_liner     s    DA			!	!!	'B		!	%	%a	+B

+
+B1
5C,,RA''BDXX//1!4F<<./1444r@   dtype_y_strc                 8   [         R                  R                  S5      n[         R                  " S/S-  S/S-  -   U S9nUR	                  SSUR
                  S9nS	n[        R                  " [        US
9   [        X#5        SSS5        g! , (       d  f       g= f)zKCheck error message when a `pos_label` is not specified with `str` targets.r5   spamr   eggsrG   dtyper   rJ   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)
rS   rT   rU   r   randintrK   rZ   r[   r\   r   )r  rngy1y2err_msgs        r>   *test_calibration_curve_pos_label_error_strr    s     ))


#C	6(Q,&A-[	AB	Q	(B	$ 
 
z	1"! 
2	1	1s   6B
Bc                    [         R                  " / SQ5      n[         R                  " SS/U S9nX!   n[         R                  " / SQ5      n[        XSS9u  pV[        U/ SQ5        [        X4SSS	9u  pV[        U/ SQ5        [        US
U-
  SSS	9u  pV[        U/ SQ5        [        US
U-
  SSS	9u  pV[        U/ SQ5        g)z8Check the behaviour when passing explicitly `pos_label`.)	r   r   r   rN   rN   rN   rN   rN   rN   r  eggr  )	r   r   g333333?r  r   gffffff?r   r   r   r  r   )r   r   rN   rN   )r   	pos_labelrN   r   )r   r   r   rN   N)rS   r   r   r   )r  r   r   
y_true_strr   r   r  s          r>    test_calibration_curve_pos_labelr    s     XX12Fhhk:GJXXDEF %VA>LII~.$ZUSLII~.$VQZQOLII~.$ZVAQWXLII~.r@   kwargsred-.)clwls)color	linewidth	linestylec                 .   Uu  p4[        5       R                  X45      n[        R                  " XSU40 UD6nUR                  R                  5       S:X  d   eUR                  R                  5       S:X  d   eUR                  R                  5       S:X  d   eg)z*Check that matplotlib aliases are handled.r  rG   r  N)r   rX   r   r^  rb  	get_colorget_linewidthget_linestyle)rq  rZ  r  r<   r=   rK  rr  s          r>   test_calibration_display_kwargsr  2  s     DA			!	!!	'B

+
+B1
?
?C99 E)))99""$)))99""$,,,r@   zpos_label, expected_pos_label))NrN   r
  )rN   rN   c                    Uu  pE[        5       R                  XE5      n[        R                  " XdXRS9nUR	                  U5      SS2U4   n[        XXUS9u  p[        UR                  U	5        [        UR                  U
5        [        UR                  U5        UR                  R                  5       SU S3:X  d   eUR                  R                  5       SU S3:X  d   eUR                  R                  S/nUR                  R                  5       R!                  5       n[#        U5      [#        U5      :X  d   eU H  nUR%                  5       U;   a  M   e   g)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   rX   r   r^  rY   r   r   r   r  r_  rf  rl  rm  	__class__r   rn  ro  ru   rp  )rq  rZ  r  expected_pos_labelr<   r=   rK  rr  r_  r   r  rt  ru  rv  s                 r>   "test_calibration_display_pos_labelr  E  sR   
 DA			!	!!	'B

+
+B1
JCa $6!67F,Q)LICMM9-CMM9-CJJ' 	9:L9MQO	P	P 	45G4HJ	K	K !ll335KLGG&&(224M}%;!<<<< $::::  r@   c                 J   [        SS9u  p#[        5       R                  U5      nUSS USS p2[        R                  " U5      S-  n[        R
                  " UR                  S   S-  UR                  S   4UR                  S9nX%SSS2SS24'   X%SSS2SS24'   [        R
                  " UR                  S   S-  UR                  S9nX6SSS2'   X6SSS2'   [        5       n[        UU USS	9n[        U5      n	U	R                  X#US
9  UR                  XV5        [        U	R                  UR                  5       H9  u  p[        U
R                  R                   UR                  R                   5        M;     U	R#                  U5      nUR#                  U5      n[        X5        g)znCheck that passing repeating twice the dataset `X` is equivalent to
passing a `sample_weight` with a factor 2.TrV  Nr   rG   r   rN   r  )rB   rE   rP   rL   )r   r%   fit_transformrS   	ones_liker   r   r  r   r   r   rX   r  rl   r   rm   coef_rY   )rB   rE   r<   r=   rM   X_twicey_twicerm   calibrated_clf_without_weightscalibrated_clf_with_weightsest_with_weightsest_without_weightsy_pred_with_weightsy_pred_without_weightss                 r>   ?test_calibrated_classifier_cv_double_sample_weights_equivalencer  f  s   
 %DA&&q)ATc7AdsGqLLOa'M hh
Q
3177CGCaCFOADqD!GhhqwwqzA~QWW5GCaCLADqDM"$I%;	&" #((F"G##A#F"&&w8 25#;;&>>2- 	&&,,))//	
	2 6CCAF;II!L'@r@   fit_params_typelistr   c                     Uu  p#[        X05      [        X05      S.n[        SS/S9n[        U5      nUR                  " X#40 UD6  g)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  )expected_fit_paramsN)r)   r(   r   rX   )r  r?   r<   r=   
fit_paramsrb   pc_clfs          r>    test_calibration_with_fit_paramsr    sM     DA33J
 #s
<C#C(F
JJq"z"r@   rM   r   c                 T    Uu  p#[        SS9n[        U5      nUR                  X#U S9  g)zETests that sample_weight is passed to the underlying base
estimator.
T)expected_sample_weightrL   N)r(   r   rX   )rM   r?   r<   r=   rb   r  s         r>   -test_calibration_with_sample_weight_estimatorr    s/     DA
D
9C#C(F
JJq=J1r@   c                    U u  p[         R                  " U5      n " S S[        5      nU" 5       n[        U5      n[        R
                  " [        5         UR                  XUS9  SSS5        g! , (       d  f       g= f)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                   (   ^  \ rS rSrU 4S jrSrU =r$ )Ptest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeighti  c                 6   > SU;  d   e[         TU ]  " X40 UD6$ )NrM   superrX   )r   r<   r=   r  r  s       r>   rX   Ttest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeight.fit  s%    "*4447;q2z22r@   r   r   r   r   r   rX   r   __classcell__r  s   @r>   ClfWithoutSampleWeightr    s    	3 	3r@   r  rL   N)rS   r  r(   r   rZ   rD  UserWarningrX   )r?   r<   r=   rM   r  rb   r  s          r>   0test_calibration_without_sample_weight_estimatorr    sa     DALLOM3!3 3
 !
"C#C(F	k	"

1}
5 
#	"	"s   A00
A>c           
           " S S[         5      n[        U" 5       S9R                  " U S[        R                  " [        U S   5      S-   5      06  g)zWCheck that CalibratedClassifierCV does not enforce sample alignment
for fit parameters.c                   ,   ^  \ rS rSrSU 4S jjrSrU =r$ )Jtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifieri  c                 *   > Uc   e[         TU ]  XUS9$ )NrL   r  )r   r<   r=   rM   	fit_paramr  s        r>   rX   Ntest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit  s"    (((7;q=;AAr@   r   )NNr  r  s   @r>   TestClassifierr    s    	B 	Br@   r  rO  r  rN   N)r   r   rX   rS   r   ru   )r?   r  s     r>   2test_calibration_with_non_sample_aligned_fit_paramr    sJ    B+ B
 ^%56::	T!W!12r@   c           	         SnSn[         R                  R                  U 5      R                  US9n[         R                  " S/[        X!-  5      -  S/U[        X!-  5      -
  -  -   5      nSUR                  S5      -  U-   n[        SUS	S
9nUR                  XT5      nU HV  u  pXX   XH   pXY   n[        SU S9nUR                  X5        UR                  U5      nUS:  R                  5       (       a  MV   e   [        [        SU S9SS9n[        XUSS9n[        [        SU S9SS9n[        UXTSS9n[        UU5        g)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  rJ   rN   r   g     j@)r   rN   NT)rP   r=   
classifiersquared_hinge)lossr9   g     @rC   r   roc_auc)scoringrD   )rS   rT   default_rngnormalr   intr(  r   splitr   rX   r   anyr   r   r   )global_random_seedprobnrandom_noiser=   r<   rP   indicestraintestr]   r^   r`   sgd_clfpredictionsclf_sigmoidscore_sigmoidclf_isotonicscore_isotonics                      r>   @test_calibrated_classifier_cv_works_with_large_confidence_scoresr    sY    DA99(();<CCCKL
!s18}$sa#ah-.?'@@AAaii  </A 
TQ4	0BhhqnG8QX_CUVG%//7c!&&((((  )?9KLK $KAyIM *?9KLL %\1KN M>2r@   c                 .   [         R                  R                  U S9nSnUR                  SSUS9nUR	                  SSSS9nSn[        UUUS	9u  pgS
n[        UUUS	9u  p[        UUS9u  pSn[        XiUS9  [        XUS9  [        XzUS9  [        XUS9  g )NrH   r   r   rG   rJ   )lowhighrK   r   )r  r=   max_abs_prediction_thresholdr   )r  r=   gư>)atol)rS   rT   rU   r  rV   r
   r   )r  r9   r  r=   predictions_smallthreshold_1a1b1threshold_2a2b2a3b3r
  s                 r>   5test_sigmoid_calibration_max_abs_prediction_thresholdr    s    99((.@(ALAQ*A %,,!#,F K!%
%0FB K!%
%0FB "%
FB DB&B&B&B&r@   use_sample_weightc                    U(       a'  [         R                  " U S   [         R                  S9nOSn " S S[        5      nU" 5       n[	        XRS9nUR
                  " U SU06  U" 5       R
                  " U SU06n[	        [        U5      US9nUR
                  " U SU06  U" 5       R
                  " U SU06n[	        XRSS	9n[        R                  " [        5         UR
                  " U SU06  SSS5        g! , (       d  f       g= f)
ztCheck that CalibratedClassifierCV works with float32 predict proba.

Non-regression test for gh-28245 and gh-28247.
rN   r  Nc                   (   ^  \ rS rSrU 4S jrSrU =r$ )4test_float32_predict_proba.<locals>.DummyClassifer32iP  c                 \   > [         TU ]  U5      R                  [        R                  5      $ r%  )r  rY   astyperS   float32)r   r<   r  s     r>   rY   Btest_float32_predict_proba.<locals>.DummyClassifer32.predict_probaQ  s"    7(+222::>>r@   r   )r   r   r   r   rY   r   r  r  s   @r>   DummyClassifer32r  P  s    	? 	?r@   r  r   rM   r   r   )
rS   r  float64r   r   rX   r   rZ   rD  rE  )r?   r  rB   rM   r  modelr   s          r>   test_float32_predict_probar   <  s      T!WBJJ??? ? E'=JNND66 ""DFFE'(>vNJNND66 ""DFFE'JJ	m	$:M: 
%	$	$s   C55
Dc                      [         R                  R                  SS9n S/S-  S/S-  -   n[        SS9R	                  X5        g)	zdCheck that CalibratedClassifierCV works with string targets.

non-regression test for issue #28841.
)   r   rJ   r  r   r  r   rk   N)rS   rT   r  r   rX   r;   s     r>   (test_error_less_class_samples_than_foldsr#  h  sD    
 			g&A	
cURZAa $$Q*r@   )numpyrS   rZ   numpy.testingr   sklearn.baser   r   r   sklearn.calibrationr   r   r	   r
   r   r   sklearn.datasetsr   r   r   sklearn.dummyr   sklearn.ensembler   r   sklearn.exceptionsr   sklearn.feature_extractionr   sklearn.frozenr   sklearn.imputer   sklearn.isotonicr   sklearn.linear_modelr   r   sklearn.metricsr   sklearn.model_selectionr   r   r   r   r   r    sklearn.naive_bayesr!   sklearn.pipeliner"   r#   sklearn.preprocessingr$   r%   sklearn.svmr&   sklearn.treer'   sklearn.utils._mockingr(   sklearn.utils._testingr)   r*   r+   r,   r-   sklearn.utils.extmathr.   sklearn.utils.fixesr/   r:   fixturer?   markparametrizeri   rq   ry   r~   r   r   rP  r   r   rE  r   r   r   r  r  r  r  rT   rU   r  r,  r7  r<  r@  paramrF  rI  rT  rX  rZ  rw  ry  r  r  r  r  rQ  objectr  r  r  r  r  r  r   r  r  r  r  r  r   r#  r   r@   r>   <module>rA     s     ) > >  H G ) . 5 * ( / B ,  . 4 > ! / 5  * .	 h  
 .9Iz#:;dE]38 4 < :8v+ dE]3D 4D Iz#:;dE]3 4 <, Iz#:;dE]36 4 <6, Iz#:;dE]3 q*:7 + 4 <
:7z22 -(.95 : )5p Iz#:;5 <50>"C> dE]3 4 dE]3@ 4@ dE]3N 4N: 
		b!''Aq1
		b!''Aq!4( 	" 	" % %4 Y^Q'Y^X.66*	#  h&  & h  
 Ar7+i%<=&; > ,&;R; -/CD;; ;( +.>@R-ST; U;D
5 f6" 7" f6/ 7/( 1D)ad;	-	- 8:UV; W;@ Iz#:;dE]3-A 4 <-A` *VW,=># ?#$ 		
	226,/3d&'R ,tUm<Iz#:;'; < =';T+r@   