o
    ?Hh                  
   @   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)scopec                  C   s   t tddd\} }| |fS )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLESXy r<   ^/home/air/sanwanet/gpt-api/venv/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   r3   seedsizesample_weight   cvrC      r@   rM   rC   rA   )r8   nprandomRandomStateuniformrH   minr    fitpredict_probar   pytestraises
ValueErrorr   r*   )r>   r@   r?   rC   r5   r:   r;   rJ   X_trainy_trainsw_trainX_testy_testclfprob_pos_clfcal_clfthis_X_trainthis_X_testprob_pos_cal_clfprob_pos_cal_clf_relabeledr<   r<   r=   test_calibration?   sF   (





rf   c                 C   s<   | \}}t dd}||| |jd j}t|tsJ d S )NrD   rM   r   )r   rU   calibrated_classifiers_	estimator
isinstancer%   )r>   r:   r;   	calib_clfbase_estr<   r<   r=   "test_calibration_default_estimator}   s
   
rm   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 )NrN   n_splitsrL   rK   )r   r   rj   rM   ro   rU   lenrh   )r>   rC   r:   r;   splitskfoldrk   expected_n_clfr<   r<   r=   test_calibration_cv_splitter   s   
rt   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   rn   TrL   z$Requesting 101-fold cross-validationmatchz!LeaveOneOut cross-validation does)r   r   rW   rX   rY   rU   r   )r>   r:   r;   rr   rk   r<   r<   r=   test_calibration_cv_nfold   s   
"rx   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 )	NrD   r3   rE   rG   r7   )r@   rC   rI   皙?)r8   rP   rQ   rR   rS   rp   r%   r   rU   rV   linalgnorm)r>   r@   rC   r5   r:   r;   rJ   rZ   r[   r\   r]   ri   calibrated_clfprobs_with_swprobs_without_swdiffr<   r<   r=   test_sample_weight   s   (


r   c                 C   s   | \}}t ||dd\}}}}tt tdd}	t|	|d|d}
|
|| |
|}t|	|d|d}||| ||}t|| dS )zTest parallel calibrationr3   ry   rD   )r@   n_jobsrC   rK   N)r   r"   r$   r%   r   rU   rV   r   )r>   r@   rC   r:   r;   rZ   r]   r[   r^   ri   cal_clf_parallelprobs_parallelcal_clf_sequentialprobs_sequentialr<   r<   r=   test_parallel_execution   s   

r   rF   rD   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 )NrD   r   )rP   eyesumshape)y_true
proba_pred	n_classesY_onehotr<   r<   r=   multiclass_brier   s   z5test_calibration_multiclass.<locals>.multiclass_brier   ry   i  d   
         .@r5   r6   r7   centerscluster_stdrD   r   rK   rN   rO   axis?gffffff?)r   g?   r3   )n_estimatorsr7   )r%   r   rP   uniquer   rU   r   rV   r   r   onesrp   scorer-   decision_functionr   )r@   rC   rF   r   r_   r:   r;   r   rZ   r[   r]   r^   ra   probasuncalibrated_briercalibrated_brier	clf_probs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   )rP   zerosr   selfr:   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   )ri   calibratorsclasses      ?)r   r   rU   r   classes_rV   r   
