
    h$,f              
       `   d dl Zd dl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l.m/Z/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z<m=Z=m>Z> d dl?m@Z@ d dlAmBZB dZC ej                  d      d        ZEej                  j                  deB      ej                  j                  dddg      ej                  j                  d d!d"g      d#                      ZHd$ ZIej                  j                  d d!d"g      d%        ZJej                  j                  dddg      ej                  j                  d d!d"g      d&               ZKej                  j                  dddg      ej                  j                  d d!d"g      d'               ZLej                  j                  dddg      ej                  j                  d d!d"g      ej                  j                  d( eMd)            d*                      ZNd+ ZOej                  j                  deB      d,        ZPej                  j                  dddg      d-        ZQd. ZRd/ ZSej                  j                  d d!d"g      d0        ZTej                  j                  d d!d"g      d1        ZUej                  j                  d d!d"g      d2        ZVej                  j                  d3ej                  j                  d4      j                  d5d6d)      ej                  j                  d4      j                  d5d6d)d7      g      d8        ZZej                  d9        Z[ej                  d:        Z\d; Z]ej                  j                  d< ej                   e5d=d>?      d)       ej                   e5d=d>?      d@      g      dA        Z_dB Z`dC Za ej                  d      dD        Zb ej                  d      dE        Zcej                  j                  dFd6dGg      ej                  j                  dHdIdJg      dK               ZddL Zeej                  j                  dMdNdOg      dP        ZfdQ Zgej                  j                  dRdSdTg      dU        ZhdV Ziej                  j                  dWejekg      dX        Zlej                  j                  dWejekg      dY        Zmej                  j                  dZg d[      d\        Znej                  j                  dddg      ej                  j                  d d!d"g      d]               Zoej                  j                  d^d_d`g      da        Zpej                  j                  dbdcgeCz   ej                  eC      g      dd        Zrde Zsej                  j                  dddg      ej                  j                  d d!d"g      df               Ztdg Zudh Zvdi Zwdj Zxy)k    N)assert_allclose)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)CSR_CONTAINERS   module)scopec                  4    t        t        dd      \  } }| |fS )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLES)Xys     >lib/python3.12/site-packages/sklearn/tests/test_calibration.pydatar;   7   s    qrRDAqa4K    csr_containermethodsigmoidisotonicensembleTFc                    t         dz  }| \  }}t        j                  j                  d      j	                  |j
                        }||j                         z  }|d | |d | |d | }
}	}||d  ||d  }}t               j                  ||	|
      }|j                  |      d d df   }t        ||j
                  dz   |      }t        j                  t              5  |j                  ||       d d d        ||f ||       ||      ffD ]>  \  }}t        ||d|	      }|j                  ||	|
       |j                  |      d d df   }t        ||      t        ||      kD  sJ |j                  ||	dz   |
       |j                  |      d d df   }t        ||       |j                  |d|	z  dz
  |
       |j                  |      d d df   }t        ||       |j                  ||	dz   dz  |
       |j                  |      d d df   }|d
k(  rt        |d|z
         t        ||      t        |dz   dz  |      kD  r?J  y # 1 sw Y   axY w)N   r2   seedsizesample_weight   cvrA      r>   rL   rA   r?   )r7   nprandomRandomStateuniformrG   minr   fitpredict_probar   pytestraises
ValueErrorr   r)   )r;   r>   r=   rA   r4   r8   r9   rI   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_calibrationre   =   s   
 QIDAqII))r)2:::GMLA "#:I*9}Zi?XhWGyz]AijMFF /

gwh

GC$$V,QT2L$SQVVaZ(KG	z	" Aq
 
&	w	v!67& #!k )V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 $FL9<L!q "<=   C#	 s   IIc                     | \  }}t        d      }|j                  ||       |j                  d   j                  }t	        |t
              sJ y )NrC   rL   r   )r   rT   calibrated_classifiers_	estimator
isinstancer$   )r;   r8   r9   	calib_clfbase_ests        r:   "test_calibration_default_estimatorrm   {   sI    DAq&!,IMM!Q003==Hh	***r<   c                    | \  }}d}t        |      }t        ||      }t        |j                  t               sJ |j                  j                  |k(  sJ |j                  ||       |r|nd}t        |j                        |k(  sJ y )NrM   )n_splitsrK   rJ   )r   r   rj   rL   ro   rT   lenrh   )r;   rA   r8   r9   splitskfoldrk   expected_n_clfs           r:   test_calibration_cv_splitterrt      s     DAqF6"E&%(CIillE***<<  F***MM!Q'VQNy001^CCCr<   c                    t         dz  }| \  }}t        j                  j                  d      j	                  t        |            }|d | |d | |d | }	}}||d  }
t        dd      }t        |||      }|j                  |||	       |j                  |
      }|j                  ||       |j                  |
      }t        j                  j                  ||z
        }|d	kD  sJ y )
