o
    &ѹg                  
   @   s  d dl Zd dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZmZmZmZmZ d dlmZmZmZ d dlmZ d dl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$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z>m?Z?m@Z@mAZAmBZB d dlCmDZD d dlEmFZF dZGejHdddd ZIejJKdeFejJKdd d!gejJKd"d#d$gd%d& ZLd'd( ZMejJKd"d#d$gd)d* ZNd+d, ZOejJKdd d!gejJKd"d#d$gd-d. ZPejJKdd d!gejJKd"d#d$gd/d0 ZQejJKdd d!gejJKd"d#d$gejJKd1eRd2d3d4 ZSd5d6 ZTeBeUd7ejJKdeFd8d9 ZVejJKdd d!gd:d; ZWd<d= ZXd>d? ZYejJKd"d#d$gd@dA ZZejJKd"d#d$gdBdC Z[ejJKd"d#d$gdDdE Z\ejJKdFej]^dG_dHdId2ej]^dG_dHdId2dJgdKdL Z`ejHdMdN ZaejHdOdP ZbdQdR ZcejJKdSede8dTdUd2ede8dTdUdVgdWdX ZedYdZ Zfd[d\ ZgejHddd]d^ ZhejHddd_d` ZiejJKdadIdbgejJKdcdddegdfdg Zjdhdi ZkejJKdjdkdlgdmdn Zldodp ZmejJKdqdrdsgdtdu Zndvdw ZoejJKdxepeqgdydz ZrejJKdxepeqgd{d| ZsejJKd}d~d2ddd~d2ddgdd ZtejJKdg ddd ZuejJKdd d!gejJKd"d#d$gdd ZvejJKdddgdd ZwejJKddgeG exeGgdd Zydd Zzdd Z{dd Z|dd Z}dd Z~dd ZdS )    N)assert_allclose)BaseEstimatorClassifierMixinclone)CalibratedClassifierCVCalibrationDisplay_CalibratedClassifier_sigmoid_calibration_SigmoidCalibrationcalibration_curve)	load_iris
make_blobsmake_classification)DummyClassifier)RandomForestClassifierVotingClassifier)NotFittedError)DictVectorizer)FrozenEstimator)SimpleImputer)IsotonicRegression)LogisticRegressionSGDClassifier)brier_score_loss)KFoldLeaveOneOutcheck_cvcross_val_predictcross_val_scoretrain_test_split)MultinomialNB)Pipelinemake_pipeline)LabelEncoderStandardScaler)	LinearSVC)DecisionTreeClassifier)CheckingClassifier)_convert_containerassert_almost_equalassert_array_almost_equalassert_array_equalignore_warnings)softmax)CSR_CONTAINERS   module)Zscopec                  C   s   t tddd\} }| |fS )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLESXy r;   b/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/tests/test_calibration.pydata9   s   r=   csr_containermethodsigmoidisotonicensembleTFc                 C   s2  t d }| \}}tjjddj|jd}|| 8 }|d | |d | |d | }}	}
||d  ||d  }}t j||	|
d}|	|d d df }t
||jd |d}tt ||| W d    n1 sow   Y  ||f||||ffD ]\}}t
||d|d	}|j||	|
d |	|d d df }t||t||ksJ |j||	d |
d |	|d d df }t|| |j|d|	 d |
d |	|d d df }t|| |j||	d d |
d |	|d d df }|d
krt|d|  qt||t|d d |ksJ qd S )N   r2   seedsizesample_weight   cvrB      r?   rL   rB   r@   )r7   nprandomRandomStateuniformrG   minr    fitpredict_probar   pytestraises
ValueErrorr   r*   )r=   r?   r>   rB   r4   r9   r:   rI   X_trainy_trainsw_trainX_testy_testclfprob_pos_clfcal_clfZthis_X_trainthis_X_testZprob_pos_cal_clfZprob_pos_cal_clf_relabeledr;   r;   r<   test_calibration?   sF   (





rb   c                 C   s<   | \}}t dd}||| |jd j}t|tsJ d S )NrC   rL   r   )r   rT   calibrated_classifiers_	estimator
isinstancer%   )r=   r9   r:   	calib_clfZbase_estr;   r;   r<   "test_calibration_default_estimator}   s
   
