a
    h                  
   @   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<   Z/var/www/html/assistant/venv/lib/python3.9/site-packages/sklearn/tests/test_calibration.pydata9   s    r>   csr_containermethodsigmoidisotonicensembleTFc                 C   s8  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 s0    Y  ||f||||ffD ]0\}}t
||d|d	}|j||	|
d |	|d d df }t||t||ksZJ |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|  n"t||t|d d |ks J q d 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_clfZthis_X_trainthis_X_testZprob_pos_cal_clfZprob_pos_cal_clf_relabeledr<   r<   r=   test_calibration?   sB    (*




rc   c                 C   s<   | \}}t dd}||| |jd j}t|ts8J d S )NrD   rM   r   )r   rU   calibrated_classifiers_	estimator
isinstancer%   )r>   r:   r;   	calib_clfZbase_estr<   r<   r=   "test_calibration_default_estimator}   s
    
ri   c                 C   sp   | \}}d}t |d}t||d}t|jt s2J |jj|ksBJ ||| |rV|nd}t|j|kslJ d S )NrN   n_splitsrL   rK   )r   r   rg   rM   rk   rU   lenre   )r>   rC   r:   r;   Zsplitskfoldrh   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 sN0    Y  tt dd}tjtdd ||| W d    n1 s0    Y  d S )Ne   rj   TrL   z$Requesting 101-fold cross-validationmatchz!LeaveOneOut cross-validation does)r   r   rW   rX   rY   rU   r   )r>   r:   r;   rm   rh   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sJ d S )	NrD   r3   rE   rG   r7   )r@   rC   rI   皙?)r8   rP   rQ   rR   rS   rl   r%   r   rU   rV   ZlinalgZnorm)r>   r@   rC   r5   r:   r;   rJ   rZ   r[   r\   r]   rf   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 calibrationr3   rs   rD   )r@   Zn_jobsrC   rK   N)r   r"   r$   r%   r   rU   rV   r   )r>   r@   rC   r:   r;   rZ   r]   r[   r^   rf   Zcal_clf_parallelZprobs_parallelZcal_clf_sequentialZprobs_sequentialr<   r<   r=   test_parallel_execution   s    

rx   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 rdk sn J ||
|d||
| ksJ ||t||
|d}||||d}|d| k sLJ 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_trueZ
proba_pred	n_classesZY_onehotr<   r<   r=   multiclass_brier   s    z5test_calibration_multiclass.<locals>.multiclass_brier   rs   i  d   
         .@r5   r6   r7   ZcentersZcluster_stdrD   r   rK   rN   rO   Zaxis?gffffff?)r}   g?   r3   )n_estimatorsr7   )r%   r   rP   uniquer{   rU   r   rV   r   rz   onesrl   Zscorer-   decision_functionr   )r@   rC   rF   r~   r_   r:   r;   r}   rZ   r[   r]   r^   ra   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   )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   )rf   Zcalibratorsclasses      ?)r   r   rU   r   classes_rV   r   Z
n_classes_)r   r:   r;   r_   
calibratorra   r   r<   r<   r=   !test_calibration_zero_probability  s    



r   )categoryc              
   C   s8  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 s0    Y  |||| ||dddf }||f| || |ffD ]\}}dD ]}t||d