NrC   r2   rD   rF   autodualr6   )r>   rA   rH   皙?)r7   rO   rP   rQ   rR   rp   r$   r   rT   rU   linalgnorm)r;   r>   rA   r4   r8   r9   rI   rY   rZ   r[   r\   ri   calibrated_clfprobs_with_swprobs_without_swdiffs                   r:   test_sample_weightr      s     QIDAqII))r)2::A:GM!":I*9}Zi?XhWGyz]FvB7I+IfxXNwx@"008M w(%33F;99>>-*::;D#::r<   c                 R   | \  }}t        ||d      \  }}}}t        t               t        dd            }	t	        |	|d|      }
|
j                  ||       |
j                  |      }t	        |	|d|      }|j                  ||       |j                  |      }t        ||       y)	zTest parallel calibrationr2   )r6   rv   rw   rC   )r>   n_jobsrA   rJ   N)r   r!   r#   r$   r   rT   rU   r   )r;   r>   rA   r8   r9   rY   r\   rZ   r]   ri   cal_clf_parallelprobs_parallelcal_clf_sequentialprobs_sequentials                 r:   test_parallel_executionr      s     DAq'712'N$GVWfn.	vTV0WXI-&X '*%33F;N/&X 7G,)77?N$45r<   rE   rC   c                    d }t        dd      }t        dd|dd	      \  }}d
||d
kD  <   t        j                  |      j                  d   }|d d d
   |d d d
   }	}|dd d
   |dd d
   }}
|j                  ||	       t        || d|      }|j                  ||	       |j                  |
      }t        t        j                  |d      t        j                  t        |
                   d|j                  |
|      cxk  rdk  sJ  J |j                  |
|      d|j                  |
|      z  kD  sJ  ||t        |j                  |
            |      } ||||      }|d|z  k  sJ t        dd      }|j                  ||	       |j                  |
      } ||||      }t        || d|      }|j                  ||	       |j                  |
      } ||||      }|d|z  k  sJ y )Nc                     t        j                  |      |    }t        j                  ||z
  dz        |j                  d   z  S )NrC   r   )rO   eyesumshape)y_true
proba_pred	n_classesY_onehots       r:   multiclass_brierz5test_calibration_multiclass.<locals>.multiclass_brier   s<    66)$V,vvx*,23hnnQ6GGGr<   rv      rw   i  d   
         .@r4   r5   r6   centerscluster_stdrC   r   rJ   rM   rN   axis?gffffff?)r   g?   r2   )n_estimatorsr6   )r$   r   rO   uniquer   rT   r   rU   r   r   onesrp   scorer+   decision_functionr   )r>   rA   rE   r   r^   r8   r9   r   rY   rZ   r\   r]   r`   probasuncalibrated_briercalibrated_brier	clf_probscal_clf_probss                     r:   test_calibration_multiclassr      s   H a
0C#D"RVDAq Aa!eH		!""1%I1vq1vWGqt!tWa1gFFGGGW$SAQGKK!""6*FBFF6*BGGCK,@A
 #))FF+2d22222 ==(4#))FF2K+KKKK
 *--f56) ()Lc$66666 !br
BCGGGW!!&)I)&)yQ$SAQGKK!))&1M'Sc$66666r<   c                      G d d      } t        ddddd      \  }}t               j                  ||      } |        }t        ||g|j                        }|j                  |      }t        |d	|j                  z         y )
Nc                       e Zd Zd Zy)9test_calibration_zero_probability.<locals>.ZeroCalibratorc                 F    t        j                  |j                  d         S )Nr   )rO   zerosr   selfr8   s     r:   predictzAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predict  s    88AGGAJ''r<   N)__name__
__module____qualname__r    r<   r:   ZeroCalibratorr     s    	(r<   r   2   r   r   r   r   )ri   calibratorsclasses      ?)r   r   rT   r   classes_rU   r   
n_classes_)r   r8   r9   r^   
calibratorr`   r   s          r:   !test_calibration_zero_probabilityr     s    
( (
 !RTDAq 


1
%C!J#J<G ""1%F FC#..01r<   c                    d}t        d|z  dd      \  }}t        j                  j                  d      j	                  |j
                        }||j                         z  }|d| |d| |d| }}}||d	|z   ||d	|z   ||d	|z   }
}	}|d	|z  d |d	|z  d }}t               }t        |d
      }t        j                  t              5  |j                  ||	       ddd       |j                  |||       |j                  |      dddf   }||f | |       | |      ffD ]  \  }}dD ]  }t        ||d
      }|
