o
    \i[                  	   @   sb  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d Z/dd Z0edddd Z1ej23dddgedddd Z4ej23d e
d!d"d d#ed d$gd%d& Z5ej23d'd(dd)e6d*fd+d,d)e6d-fd(d,d)ed.fgd/d0 Z7ej23d1e e& ed2d3gej23d4g d5ej23d6eegd7d8 Z8ej23d4g d9d:d; Z9d<d= Z:ej23d4g d9ej23d>eeeed?d@gdAdB Z;ej23dCdd,gedddDdE Z<ej23dddgedddFdG Z=edddHdI Z>dJdK Z?ej23dLdd,gdMdN Z@dOdP ZAdQdR ZBej23d4g dSdTdU ZCej23dVdWdXgej23dYd d"gdZd[ ZDeddd\d] ZEej23d^g d_d`da ZFdbdc ZGdS )d    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               
   C   s   t ddd\} }tdtdd}}t }ttdddi d}t|| |i |||i d	\}}t|d
d |dd
 ks?J t|tj	sGJ t
|dk|dk sUJ 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-   r.   
classifierr/   scoresr)    r=   /var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/lib/python3.10/site-packages/sklearn/model_selection/tests/test_classification_threshold.py0test_fit_and_score_over_thresholds_curve_scorers+   s.   
" r?   c               
   C   s   t ddd\} }dtdd}}tdd| |}|| | || tdks+J tt	dd	d
i d}t
|| |i |||i d\}}t|dd |dd ksSJ t|ddg dS )z-Check the behaviour with a prefit classifier.r   r   r   Nr!   r    g      ?r"   r#      r%   r+   r1         ?)r   r2   r3   r   fitscorepytestapproxr   r   r   r4   r   r8   r=   r=   r>   )test_fit_and_score_over_thresholds_prefitI   s.   "
"
rG   T)enable_metadata_routingc               
   C   s6  t dd\} }| dd |dd } }t| | |dk gt|||dk g}}t|}|dd  d9  < t }t|jd }t|jd }tt	dd	d
i d}t
|||i |||i d\}	}
t| jd t| jd }}t
|jdd| |d|i|||jddd|id\}}t|
| t|	| dS )zICheck that we dispatch the sample-weight to fit and score the classifier.T
return_X_yNr   r   r!   rA   r"   r#   r$   r%   r+   sample_weightrL   )r   r2   vstackhstack	ones_liker   r3   shaper   r   r   set_fit_requestset_score_requestr   )r9   r:   
X_repeated
y_repeatedrL   r;   train_repeated_idxval_repeated_idxr/   scores_repeatedthresholds_repeatedr-   r.   r<   r)   r=   r=   r>   0test_fit_and_score_over_thresholds_sample_weighti   sL   .

"



rY   fit_params_typelistarrayc              
   C   s   t ddd\}}t|| t|| d}tddgdd}|jddd td	td	d}}ttd
ddi d}t|||||||i d dS )ECheck that we pass `fit_params` to the classifier when calling `fit`.r   r   r   abr_   r`   expected_fit_paramsr    Tr!   r"   r#   r$   r%   r+   N)	r   r   r   rQ   r2   r3   r   r   r   )rZ   r9   r:   r,   r;   r-   r.   r/   r=   r=   r>   -test_fit_and_score_over_thresholds_fit_params   s0   
rc   data   r"   )	n_classesn_clusters_per_classr    r@   c                 C   sH   d}t jt|d tt j|   W d   dS 1 sw   Y  dS )zHCheck that we raise an informative error message for non-binary problem.z(Only binary classification is supported.matchN)rE   raises
ValueErrorr   r   rC   )rd   err_msgr=   r=   r>   )test_tuned_threshold_classifier_no_binary   s   	"rm   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                 C   s^   t ddd\}}tj||d tt fi | || W d   dS 1 s(w   Y  dS )zhCheck that we raise an informative error message when `cv` and `refit`
    cannot be used together.
    r   r   r   rh   N)r   rE   rj   r   r   rC   )paramserr_typerl   r9   r:   r=   r=   r>   1test_tuned_threshold_classifier_conflict_cv_refit   s   "rt   	estimator   )n_estimatorsr(   r#   predict_log_probadecision_functionThresholdClassifierc                 C   s   t ddd\}}| |d}t||t||ksJ ||| t||t||ks+J t||rFt|||}t|j||}t|| dS dS )zoCheck that `TunedThresholdClassifierCV` exposes the same response methods as the
    underlying estimator.
    r   r   r   ru   N)r   hasattrrC   getattr
