
    Q3j]              	          d dl Zd dlZd dlmZ d dlmZ d dlmZm	Z	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mZmZmZ d d
lmZ d dlmZ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) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/ d Z0d Z1 ed      d        Z2ejf                  ji                  dddg       ed      d               Z5ejf                  ji                  d e
ddd        ed        g      d!        Z6ejf                  ji                  d"d#dd$e7d%fd&d'd$e7d(fd#d'd$ed)fg      d*        Z8ejf                  ji                  d+ e        e'        ed,-      g      ejf                  ji                  d.g d/      ejf                  ji                  d0eeg      d1                      Z9ejf                  ji                  d.g d2      d3        Z:d4 Z;ejf                  ji                  d.g d2      ejf                  ji                  d5 ee       eed67      g      d8               Z<ejf                  ji                  d9dd'g       ed      d:               Z=ejf                  ji                  dddg       ed      d;               Z> ed      d<        Z?d= Z@ejf                  ji                  d>dd'g      d?        ZAd@ ZBdA ZCejf                  ji                  d.g dB      dC        ZDejf                  ji                  dDdEdFg      ejf                  ji                  dGd dg      dH               ZE ed      dI        ZFejf                  ji                  dJg dK      dL        ZGdM ZHy)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
get_scorermake_scorer)_CurveScorer)FixedThresholdClassifierStratifiedShuffleSplitTunedThresholdClassifierCV)_fit_and_score_over_thresholds)make_pipeline)StandardScaler)SVC)DecisionTreeClassifier)CheckingClassifier)_convert_containerassert_allcloseassert_array_equalc            
         t        dd      \  } }t        j                  d      t        j                  dd      }}t               }t	        t
        dddi       }t        || |i |||i 	      \  }}t        j                  |d
d |dd
 k        sJ t        |t        j                        sJ t        j                  |dk\  |dk        j                         sJ y
)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yr0   r1   
classifierr2   scoresr,   s           h/DATA/.local/lib/python3.12/site-packages/sklearn/model_selection/tests/test_classification_threshold.py0test_fit_and_score_over_thresholds_curve_scorersrA   ,   s     1=DAq2		"c(:wI#%J*'L 8		!	FJ 66*Sb/Z^3444fbjj)))>>&A+v{377999    c            
         t        dd      \  } }dt        j                  dd      }}t        d      j	                  | |      }|j                  | |   ||         t        j                  d      k(  sJ t        t        dd	d
i       }t        || |i |||i       \  }}t        j                  |dd |dd k        sJ t        |ddg       y)z-Check the behaviour with a prefit classifier.r    r   r!   Nr$   r#   g      ?r%   r&      r(   r.   r4         ?)r   r5   r6   r   fitscorepytestapproxr   r   r   r7   r   r;   s           r@   )test_fit_and_score_over_thresholds_prefitrK   J   s    1=DAq ryyS1wI'Q7;;AqAJ AgJ'
3v}}S7IIII*'L 8		!	FJ 66*Sb/Z^3444FS#J'rB   T)enable_metadata_routingc                     t        d      \  } }| dd |dd }} t        j                  | | |dk(     g      t        j                  |||dk(     g      }}t        j                  |      }|ddxxx dz  ccc t               }t        j                  |j                  d         }t        j                  |j                  d         }t        t        dd	d
i       }t        |||i |||i       \  }	}
t        j                  | j                  d         t        j                  | j                  d         }}t        |j                  d      | |d|i|||j                  d      d|i      \  }}t        |
|       t        |	|       y)zICheck that we dispatch the sample-weight to fit and score the classifier.T
return_X_yNr    r   r$   rE   r%   r&   r'   r(   r.   sample_weightrQ   )r   r5   vstackhstack	ones_liker   r6   shaper   r   r   set_fit_requestset_score_requestr   )r<   r=   
X_repeated
y_repeatedrQ   r>   train_repeated_idxval_repeated_idxr2   scores_repeatedthresholds_repeatedr0   r1   r?   r,   s                  r@   0test_fit_and_score_over_thresholds_sample_weightr^   j   s    %DAqTc7AdsGqA  YY1Q!V9~6		1aQi.8Q
JLLOM#2!#%J:#3#3A#67yy!1!1!!45*'L ,J$ !	,(O( 1771:.		!''!*0EwI7"""6		#]3!33$3G%}5	FJ '4OV,rB   fit_params_typelistarrayc           
      >   t        dd      \  }}t        ||       t        ||       d}t        ddgd      }|j                  dd       t	        j
                  d	      t	        j
                  d	d      }}t        t        d