dfD ]  }|j                  ||	|       |j                  |      }|j                  |      }|dddf   }t        |t        j                   ddg      t        j"                  |d                t%        ||      t%        ||      kD  rJ    y# 1 sw Y   xY w)z*Test calibration for prefitted classifiersr      r1   r2   r3   rD   rF   NrC   prefitrg   rJ   )r@   r?   )r>   rL   rH   r   r   )r   rO   rP   rQ   rR   rG   rS   r   r   rV   rW   r   rT   rU   r   r*   arrayargmaxr   )r=   r4   r8   r9   rI   rY   rZ   r[   X_caliby_calibsw_calibr\   r]   r^   	unfit_clfr_   this_X_calibrb   r>   r`   swy_proby_predrc   s                           r:   test_calibration_prefitr      s0    IY1SUVDAqII))r)2:::GMLA "#:I*9}Zi?XhWG	)a)m$	)a)m$i!i-0 WG
 q9}'1y=?);FF /C&sx8I	~	& (gw'( GGGWh'$$V,QT2L 
&	w	v!67& !k . 	F,SHMG& 	L'D ..{; 5#)!Q$< "6288QF+;BIIfST<U+VW'=@P,A   			( (s   GG)c                    | \  }}t        dd      }t        ||dd      }|j                  ||       |j                  |      }t	        |||dd      }|d	k(  rt        d
      }n
t               }|j                  ||       |j                  ||       |j                  |      }	|j                  |	      }
t        |d d df   |
       y )Nrv   r   rw   r   FrN   r   )rL   r>   r@   clip)out_of_boundsrJ   )
r$   r   rT   rU   r   r   r
   r   r   r   )r;   r>   r8   r9   r^   r`   
cal_probasunbiased_predsr   clf_dfmanual_probass              r:   test_calibration_ensemble_falser   P  s     DAq
a
0C$SANGKK1&&q)J 'sAqQ?RSN'f=
(*
NN>1%GGAqM""1%F&&v.MJq!t$m4r<   c                  0   t        j                  g d      } t        j                  g d      }t        j                  ddg      }t        |t        | |      d       ddt        j                  |d   | z  |d   z         z   z  }t               j                  | |      j                  |       }t        ||d	       t        j                  t              5  t               j                  t        j                  | | f      |       d
d
d
       y
# 1 sw Y   y
xY w)z0Test calibration values with Platt sigmoid model)rM   r   )rJ   r   gj=ɿgY90(?r   r   r   rJ   r1   N)rO   r   r)   r	   expr
   rT   r   rV   rW   rX   vstack)exFexYAB_lin_libsvmlin_probsk_probs        r:   test_sigmoid_calibrationr   i  s    
((<
 C
((;
CHH24GHIMm-A#s-KQOcBFF=#3c#9M!<L#LMMNH!#''S199#>Gh3 
z	" >!!"))S#J"7=> > >s   0DDc                     t        j                  g d      } t        j                  g d      }t        | |d      \  }}t        |      t        |      k(  sJ t        |      dk(  sJ t	        |ddg       t	        |ddg       t        j                  t              5  t        dgd	g       d
d
d
       t        j                  g d      }t        j                  g d      }t        ||dd      \  }}t        |      t        |      k(  sJ t        |      dk(  sJ t	        |ddg       t	        |ddg       t        j                  t              5  t        ||d       d
d
d
       y
# 1 sw Y   xY w# 1 sw Y   y
xY w)z Check calibration_curve function)r   r   r   rJ   rJ   rJ   )        ry   皙?皙??r   rC   n_binsr   rJ   ry   r   gN)r   r   r   r   rJ   rJ   )r   ry   r         ?r   r   quantiler   strategygUUUUUU?r   
percentile)r   )rO   r   r   rp   r(   rV   rW   rX   )r   r   	prob_true	prob_predy_true2y_pred2prob_true_quantileprob_pred_quantiles           r:   test_calibration_curver   z  si   XX()FXX45F,VVAFIyy>S^+++y>Q	Aq6*	C:. 
z	" '1#v&' hh)*Ghh56G->Z.** !"c*<&====!"a'''*QJ7*S#J7 
z	" C'7\BC C!' ' C Cs   E+E7+E47F c                     t        ddddd      \  }}t        j                  |d<   t        dt	               fdt        d	
      fg      }t        |dd|       }|j                  ||       |j                  |       y)z$Test that calibration can accept nanr   rC   r   r2   )r4   r5   n_informativen_redundantr6   r   r   imputerrfrJ   )r   r@   )rL   r>   rA   N)	r   rO   nanr    r   r   r   rT   r   )rA   r8   r9   r^   clf_cs        r:   test_calibration_nan_imputerr     s}     !QSDAq ffAdG

]_	%.DRS.T'UVC #31Z(SE	IIaO	MM!r<   c                 2   d}t        dd|      \  }}t        ddd      }t        |d	t               | 
      }|j	                  ||       |j                  |      }t        |j                  d      t        j                  |j                  d                y )NrC   r   rM   )r4   r5   r   rv   r   r   rx   Cr6   r?   rN   rJ   r   r   )r   r$   r   r   rT   rU   r)   r   rO   r   r   )rA   num_classesr8   r9   r^   clf_probprobss          r:   test_calibration_prob_sumr     s     K[QDAq
3Q
7C%I+-(H LLA""1%EeiiQi/Q1HIr<   c           	      ~   t         j                  j                  dd      }t        j                  d      }t	        ddd      }t        |dt               |       }|j                  ||       t        |j                        D ]  \  }}|j                  |      }| ryt        |d d |f   t        j                  t        |                   t        j                  |d d d |f   d	kD        sJ t        j                  |d d |d
z   d f   d	kD        rJ t        j                  |d
|j                   d	   z        rJ  y )Nr   rM   rv   r   r   r   r?   rN   r   rJ   )rO   rP   randnaranger$   r   r   rT   	enumeraterh   rU   r*   r   rp   allallcloser   )rA   r8   r9   r^   r`   icalibrated_classifierprobas           r:   test_calibration_less_classesr
    s    			AA
		"A