n_classes_)r   r:   r;   r_   
calibratorra   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      r2   r3   r4   rE   rG   NrD   prefitrg   rK   )rB   rA   )r@   rM   r@   rI   r   r   )r   rP   rQ   rR   rS   rH   rT   r    r   rW   rX   r   rU   rV   r   r   r+   arrayargmaxr   )r?   r5   r:   r;   rJ   rZ   r[   r\   X_caliby_calibsw_calibr]   r^   r_   	unfit_clfr`   this_X_calibrc   r@   cal_clf_prefitcal_clf_frozenswy_prob_prefity_prob_frozeny_pred_prefity_pred_frozenprob_pos_cal_clf_prefit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   ry   r   FrO   r   )rM   r@   rB   clip)out_of_boundsrK   )
r%   r   rU   rV   r   r   r
   r   r   r   )r>   r@   r:   r;   r_   ra   
cal_probasunbiased_predsr   clf_df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)rN   r   )rK   r   gj=ɿgY90(?r   r   r   rK   r2   N)rP   r   r*   r	   expr
   rU   r   rW   rX   rY   vstack)exFexYAB_lin_libsvmlin_prob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   rK   rK   rK   )        rz   皙?皙??r   rD   n_binsr   rK   rz   r   gN)r   r   r   r   rK   rK   )r   rz   r         ?r   r   quantiler   strategygUUUUUU?r   
percentile)r   )rP   r   r   rp   r)   rW   rX   rY   )r   y_pred	prob_true	prob_predy_true2y_pred2prob_true_quantile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   rD   r   r3   )r5   r6   n_informativen_redundantr7   r   r   imputerrfrK   )r   rB   )rM   r@   rC   N)	r   rP   nanr!   r   r   r   rU   r   )rC   r:   r;   r_   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   rN   rD   )r5   r6   r   )
rK   rK   rK   rK   rK   r   r   r   r   r   r   r   )Cr7   rA   r   rn   rO   rK   r   )r   r%   r   r   rU   r   rV   r   )rC   r:   _r;   r_   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   rN   )r   r   r   rK   )rK   rK   rD   rD   )rD   r   r   r   r   ry   rA   r   rO      r   rD   rK   r   )rP   rQ   randnr&   r   r   rU   arangeziprh   rV   r+   r   rp   allr*   r   r   r   )	rC   r:   r;   r_   ra   r   calib_iclass_iprobar<   r<   r=   test_calibration_less_classes  s    

 $
"r  r:   r3      rN   r2   c                 C   s4   g d}G dd dt t}t| }|| | dS )z;Test that calibration accepts n-dimensional arrays as input)rK   r   r   rK   rK   r   rK   rK   r   r   rK   r   r   rK   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)rP   r   r   )r   r:   r;   r<   r<   r=   rU     s   zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitc                 S   s   | |jd djddS )Nr   r   rK   r   )reshaper   r   r   r<   r<   r=   r     s   zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionN)r   r   r   __doc__rU   r   r<   r<   r<   r=   MockTensorClassifier  s    r	  N)r   r   r   rU   )r:   r;   r	  r}   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 )
NNYadult)stateageTXVTchildCTBR)rK   r   rK   rK   r   r<   )	dict_datatext_labelsr<   r<   r=   r  
  s   r  c                 C   s,   | \}}t dt fdt fg}|||S )N
vectorizerr_   )r!   r   r   rU   )r  r:   r;   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
    rD   rg   n_features_in_N)r   r   rU   r+   r   hasattrr   rV   )r  r  r:   r;   r_   rk   r<   r<   r=   test_calibration_dict_pipeline   s   	
r  zclf, cvrK   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   rN   rD   r   r5   r6   r   r7   r   rg   rK   )r   rU   r   r+   r   r  r#   r   )r_   rM   r:   r;   rk   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   rN   rD   r   r  rK   r  zAX has 3 features, but LinearSVC is expecting 5 features as input.rv   r   )r   r%   rU   r   r   rW   rX   rY   )r:   r;   r_   rk   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   rN   rD   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   soft)
estimatorsvotingri   )r   r   rangerU   r   r   )r:   r;   voterk   r<   r<   r=   !test_calibration_votingclassifier^  s   r,  c                   C   s
   t ddS )NT
return_X_y)r   r<   r<   r<   r=   	iris_datan  s   
r/  c                 C   s    | \}}||dk  ||dk  fS )NrD   r<   )r/  r:   r;   r<   r<   r=   iris_data_binarys  s   r0  r   r   r   rS   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   alpharK   r   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rU   r   from_estimatorrV   r   r   r   r   y_probestimator_name
matplotlibrj   line_linesLine2D	get_alphaax_axesAxesfigure_figureFigure
get_xlabel
get_ylabel
get_legend	get_textsrp   get_text)pyplotr0  r   r   r:   r;   r!  vizr4  r   r   mplexpected_legend_labelslegend_labelslabelsr<   r<   r=    test_calibration_display_computey  s4   
rL  c           	      C   sz   |\}}t t t }||| t|||}|jdg}|j 	 }t
|t
|ks.J |D ]
}| |v s:J q0d S )Nr2  )r"   r$   r   rU   r   r3  r5  r;  rC  rD  rp   rE  )	rF  r0  r:   r;   r_   rG  rI  rJ  rK  r<   r<   r=   $test_plot_calibration_curve_pipeline  s   
rM  zname, expected_label)N_line1)my_estrO  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   rK   rK   r   r   r   r   皙?r5  r2  )
rP   r   r   plotappendr;  rC  rD  rp   rE  )
rF  nameexpected_labelr   r   r4  rG  rI  rJ  rK  r<   r<   r=   'test_calibration_display_default_labels  s   

rX  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 )NrP  rQ  zname onerS  zname tworV  r2  )
rP   r   r   r5  rT  r;  rC  rD  rp   rE  )	rF  r   r   r4  rV  rG  rI  rJ  rK  r<   r<   r=   )test_calibration_display_label_class_plot  s   
rZ  constructor_namer3  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 namerK   r3  rV  r   r2  another_namerY  )r   rU   rV   getattrr   r5  closerT  r;  rC  rD  rp   rE  )r[  rF  r0  r:   r;   clf_namer_   r4  constructorparamsrG  rI  rJ  rK  r<   r<   r=   ,test_calibration_display_name_multiple_calls  s,   


rc  c           	      C   sj   |\}}t  ||}t ||}t|||}tj||||jd}|j d }|ddks3J d S )N)axrK   r2  )r   rU   r&   r   r3  r;  get_legend_handles_labelscount)	rF  r0  r:   r;   r!  dtrG  viz2rK  r<   r<   r=   !test_calibration_display_ref_line  s   ri  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.r3   spamr   eggsrD   dtyper   rG   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 explicitlyrv   N)
rP   rQ   rR   r   randintrH   rW   rX   rY   r   )rj  rngy1y2err_msgr<   r<   r=   *test_calibration_curve_pos_label_error_str	  s   "rt  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   rK   rK   rK   rK   rK   rK   rk  eggrm  )	rz   r   g333333?rR  r   gffffff?r   r   r   r   r   )r   r   rK   rK   )r   	pos_labelrK   r   )r   r   r   rK   N)rP   r   r   r   )rj  r   r   
y_true_strr   r   r   r<   r<   r=    test_calibration_curve_pos_label  s   rx  kwargsred-.)clwls)color	linewidth	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.rz  rD   r{  N)r   rU   r   r3  r7  	get_colorget_linewidthget_linestyle)rF  r0  ry  r:   r;   r!  rG  r<   r<   r=   test_calibration_display_kwargs.  s   	r  zpos_label, expected_pos_label))NrK   r   )rK   rK   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`.)rv  Nz,Mean predicted probability (Positive class: )z'Fraction of positives (Positive class: r2  )r   rU   r   r3  rV   r   r   r   r   r4  r;  rA  rB  	__class__r   rC  rD  rp   rE  )rF  r0  rv  expected_pos_labelr:   r;   r!  rG  r4  r   r   rI  rJ  rK  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   rD   r   rK   rm  )r@   rC   rM   rI   )r   r$   fit_transformrP   	ones_liker   r   rn  r   r   r   rU   r   rh   r   ri   coef_rV   )r@   rC   r:   r;   rJ   X_twicey_twiceri   calibrated_clf_without_weightscalibrated_clf_with_weightsest_with_weightsest_without_weightsy_pred_with_weights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  )expected_fit_paramsN)r(   r'   r   rU   )r  r>   r:   r;   
fit_paramsr_   pc_clfr<   r<   r=    test_calibration_with_fit_params  s   r  rJ   r   c                 C   s.   |\}}t dd}t|}|j||| d dS )zMTests that sample_weight is passed to the underlying base
    estimator.
    T)expected_sample_weightrI   N)r'   r   rU   )rJ   r>   r:   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 )NrJ   superrU   )r   r:   r;   r  r  r<   r=   rU     s   zTtest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeight.fitr   r   r   rU   __classcell__r<   r<   r  r=   ClfWithoutSampleWeight      r  rI   N)rP   r  r'   r   rW   warnsUserWarningrU   )r>   r:   r;   rJ   r  r_   r  r<   r<   r=   0test_calibration_without_sample_weight_estimator  s   