ddi       }t        |||||||i        y)ECheck that we pass `fit_params` to the classifier when calling `fit`.r    r   r!   abre   rf   expected_fit_paramsr#   Tr$   r%   r&   r'   r(   r.   N)	r   r   r   rV   r5   r6   r   r   r   )r_   r<   r=   r/   r>   r0   r1   r2   s           r@   -test_fit_and_score_over_thresholds_fit_paramsri      s     1=DAq?3?3J
 $c
QRSJ.2		"c(:wI*'L #		!	rB   data   r%   )	n_classesn_clusters_per_classr#   rD   c                     d}t        j                  t        |      5   t        t	                     j
                  |   ddd       y# 1 sw Y   yxY w)zHCheck that we raise an informative error message for non-binary problem.z(Only binary classification is supported.matchN)rI   raises
ValueErrorr   r   rG   )rj   err_msgs     r@   )test_tuned_threshold_classifier_no_binaryrt      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                     t        dd      \  }}t        j                  ||      5  t        t	               fi | j                  ||       ddd       y# 1 sw Y   yxY w)zhCheck that we raise an informative error message when `cv` and `refit`
    cannot be used together.
    r    r   r!   ro   N)r   rI   rq   r   r   rG   )paramserr_typers   r<   r=   s        r@   1test_tuned_threshold_classifier_conflict_cv_refitr{      sL    0 1=DAq	xw	/"#5#7B6BFFq!L 
0	/	/s   %AA	estimator   )n_estimatorsr+   r&   predict_log_probadecision_functionThresholdClassifierc                 T   t        dd      \  }} | |      }t        ||      t        ||      k(  sJ |j                  ||       t        ||      t        ||      k(  sJ t        ||      r; t        ||      |      } t        |j                  |      |      }t        ||       yy)zoCheck that `TunedThresholdClassifierCV` exposes the same response methods as the
    underlying estimator.
    r    r   r!   r|   N)r   hasattrrG   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=DAq)4E5/*gi.QQQQ	IIaO5/*gi.QQQQuo&77:&Pge.>.>&PQR&S#'BC	 'rB   )autor   r&   c                    t        d      \  }}|ddddf   }t        j                  |dk(        }|d|j                  dz   }t        j                  |dk(        }t        j                  ||   ||   g      }t        j
                  ||   ||   g      }t        t               t                     j                  ||      }d}t        |d	| |d
      }t        ||j                  ||      j                  |            }t        ||j                  |            }	||	kD  sJ |j                  d   j                  |fk(  sJ |j                  d   j                  |fk(  sJ y)zSCheck that `TunedThresholdClassifierCV` is optimizing a given objective
    metric.TrN   N   r%   r$   r   r    balanced_accuracy)r|   scoringr+   r,   store_cv_resultsr,   r?   )r   r5   flatnonzerosizerR   rS   r   r   r   rG   r   r   predictcv_results_rU   )
r+   r<   r=   indices_posindices_neglrr,   r   score_optimizedscore_baselines
             r@   8test_tuned_threshold_classifier_without_constraint_valuer     s\    .DAq	!RaR%A ..a(K6 0 0B 67K..a(K
		1[>1[>23A
		1[>1[>23A	~');)=	>	B	B1a	HBJ&#'E .a1a1H1H1KLO,Q

1>N^+++\*00ZMAAAX&,,===rB   c                  6   t        d      \  } }t        t               t                     j	                  | |      }t        |t        t        d            j	                  | |      }t        |t        t        d            j	                  | |      }t        |t        t                    j	                  | |      }|j                  t        j                  |j                        k(  sJ |j                  t        j                  |j                        k7  sJ y)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`.
    TrN   r%   )beta)r|   r   rE   N)r   r   r   r   rG   r   r   r   r   best_threshold_rI   rJ   )r<   r=   r   model_fbeta_1model_fbeta_2model_f1s         r@   5test_tuned_threshold_classifier_metric_with_parameterr   (  s    
 .DAq	~');)=	>	B	B1a	HB.k+A>	c!Qi  /k+A>	c!Qi  *k(3	c!Qi  ((FMM(:R:R,SSSS((FMM-:W:W,XXXXrB   metriccancer)	pos_labelc                    t        d      \  }}t        j                  ddgt              }||   }t	        t        t               t                     || d      j                  ||      }t        |j                  t        j                  |             |j                  |      }t        t        j                  |      t        j                  |             y)	zCheck that targets represented by str are properly managed.
    Also, check with several metrics to be sure that `pos_label` is properly
    dispatched.
    TrN   r   healthy)dtyper    )r|   r   r+   r,   N)r   r5   ra   objectr   r   r   r   rG   r   classes_sortr   unique)r+   r   r<   r=   classesr   y_preds          r@   3test_tuned_threshold_classifier_with_string_targetsr   =  s     .DAq hh),F;G
A& 02D2FG'	
 
c!Qi 
 u~~rwww'78]]1Fryy("'''*:;rB   with_sample_weightc                    t         j                  j                  |      }t        dd      \  }}| r6|j	                  |j
                  d         }t        j                  ||      }nd}t               j                  d      }t        |dt        d      j                  d      	      j                  |||      }|j                  |usJ |j                  |||       t        |j                  j                  |j                         t        |j                  j                   |j                          t               j                  d      }|j                  |||       |j                  j#                         }t        |d
dt        d      j                  d            j                  |||      }|j                  |u sJ t        |j                  j                  |       t               j                  d      }t        j$                  d      t        j$                  dd      fg}	t        ||	dt        d      j                  d            j                  |||      }|j                  |usJ | r||	d   d      }
nd}
|j                  ||	d   d      ||	d   d      |
       t        |j                  j                  |j                         y)z-Check the behaviour of the `refit` parameter.r    r   r!   )outNTrP   r   )rw   r   ru   F)rv   rw   r   r$   )r5   randomRandomStater   randnrU   absr   rV   r   r   rW   rG   r   r   coef_
intercept_copyr6   )r   global_random_seedrngr<   r=   rQ   r|   r   coefrv   sw_trains              r@   %test_tuned_threshold_classifier_refitr   ]  s    ))

 2
3C1=DAq		!''!*-}-@ #$4444HI&./AAPTAU 
c!Qmc,	 
 9,,,MM!QmM4E$$**IOO<E$$//1E1EF #$4444HIMM!QmM4??!D&./AAPTAU	
 
c!Qmc, 
 y(((E$$**D1 #$4444HI	2		"c*+
B './AAPTAU	
 
c!Qmc, 
 9,,, Aq*MM!BqE!H+qAq{(MCE$$**IOO<rB   c                     t        dd      \  }}t        ||       t        ||       d}t        ddgd      }|j                  dd       t	        |      } |j
                  ||fi | y	)
rc   r    r   r!   rd   re   rf   rg   TN)r   r   r   rV   r   rG   )r_   r<   r=   r/   r>   r   s         r@   *test_tuned_threshold_classifier_fit_paramsr     st     1=DAq?3?3J
 $c
QRSJ.&z2EEIIa!j!rB   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               j                  d	      j                  d	      }t        |d
      }t        |dt        d      j                  d	            }|j                  | ||	       |j                  | ddd   |ddd          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.TrN   N(   r$   Z   r%   rE   rP   )rv   r   )rv   r   )r   r   fit_transformr5   rR   rS   
zeros_liker   rV   rW   r   r   rG   r   r   r   r&   )r<   r=   rQ   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     sm    %DAq&&q)A 			1Sb61R8$%A
		1Sb61R8$%AMM!$MM#A# 		t	,				. 
 7yQG3./AAPTAU 1a}=a!fa!f-%%++-B-M-M-S-S -::1=2@@C')?@rB   c                      t        d      \  } }t               }t        j                  ddd      }t	        ||dd      j                  | |      }t        |j                  d   |       y	)
zeCheck that we can pass an array to `thresholds` and it is used as candidate
    threshold internally.r   rD   r%      r&   T)r,   r+   r   r,   N)r   r   r5   linspacer   rG   r   r   )r<   r=   r|   r,   tuned_models        r@   0test_tuned_threshold_classifier_thresholds_arrayr     si     A.DAq"$IQ2&J,'	
 
c!Qi  K++L9:FrB   r   c                     t        d      \  }}t               }t        ||       j                  ||      }| rt	        |d      sJ yt	        |d      rJ y)zCCheck that if `cv_results_` exists depending on `store_cv_results`.r   rD   )r   r   N)r   r   r   rG   r   )r   r<   r=   r|   r   s        r@   0test_tuned_threshold_classifier_store_cv_resultsr     s`     A.DAq"$I,$4	c!Qi  {M222;6666rB   c                  X   t        d      \  } }d}t               }t        ||dd      j                  | |      }|j                  | |       t	        d|d      }t        |j                  | |            \  }}t        |      j                  | |   ||         }t        |j                  j                  |j                         |j                  d	      j                  | |       t        |      j                  | |      }t        |j                  j                  |j                         y
)z0Check the behaviour when `cv` is set to a float.r   rD   g333333?F)rv   rw   r#   r%   )n_splits	test_sizer#   T)rw   N)r   r   r   rG   r   nextsplitr   r   r   r   
set_params)	r<   r=   r   r|   r   rv   r0   r1   cloned_estimators	            r@   (test_tuned_threshold_classifier_cv_floatr     s	   A.DAq
 I"$I,iu1	c!Qi  OOAq	ia	PBbhhq!n-IwY'++AiL!I,GK**002B2H2HI &**1a0Y'++Aq1K**002B2H2HIrB   c                      t        d      \  } }t        dd      }t        |d      }d}t        j                  t
        |	      5  |j                  | |       d
d
d
       y
# 1 sw Y   y
xY w)zCheck that we raise a ValueError if the underlying classifier returns constant
    probabilities such that we cannot find any threshold.
    r   rD   constantr%   )strategyr   r&   )r+   z1The provided estimator makes constant predictionsro   N)r   r	   r   rI   rq   rr   rG   )r<   r=   r|   r   rs   s        r@   8test_tuned_threshold_classifier_error_constant_predictorr     sW     A.DAqa@I,YXKAG	z	11 
2	1	1s   A##A,)r   r&   r   c                 p   t        d      \  }}t               j                  ||      }t        t	        |      |       }|j                  ||       | dv r|j                  |      dddf   }d}n|j                  |      }d}||k\  j                  t              }t        |j                  |      |       y)	z`Check that `FixedThresholdClassifier` has the same behaviour as the vanilla
    classifier.
    r   rD   )r|   r+   )r   r&   Nr%   rF   g        )r   r   rG   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.DAq#%))!Q/J#;
#_$  !$$Q* 33.<<Q?1E	.@@C	I%--c2I088;YGrB   zresponse_method, threshold)r&   gffffff?)r   g       @r   c           	         t        dd      \  }}t               j                  ||      }t        t	        |      || |      j                  ||      }t        |j                  j                  |j                         | dk(  r|j                  |      dd|f   }n|j                  |      }|dk(  r|n| }|dk(  rt        j                  ddg      nt        j                  ddg      }|||k\  j                  t                 }	t        |j                  |      |	       dD ]d  }
t         t        ||
      |       t        ||
      |             t         t        |j                  |
      |       t        ||
      |             f y)	zCheck 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   rG   r   r   r   r   r   r&   r   r5   ra   r   r   r   r   )r+   r   r   r<   r=   logistic_regressionr   r   map_to_labelr   methods              r@   test_fixed_threshold_classifierr   .  sv    !<DAq,.221a8$+,'	
 
c!Qi 
 E$$**,?,E,EF /)%%a(I6))!,&!^'' (1A~288QF#288QF;KLg2::3?@IEMM!$i0M"GE6"1%'Kw/BF'KA'N	
 	-GE$$f-a00G'03	
	 NrB   c                  ^   t        d      \  } }t        j                  |      }d|ddd<   t               j	                  d      }|j                  | ||       t        t        |            }|j                  | ||       t        |j                  j                  |j                         y)z2Check that everything works with metadata routing.r   rD   rE   NTrP   r   )r   r5   rT   r   rV   rG   r   r   r   r   r   )r<   r=   rQ   r>   r   s        r@   0test_fixed_threshold_classifier_metadata_routingr   X  s     A.DAqLLOMM#A##%55D5IJNN1a}N5#;eJFW#X  $$Q$G0;;AA:CSCSTrB   r   )r&   r   r   r   c                     t        d      \  }}t               j                  ||      }t        |      } t	        ||       |       y)zMCheck that if the underlying estimator is already fitted, no fit is required.r   rD   r   N)r   r   rG   r   r   )r   r<   r=   r>   fixed_threshold_classifiers        r@   0test_fixed_threshold_classifier_fitted_estimatorr   e  sD    
 A.DAq#%))!Q/J!9J!O/G&/2rB   c                  J   t        d      \  } }t        j                  t        d      5  t	        t                     j                   ddd       t               j                  | |      }t	        |      }t        |j                  |j                         y# 1 sw Y   PxY w)z2Check that the classes_ attribute is properly set.r   rD   z+The underlying estimator is not fitted yet.ro   r   N)	r   rI   rq   AttributeErrorr   r   r   rG   r   )r<   r=   r>   r   s       r@   (test_fixed_threshold_classifier_classes_r   q  s    A.DAq	K
 	!+=+?@II

 $%))!Q/J!9J!O1::J<O<OP
 
s   BB")Inumpyr5   rI   sklearnr   sklearn.baser   sklearn.datasetsr   r   r   r   sklearn.dummyr	   sklearn.ensembler
   sklearn.exceptionsr   sklearn.linear_modelr   sklearn.metricsr   r   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   rA   rK   r^   markparametrizeri   rt   rr   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    rB   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}=-7= . >7=t *VW,=>-" . ?" -"A ."AJG +dE];
7 <
7J8	 EHH.  #9;U"V q!f-#
 .#
L -	U .	U T33
QrB   