3Q
7C$I+-(G KK1$-g.M.M$N 
:  %33A6uQT{BHHSV,<=66%2A2,*+++66%1q57
+a/000 ;;ua%++a.&8999
:r<   r8   r2      rM   r1   c                 n    g d} G d dt               }t         |             }|j                  | |       y)z;Test that calibration accepts n-dimensional arrays as input)rJ   r   r   rJ   rJ   r   rJ   rJ   r   r   rJ   r   r   rJ   r   c                        e Zd ZdZdZd Zd Zy)>test_calibration_accepts_ndarray.<locals>.MockTensorClassifierz*A toy estimator that accepts tensor inputs
classifierc                 :    t        j                  |      | _        | S N)rO   r   r   )r   r8   r9   s      r:   rT   zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fit  s    IIaLDMKr<   c                 `    |j                  |j                  d   d      j                  d      S )Nr   r   rJ   r   )reshaper   r   r   s     r:   r   zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_function  s)    99QWWQZ,00a088r<   N)r   r   r   __doc___estimator_typerT   r   r   r<   r:   MockTensorClassifierr    s    8&		9r<   r  N)r   r   rT   )r8   r9   r  r|   s       r:    test_calibration_accepts_ndarrayr    s5     	6A9} 9 ,,@,BCNq!r<   c                  .    dddddddddg} g d}| |fS )NNYadult)stateageTXVTchild)rJ   r   rJ   r   )	dict_datatext_labelss     r:   r   r     s5     w'w'w'I
 Kk!!r<   c                 r    | \  }}t        dt               fdt               fg      }|j                  ||      S )N
vectorizerr^   )r    r   r   rT   )r   r8   r9   pipeline_prefits       r:   dict_data_pipeliner%    sC    DAq
(	)E3I3K+LMO q!$$r<   c                    | \  }}|}t        |d      }|j                  ||       t        |j                  |j                         t	        |d      rJ t	        |d      rJ |j                  |       |j                  |       y)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   rg   n_features_in_N)r   rT   r*   r   hasattrr   rU   )r   r%  r8   r9   r^   rk   s         r:   test_calibration_dict_pipeliner)    s     DAq
C&sx8IMM!Qy))3<<8 s,---y"2333 aAr<   zclf, cvrv   rJ   rx   r   r   c                    t        dddd      \  }}|dk(  r| j                  ||      } t        | |      }|j                  ||       |dk(  r<t        |j                  | j                         |j
                  | j
                  k(  sJ y t               j                  |      j                  }t        |j                  |       |j
                  |j                  d   k(  sJ y )	Nr   rM   rC   r   r4   r5   r   r6   r   rg   rJ   )r   rT   r   r*   r   r'  r"   r   )r^   rL   r8   r9   rk   r   s         r:   test_calibration_attributesr-    s     QUVWDAq	X~ggam&sr2IMM!Q	X~9--s||<''3+=+====.$$Q'009--w7''1771:555r<   c                     t        dddd      \  } }t        dd      j                  | |      }t        |d	
      }d}t	        j
                  t        |      5  |j                  | d d d df   |       d d d        y # 1 sw Y   y xY w)Nr   rM   rC   r   r,  rv   rJ   r*  r   rg   zAX has 3 features, but LinearSVC is expecting 5 features as input.matchr   )r   r$   rT   r   rV   rW   rX   )r8   r9   r^   rk   msgs        r:   2test_calibration_inconsistent_prefit_n_features_inr2  6  s     QUVWDAq
1
%
)
)!Q
/C&sx8I
MC	z	- #a2A2h"# # #s   A??Bc            	         t        dddd      \  } }t        t        d      D cg c]  }dt        |      z   t	               f c}d	      }|j                  | |       t        |d
      }|j                  | |       y c c}w )Nr   rM   rC   r   r,  r   lrsoft)
estimatorsvotingr   )ri   rL   )r   r   rangestrr   rT   r   )r8   r9   r  voterk   s        r:   !test_calibration_votingclassifierr;  B  sx     QUVWDAqCH8LaTCF]$6$89LD 	HHQN&(CIMM!Q Ms   A?c                      t        d      S )NT
return_X_y)r   r   r<   r:   	iris_datar?  R  s    %%r<   c                 ,    | \  }}||dk     ||dk     fS )NrC   r   )r?  r8   r9   s      r:   iris_data_binaryrA  W  s&    DAqQU8Qq1uXr<   r   r   r   rR   r   c                    |\  }}t               j                  ||      }t        j                  |||||d      }|j	                  |      d d df   }t        ||||      \  }	}
t        |j                  |	       t        |j                  |
       t        |j                  |       |j                  dk(  sJ dd l}t        |j                  |j                  j                        sJ |j                  j!                         dk(  sJ t        |j"                  |j$                  j&                        sJ t        |j(                  |j*                  j,                        sJ |j"                  j/                         dk(  sJ |j"                  j1                         dk(  sJ dd	g}|j"                  j3                         j5                         }t7        |      t7        |      k(  sJ |D ]  }|j9                         |v rJ  y )
Nr   )r   r   alpharJ   r   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rT   r   from_estimatorrU   r   r   r   r   r   estimator_name
matplotlibrj   line_linesLine2D	get_alphaax_axesAxesfigure_figureFigure
get_xlabel
get_ylabel
get_legend	get_textsrp   get_text)pyplotrA  r   r   r8   r9   r4  vizr   r   r   mplexpected_legend_labelslegend_labelslabelss                  r:    test_calibration_display_computer]  ]  s    DAq			!	!!Q	'B

+
+
Aq(#C a A&F,	6&8Iy CMM9-CMM9-CJJ'!5555 cii!1!122299 C'''cggsxx}}---ckk3::#4#455577#SSSS77#NNNN24JKGG&&(224M}%;!<<<< ; $::::;r<   c                 l   |\  }}t        t               t                     }|j                  ||       t	        j
                  |||      }|j                  dg}|j                  j                         j                         }t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  y )NrD  )r!   r#   r   rT   r   rE  rF  rL  rT  rU  rp   rV  )	rW  rA  r8   r9   r^   rX  rZ  r[  r\  s	            r:   $test_plot_calibration_curve_pipeliner_    s    DAq
(*<*>
?CGGAqM

+
+CA
6C!002HIGG&&(224M}%;!<<<< ; $::::;r<   zname, expected_label)N_line1)my_estra  c                    t        j                  g d      }t        j                  g d      }t        j                  g       }t        ||||      }|j                          |g n|g}|j	                  d       |j
                  j                         j                         }t        |      t        |      k(  sJ |D ]  }	|	j                         |v rJ  y )Nr   rJ   rJ   r   r   r   r   皙?rF  rD  )
rO   r   r   plotappendrL  rT  rU  rp   rV  )
rW  nameexpected_labelr   r   r   rX  rZ  r[  r\  s
             r:   'test_calibration_display_default_labelsrk    s     &I-.IXXb\F
Y	6$
OCHHJ#'<RdV!!"89GG&&(224M}%;!<<<< ; $::::;r<   c                    t        j                  g d      }t        j                  g d      }t        j                  g       }d}t        ||||      }|j                  |k(  sJ d}|j	                  |       |dg}|j
                  j                         j                         }t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  y )Nrc  rd  zname onerf  zname twori  rD  )
rO   r   r   rF  rg  rL  rT  rU  rp   rV  )	rW  r   r   r   ri  rX  rZ  r[  r\  s	            r:   )test_calibration_display_label_class_plotrn    s     &I-.IXXb\FD
Y	6$
OC%%%DHH$H"$:;GG&&(224M}%;!<<<< ; $::::;r<   constructor_namerE  from_predictionsc                    |\  }}d}t               j                  ||      }|j                  |      d d df   }t        t        |       }| dk(  r|||fn||f}	 ||	d|i}
|
j
                  |k(  sJ |j                  d       |
j                          |dg}|
j                  j                         j                         }t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  |j                  d       d}|
j                  |       t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  y )	Nzmy hand-crafted namerJ   rE  ri  r  rD  another_namerm  )r   rT   rU   getattrr   rF  closerg  rL  rT  rU  rp   rV  )ro  rW  rA  r8   r9   clf_namer^   r   constructorparamsrX  rZ  r[  r\  s                 r:   ,test_calibration_display_name_multiple_callsrx    sc    DAq%H


"
"1a
(Cq!!Q$'F,.>?K,0@@c1a[q&kF
v
-H
-C)))
LLHHJ&(>?GG&&(224M}%;!<<<< ; $::::; LLHHH(H}%;!<<<< ; $::::;r<   c                 P   |\  }}t               j                  ||      }t               j                  ||      }t        j                  |||      }t        j                  ||||j
                        }|j
                  j                         d   }|j                  d      dk(  sJ y )N)axrJ   rD  )r   rT   r%   r   rE  rL  get_legend_handles_labelscount)	rW  rA  r8   r9   r4  dtrX  viz2r\  s	            r:   !test_calibration_display_ref_liner    s    DAq			!	!!Q	'B		!	%	%a	+B

+
+B1
5C,,RA#''BDXX//1!4F<<./1444r<   dtype_y_strc                 >   t         j                  j                  d      }t        j                  dgdz  dgdz  z   |       }|j	                  dd|j
                        }d	}t        j                  t        |
      5  t        ||       ddd       y# 1 sw Y   yxY w)zKCheck error message when a `pos_label` is not specified with `str` targets.r2   spamr   eggsrC   dtyper   rF   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)
rO   rP   rQ   r   randintrG   rV   rW   rX   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 ""b!" " "s   =BBc                    t        j                  g d      }t        j                  ddg|       }||   }t        j                  g d      }t        ||d      \  }}t        |g d       t        ||dd	      \  }}t        |g d       t        |d
|z
  dd	      \  }}t        |g d       t        |d
|z
  dd	      \  }}t        |g d       y)z8Check the behaviour when passing explicitly `pos_label`.)	r   r   r   rJ   rJ   rJ   rJ   rJ   rJ   r  eggr  )	ry   r   g333333?re  r   gffffff?r   r   r      r   )r   r   rJ   rJ   )r   	pos_labelrJ   r   )r   r   r   rJ   N)rO   r   r   r   )r  r   r   
y_true_strr   r   _s          r:    test_calibration_curve_pos_labelr    s     XX12Fhhk:GJXXDEF %VVA>LIqI~.$ZUSLIqI~.$VQZQOLIqI~.$ZVAQWXLIqI~.r<   zpos_label, expected_pos_label))NrJ   r   )rJ   rJ   c                    |\  }}t               j                  ||      }t        j                  ||||      }|j	                  |      dd|f   }t        |||      \  }	}
t        |j                  |	       t        |j                  |
       t        |j                  |       |j                  j                         d| dk(  sJ |j                  j                         d| dk(  sJ |j                  j                  dg}|j                  j                         j!                         }t#        |      t#        |      k(  sJ |D ]  }|j%                         |v rJ  y)z?Check the behaviour of `pos_label` in the `CalibrationDisplay`.)r  Nz,Mean predicted probability (Positive class: )z'Fraction of positives (Positive class: rD  )r   rT   r   rE  rU   r   r   r   r   r   rL  rR  rS  	__class__r   rT  rU  rp   rV  )rW  rA  r  expected_pos_labelr8   r9   r4  rX  r   r   r   rZ  r[  r\  s                 r:   "test_calibration_display_pos_labelr    sc   
 DAq			!	!!Q	'B

+
+B1	
JCa $6!67F,Q)LIyCMM9-CMM9-CJJ' 	9:L9MQO	P	P 	45G4HJ	K	K !ll335KLGG&&(224M}%;!<<<< ; $::::;r<   c                 r   t        d      \  }}t               j                  |      }|dd |dd }}t        j                  |      dz  }t        j
                  |j                  d   dz  |j                  d   f|j                        }||dddddf<   ||dddddf<   t        j
                  |j                  d   dz  |j                        }||ddd<   ||ddd<   t               }t        || |d	      }t        |      }	|	j                  |||
       |j                  ||       t        |	j                  |j                        D ]9  \  }
}t        |
j                  j                   |j                  j                          ; |	j#                  |      }|j#                  |      }t        ||       y)zrCheck that passing repeating twice the dataset `X` is equivalent to
    passing a `sample_weight` with a factor 2.Tr=  Nr   rC   r   rJ   r  r>   rA   rL   rH   )r   r#   fit_transformrO   	ones_liker   r   r  r   r   r   rT   ziprh   r   ri   coef_rU   )r>   rA   r8   r9   rI   X_twicey_twiceri   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  3  s   
 %DAq&&q)ATc7AdsGqALLOa'M hh
Q
3177CGGCaCFOGADqD!GhhqwwqzA~QWW5GGCaCLGADqDM"$I%;	&" #((F"G##Aq#F"&&w8 25#;;&>>2 
-- 	&&,,))//	
	
 6CCAF;II!L')?@r<   fit_params_typelistr   c                     |\  }}t        ||       t        ||       d}t        ddg      }t        |      } |j                  ||fi | y)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   rT   )r  r;   r8   r9   
fit_paramsr^   pc_clfs          r:    test_calibration_with_fit_paramsr  e  sW     DAq?3?3J
 #s
<C#C(FFJJq!"z"r<   rI   r   c                 d    |\  }}t        d      }t        |      }|j                  |||        y)zMTests that sample_weight is passed to the underlying base
    estimator.
    T)expected_sample_weightrH   N)r&   r   rT   )rI   r;   r8   r9   r^   r  s         r:   -test_calibration_with_sample_weight_estimatorr  x  s3     DAq
D
9C#C(F
JJq!=J1r<   c                     | \  }}t        j                  |      } G d dt              } |       }t        |      }t	        j
                  t              5  |j                  |||       ddd       y# 1 sw Y   yxY w)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                        e Zd Z fdZ xZS )Ptest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeightc                 2    d|vsJ t        |   ||fi |S )NrI   superrT   )r   r8   r9   r  r  s       r:   rT   zTtest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeight.fit  s'    "*4447;q!2z22r<   r   r   r   rT   __classcell__r  s   @r:   ClfWithoutSampleWeightr    s    	3 	3r<   r  rH   N)rO   r  r&   r   rV   warnsUserWarningrT   )r;   r8   r9   rI   r  r^   r  s          r:   0test_calibration_without_sample_weight_estimatorr    sn     DAqLLOM3!3 3
 !
"C#C(F	k	" 6

1a}
56 6 6s   A33A<c                    t        d      \  }}t               j                  |      }t        j                  |dd |dd f      }t        j
                  |dd |dd f      }t        j                  |      }d|ddd<   t               }t        || |d	      }t        |      }|j                  |||
       |j                  |ddd   |ddd          t        |j                  |j                        D ]9  \  }}	t        |j                  j                  |	j                  j                         ; |j!                  |      }
|j!                  |      }t        |
|       y)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   rJ   rC   r  rH   )r   r#   r  rO   r   hstack
zeros_liker   r   r   rT   r  rh   r   ri   r  rU   )r>   rA   r8   r9   rI   ri   r  r  r  r  r  r  s               r:   >test_calibrated_classifier_cv_zeros_sample_weights_equivalencer    sj   
 %DAq&&q)A 			1Sb61R8$%A
		1Sb61R8$%AMM!$MM#A#"$I%;	&" #((F"G##Aq#F"&&q1vq1v6 25#;;&>>2 
-- 	&&,,))//	
	
 6CCAF;II!L')?@r<   c           
           G d dt               } t         |             j                  | dt        j                  t        | d         dz         i y)z[Check that CalibratedClassifierCV does not enforce sample alignment
    for fit parameters.c                         e Zd Zd fd	Z xZS )Jtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifierc                 0    |J t         |   |||      S )NrH   r  )r   r8   r9   rI   	fit_paramr  s        r:   rT   zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit  s$    (((7;q!=;AAr<   )NNr  r  s   @r:   TestClassifierr    s    	B 	Br<   r  )ri   r  rJ   N)r   r   rT   rO   r   rp   )r;   r  s     r:   2test_calibration_with_non_sample_aligned_fit_paramr    sM    B+ B
 ;^%56::	T!W!12r<   c           	         d}d}t         j                  j                  |       j                  |      }t        j                  dgt        ||z        z  dg|t        ||z        z
  z  z         }d|j                  d      z  |z   }t        d|d	
      }|j                  ||      }|D ]Y  \  }}	||   ||   }}
||	   }t        d|       }|j                  |
|       |j                  |      }|dkD  j                         rYJ  t        t        d|       d      }t        |||d      }t        t        d|       d      }t        |||d      }t        ||       y)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  rF   rJ   r   g     j@)r   rJ   NT)rL   r9   r  squared_hinge)lossr6   g     @r?   )r>   roc_auc)scoringr@   )rO   rP   default_rngnormalr   intr  r   splitr   rT   r   anyr   r   r   )global_random_seedprobnrandom_noiser9   r8   rL   indicestraintestrY   rZ   r\   sgd_clfpredictionsclf_sigmoidscore_sigmoidclf_isotonicscore_isotonics                      r:   @test_calibrated_classifier_cv_works_with_large_confidence_scoresr    sq    DA99(();<CCCKL
!s1t8}$sa#a$h-.?'@@AAaii  </A 
TQ4	0Bhhq!nG )tU8QuX4_CUVGW%//7c!&&((() )?9KLK $KAyIM *?9KLL %\1aKN M>2r<   c                 x   t         j                  j                  |       }d}|j                  dd|      }|j	                  ddd      }d}t        |||	      \  }}d
}t        |||	      \  }	}
t        ||      \  }}d}t        ||	|       t        |	||       t        ||
|       t        |
||       y )NrD   r   r   rC   rF   )lowhighrG   ry   )r  r9   max_abs_prediction_thresholdr   )r  r9   gư>)atol)rO   rP   rQ   r  rR   r	   r   )r  r6   r  r9   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<   c                 d     G d dt               } |       }t        |      } |j                  |   y)zoCheck that CalibratedClassifierCV works with float32 predict proba.

    Non-regression test for gh-28245.
    c                        e Zd Z fdZ xZS )4test_float32_predict_proba.<locals>.DummyClassifer32c                 \    t         |   |      j                  t        j                        S r  )r  rU   astyperO   float32)r   r8   r  s     r:   rU   zBtest_float32_predict_proba.<locals>.DummyClassifer32.predict_proba<  s"    7(+222::>>r<   )r   r   r   rU   r  r  s   @r:   DummyClassifer32r  ;  s    	? 	?r<   r  N)r   r   rT   )r;   r  modelr   s       r:   test_float32_predict_probar  5  s0    ?? ? E'.JJNNDr<   )ynumpyrO   rV   numpy.testingr   sklearn.baser   r   sklearn.calibrationr   r   r   r	   r
   r   sklearn.datasetsr   r   r   sklearn.dummyr   sklearn.ensembler   r   sklearn.exceptionsr   sklearn.feature_extractionr   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*   sklearn.utils.extmathr+   sklearn.utils.fixesr,   r7   fixturer;   markparametrizere   rm   rt   r   r   r8  r   r   r   r   r   r   r   r   r
  rP   rQ   r  r  r   r%  r)  paramr-  r2  r;  r?  rA  r]  r_  rk  rn  rx  r  r9  objectr  r  r  r  r  r   r  r  r  r  r  r  r  r   r<   r:   <module>r     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, :,^ Iz#:;5 <50>"C> dE]3 4 dE]3J 4J dE]3: 4:4 
		b!''Aq1
		b!''Aq!4, " " % %4 YFa0!4YFa0(;66"	#  h&  & h  
 Ar7+i%<=&; > ,&;R; -/CD;; ;( +.>@R-ST; U;D
5 f6" 7" f6/ 7/( 8:UV; W;@ Iz#:;dE]3-A 4 <-A` *VW,=># ?#$ 			226, Iz#:;dE]3'A 4 <'AT/3d&'Rr<   