rh   c                 C   sp   | \}}d}t |d}t||d}t|jt sJ |jj|ks!J ||| |r+|nd}t|j|ks6J d S )NrM   n_splitsrK   rJ   )r   r   rf   rL   rj   rT   lenrd   )r=   rB   r9   r:   splitskfoldrg   Zexpected_n_clfr;   r;   r<   test_calibration_cv_splitter   s   
rn   c                 C   s   | \}}t dd}t|dd}tjtdd ||| W d    n1 s'w   Y  tt dd}tjtdd ||| W d    d S 1 sLw   Y  d S )Ne   ri   TrK   z$Requesting 101-fold cross-validationmatchz!LeaveOneOut cross-validation does)r   r   rV   rW   rX   rT   r   )r=   r9   r:   rm   rg   r;   r;   r<   test_calibration_cv_nfold   s   
"rr   c                 C   s   t d }| \}}tjjddjt|d}|d | |d | |d | }}}	||d  }
tdd}t|||d}|j|||	d |	|
}||| |	|
}tj
|| }|dksaJ d S )	NrC   r2   rD   rF   r6   )r?   rB   rH   皙?)r7   rO   rP   rQ   rR   rk   r%   r   rT   rU   ZlinalgZnorm)r=   r?   rB   r4   r9   r:   rI   rY   rZ   r[   r\   re   calibrated_clfZprobs_with_swZprobs_without_swdiffr;   r;   r<   test_sample_weight   s   (


rw   c                 C   s   | \}}t ||dd\}}}}tt tdd}	t|	|d|d}
|
|| |
|}t|	|d|d}||| ||}t|| dS )zTest parallel calibrationr2   rs   rC   )r?   Zn_jobsrB   rJ   N)r   r"   r$   r%   r   rT   rU   r   )r=   r?   rB   r9   r:   rY   r\   rZ   r]   re   Zcal_clf_parallelZprobs_parallelZcal_clf_sequentialZprobs_sequentialr;   r;   r<   test_parallel_execution   s   

rx   rE   rC   c                 C   s  dd }t dd}tdd|ddd	\}}d
||d
k< t|jd }|d d d
 |d d d
 }}	|dd d
 |dd d
 }
}|||	 t|| d|d}|||	 ||
}ttj	|ddt
t|
 d||
|  k rwdk szJ  J ||
|d||
| ksJ ||t||
|d}||||d}|d| k sJ tddd}|||	 ||
}||||d}t|| d|d}|||	 ||
}||||d}|d| k sJ d S )Nc                 S   s*   t ||  }t || d |jd  S )NrC   r   )rO   Zeyesumshape)y_trueZ
proba_pred	n_classesZY_onehotr;   r;   r<   multiclass_brier   s   z5test_calibration_multiclass.<locals>.multiclass_brier   rs   i  d   
         .@r4   r5   r6   ZcentersZcluster_stdrC   r   rJ   rM   rN   Zaxis?gffffff?)r|   g?   r2   )n_estimatorsr6   )r%   r   rO   uniquerz   rT   r   rU   r   ry   onesrk   Zscorer-   decision_functionr   )r?   rB   rE   r}   r^   r9   r:   r|   rY   rZ   r\   r]   r`   probasZuncalibrated_brierZcalibrated_brierZ	clf_probsZcal_clf_probsr;   r;   r<   test_calibration_multiclass   s<   



$ 

r   c                  C   sh   G dd d} t dddddd\}}t ||}|  }t||g|jd}||}t|d	|j  d S )
Nc                   @   s   e Zd Zdd ZdS )z9test_calibration_zero_probability.<locals>.ZeroCalibratorc                 S   s   t |jd S )Nr   )rO   zerosrz   selfr9   r;   r;   r<   predict  s   zAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predictN)__name__
__module____qualname__r   r;   r;   r;   r<   ZeroCalibrator  s    r   2   r   r~   r   r   )re   Zcalibratorsclasses      ?)r   r   rT   r   classes_rU   r   Z
n_classes_)r   r9   r:   r^   
calibratorr`   r   r;   r;   r<   !test_calibration_zero_probability  s   



r   )categoryc              
   C   s2  d}t d| ddd\}}tjjddj|jd}|| 8 }|d| |d| |d| }}}||d	|  ||d	|  ||d	|  }}	}
