
    sg[              	          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 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 Z/d Z0 ed      d        Z1ejd                  jg                  dddg       ed      d               Z4ejd                  jg                  d e
ddd        ed        g      d!        Z5ejd                  jg                  d"d#dd$e6d%fd&d'd$e6d(fd#d'd$ed)fg      d*        Z7ejd                  jg                  d+ e        e&        ed,-      g      ejd                  jg                  d.g d/      ejd                  jg                  d0eeg      d1                      Z8ejd                  jg                  d.g d2      d3        Z9d4 Z:ejd                  jg                  d.g d2      ejd                  jg                  d5 ee       eed67      g      d8               Z;ejd                  jg                  d9dd'g       ed      d:               Z<ejd                  jg                  dddg       ed      d;               Z= ed      d<        Z>d= Z?ejd                  jg                  d>dd'g      d?        Z@d@ ZAdA ZBejd                  jg                  d.g dB      dC        ZCejd                  jg                  dDdEdFg      ejd                  jg                  dGd dg      dH               ZD ed      dI        ZEejd                  jg                  dJg dK      dL        ZFdM ZGy)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            
         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yr/   r0   
classifierr1   scoresr+   s           n/var/www/html/venv/lib/python3.12/site-packages/sklearn/model_selection/tests/test_classification_threshold.py0test_fit_and_score_over_thresholds_curve_scorersr@   +   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-   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=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'rA   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#   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   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,rA   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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=DAq?3?3J
 $c
QRSJ.2		"c(:wI*'L #		!	rA   data   r$   )	n_classesn_clusters_per_classr"   rC   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)rH   raises
ValueErrorr   r   rF   )ri   err_msgs     r?   )test_tuned_threshold_classifier_no_binaryrs      sJ     9G	z	1 D<"#5#78<<dCD D Ds   !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    rn   N)r   rH   rp   r   r   rF   )paramserr_typerr   r;   r<   s        r?   1test_tuned_threshold_classifier_conflict_cv_refitr{      sY    0 1=DAq	xw	/ M"#5#7B6BFFq!LM M M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   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=DAq)4E5/*gi.QQQQ	IIaO5/*gi.QQQQuo&77:&Pge.>.>&PQR&S#'BC	 'rA   )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.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     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&,,===rA   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`.
    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    
 .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XrA   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.
    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     .DAq hh),F;G
A& 02D2FG'	
 
c!Qi 
 u~~rwww'78]]1Fryy("'''*:;rA   with_sample_weightc                    t         j                  j                  |      }t        dd      \  }}| r6|j	                  |j
                  d         }t        j                  ||      }nd}t               j                  d      }t        |d      j                  |||      }|j                  |usJ |j                  |||       t        |j                  j                  |j                         t        |j                  j                  |j                         t               j                  d      }|j                  |||       |j                  j                         }t        |d	d
      j                  |||      }|j                  |u sJ t        |j                  j                  |       t               j                  d      }t        j                   d      t        j                   dd      fg}	t        ||	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    )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   \  sU    ))

 2
3C1=DAq		!''!*-}-@ #$4444HI&y=AA	1M B E 9,,,MM!QmM4E$$**IOO<E$$//1E1EF #$4444HIMM!QmM4??!D&yXUKOO	1M P E y(((E$$**D1 #$4444HI	2		"c*+
B 'yRuEII	1M J E 9,,, Aq*MM!BqE!H+qAq{(MCE$$**IOO<rA   c                     t        dd      \  }}t        ||       t        ||       d}t        ddgd      }|j                  dd       t	        |      } |j
                  ||fi | y	)
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     st     1=DAq?3?3J
 $c
QRSJ.&z2EEIIa!j!rA   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	      }t        |d
      }t        |      }|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.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     s;    %DAq&&q)A 			1Sb61R8$%A
		1Sb61R8$%AMM!$MM#A#"$4444HI6yQG451a}=a!fa!f-%%++-B-M-M-S-S -::1=2@@C')?@rA   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   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     si     A.DAq"$IQ2&J,'	
 
c!Qi  K++L9:FrA   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   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.DAq"$I,$4	c!Qi  {M222;6666rA   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   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.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IrA   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   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     s`     A.DAqa@I,YXKAG	z	1 1  s   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   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.DAq#%))!Q/J#;
#_$  !$$Q* 33.<<Q?1E	.@@C	I%--c2I088;YGrA   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   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     sz    !<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	
	
rA   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   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.DAqLLOMM#A##%55D5IJNN1a}N5#;eJFW#X  $$Q$G0;;AA:CSCSTrA   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   rC   r   N)r   r   rF   r   r   )r   r;   r<   r=   fixed_threshold_classifiers        r?   0test_fixed_threshold_classifier_fitted_estimatorr   T  sD    
 A.DAq#%))!Q/J!9J!O/G&/2rA   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   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.DAq	K
 J 	!+=+?@IIJ
 $%))!Q/J!9J!O1::J<O<OPJ J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   