"r  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 )NrI   r  )r   r:   r;   rJ   	fit_paramr  r<   r=   rU     s   zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit)NNr  r<   r<   r  r=   TestClassifier  s    r  r)  r  rK   N)r   r   rU   rP   r   rp   )r>   r  r<   r<   r=   2test_calibration_with_non_sample_aligned_fit_param  s   
r  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  rG   rK   r   g     j@)r   rK   NT)rM   r;   
classifiersquared_hinge)lossr7   g     @rA   r   roc_auc)scoringrB   )rP   rQ   default_rngnormalr   intr  r   splitr   rU   r   anyr   r   r   )global_random_seedprobnrandom_noiser;   r:   rM   indicestraintestrZ   r[   r]   sgd_clfpredictionsclf_sigmoidscore_sigmoidclf_isotonicscore_isotonicr<   r<   r=   @test_calibrated_classifier_cv_works_with_large_confidence_scores  s2   	.


r  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 )NrE   r   r   rD   rG   )lowhighrH   rz   )r  r;   max_abs_prediction_thresholdr   )r  r;   gư>)atol)rP   rQ   rR   ro  rS   r	   r   )r  r7   r  r;   predictions_smallthreshold_1a1b1threshold_2a2b2a3b3r  r<   r<   r=   5test_sigmoid_calibration_max_abs_prediction_threshold  s2   