|d	| d |d	| d }}t }t|d
d}t	
t |||	 W d   n1 s}w   Y  |||| ||dddf }||f| || |ffD ]v\}}dD ]o}t||d
d}tt||d}|
dfD ]Y}|j||	|d |j||	|d ||}||}||}||}|dddf }|dddf }t|| t|tddgtj|dd  t||t||ksJ qqqdS )z*Test calibration for prefitted classifiersr      r1   r2   r3   rD   rF   NrC   prefitrc   rJ   )rA   r@   )r?   rL   r?   rH   r   r   )r   rO   rP   rQ   rR   rG   rS   r    r   rV   rW   r   rT   rU   r   r   r+   arrayZargmaxr   )r>   r4   r9   r:   rI   rY   rZ   r[   ZX_calibZy_calibZsw_calibr\   r]   r^   Z	unfit_clfr_   Zthis_X_calibra   r?   Zcal_clf_prefitZcal_clf_frozenswZy_prob_prefitZy_prob_frozenZy_pred_prefitZy_pred_frozenZprob_pos_cal_clf_prefitZprob_pos_cal_clf_frozenr;   r;   r<   test_calibration_prefit0  sT   (
"





r   c                 C   s   | \}}t dd}t||ddd}||| ||}t|||ddd}|dkr/td	d
}nt }||| ||| ||}	||	}
t	|d d df |
 d S )Nr~   rs   r   FrN   r   )rL   r?   rA   Zclip)Zout_of_boundsrJ   )
r%   r   rT   rU   r   r   r
   r   r   r   )r=   r?   r9   r:   r^   r`   Z
cal_probasZunbiased_predsr   Zclf_dfZmanual_probasr;   r;   r<   test_calibration_ensemble_falsej  s   



r   c                  C   s   t g d} t g d}t ddg}t|t| |d ddt |d |  |d    }t | || }t||d	 t	t
 t t | | f| W d
   d
S 1 s\w   Y  d
S )z0Test calibration values with Platt sigmoid model)rM   r   )rJ   r   gj=ɿgY90(?r   r   r   rJ   r1   N)rO   r   r*   r	   expr
   rT   r   rV   rW   rX   Zvstack)ZexFZexYZAB_lin_libsvmZlin_probZsk_probr;   r;   r<   test_sigmoid_calibration  s   ""r   c                  C   sT  t g d} t g d}t| |dd\}}t|t|ks!J t|dks)J t|ddg t|ddg tt tdgd	g W d
   n1 sNw   Y  t g d}t g d}t||ddd\}}t|t|ksuJ t|dks}J t|ddg t|ddg tt t||dd W d
   d
S 1 sw   Y  d
S )z Check calibration_curve function)r   r   r   rJ   rJ   rJ   )        rt   皙?皙??r   rC   n_binsr   rJ   rt   r   gN)r   r   r   r   rJ   rJ   )r   rt   r         ?r   r   quantiler   strategygUUUUUU?r   Z
percentile)r   )rO   r   r   rk   r)   rV   rW   rX   )r{   y_pred	prob_true	prob_predZy_true2Zy_pred2Zprob_true_quantileZprob_pred_quantiler;   r;   r<   test_calibration_curve  s,   
"r   c                 C   sf   t dddddd\}}tj|d< tdt fdtd	d
fg}t|dd| d}||| || dS )z$Test that calibration can accept nanr   rC   r   r2   )r4   r5   Zn_informativeZn_redundantr6   r   r   ZimputerrfrJ   )r   rA   )rL   r?   rB   N)	r   rO   nanr!   r   r   r   rT   r   )rB   r9   r:   r^   Zclf_cr;   r;   r<   test_calibration_nan_imputer  s   


r   c                 C   sd   t dddd\}}g d}tddd}t|d	td
d| d}||| t||jddd d S )Nr   rM   rC   )r4   r5   r|   )
rJ   rJ   rJ   rJ   rJ   r   r   r   r   r   r   r~   )Cr6   r@   r   ri   rN   rJ   r   )r   r%   r   r   rT   r   rU   ry   )rB   r9   _r:   r^   Zclf_probr;   r;   r<   test_calibration_prob_sum  s   r   c           	      C   s  t jdd}g dg d g d }tdd}t|dtd	| d
}||| | rht d}tddgdd	gD ]-\}}|j	| 
|}t|d d |f t t| t |d d ||kf dkseJ q8d S |j	d 
|}t|jddt |jd  d S )N   rM   )r   r   r   rJ   )rJ   rJ   rC   rC   )rC   r   r   r   r~   rs   r@   r   rN      r   rC   rJ   r   )rO   rP   randnr&   r   r   rT   Zarangeziprd   rU   r+   r   rk   allr*   ry   r   rz   )	rB   r9   r:   r^   r`   r   Zcalib_iZclass_iZprobar;   r;   r<   test_calibration_less_classes  s    

 $