estimator_r   )r{   ru   r(   r9   r:   modely_pred_cutoffy_pred_underlying_estimatorr=   r=   r>   4test_threshold_classifier_estimator_response_methods   s   

r   )autorz   r#   c           
      C   s  t dd\}}|ddddf }t|dk}|d|jd  }t|dk}t|| || g}t|| || g}tt t 	||}d}t
|d	| |dd
}t||	|||}t|||}	||	ksnJ |jd j|fksyJ |jd j|fksJ dS )zSCheck that `TunedThresholdClassifierCV` is optimizing a given objective
    metric.TrI   N   r"   r!   r   r   balanced_accuracy)ru   scoringr(   r)   store_cv_resultsr)   r<   )r   r2   flatnonzerosizerM   rN   r   r   r   rC   r   r   predictcv_results_rP   )
r(   r9   r:   indices_posindices_neglrr)   r   score_optimizedscore_baseliner=   r=   r>   8test_tuned_threshold_classifier_without_constraint_value  s*   r   c                  C   s   t dd\} }tt t | |}t|ttddd| |}t|ttddd| |}t|ttd| |}|j	t
|j	ksEJ |j	t
|j	ksPJ 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`.
    TrI   r"   )beta)ru   r   rA   N)r   r   r   r   rC   r   r   r   r   best_threshold_rE   rF   )r9   r:   r   model_fbeta_1model_fbeta_2model_f1r=   r=   r>   5test_tuned_threshold_classifier_metric_with_parameter'  s&   r   metriccancer)	pos_labelc                 C   s   t dd\}}tjddgtd}|| }ttt t || dd||}t	|j
t| ||}t	t|t| 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.
    TrI   r   healthy)dtyper   )ru   r   r(   r)   N)r   r2   r\   objectr   r   r   r   rC   r   classes_sortr   unique)r(   r   r9   r:   classesr   y_predr=   r=   r>   3test_tuned_threshold_classifier_with_string_targets<  s   
r   with_sample_weightc                 C   s  t j|}tddd\}}| r ||jd }t j||d}nd}t jdd}t	|ddj
|||d}|j|us<J |j
|||d t|jj|j t|jj|j t jdd}|j
|||d |j }t	|d	d
dj
|||d}|j|u s|J t|jj| t jdd}t dt ddfg}	t	||	d
dj
|||d}|j|usJ | r||	d d  }
nd}
|j
||	d d  ||	d d  |
d t|jj|j dS )z-Check the behaviour of the `refit` parameter.r   r   r   )outNTrK   rq   rn   Fro   r!   )r2   randomRandomStater   randnrP   absr   rQ   r   rC   r   r   coef_
intercept_copyr3   )r   global_random_seedrngr9   r:   rL   ru   r   coefrp   sw_trainr=   r=   r>   %test_tuned_threshold_classifier_refit\  sD   
(r   c                 C   sd   t ddd\}}t|| t|| d}tddgdd}|jddd t|}|j||fi | d	S )
r]   r   r   r   r^   r_   r`   ra   TN)r   r   r   rQ   r   rC   )rZ   r9   r:   r,   r;   r   r=   r=   r>   *test_tuned_threshold_classifier_fit_params  s   r   c                  C   s   t dd\} }t | } t| dd | dd f} t|dd |dd f}t|}d|ddd< t jdd	}t	|dd
}t
|}|j| ||d	 || 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.TrI   N(   r!   Z   r"   rA   rK   )rp   )r   r   fit_transformr2   rM   rN   
zeros_liker   rQ   r   r   rC   r   r   r   r#   )r9   r:   rL   ru   model_without_weightsmodel_with_weightsy_pred_with_weightsy_pred_without_weightsr=   r=   r>   Ctest_tuned_threshold_classifier_cv_zeros_sample_weights_equivalence  s"   
 

r   c                  C   sN   t dd\} }t }tddd}t||d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   r@   r"      r#   T)r)   r(   r   r)   N)r   r   r2   linspacer   rC   r   r   )r9   r:   ru   r)   tuned_modelr=   r=   r>   0test_tuned_threshold_classifier_thresholds_array  s   r   r   c                 C   sP   t dd\}}t }t|| d||}| 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   r@   )r   r   N)r   r   r   rC   r}   )r   r9   r:   ru   r   r=   r=   r>   0test_tuned_threshold_classifier_store_cv_results  s   r   c            	      C   s   t dd\} }d}t }t||ddd| |}|| | td|dd}t|| |\}}t|| | || }t|j	j
|j
 |jdd	| | t|| |}t|j	j