d}tt||d}|
dfD ]}|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svJ qvqLq@dS )z*Test calibration for prefitted classifiersr      r2   r3   r4   rE   rG   NrD   prefitrd   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+   arrayZargmaxr   )r?   r5   r:   r;   rJ   rZ   r[   r\   ZX_calibZy_calibZsw_calibr]   r^   r_   Z	unfit_clfr`   Zthis_X_calibrb   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  sL    (
"*





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   FrO   r   )rM   r@   rB   Zclip)Zout_of_boundsrK   )
r%   r   rU   rV   r   r   r
   r   r   r   )r>   r@   r:   r;   r_   ra   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
   n1 s0    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   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sBJ t|dksRJ t|ddg t|ddg tt tdgd	g W d
   n1 s0    Y  t g d}t g d}t||ddd\}}t|t|ksJ t|dksJ t|ddg t|ddg tt t||dd W d
   n1 sF0    Y  d
S )z Check calibration_curve function)r   r   r   rK   rK   rK   )        rt   皙?皙??r   rD   n_binsr   rK   rt   r   gN)r   r   r   r   rK   rK   )r   rt   r         ?r   r   quantiler   strategygUUUUUU?r   Z
percentile)r   )rP   r   r   rl   r)   rW   rX   rY   )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   rD   r   r3   )r5   r6   Zn_informativeZn_redundantr7   r   r   ZimputerrfrK   )r   rB   )rM   r@   rC   N)	r   rP   nanr!   r   r   r   rU   r   )rC   r:   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   rN   rD   )r5   r6   r}   )
rK   rK   rK   rK   rK   r   r   r   r   r   r   r   )Cr7   rA   r   rj   rO   rK   r   )r   r%   r   r   rU   r   rV   rz   )rC   r:   _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
}||| | rt d}tddgdd	gD ]Z\}}|j	| 
|}t|d d |f t t| t |d d ||kf dkspJ qpn.|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   rs   rA   r   rO      r   rD   rK   r   )rP   rQ   randnr&   r   r   rU   Zarangezipre   rV   r+   r   rl   allr*   rz   r   r{   )	rC   r:   r;   r_   ra   r   Zcalib_iZclass_iZ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{   rz   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   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NYadult)stateZageZTXVTchildCTZBR)rK   r   rK   rK   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   rU   )r   r:   r;   Zpipeline_prefitr<   r<   r=   dict_data_pipeline  s
    r   c                 C   sj   | \}}|}t t|dd}||| t|j|j t|drDJ t|drRJ || || 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   rd   n_features_in_N)r   r   rU   r+   r   hasattrr   rV   )r   r   r:   r;   r_   rh   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rht|j| j |j| jksJ n.t |j}t|j| |j|jd ksJ d S )	Nr   rN   rD   r   r5   r6   r}   r7   r   rd   rK   )r   rU   r   r+   r   r   r#   r{   )r_   rM   r:   r;   rh   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    n1 sv0    Y  d S )Nr   rN   rD   r   r   rK   r   zAX has 3 features, but LinearSVC is expecting 5 features as input.rp   r   )r   r%   rU   r   r   rW   rX   rY   )r:   r;   r_   rh   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      z5test_calibration_votingclassifier.<locals>.<listcomp>r   Zsoft)Z
estimatorsZvotingrf   )r   r   rangerU   r   r   )r:   r;   Zvoterh   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 )NrD   r<   )r   r:   r;   r<   r<   r=   iris_data_binarys  s    r   r   r   r   rS   r   c                 C   sR  |\}}t  ||}tj|||||dd}||d d df }t||||d\}	}
t|j|	 t|j|
 t|j	| |j
dksJ dd l}t|j|jjsJ |j dksJ t|j|jjsJ t|j|jjsJ |j dksJ |j dksJ dd	g}|j  }t|t|ks0J |D ]}| |v s4J q4d 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_nameZ
matplotlibrg   line_linesZLine2DZ	get_alphaax_ZaxesZAxesZfigure_ZfigureZFigure
get_xlabel
get_ylabel
get_legend	get_textsrl   get_text)pyplotr   r   r   r:   r;   r   vizr   r   r   Zmplexpected_legend_labelslegend_labelslabelsr<   r<   r=    test_calibration_display_computey  s2    
r   c           	      C   sz   |\}}t t t }||| t|||}|jdg}|j 	 }t
|t
|ks\J |D ]}| |v s`J q`d S )Nr   )r"   r$   r   rU   r   r   r   r   r   r   rl   r   )	r   r   r:   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 rJg n|g}|d |j  }t|t|ks|J |D ]}	|		 |v sJ qd S )Nr   rK   rK   r   r   r   r   皙?r   r   )
rP   r   r   plotappendr   r   r   rl   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sHJ d}|j|d |dg}|j  }t|t|ksJ |D ]}|	 |v sJ qd S )Nr   r   zname oner   zname twor   r   )
rP   r   r   r   r   r   r   r   rl   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rL|||fn||f}	||	d|i}
|
j|kspJ |d |
  |dg}|
j	 
 }t|t|ksJ |D ]}| |v sJ q|d d}|
j|d t|t|ksJ |D ]}| |v sJ qd S )	Nzmy hand-crafted namerK   r   r   r   r   Zanother_namer   )r   rU   rV   getattrr   r   closer   r   r   r   rl   r   )r  r   r   r:   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sfJ d S )N)axrK   r   )r   rU   r&   r   r   r   Zget_legend_handles_labelscount)	r   r   r:   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   n1 sn0    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 explicitlyrp   N)
rP   rQ   rR   r   randintrH   rW   rX   rY   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   rK   rK   rK   rK   rK   rK   r  eggr  )	rt   r   g333333?r   r   gffffff?r   r   r   r   r   )r   r   rK   rK   )r   	pos_labelrK   r   )r   r   r   rK   N)rP   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sPJ |j dksbJ dS )z*Check that matplotlib aliases are handled.r  rD   r  N)r   rU   r   r   r   	get_colorZget_linewidthZget_linestyle)r   r   r  r:   r;   r   r   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sJ |j
 d| dksJ |jjdg}|j
  }t|t|ksJ |D ]}| |v sJ qd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   rU   r   r   rV   r   r   r   r   r   r   r   r   	__class__r   r   r   rl   r   )r   r   r  Zexpected_pos_labelr:   r;   r   r   r   r   r   r   r   r   r<   r<   r=   "test_calibration_display_pos_labelA  s*    

r#  c                 C   sP  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   r  )r@   rC   rM   rI   )r   r$   Zfit_transformrP   	ones_liker   r{   r  r   r   r   rU   r   re   r   rf   Zcoef_rV   )r@   rC   r:   r;   rJ   ZX_twiceZy_twicerf   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   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)Z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   sn   | \}}t |}G dd dt}| }t|}tt  |j|||d W d   n1 s`0    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                       s   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<   r0  r=   ClfWithoutSampleWeight  s   r3  rI   N)rP   r$  r'   r   rW   ZwarnsUserWarningrU   )r>   r:   r;   rJ   r3  r_   r+  r<   r<   r=   0test_calibration_without_sample_weight_estimator  s    
r5  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_paramr0  r<   r=   rU     s    zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit)NNr1  r<   r<   r0  r=   TestClassifier  s   r7  r   r6  rK   N)r   r   rU   rP   r   rl   )r>   r7  r<   r<   r=   2test_calibration_with_non_sample_aligned_fit_param  s    r8  c                 C   s&  d}d}t j| j|d}t dgt||  dg|t||    }d|d | }td|d	d
}|||}|D ]T\}}	|| ||  }
}||	 }t	d| d}|
|
| ||}|dk szJ qzt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;   
classifierZsquared_hinge)Zlossr7   g     @rA   r   Zroc_auc)ZscoringrB   )rP   rQ   Zdefault_rngnormalr   intr   r   splitr   rU   r   anyr   r   r   )global_random_seedZprobnZrandom_noiser;   r:   rM   indicestraintestrZ   r[   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    	.


rD  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   rt   )rC  r;   Zmax_abs_prediction_thresholdr   )rC  r;   gư>)atol)rP   rQ   rR   r  rS   r	   r   )r>  r7   r?  r;   Zpredictions_smallZthreshold_1Za1b1Zthreshold_2Za2b2a3Zb3rH  r<   r<   r=   5test_sigmoid_calibration_max_abs_prediction_threshold  s2    


rL  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                       s   e Zd Z fddZ  ZS )z4test_float32_predict_proba.<locals>.DummyClassifer32c                    s   t  |tjS r   )r/  rV   ZastyperP   Zfloat32r   r0  r<   r=   rV   ?  s    zBtest_float32_predict_proba.<locals>.DummyClassifer32.predict_proba)r   r   r   rV   r2  r<   r<   r0  r=   DummyClassifer32>  s   rM  N)r   r   rU   )r>   rM  modelr   r<   r<   r=   test_float32_predict_proba8  s    rO  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   rd   N)rP   rQ   r:  r   rU   r9   r<   r<   r=   (test_error_less_class_samples_than_foldsH  s    rQ  )numpyrP   rW   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.   r8   Zfixturer>   markZparametrizerc   ri   rn   rr   rw   rx   r   r   r   FutureWarningr   r   r   r   r   r   r   rQ   rR   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-  r5  r8  rD  rL  rO  rQ  r<   r<   r<   r=   <module>   s     

;

=8











)

"





 0

2)