"r   r9   r2      rM   r1   c                 C   s4   g d}G dd dt t}t| }|| | dS )z;Test that calibration accepts n-dimensional arrays as input)rJ   r   r   rJ   rJ   r   rJ   rJ   r   r   rJ   r   r   rJ   r   c                   @   s    e Zd ZdZdd Zdd ZdS )z>test_calibration_accepts_ndarray.<locals>.MockTensorClassifierz*A toy estimator that accepts tensor inputsc                 S   s   t || _| S N)rO   r   r   )r   r9   r:   r;   r;   r<   rT     s   zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitc                 S   s   | |jd djddS )Nr   r   rJ   r   )reshaperz   ry   r   r;   r;   r<   r     s   zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionN)r   r   r   __doc__rT   r   r;   r;   r;   r<   MockTensorClassifier  s    r   N)r   r   r   rT   )r9   r:   r   ru   r;   r;   r<    test_calibration_accepts_ndarray  s   	
r   c                  C   s<   dddddddddddddddg} g d	}| |fS )
NZNYZadult)stateZageZTXVTchildZCTZBR)rJ   r   rJ   rJ   r   r;   )	dict_dataZtext_labelsr;   r;   r<   r   
  s   r   c                 C   s,   | \}}t dt fdt fg}|||S )NZ
vectorizerr^   )r!   r   r   rT   )r   r9   r:   Zpipeline_prefitr;   r;   r<   dict_data_pipeline  s
   r   c                 C   sj   | \}}|}t t|dd}||| t|j|j t|dr"J t|dr)J || || dS )aR  Test that calibration works in prefit pipeline with transformer

    `X` is not array-like, sparse matrix or dataframe at the start.
    See https://github.com/scikit-learn/scikit-learn/issues/8710

    Also test it can predict without running into validation errors.
    See https://github.com/scikit-learn/scikit-learn/issues/19637
    rC   rc   n_features_in_N)r   r   rT   r+   r   hasattrr   rU   )r   r   r9   r:   r^   rg   r;   r;   r<   test_calibration_dict_pipeline   s   	
r   zclf, cvrJ   r   r   c                 C   s   t ddddd\}}|dkr| ||} t| |d}||| |dkr5t|j| j |j| jks3J d S t |j}t|j| |j|jd ksLJ d S )	Nr   rM   rC   r~   r4   r5   r|   r6   r   rc   rJ   )r   rT   r   r+   r   r   r#   rz   )r^   rL   r9   r:   rg   r   r;   r;   r<   test_calibration_attributes:  s   	r   c                  C   s   t ddddd\} }tdd| |}tt|}d}tjt|d	 || d d d d
f | W d    d S 1 s<w   Y  d S )Nr   rM   rC   r~   r   rJ   r   zAX has 3 features, but LinearSVC is expecting 5 features as input.rp   r   )r   r%   rT   r   r   rV   rW   rX   )r9   r:   r^   rg   msgr;   r;   r<   2test_calibration_inconsistent_prefit_n_features_inR  s   "r   c                  C   sX   t ddddd\} }tdd tdD d	d
}|| | tt|d}|| | d S )Nr   rM   rC   r~   r   c                 S   s   g | ]}d t | t fqS )lr)strr   ).0ir;   r;   r<   
<listcomp>d  s    z5test_calibration_votingclassifier.<locals>.<listcomp>r   Zsoft)Z
estimatorsZvotingre   )r   r   rangerT   r   r   )r9   r:   Zvoterg   r;   r;   r<   !test_calibration_votingclassifier^  s   r   c                   C   s
   t ddS )NTZ
return_X_y)r   r;   r;   r;   r<   	iris_datan  s   
r   c                 C   s    | \}}||dk  ||dk  fS )NrC   r;   )r   r9   r:   r;   r;   r<   iris_data_binarys  s   r   r   r   r   rR   r   c                 C   sJ  |\}}t  ||}tj|||||dd}||d d df }t||||d\}	}
t|j|	 t|j|
 t|j	| |j
