
    Pi[              	       v   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j2        3                    dddg           ed          d                         Z4ej2        3                    d e
ddd            ed            g          d!             Z5ej2        3                    d"d#dd$e6d%fd&d'd$e6d(fd#d'd$ed)fg          d*             Z7ej2        3                    d+ e             e&             ed,-          g          ej2        3                    d.g d/          ej2        3                    d0eeg          d1                                     Z8ej2        3                    d.g d2          d3             Z9d4 Z:ej2        3                    d.g d2          ej2        3                    d5 ee           eed67          g          d8                         Z;ej2        3                    d9dd'g           ed          d:                         Z<ej2        3                    dddg           ed          d;                         Z= ed          d<             Z>d= Z?ej2        3                    d>dd'g          d?             Z@d@ ZAdA ZBej2        3                    d.g dB          dC             ZCej2        3                    dDdEdFg          ej2        3                    dGd dg          dH                         ZD ed          dI             ZEej2        3                    dJg dK          dL             ZFdM ZGdS )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                                              sJ d
S )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           /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/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 6*SbS/Z^344444fbj)))))>&A+v{33779999999    c            
         t          dd          \  } }dt          j        dd          }}t          d                              | |          }|                    | |         ||                   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           dS )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 ryS11wI'Q777;;AqAAJ AgJ'