r  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  rV   astyperP   float32r   r  r<   r=   rV   ?  s   zBtest_float32_predict_proba.<locals>.DummyClassifer32.predict_proba)r   r   r   rV   r  r<   r<   r  r=   DummyClassifer32>  r  r  N)r   r   rU   )r>   r  modelr   r<   r<   r=   test_float32_predict_proba8  s   r  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   rG   r  r   r  r   rg   N)rP   rQ   r  r   rU   r9   r<   r<   r=   (test_error_less_class_samples_than_foldsH  s   r  )numpyrP   rW   numpy.testingr   sklearn.baser   r   r   sklearn.calibrationr   r   r   r	   r
   r   sklearn.datasetsr   r   r   sklearn.dummyr   sklearn.ensembler   r   sklearn.exceptionsr   sklearn.feature_extractionr   sklearn.frozenr   sklearn.imputer   sklearn.isotonicr   sklearn.linear_modelr   r   sklearn.metricsr   sklearn.model_selectionr   r   r   r   r   r   sklearn.naive_bayesr    sklearn.pipeliner!   r"   sklearn.preprocessingr#   r$   sklearn.svmr%   sklearn.treer&   sklearn.utils._mockingr'   sklearn.utils._testingr(   r)   r*   r+   r,   sklearn.utils.extmathr-   sklearn.utils.fixesr.   r8   fixturer>   markparametrizerf   rm   rt   rx   r   r   r*  r   r   FutureWarningr   r   r   r   r   r   r  rQ   rR   r   r
  r  r  r  paramr  r   r,  r/  r0  rL  rM  rX  rZ  rc  ri  r"  objectrt  rx  r  r  r  r  r   r  r  r  r  r  r  r  r<   r<   r<   r=   <module>   s     

;

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