dksDJ dd l}t|j|jjsRJ |j dks[J t|j|jjseJ t|j|jjsoJ |j dksxJ |j dksJ dd	g}|j  }t|t|ksJ |D ]
}| |v sJ qd S )
Nr   )r   r   alpharJ   r   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rT   r   from_estimatorrU   r   r   r   r   y_probestimator_nameZ
matplotlibrf   line_linesZLine2DZ	get_alphaax_ZaxesZAxesZfigure_figureZFigure
get_xlabel
get_ylabel
get_legend	get_textsrk   get_text)pyplotr   r   r   r9   r:   r   vizr   r   r   Zmplexpected_legend_labelslegend_labelslabelsr;   r;   r<    test_calibration_display_computey  s4   
r   c           	      C   sz   |\}}t t t }||| t|||}|jdg}|j 	 }t
|t
|ks.J |D ]
}| |v s:J q0d S )Nr   )r"   r$   r   rT   r   r   r   r   r   r   rk   r   )	r   r   r9   r:   r^   r   r   r   r   r;   r;   r<   $test_plot_calibration_curve_pipeline  s   
r   zname, expected_label)NZ_line1)my_estr   c           
      C   s   t g d}t g d}t g }t||||d}|  |d u r%g n|g}|d |j  }t|t|ks>J |D ]
}	|		 |v sJJ q@d S )Nr   rJ   rJ   r   r   r   r   皙?r   r   )
rO   r   r   plotappendr   r   r   rk   r   )
r   nameZexpected_labelr   r   r   r   r   r   r   r;   r;   r<   'test_calibration_display_default_labels  s   

r   c           	      C   s   t g d}t g d}t g }d}t||||d}|j|ks$J d}|j|d |dg}|j  }t|t|ksAJ |D ]
}|	 |v sMJ qCd S )Nr   r   zname oner   zname twor   r   )
rO   r   r   r   r   r   r   r   rk   r   )	r   r   r   r   r   r   r   r   r   r;   r;   r<   )test_calibration_display_label_class_plot  s   
r   constructor_namer   Zfrom_predictionsc                 C   s  |\}}d}t  ||}||d d df }tt| }| dkr&|||fn||f}	||	d|i}
|
j|ks8J |d |
  |dg}|
j	 
 }t|t|ksVJ |D ]
}| |v sbJ qX|d d}|
j|d t|t|kszJ |D ]
}| |v sJ q|d S )	Nzmy hand-crafted namerJ   r   r   r   r   Zanother_namer   )r   rT   rU   getattrr   r   closer   r   r   r   rk   r   )r   r   r   r9   r:   Zclf_namer^   r   constructorparamsr   r   r   r   r;   r;   r<   ,test_calibration_display_name_multiple_calls  s,   


r  c           	      C   sj   |\}}t  ||}t ||}t|||}tj||||jd}|j d }|ddks3J d S )N)axrJ   r   )r   rT   r&   r   r   r   Zget_legend_handles_labelscount)	r   r   r9   r:   r   dtr   Zviz2r   r;   r;   r<   !test_calibration_display_ref_line  s   r  dtype_y_strc                 C   s~   t jd}t jdgd dgd  | d}|jdd|jd}d	}tjt|d
 t	|| W d   dS 1 s8w   Y  dS )zKCheck error message when a `pos_label` is not specified with `str` targets.r2   spamr   eggsrC   dtyper   rF   zy_true takes value in {'eggs', 'spam'} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitlyrp   N)