33v}S7I7IIIII*'  L 8		!	 	 	FJ 6*SbS/Z^344444FS#J'''''rA   T)enable_metadata_routingc                  f   t          d          \  } }| dd         |dd         }} t          j        | | |dk             g          t          j        |||dk             g          }}t          j        |          }|ddxx         dz  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          |                    d          | |d|i|||                    d          d|i          \  }}t          |
|           t          |	|           dS )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TcT7AdsdGqA  Y1Q!V9~66	1aQi.8Q8Q
JLOOM#2#!#%%J:#3A#677y!1!!455*'  L ,J$ !	, 	, 	,(O( 171:..	!'!*0E0EwI7"""66		#]3!33$3GG%}5	 	 	FJ '444OV,,,,,rA   fit_params_typelistarrayc           
      v   t          dd          \  }}t          ||           t          ||           d}t          ddgd          }|                    dd           t	          j        d	          t	          j        d	d          }}t          t          d
ddi           }t          |||||||i            dS )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?33 J
 $c
QRSS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           dS # 1 swxY w Y   dS )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	1	1 D D<"#5#7#788<dCCD D D D D D D D D D D D D D D D D Ds   $A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 |                     ||           ddd           dS # 1 swxY w Y   dS )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{      s    0 1===DAq	xw	/	/	/ M M"#5#7#7BB6BBFFq!LLLM M M M M M M M M M M M M M M M M Ms   .A&&A*-A*	estimator   )n_estimatorsr*   r%   predict_log_probadecision_functionThresholdClassifierc                    t          dd          \  }} | |          }t          ||          t          ||          k    sJ |                    ||           t          ||          t          ||          k    sJ t          ||          rI t          ||          |          } t          |j        |          |          }t          ||           dS dS )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)444E5/**gi.Q.QQQQQ	IIaOOO5/**gi.Q.QQQQQuo&& D777::&Pge.>&P&PQR&S&S#'BCCCCC	D D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                                	                    ||          }d}t          |d	| |d
          }t          ||	                    ||                              |                    }t          ||                    |                    }	||	k    sJ |j        d         j        |fk    sJ |j        d         j        |fk    sJ dS )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 0B 667K.a((K
	1[>1[>233A
	1[>1[>233A	~'');)=)=	>	>	B	B1a	H	HBJ&#'  E .a1a1H1H1K1KLLO,Q

1>>N^++++\*0ZMAAAAX&,======rA   c                     t          d          \  } }t          t                      t                                                    | |          }t          |t          t          d                                        | |          }t          |t          t          d                                        | |          }t          |t          t                                                  | |          }|j	        t          j        |j	                  k    sJ |j	        t          j        |j	                  k    sJ dS )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	H	HB.k+A>>>  	c!Qii  /k+A>>>  	c!Qii  *k(33  	c!Qii  (FM(:R,S,SSSSS(FM-:W,X,XXXXXXXrA   metriccancer)	pos_labelc                    t          d          \  }}t          j        ddgt                    }||         }t	          t          t                      t                                || d                              ||          }t          |j
        t          j        |                     |                    |          }t          t          j        |          t          j        |                     dS )	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 h),F;;;G
A& 0 02D2F2FGG'	  
 
c!Qii 
 u~rww'7'7888]]1Fry(("''*:*:;;;;;rA   with_sample_weightc                 \   t           j                            |          }t          dd          \  }}| r7|                    |j        d                   }t          j        ||          }nd}t                                          d          }t          |d          
                    |||          }|j        |usJ |
                    |||           t          |j        j        |j                   t          |j        j        |j                   t                                          d          }|
                    |||           |j                                        }t          |d	d
          
                    |||          }|j        |u sJ t          |j        j        |           t                                          d          }t          j        d          t          j        dd          fg}	t          ||	d
          
                    |||          }|j        |usJ | r||	d         d                  }
nd}
|
                    ||	d         d                  ||	d         d                  |
           t          |j        j        |j                   dS )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
3
3C1===DAq 		!'!*--}-@@@ #$$4444HHI&y===AA	1M B  E 9,,,,MM!QmM444E$*IO<<<E$/1EFFF #$$4444HHIMM!QmM444?!!D&yXUKKKOO	1M P  E y((((E$*D111 #$$4444HHI	2	"c**+
B 'yRuEEEII	1M J  E 9,,,,  Aq*MM!BqE!H+qAq{(MCCCE$*IO<<<<<rA   c                     t          dd          \  }}t          ||           t          ||           d}t          ddgd          }|                    dd           t	          |          } |j        ||fi | d	S )
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     s     1===DAq?33?33 J
 $c
QRSSSJ...&z22EEIa!!j!!!!!rA   c                     t          d          \  } }t                                          |           } t          j        | dd         | dd         f          } t          j        |dd         |dd         f          }t          j        |          }d|ddd<   t                                          d	          }t          |d
          }t          |          }|                    | ||	           |                    | ddd         |ddd                    t          |j        j        |j        j                   |                    |           }|                    |           }t          ||           dS )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 		1SbS61RU8$%%A
	1SbS61RU8$%%AM!$$MM##A#"$$4444HHI6yQGGG4551a}===a!fa!f---%+-B-M-S   -::1==2@@CC')?@@@@@rA   c                      t          d          \  } }t                      }t          j        ddd          }t	          ||dd                              | |          }t          |j        d         |           d	S )
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     s     A...DAq"$$IQ2&&J,'	  
 
c!Qii  K+L9:FFFFFrA   r   c                     t          d          \  }}t                      }t          ||                               ||          }| rt	          |d          sJ dS t	          |d          rJ dS )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!Qii   7{M2222222;6666666rA   c                     t          d          \  } }d}t                      }t          ||dd                              | |          }|                    | |           t	          d|d          }t          |                    | |                    \  }}t          |                              | |         ||                   }t          |j	        j
        |j
                   |                    d	                              | |           t          |                              | |          }t          |j	        j
        |j
                   d
S )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     sD   A...DAq
 I"$$I,iu1  	c!Qii  OOAq	ia	P	P	PBbhhq!nn--IwY''++AiL!I,GGK*02B2HIII &&**1a000Y''++Aq11K*02B2HIIIIIrA   c                     t          d          \  } }t          dd          }t          |d          }d}t          j        t
          |	          5  |                    | |           d
d
d
           d
S # 1 swxY w Y   d
S )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XXXKAG	z	1	1	1  1                 s   A77A;>A;)r   r%   r   c                    t          d          \  }}t                                          ||          }t          t	          |          |           }|                    ||           | dv r"|                    |          dddf         }d}n|                    |          }d}||k                        t                    }t          |
                    |          |           dS )	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3.<<Q??1E		.@@CC	I%--c22I088;;YGGGGGrA   zresponse_method, threshold)r%   gffffff?)r   g       @r   c           	         t          dd          \  }}t                                          ||          }t          t	          |          || |                              ||          }t          |j        j        |j                   | dk    r |                    |          dd|f         }n |	                    |          }|dk    r|n| }|dk    rt          j        ddg          nt          j        ddg          }|||k                        t                             }	t          |                    |          |	           dD ]}
t           t          ||
          |           t          ||
          |                     t           t          |j        |
          |           t          ||
          |                     dS )	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     s    !<<<DAq,..221a88$+,,'	  
 
c!Qii 
 E$*,?,EFFF /))%%a((I6))!,,&!^^''' (1A~~28QF###28QF;K;KLg2::3??@IEMM!$$i000M 
 
"GE6""1%%'Kw/BF'K'KA'N'N	
 	
 	
 	-GE$f--a000G'0033	
 	
 	
 	
	
 
rA   c                     t          d          \  } }t          j        |          }d|ddd<   t                                          d          }|                    | ||           t          t          |                    }|                    | ||           t          |j	        j
        |j
                   dS )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LOOMM##A##%%55D5IIJNN1a}N555#;eJFWFW#X#X#X  $$Q$GGG0;A:CSTTTTTrA   r   )r%   r   r   r   c                     t          d          \  }}t                                          ||          }t          |          } t	          ||           |           dS )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  se    
 A...DAq#%%))!Q//J!9J!O!O!O/G&//22222rA   c                  l   t          d          \  } }t          j        t          d          5  t	          t                                j         ddd           n# 1 swxY w Y   t                                          | |          }t	          |          }t          |j        |j                   dS )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 J 	!+=+?+?@@@IIJ J J J J J J J J J J J J J J
 $%%))!Q//J!9J!O!O!O1:J<OPPPPPs   "AA!$A!)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	     sE        " " " " " "                  * ) ) ) ) ) 7 7 7 7 7 7 - - - - - - 3 3 3 3 3 3            1 0 0 0 0 0         
      + * * * * * 0 0 0 0 0 0       / / / / / / 5 5 5 5 5 5         : : :<( ( (@ ----- -- .---` *VW,=>>---  .- ?>> 
aaaPPP&&A666 D D D  d++5	
 &&?	
 e,,)	
 (M M) (M 3355"<"<!"L"L"LM  RRR  46PQ D D  	 D* EEE > > >>Y Y Y* EEE  +,,H111 < <  <, -e}==---/= /= .- >=/=d *VW,=>>---" " .- ?>" ---A A .-A:G G G +dE];;
7 
7 <;
7J J J8	 	 	 EEE H H H.  #9;U"V  q!f--#
 #
 .- #
L ---	U 	U .-	U TTT 3 3 3
Q 
Q 
Q 
Q 
QrA   