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   Z	n_samplesrandom_state2      predict_proba
   Z
score_funcsignresponse_method
thresholdskwargs
fit_params	train_idxval_idxcurve_scorerZscore_paramsN)r   nparanger   r   r   r   all
isinstanceZndarraylogical_andXyr+   r,   
classifierr-   scoresr'    r9   u/home/kim/smarthome/.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)   r.         ?)r   r/   r0   r   fitZscorepytestapproxr   r   r   r1   r   r4   r9   r9   r:   )test_fit_and_score_over_thresholds_prefitI   s.   "
"
rB   T)Z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Z
return_X_yNr   r   r    r=   r!   r"   r#   r$   r)   sample_weightrE   )r   r/   vstackhstack	ones_liker   r0   shaper   r   r   set_fit_requestZset_score_requestr   )r5   r6   Z
X_repeatedZ
y_repeatedrE   r7   Ztrain_repeated_idxZval_repeated_idxr-   Zscores_repeatedZthresholds_repeatedr+   r,   r8   r'   r9   r9   r:   0test_fit_and_score_over_thresholds_sample_weighti   sL   .

"



rK   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brQ   rR   Zexpected_fit_paramsr   Tr    r!   r"   r#   r$   r)   N)	r   r   r   rJ   r/   r0   r   r   r   )rL   r5   r6   r*   r7   r+   r,   r-   r9   r9   r:   -test_fit_and_score_over_thresholds_fit_params   s0   
rT   data   r!   )Z	n_classesZ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)r@   raises
ValueErrorr   r   r?   )rU   err_msgr9   r9   r:   )test_tuned_threshold_classifier_no_binary   s   	"r\   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   rW   N)r   r@   rY   r   r   r?   )paramsZerr_typer[   r5   r6   r9   r9   r:   1test_tuned_threshold_classifier_conflict_cv_refit   s   "rb   	estimator   )Z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   rc   N)r   hasattrr?   getattr
estimator_r   )rh   rc   r&   r5   r6   modelZy_pred_cutoffZy_pred_underlying_estimatorr9   r9   r:   4test_threshold_classifier_estimator_response_methods   s   

rn   )autorg   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.TrC   N   r!   r    r   r   Zbalanced_accuracy)rc   scoringr&   r'   store_cv_resultsr'   r8   )r   r/   ZflatnonzerosizerF   rG   r   r   r   r?   r   r   predictcv_results_rI   )
r&   r5   r6   Zindices_posZindices_neglrr'   rm   Zscore_optimizedZscore_baseliner9   r9   r:   8test_tuned_threshold_classifier_without_constraint_value  s*   rw   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`.
    TrC   r!   )beta)rc   rq   r=   N)r   r   r   r   r?   r   r   r   r   Zbest_threshold_r@   rA   )r5   r6   rv   Zmodel_fbeta_1Zmodel_fbeta_2Zmodel_f1r9   r9   r:   5test_tuned_threshold_classifier_metric_with_parameter'  s&   ry   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.
    TrC   r{   Zhealthy)Zdtyper   )rc   rq   r&   r'   N)r   r/   rN   objectr   r   r   r   r?   r   classes_sortrt   unique)r&   rz   r5   r6   classesrm   Zy_predr9   r9   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   )outNTrD   r`   r]   Fr^   r    )r/   randomZRandomStater   ZrandnrI   absr   rJ   r   r?   rl   r   coef_Z
intercept_copyr0   )r   Zglobal_random_seedrngr5   r6   rE   rc   rm   Zcoefr_   Zsw_trainr9   r9   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 )
rO   r   r   r   rP   rQ   rR   rS   TN)r   r   r   rJ   r   r?   )rL   r5   r6   r*   r7   rm   r9   r9   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.TrC   N(   r    Z   r!   r=   rD   )r_   )r   r   Zfit_transformr/   rF   rG   Z
zeros_liker   rJ   r   r   r?   r   rl   r   r"   )r5   r6   rE   rc   Zmodel_without_weightsZmodel_with_weightsZy_pred_with_weightsZy_pred_without_weightsr9   r9   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&   rr   r'   N)r   r   r/   Zlinspacer   r?   r   ru   )r5   r6   rc   r'   tuned_modelr9   r9   r:   0test_tuned_threshold_classifier_thresholds_array  s   r   rr   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<   )rr   ru   N)r   r   r   r?   rj   )rr   r5   r6   rc   r   r9   r9   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)r_   r`   r   r!   )Zn_splits	test_sizer   Tr   N)r   r   r   r?   r   nextsplitr   r   rl   r   Z
set_params)	r5   r6   r   rc   r   r_   r+   r,   Zcloned_estimatorr9   r9   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!   )Zstrategyr   r"   )r&   z1The provided estimator makes constant predictionsrW   N)r   r   r   r@   rY   rZ   r?   )r5   r6   rc   r   r[   r9   r9   r:   8test_tuned_threshold_classifier_error_constant_predictor  s   "r   )ro   r"   rg   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<   )rc   r&   )ro   r"   Nr!   r>   g        )r   r   r?   r   r   r"   rg   astypeintr   rt   )r&   r5   r6   r7   classifier_default_thresholdy_score	threshold	y_pred_lrr9   r9   r:   3test_fixed_threshold_classifier_equivalence_default  s   
r   zresponse_method, threshold)r"   gffffff?)rg   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   )rc   r   r&   r|   r"   Nr!   re   )r   r   r?   r   r   r   rl   r   r"   rg   r/   rN   r   r   rt   rk   )r&   r   r|   r5   r6   Zlogistic_regressionrm   r   Zmap_to_labelr   methodr9   r9   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<   r=   NTrD   ri   )r   r/   rH   r   rJ   r?   r   r   r   rl   r   )r5   r6   rE   r7   r   r9   r9   r:   0test_fixed_threshold_classifier_metadata_routingG  s   
r   r   )r"   rg   rt   rf   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<   ri   N)r   r   r?   r   rk   )r   r5   r6   r7   fixed_threshold_classifierr9   r9   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.rW   ri   N)	r   r@   rY   AttributeErrorr   r   r~   r?   r   )r5   r6   r7   r   r9   r9   r:   (test_fixed_threshold_classifier_classes_`  s   
r   )Hnumpyr/   r@   Zsklearnr   Zsklearn.baser   Zsklearn.datasetsr   r   r   r   Zsklearn.dummyr   Zsklearn.ensembler	   Zsklearn.exceptionsr
   Zsklearn.linear_modelr   Zsklearn.metricsr   r   r   r   Zsklearn.metrics._scorerr   Zsklearn.model_selectionr   r   r   Z1sklearn.model_selection._classification_thresholdr   Zsklearn.pipeliner   Zsklearn.preprocessingr   Zsklearn.svmr   Zsklearn.treer   Zsklearn.utils._mockingr   Zsklearn.utils._testingr   r   r   r;   rB   rK   markZparametrizerT   r\   rZ   rb   rn   rw   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r9   r9   r9   r:   <module>   s     
0

	

2


&

	