rO   rP   rQ   r   randintrG   rV   rW   rX   r   )r	  rngy1y2err_msgr;   r;   r<   *test_calibration_curve_pos_label_error_str	  s   "r  c                 C   s   t g d}t jddg| d}|| }t g d}t||dd\}}t|g d t||ddd	\}}t|g d t|d
| ddd	\}}t|g d t|d
| ddd	\}}t|g d dS )z8Check the behaviour when passing explicitly `pos_label`.)	r   r   r   rJ   rJ   rJ   rJ   rJ   rJ   r
  eggr  )	rt   r   g333333?r   r   gffffff?r   r   r   r   r   )r   r   rJ   rJ   )r   	pos_labelrJ   r   )r   r   r   rJ   N)rO   r   r   r   )r	  r{   r   Z
y_true_strr   r   r   r;   r;   r<    test_calibration_curve_pos_label  s   r  kwargsred-.)cZlwZls)colorZ	linewidthZ	linestylec                 C   sf   |\}}t  ||}tj|||fi |}|j dksJ |j dks(J |j dks1J dS )z*Check that matplotlib aliases are handled.r  rC   r  N)r   rT   r   r   r   Z	get_colorZget_linewidthZget_linestyle)r   r   r  r9   r:   r   r   r;   r;   r<   test_calibration_display_kwargs.  s   	r  zpos_label, expected_pos_label))NrJ   r   )rJ   rJ   c                 C   s   |\}}t  ||}tj||||d}||dd|f }t|||d\}	}
t|j|	 t|j|
 t|j	| |j
 d| dksGJ |j
 d| dksTJ |jjdg}|j
  }t|t|kskJ |D ]
}| |v swJ qmdS )z?Check the behaviour of `pos_label` in the `CalibrationDisplay`.)r  Nz,Mean predicted probability (Positive class: )z'Fraction of positives (Positive class: r   )r   rT   r   r   rU   r   r   r   r   r   r   r   r   	__class__r   r   r   rk   r   )r   r   r  Zexpected_pos_labelr9   r:   r   r   r   r   r   r   r   r   r;   r;   r<   "test_calibration_display_pos_labelA  s,   

r  c                 C   sN  t dd\}}t |}|dd |dd }}t|d }tj|jd d |jd f|jd}||dddddf< ||dddddf< tj|jd d |jd}||ddd< ||ddd< t }t	|| |dd	}t
|}	|	j|||d
 ||| t|	j|jD ]\}
}t|
jj|jj q|	|}||}t|| dS )zrCheck that passing repeating twice the dataset `X` is equivalent to
    passing a `sample_weight` with a factor 2.Tr   Nr   rC   r   rJ   r  )r?   rB   rL   rH   )r   r$   Zfit_transformrO   	ones_liker   rz   r  r   r   r   rT   r   rd   r   re   Zcoef_rU   )r?   rB   r9   r:   rI   ZX_twiceZy_twicere   Zcalibrated_clf_without_weightsZcalibrated_clf_with_weightsZest_with_weightsZest_without_weightsZy_pred_with_weightsZy_pred_without_weightsr;   r;   r<   ?test_calibrated_classifier_cv_double_sample_weights_equivalenceb  s>   $

r!  fit_params_typelistr   c                 C   sL   |\}}t || t || d}tddgd}t|}|j||fi | dS )zTests that fit_params are passed to the underlying base estimator.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/12384
    )abr$  r%  )Zexpected_fit_paramsN)r(   r'   r   rT   )r"  r=   r9   r:   
fit_paramsr^   pc_clfr;   r;   r<    test_calibration_with_fit_params  s   r(  rI   r   c                 C   s.   |\}}t dd}t|}|j||| d dS )zMTests that sample_weight is passed to the underlying base
    estimator.
    T)Zexpected_sample_weightrH   N)r'   r   rT   )rI   r=   r9   r:   r^   r'  r;   r;   r<   -test_calibration_with_sample_weight_estimator  s   
r)  c                 C   sp   | \}}t |}G dd dt}| }t|}tt |j|||d W d   dS 1 s1w   Y  dS )zCheck that even if the estimator doesn't support
    sample_weight, fitting with sample_weight still works.

    There should be a warning, since the sample_weight is not passed
    on to the estimator.
    c                          e Zd Z fddZ  ZS )zPtest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeightc                    s"   d|vsJ t  j||fi |S )NrI   superrT   )r   r9   r:   r&  r  r;   r<   rT     s   zTtest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeight.fitr   r   r   rT   __classcell__r;   r;   r-  r<   ClfWithoutSampleWeight      r0  rH   N)rO   r   r'   r   rV   ZwarnsUserWarningrT   )r=   r9   r:   rI   r0  r^   r'  r;   r;   r<   0test_calibration_without_sample_weight_estimator  s   
"r3  c                 C   s>   G dd dt }t| dj| dtt| d d i dS )z[Check that CalibratedClassifierCV does not enforce sample alignment
    for fit parameters.c                       s   e Zd Zd fdd	Z  ZS )zJtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifierNc                    s   |d usJ t  j|||dS )NrH   r+  )r   r9   r:   rI   	fit_paramr-  r;   r<   rT     s   zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit)NNr.  r;   r;   r-  r<   TestClassifier  s    r5  r   r4  rJ   N)r   r   rT   rO   r   rk   )r=   r5  r;   r;   r<   2test_calibration_with_non_sample_aligned_fit_param  s   