|j
 d
S )z0Check the behaviour when `cv` is set to a float.r   r@   g333333?F)rp   rq   r    r"   )n_splits	test_sizer    Tr   N)r   r   r   rC   r   nextsplitr   r   r   r   
set_params)	r9   r:   r   ru   r   rp   r-   r.   cloned_estimatorr=   r=   r>   (test_tuned_threshold_classifier_cv_float  s    r   c                  C   sj   t dd\} }tddd}t|dd}d}tjt|d	 || | W d
   d
S 1 s.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   r@   constantr"   )strategyr   r#   )r(   z1The provided estimator makes constant predictionsrh   N)r   r   r   rE   rj   rk   rC   )r9   r:   ru   r   rl   r=   r=   r>   8test_tuned_threshold_classifier_error_constant_predictor  s   "r   )r   r#   rz   c                 C   s   t dd\}}t ||}tt|| d}||| | 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   r@   )ru   r(   )r   r#   Nr"   rB   g        )r   r   rC   r   r   r#   rz   astypeintr   r   )r(   r9   r:   r;   classifier_default_thresholdy_score	threshold	y_pred_lrr=   r=   r>   3test_fixed_threshold_classifier_equivalence_default  s   
r   zresponse_method, threshold)r#   gffffff?)rz   g       @r   c                 C   s  t ddd\}}t ||}tt||| |d||}t|jj|j | dkr5||dd|f }n|	|}|dkr@|n| }|dkrNt
ddgnt
ddg}|||kt }	t|||	 dD ]!}
tt||
|t||
| tt|j|
|t||
| qhdS )	zCheck that applying `predict` lead to the same prediction as applying the
    threshold to the output of the response method.
    r!   r   r   )ru   r   r(   r   r#   Nr"   rx   )r   r   rC   r   r   r   r   r   r#   rz   r2   r\   r   r   r   r~   )r(   r   r   r9   r:   logistic_regressionr   r   map_to_labelr   methodr=   r=   r>   test_fixed_threshold_classifier  s6   
$r   c                  C   sv   t dd\} }t|}d|ddd< t jdd}|j| ||d tt|d}|j| ||d t|j	j
|j
 dS )z2Check that everything works with metadata routing.r   r@   rA   NTrK   r|   )r   r2   rO   r   rQ   rC   r   r   r   r   r   )r9   r:   rL   r;   r   r=   r=   r>   0test_fixed_threshold_classifier_metadata_routingG  s   
r   r   )r#   rz   r   ry   c                 C   s8   t dd\}}t ||}t|d}t|| | dS )zMCheck that if the underlying estimator is already fitted, no fit is required.r   r@   r|   N)r   r   rC   r   r~   )r   r9   r:   r;   fixed_threshold_classifierr=   r=   r>   0test_fixed_threshold_classifier_fitted_estimatorT  s   
r   c                  C   st   t dd\} }tjtdd tt dj W d   n1 s w   Y  t | |}t|d}t|j|j dS )z2Check that the classes_ attribute is properly set.r   r@   z+The underlying estimator is not fitted yet.rh   r|   N)	r   rE   rj   AttributeErrorr   r   r   rC   r   )r9   r:   r;   r   r=   r=   r>   (test_fixed_threshold_classifier_classes_`  s   
r   )Hnumpyr2   rE   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?   rG   rY   markparametrizerc   rm   rk   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r=   r=   r>   <module>   s     
0

	

2


&

	