
    -i[              
          S SK rS SKrS SKJr  S SKJr  S SKJrJ	r	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JrJrJr  S S
KJr  S SKJr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*  S SK+J,r,J-r-J.r.  S r/S r0\" SS9S 5       r1\Rd                  Rg                  SSS/5      \" SS9S 5       5       r4\Rd                  Rg                  S\
" SSS S9\" S S 9/5      S! 5       r5\Rd                  Rg                  S"S#SS$.\6S%4S&S'S$.\6S(4S#S'S$.\S)4/5      S* 5       r7\Rd                  Rg                  S+\" 5       \&" 5       \" S,S-9/5      \Rd                  Rg                  S./ S/Q5      \Rd                  Rg                  S0\\/5      S1 5       5       5       r8\Rd                  Rg                  S./ S2Q5      S3 5       r9S4 r:\Rd                  Rg                  S./ S2Q5      \Rd                  Rg                  S5\" \5      \" \S6S79/5      S8 5       5       r;\Rd                  Rg                  S9SS'/5      \" SS9S: 5       5       r<\Rd                  Rg                  SSS/5      \" SS9S; 5       5       r=\" SS9S< 5       r>S= r?\Rd                  Rg                  S>SS'/5      S? 5       r@S@ rASA rB\Rd                  Rg                  S./ SBQ5      SC 5       rC\Rd                  Rg                  SDSESF/5      \Rd                  Rg                  SGS S/5      SH 5       5       rD\" SS9SI 5       rE\Rd                  Rg                  SJ/ SKQ5      SL 5       rFSM rGg)N    N)config_context)clone)load_breast_cancer	load_irismake_classificationmake_multilabel_classification)DummyClassifier)GradientBoostingClassifier)NotFittedError)LogisticRegression)balanced_accuracy_scoref1_scorefbeta_scoremake_scorer)_CurveScorer)FixedThresholdClassifierStratifiedShuffleSplitTunedThresholdClassifierCV)_fit_and_score_over_thresholds)make_pipeline)StandardScaler)SVC)DecisionTreeClassifier)CheckingClassifier)_convert_containerassert_allcloseassert_array_equalc                     [        SSS9u  p[        R                  " S5      [        R                  " SS5      p2[        5       n[	        [
        SSS0 S9n[        UU U0 UUU0 S	9u  pg[        R                  " US
S USS
 :*  5      (       d   e[        U[        R                  5      (       d   e[        R                  " US:  US:*  5      R                  5       (       d   eg
)z{Check that `_fit_and_score_over_thresholds` returns thresholds in ascending order
for the different accepted curve scorers.d   r   	n_samplesrandom_state2      predict_proba
   
score_funcsignresponse_method
thresholdskwargs
fit_params	train_idxval_idxcurve_scorerscore_paramsN)r   nparanger   r   r   r   all
isinstancendarraylogical_andXyr/   r0   
classifierr1   scoresr+   s           n/var/www/html/venv/lib/python3.13/site-packages/sklearn/model_selection/tests/test_classification_threshold.py0test_fit_and_score_over_thresholds_curve_scorersr@   +   s     1=DA2		"c(:w#%J*'L 8		!	F 66*Sb/Z^34444fbjj))))>>&A+v{3779999    c                     [        SSS9u  pS[        R                  " SS5      p2[        SS9R	                  X5      nUR                  X   X   5      [        R                  " S5      :X  d   e[        [        SS	S
0 S9n[        UU U0 UUU0 S9u  pg[        R                  " USS USS :*  5      (       d   e[        USS/5        g)z-Check the behaviour with a prefit classifier.r   r   r    Nr#   r"   g      ?r$   r%      r'   r-   r3         ?)r   r4   r5   r   fitscorepytestapproxr   r   r   r6   r   r:   s           r?   )test_fit_and_score_over_thresholds_prefitrJ   I   s    1=DA ryyS1w'Q7;;AAJ AJ
3v}}S7IIII*'L 8		!	F 66*Sb/Z^34444FS#J'rA   T)enable_metadata_routingc                     [        SS9u  pU SS USS p[        R                  " X US:H     /5      [        R                  " XUS:H     /5      p2[        R                  " U5      nUSS=== S-  sss& [        5       n[        R                  " UR                  S   5      n[        R                  " UR                  S   5      n[        [        SS	S
0 S9n[        UUU0 UUU0 S9u  p[        R                  " U R                  S   5      [        R                  " U R                  S   5      p[        UR                  SS9U USU0UUUR                  SS9SU0S9u  p[        X5        [        X5        g)zICheck that we dispatch the sample-weight to fit and score the classifier.T
return_X_yNr   r   r#   rD   r$   r%   r&   r'   r-   sample_weightrP   )r   r4   vstackhstack	ones_liker   r5   shaper   r   r   set_fit_requestset_score_requestr   )r;   r<   
X_repeated
y_repeatedrP   r=   train_repeated_idxval_repeated_idxr1   scores_repeatedthresholds_repeatedr/   r0   r>   r+   s                  r?   0test_fit_and_score_over_thresholds_sample_weightr]   i   sw    %DATc7AdsGq  YYQ!V9~6		1Qi.8Q
LLOM#2!#%J:#3#3A#67yy!1!1!!45*'L ,J$ !	,(O 1771:.		!''!*0Ew7"""6		#]3!33$3G%}5	F '4O,rA   fit_params_typelistarrayc                    [        SSS9u  p[        X 5      [        X 5      S.n[        SS/SS9nUR                  SSS9  [        R
                  " S	5      [        R
                  " S	S5      pe[        [        S
SS0 S9n[        UUUUUUU0 S9  g)ECheck that we pass `fit_params` to the classifier when calling `fit`.r   r   r    abrd   re   expected_fit_paramsr"   Tr#   r$   r%   r&   r'   r-   N)	r   r   r   rU   r4   r5   r   r   r   )r^   r;   r<   r.   r=   r/   r0   r1   s           r?   -test_fit_and_score_over_thresholds_fit_paramsrh      s     1=DA33J
 $c
QRSJ.2		"c(:w*'L #		!	rA   data   r$   )	n_classesn_clusters_per_classr"   rC   c                     Sn[         R                  " [        US9   [        [	        5       5      R
                  " U 6   SSS5        g! , (       d  f       g= f)zHCheck that we raise an informative error message for non-binary problem.z(Only binary classification is supported.matchN)rH   raises
ValueErrorr   r   rF   )ri   err_msgs     r?   )test_tuned_threshold_classifier_no_binaryrs      s:     9G	z	1"#5#78<<dC 
2	1	1s   !A
Azparams, err_type, err_msgprefitcvrefitz'When cv='prefit', refit cannot be True.r&   Fz1When cv has several folds, refit cannot be False.z`estimator` must be fitted.c                     [        SSS9u  p4[        R                  " XS9   [        [	        5       40 U D6R                  X45        SSS5        g! , (       d  f       g= f)z`Check that we raise an informative error message when `cv` and `refit`
cannot be used together.
r   r   r    rn   N)r   rH   rp   r   r   rF   )paramserr_typerr   r;   r<   s        r?   1test_tuned_threshold_classifier_conflict_cv_refitr{      sF    0 1=DA	x	/"#5#7B6BFFqL 
0	/	/s   $A
A	estimator   )n_estimatorsr*   r%   predict_log_probadecision_functionThresholdClassifierc                 D   [        SSS9u  p4U " US9n[        XR5      [        X5      :X  d   eUR                  X45        [        XR5      [        X5      :X  d   e[        XR5      (       a9  [        XR5      " U5      n[        UR                  U5      " U5      n[        Xg5        gg)zgCheck that `TunedThresholdClassifierCV` exposes the same response methods as the
underlying estimator.
r   r   r    r|   N)r   hasattrrF   getattr
estimator_r   )r   r|   r*   r;   r<   modely_pred_cutoffy_pred_underlying_estimators           r?   4test_threshold_classifier_estimator_response_methodsr      s      1=DA)4E5*gi.QQQQ	IIaO5*gi.QQQQu&&7:&-e.>.>&PQR&S#C	 'rA   )autor   r%   c                    [        SS9u  pUSS2SS24   n[        R                  " US:H  5      nUSUR                  S-   n[        R                  " US:H  5      n[        R                  " X   X   /5      n[        R
                  " X$   X#   /5      n[        [        5       [        5       5      R                  X5      nSn[        US	U USS
9n[        X'R                  X5      R                  U5      5      n[        X%R                  U5      5      n	X:  d   eUR                  S   R                  U4:X  d   eUR                  S   R                  U4:X  d   eg)zOCheck that `TunedThresholdClassifierCV` is optimizing a given objective
metric.TrM   N   r$   r#   r   r   balanced_accuracy)r|   scoringr*   r+   store_cv_resultsr+   r>   )r   r4   flatnonzerosizerQ   rR   r   r   r   rF   r   r   predictcv_results_rT   )
r*   r;   r<   indices_posindices_neglrr+   r   score_optimizedscore_baselines
             r?   8test_tuned_threshold_classifier_without_constraint_valuer     sH    .DA	!RaR%A ..a(K6 0 0B 67K..a(K
		1>1>23A
		1>1>23A	~');)=	>	B	B1	HBJ&#'E .a11H1H1KLO,Q

1>N+++\*00ZMAAAX&,,===rA   c                     [        SS9u  p[        [        5       [        5       5      R	                  X5      n[        U[        [        SS9S9R	                  X5      n[        U[        [        SS9S9R	                  X5      n[        U[        [        5      S9R	                  X5      nUR                  [        R                  " UR                  5      :X  d   eUR                  [        R                  " UR                  5      :w  d   eg)zCheck that we can pass a metric with a parameter in addition check that
`f_beta` with `beta=1` is equivalent to `f1` and different from `f_beta` with
`beta=2`.
TrM   r$   )beta)r|   r   rD   N)r   r   r   r   rF   r   r   r   r   best_threshold_rH   rI   )r;   r<   r   model_fbeta_1model_fbeta_2model_f1s         r?   5test_tuned_threshold_classifier_metric_with_parameterr   '  s    
 .DA	~');)=	>	B	B1	HB.k+A>	c!i  /k+A>	c!i  *k(3	c!i  ((FMM(:R:R,SSSS((FMM-:W:W,XXXXrA   metriccancer)	pos_labelc                    [        SS9u  p#[        R                  " SS/[        S9nXC   n[	        [        [        5       [        5       5      UU SS9R                  X#5      n[        UR                  [        R                  " U5      5        UR                  U5      n[        [        R                  " U5      [        R                  " U5      5        g)	zCheck that targets represented by str are properly managed.
Also, check with several metrics to be sure that `pos_label` is properly
dispatched.
TrM   r   healthy)dtyper   )r|   r   r*   r+   N)r   r4   r`   objectr   r   r   r   rF   r   classes_sortr   unique)r*   r   r;   r<   classesr   y_preds          r?   3test_tuned_threshold_classifier_with_string_targetsr   <  s     .DA hh),F;G
A& 02D2FG'	
 
c!i 
 u~~rwww'78]]1Fryy("'''*:;rA   with_sample_weightc                    [         R                  R                  U5      n[        SSS9u  p4U (       a3  UR	                  UR
                  S   5      n[         R                  " XUS9nOSn[        5       R                  SS9n[        USS9R                  X4US9nUR                  ULd   eUR                  X4US9  [        UR                  R                  UR                  5        [        UR                  R                  UR                  5        [        5       R                  SS9nUR                  X4US9  UR                  R                  5       n[        US	S
S9R                  X4US9nUR                  UL d   e[        UR                  R                  U5        [        5       R                  SS9n[         R                   " S5      [         R                   " SS5      4/n	[        XiS
S9R                  X4US9nUR                  ULd   eU (       a  XYS   S      n
OSn
UR                  X9S   S      XIS   S      U
S9  [        UR                  R                  UR                  5        g)z-Check the behaviour of the `refit` parameter.r   r   r    )outNTrO   rw   rt   Fru   r#   )r4   randomRandomStater   randnrT   absr   rU   r   rF   r   r   coef_
intercept_copyr5   )r   global_random_seedrngr;   r<   rP   r|   r   coefrv   sw_trains              r?   %test_tuned_threshold_classifier_refitr   \  s?    ))

 2
3C1=DA		!''!*-}@ #$4444HI&y=AA	M B E 9,,,MM!mM4E$$**IOO<E$$//1E1EF #$4444HIMM!mM4??!D&yXUKOO	M P E y(((E$$**D1 #$4444HI	2		"c*+
B 'yuEII	M J E 9,,, Aq*MM!qE!H+qAq{(MCE$$**IOO<rA   c                     [        SSS9u  p[        X 5      [        X 5      S.n[        SS/SS9nUR                  SSS9  [	        U5      nUR
                  " X40 UD6  g	)
rb   r   r   r    rc   rd   re   rf   TN)r   r   r   rU   r   rF   )r^   r;   r<   r.   r=   r   s         r?   *test_tuned_threshold_classifier_fit_paramsr     sj     1=DA33J
 $c
QRSJ.&z2E	IIa!j!rA   c                  v   [        SS9u  p[        5       R                  U 5      n [        R                  " U SS U SS 45      n [        R
                  " USS USS 45      n[        R                  " U5      nSUSSS2'   [        5       R                  SS	9n[        USS
9n[        U5      nUR                  XUS	9  UR                  U SSS2   USSS2   5        [        UR                  R                  UR                  R                  5        UR                  U 5      nUR                  U 5      n[        Xg5        g)zxCheck that passing removing some sample from the dataset `X` is
equivalent to passing a `sample_weight` with a factor 0.TrM   N(   r#   Z   r$   rD   rO   )rv   )r   r   fit_transformr4   rQ   rR   
zeros_liker   rU   r   r   rF   r   r   r   r%   )r;   r<   rP   r|   model_without_weightsmodel_with_weightsy_pred_with_weightsy_pred_without_weightss           r?   Ctest_tuned_threshold_classifier_cv_zeros_sample_weights_equivalencer     s4    %DA&&q)A 			1Sb61R8$%A
		1Sb61R8$%AMM!$MM#A#"$4444HI6yQG451}=a!fa!f-%%++-B-M-M-S-S -::1=2@@C'@rA   c                      [        SS9u  p[        5       n[        R                  " SSS5      n[	        UUSSS9R                  X5      n[        UR                  S   U5        g	)
zaCheck that we can pass an array to `thresholds` and it is used as candidate
threshold internally.r   rC   r$      r%   T)r+   r*   r   r+   N)r   r   r4   linspacer   rF   r   r   )r;   r<   r|   r+   tuned_models        r?   0test_tuned_threshold_classifier_thresholds_arrayr     se     A.DA"$IQ2&J,'	
 
c!i  K++L9:FrA   r   c                     [        SS9u  p[        5       n[        X0S9R                  X5      nU (       a  [	        US5      (       d   eg[	        US5      (       a   eg)zCCheck that if `cv_results_` exists depending on `store_cv_results`.r   rC   )r   r   N)r   r   r   rF   r   )r   r;   r<   r|   r   s        r?   0test_tuned_threshold_classifier_store_cv_resultsr     s_     A.DA"$I,	c!i  {M2222;66666rA   c                  ,   [        SS9u  pSn[        5       n[        X2SSS9R                  X5      nUR                  X5        [	        SUSS9n[        UR                  X5      5      u  pg[        U5      R                  X   X   5      n[        UR                  R                  UR                  5        UR                  SS	9R                  X5        [        U5      R                  X5      n[        UR                  R                  UR                  5        g
)z0Check the behaviour when `cv` is set to a float.r   rC   g333333?F)rv   rw   r"   r$   )n_splits	test_sizer"   Tr   N)r   r   r   rF   r   nextsplitr   r   r   r   
set_params)	r;   r<   r   r|   r   rv   r/   r0   cloned_estimators	            r?   (test_tuned_threshold_classifier_cv_floatr     s    A.DA
 I"$I,u1	c!i  OOA	ia	PBbhhqn-IY'++AL!,GK**002B2H2HI &**10Y'++A1K**002B2H2HIrA   c                      [        SS9u  p[        SSS9n[        USS9nSn[        R                  " [
        US	9   UR                  X5        S
S
S
5        g
! , (       d  f       g
= f)zCheck that we raise a ValueError if the underlying classifier returns constant
probabilities such that we cannot find any threshold.
r   rC   constantr$   )strategyr   r%   )r*   z1The provided estimator makes constant predictionsrn   N)r   r	   r   rH   rp   rq   rF   )r;   r<   r|   r   rr   s        r?   8test_tuned_threshold_classifier_error_constant_predictorr     sS     A.DAa@I,YXKAG	z	1 
2	1	1s   A
A$)r   r%   r   c                 `   [        SS9u  p[        5       R                  X5      n[        [	        U5      U S9nUR                  X5        U S;   a  UR                  U5      SS2S4   nSnOUR                  U5      nSnXV:  R                  [        5      n[        UR                  U5      U5        g)	zXCheck that `FixedThresholdClassifier` has the same behaviour as the vanilla
classifier.
r   rC   )r|   r*   )r   r%   Nr$   rE   g        )r   r   rF   r   r   r%   r   astypeintr   r   )r*   r;   r<   r=   classifier_default_thresholdy_score	threshold	y_pred_lrs           r?   3test_fixed_threshold_classifier_equivalence_defaultr     s     A.DA#%))!/J#;
#_$  !$$Q* 33.<<Q?1E	.@@C	%--c2I088;YGrA   zresponse_method, threshold)r%   gffffff?)r   g       @r   c                    [        SSS9u  p4[        5       R                  X45      n[        [	        U5      UU US9R                  X45      n[        UR                  R                  UR                  5        U S:X  a  UR                  U5      SS2U4   nOUR                  U5      nUS:X  a  UOU* nUS:X  a  [        R                  " SS/5      O[        R                  " SS/5      nXU:  R                  [        5         n	[        UR                  U5      U	5        S Hb  n
[        [        Xj5      " U5      [        XZ5      " U5      5        [        [        UR                  U
5      " U5      [        XZ5      " U5      5        Md     g)	zzCheck that applying `predict` lead to the same prediction as applying the
threshold to the output of the response method.
r#   r   r    )r|   r   r*   r   r%   Nr$   r   )r   r   rF   r   r   r   r   r   r%   r   r4   r`   r   r   r   r   )r*   r   r   r;   r<   logistic_regressionr   r   map_to_labelr   methods              r?   test_fixed_threshold_classifierr     s_    !<DA,.2218$+,'	
 
c!i 
 E$$**,?,E,EF /)%%a(I6))!,&!^'' (1A~288QF#288QF;KL2::3?@IEMM!$i0ME"1%w/B'KA'N	
 	E$$f-a0'03	
	 NrA   c                  <   [        SS9u  p[        R                  " U5      nSUSSS2'   [        5       R	                  SS9nUR                  XUS9  [        [        U5      S9nUR                  XUS9  [        UR                  R                  UR                  5        g)z2Check that everything works with metadata routing.r   rC   rD   NTrO   r   )r   r4   rS   r   rU   rF   r   r   r   r   r   )r;   r<   rP   r=   r   s        r?   0test_fixed_threshold_classifier_metadata_routingr   G  s     A.DALLOMM#A##%55D5IJNN1}N5#;eJFW#X  $$Q$G0;;AA:CSCSTrA   r   )r%   r   r   r   c                     [        SS9u  p[        5       R                  X5      n[        US9n[	        X@5      " U5        g)zMCheck that if the underlying estimator is already fitted, no fit is required.r   rC   r   N)r   r   rF   r   r   )r   r;   r<   r=   fixed_threshold_classifiers        r?   0test_fixed_threshold_classifier_fitted_estimatorr   T  s<    
 A.DA#%))!/J!9J!O&/2rA   c                  :   [        SS9u  p[        R                  " [        SS9   [	        [        5       S9R                    SSS5        [        5       R                  X5      n[	        US9n[        UR                  UR                  5        g! , (       d  f       NQ= f)z2Check that the classes_ attribute is properly set.r   rC   z+The underlying estimator is not fitted yet.rn   r   N)	r   rH   rp   AttributeErrorr   r   r   rF   r   )r;   r<   r=   r   s       r?   (test_fixed_threshold_classifier_classes_r   `  s{    A.DA	K
 	!+=+?@II

 $%))!/J!9J!O1::J<O<OP
 
s   B
B)Hnumpyr4   rH   sklearnr   sklearn.baser   sklearn.datasetsr   r   r   r   sklearn.dummyr	   sklearn.ensembler
   sklearn.exceptionsr   sklearn.linear_modelr   sklearn.metricsr   r   r   r   sklearn.metrics._scorerr   sklearn.model_selectionr   r   r   1sklearn.model_selection._classification_thresholdr   sklearn.pipeliner   sklearn.preprocessingr   sklearn.svmr   sklearn.treer   sklearn.utils._mockingr   sklearn.utils._testingr   r   r   r@   rJ   r]   markparametrizerh   rs   rq   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    rA   r?   <module>r	     s     "   * 7 - 3  1 
 + 0  / 5 :<(@ --- .--` *VW,=>- . ?> 
aaaP&A6DD  d+5	
 &?	
 e,)	
(M)(M 35"<!"LM R 46PQD	D* E>>>Y* E +,H1<<, -e}=-/= . >/=d *VW,=>-" . ?" -A .A:G +dE];
7 <
7J8	 EHH.  #9;U"V q!f-#
 .#
L -	U .	U T33
QrA   