r6  c                 C   s&  d}d}t j| j|d}t dgt||  dg|t||    }d|d | }td|d	d
}|||}|D ]*\}}	|| || }
}||	 }t	d| d}|
|
| ||}|dk sgJ q=tt	d| ddd}t|||dd}tt	d| ddd}t|||dd}t|| dS )zTest that :class:`CalibratedClassifierCV` works with large confidence
    scores when using the `sigmoid` method, particularly with the
    :class:`SGDClassifier`.

    Non-regression test for issue #26766.
    gq=
ףp?i  rF   rJ   r   g     j@)r   rJ   NT)rL   r:   
classifierZsquared_hinge)Zlossr6   g     @r@   r   Zroc_auc)ZscoringrA   )rO   rP   Zdefault_rngnormalr   intr   r   splitr   rT   r   anyr   r   r   )global_random_seedZprobnZrandom_noiser:   r9   rL   indicesZtraintestrY   rZ   r\   Zsgd_clfpredictionsZclf_sigmoidZscore_sigmoidZclf_isotonicZscore_isotonicr;   r;   r<   @test_calibrated_classifier_cv_works_with_large_confidence_scores  s2   	.


rA  c                 C   s   t jj| d}d}|jdd|d}|jdddd}d}t|||d	\}}d
}t|||d	\}	}
t||d\}}d}t||	|d t|	||d t||
|d t|
||d d S )NrD   r   r   rC   rF   )lowhighrG   rt   )r@  r:   Zmax_abs_prediction_thresholdr   )r@  r:   gư>)atol)rO   rP   rQ   r  rR   r	   r   )r<  r6   r=  r:   Zpredictions_smallZthreshold_1Za1b1Zthreshold_2Za2b2a3Zb3rE  r;   r;   r<   5test_sigmoid_calibration_max_abs_prediction_threshold  s2   


rI  c                 C   s,   G dd dt }| }t|}|j|   dS )zoCheck that CalibratedClassifierCV works with float32 predict proba.

    Non-regression test for gh-28245.
    c                       r*  )z4test_float32_predict_proba.<locals>.DummyClassifer32c                    s   t  |tjS r   )r,  rU   ZastyperO   Zfloat32r   r-  r;   r<   rU   ?  s   zBtest_float32_predict_proba.<locals>.DummyClassifer32.predict_proba)r   r   r   rU   r/  r;   r;   r-  r<   DummyClassifer32>  r1  rJ  N)r   r   rT   )r=   rJ  modelr   r;   r;   r<   test_float32_predict_proba8  s   rL  c                  C   s8   t jjdd} dgd dgd  }tdd| | dS )	zlCheck that CalibratedClassifierCV works with string targets.

    non-regression test for issue #28841.
    )   r   rF   r$  r   r%  r   rc   N)rO   rP   r8  r   rT   r8   r;   r;   r<   (test_error_less_class_samples_than_foldsH  s   rN  )ZnumpyrO   rV   Znumpy.testingr   Zsklearn.baser   r   r   Zsklearn.calibrationr   r   r   r	   r
   r   Zsklearn.datasetsr   r   r   Zsklearn.dummyr   Zsklearn.ensembler   r   Zsklearn.exceptionsr   Zsklearn.feature_extractionr   Zsklearn.frozenr   Zsklearn.imputer   Zsklearn.isotonicr   Zsklearn.linear_modelr   r   Zsklearn.metricsr   Zsklearn.model_selectionr   r   r   r   r   r   Zsklearn.naive_bayesr    Zsklearn.pipeliner!   r"   Zsklearn.preprocessingr#   r$   Zsklearn.svmr%   Zsklearn.treer&   Zsklearn.utils._mockingr'   Zsklearn.utils._testingr(   r)   r*   r+   r,   Zsklearn.utils.extmathr-   Zsklearn.utils.fixesr.   r7   Zfixturer=   markZparametrizerb   rh   rn   rr   rw   rx   r   r   r   FutureWarningr   r   r   r   r   r   r   rP   rQ   r   r   r   r   r   paramr   r   r   r   r   r   r   r   r   r  r  r   objectr  r  r  r  r!  r(  r   r)  r3  r6  rA  rI  rL  rN  r;   r;   r;   r<   <module>   s     

;

=8











)

"





 0

2)