o
    tBh:                  
   @   s  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	 d dl
mZ d dlmZ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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"m#Z#m$Z$ d dl%m&Z&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l0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> d dl?m@Z@ d dlmAZA dZBe jCdddd ZDe jEFd d!d"ge jEFd#d$d%gd&d' ZGe jEFd#d$d%gd(d) ZHe jEFd#d$d%gd*d+ ZId,d- ZJe jEFd#d$d%gd.d/ ZKe jEFd d!d"ge jEFd#d$d%gd0d1 ZLe jEFd d!d"ge jEFd#d$d%gd2d3 ZMe jEFd d!d"ge jEFd#d$d%ge jEFd4eNd5d6d7 ZOd8d9 ZPd:d; ZQe jEFd d!d"gd<d= ZRd>d? ZSd@dA ZTdBdC ZUe jEFd#d$d%gdDdE ZVe jEFd#d$d%gdFdG ZWe jEFd#d$d%gdHdI ZXe jEFdJejYZdK[dLdMd5ejYZdK[dLdMd5dNgdOdP Z\e jCdQdR Z]e jCdSdT Z^dUdV Z_e jEFdWe `e+dXdYd5e `e+dXdYdZgd[d\ Zad]d^ Zbd_d` Zce jCdddadb Zde jCdddcdd Zededf Zfe jEFdgdhdigdjdk Zge jEFdldMdmge jEFdndodpgdqdr Zhdsdt Zie jEFdudvdwgdxdy Zjdzd{ Zke jEFdgdhdigd|d} Zld~d Zme jEFdeneogdd Zpe jEFdeneogdd Zqe jEFdg ddd Zre jEFd d!d"ge jEFd#d$d%gdd Zse jEFdddgdd Zte jEFddgeB eueBgdd Zvdd Zwdd Zxe jEFd d!d"ge jEFd#d$d%gdd ZydS )    N)assert_allclose)sparse)BaseEstimatorclone)DummyClassifier)LeaveOneOuttrain_test_split)assert_array_almost_equalassert_almost_equalassert_array_equal)softmax)NotFittedError)make_classification
make_blobs	load_iris)LabelEncoder)KFoldcross_val_predict)MultinomialNB)RandomForestClassifierRandomForestRegressorVotingClassifier)LogisticRegressionLinearRegression)DecisionTreeClassifier)	LinearSVC)Pipelinemake_pipeline)StandardScaler)IsotonicRegression)DictVectorizer)SimpleImputer)brier_score_loss)_CalibratedClassifier_SigmoidCalibration_sigmoid_calibrationCalibratedClassifierCVCalibrationDisplaycalibration_curve)CheckingClassifier)_convert_container   module)scopec                  C   s   t tddd\} }| |fS )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLES)Xy r7   u/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/tests/test_calibration.pydata5   s   r9   methodsigmoidisotonicensembleTFc                 C   s6  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t|t|
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   r/   seedsizesample_weight   cvr=      r:   rG   r=   r;   )r4   nprandomRandomStateuniformrB   minr   fitpredict_probar&   pytestraises
ValueErrorr   
csr_matrixr"   r	   )r9   r:   r=   r1   r5   r6   rD   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_relabeledr7   r7   r8   test_calibration;   sF   (





ra   c                 C   sX   | \}}t  }t|d|d}tt ||| W d    d S 1 s%w   Y  d S )Nfoor:   r=   )r   r&   rQ   rR   rS   rO   )r9   r=   r5   r6   rZ   clf_invalid_methodr7   r7   r8   test_calibration_bad_methodx   s   "re   c                 C   sR   | \}}t t |d}tt ||| W d    d S 1 s"w   Y  d S )N)r=   )r&   r   rQ   rR   RuntimeErrorrO   )r9   r=   r5   r6   clf_base_regressorr7   r7   r8   test_calibration_regressor   s   "rh   c                 C   s<   | \}}t dd}||| |jd j}t|tsJ d S )Nr>   rG   r   )r&   rO   calibrated_classifiers_base_estimator
isinstancer   )r9   r5   r6   	calib_clfbase_estr7   r7   r8   "test_calibration_default_estimator   s
   
ro   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 )NrH   )n_splitsrF   rE   )r   r&   rl   rG   rp   rO   lenrj   )r9   r=   r5   r6   splitskfoldrm   expected_n_clfr7   r7   r8   test_calibration_cv_splitter   s   
ru   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 )	Nr>   r/   r?   rA   r3   rc   rC   皙?)r4   rJ   rK   rL   rM   rq   r   r&   rO   rP   linalgnorm)r9   r:   r=   r1   r5   r6   rD   rU   rV   rW   rX   rk   calibrated_clfprobs_with_swprobs_without_swdiffr7   r7   r8   test_sample_weight   s   (


r~   c                 C   s   | \}}t ||dd\}}}}tdd}	t|	|d|d}
|
|| |
|}t|	|d|d}||| ||}t|| dS )zTest parallel calibrationr/   rv   r>   )r:   n_jobsr=   rE   N)r   r   r&   rO   rP   r   )r9   r:   r=   r5   r6   rU   rX   rV   rY   rk   cal_clf_parallelprobs_parallelcal_clf_sequentialprobs_sequentialr7   r7   r8   test_parallel_execution   s   


r   r@   r>   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 )Nr>   r   )rJ   eyesumshape)y_true
proba_pred	n_classesY_onehotr7   r7   r8   multiclass_brier   s   z5test_calibration_multiclass.<locals>.multiclass_brier   rv   i  d   
         .@r1   r2   r3   centerscluster_stdr>   r   rE   rH   rI   axis?gffffff?)r   g?   r/   )n_estimatorsr3   )r   r   rJ   uniquer   rO   r&   rP   r   r   onesrq   scorer   decision_functionr   )r:   r=   r@   r   rZ   r5   r6   r   rU   rV   rX   rY   r\   probasuncalibrated_briercalibrated_brier	clf_probscal_clf_probsr7   r7   r8   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   )rJ   zerosr   selfr5   r7   r7   r8   predict#  s   zAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predictN)__name__
__module____qualname__r   r7   r7   r7   r8   ZeroCalibrator!  s    r   2   r   r   r   r   )rk   calibratorsclasses      ?)r   r   rO   r#   classes_rP   r   
n_classes_)r   r5   r6   rZ   
calibratorr\   r   r7   r7   r8   !test_calibration_zero_probability  s   



r   c               
   C   s  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t|t|
ffD ]N\}}dD ]G}t||d
d}|	dfD ]9}|j|||d ||}||}|dddf }t|tddgtj|dd  t||t||ksJ qqqdS )z*Test calibration for prefitted classifiersr      r.   r/   r0   r?   rA   Nr>   prefitri   rE   )r<   r;   )r:   rG   rC   r   r   )r   rJ   rK   rL   rM   rB   rN   r   r&   rQ   rR   r   rO   rP   r   rT   r   r   arrayargmaxr"   )r1   r5   r6   rD   rU   rV   rW   X_caliby_calibsw_calibrX   rY   rZ   	unfit_clfr[   this_X_calibr^   r:   r\   swy_proby_predr_   r7   r7   r8   test_calibration_prefit5  sD   (
"

"
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   rv   r   FrI   r   )rG   r:   r<   clip)out_of_boundsrE   )
r   r&   rO   rP   r   r   r$   r   r   r   )r9   r:   r5   r6   rZ   r\   
cal_probasunbiased_predsr   clf_dfmanual_probasr7   r7   r8   test_calibration_ensemble_falsed  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)rH   r   )rE   r   gj=ɿgY90(?r   r   r   rE   r.   N)rJ   r   r	   r%   expr$   rO   r   rQ   rR   rS   vstack)exFexYAB_lin_libsvmlin_probsk_probr7   r7   r8   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   rE   rE   rE           rw   皙?皙??r   r>   n_binsr   rE   rw   r   gN)r   r   r   r   rE   rE   )r   rw   r         ?r   r   quantiler   strategygUUUUUU?r   
percentile)r   )rJ   r   r(   rq   r
   rQ   rR   rS   )r   r   	prob_true	prob_predy_true2y_pred2prob_true_quantileprob_pred_quantiler7   r7   r8   test_calibration_curve  s,   
"r   c                  C   s   t g d} t g d}tt t| |ddd W d   n1 s&w   Y  tt( t| |d ddd\}}t| |dd\}}t|| t|| W d   dS 1 s[w   Y  dS )	z6Tests the `normalize` parameter of `calibration_curve`r   r   r>   F)r   	normalizeNTr   )rJ   r   rQ   warnsFutureWarningr(   r
   )r   r   prob_true_unnormalizedprob_pred_unnormalizedr   r   r7   r7   r8   .test_calibration_curve_with_unnormalized_proba  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   r>   r   r/   )r1   r2   n_informativen_redundantr3   r   r   imputerrfrE   )r   r<   )rG   r:   r=   N)	r   rJ   nanr   r!   r   r&   rO   r   )r=   r5   r6   rZ   clf_cr7   r7   r8   test_calibration_nan_imputer  s   


r   c                 C   sl   d}t dd|d\}}tddd}t|dt | d	}||| ||}t|jd
dt	|j
d  d S )Nr>   r   rH   )r1   r2   r   r   r   Cr3   r;   rI   rE   r   r   )r   r   r&   r   rO   rP   r	   r   rJ   r   r   )r=   num_classesr5   r6   rZ   clf_probprobsr7   r7   r8   test_calibration_prob_sum  s   

"r   c                 C   s   t jdd}t d}tddd}t|dt | d}||| t|j	D ]M\}}|
|}| rft|d d |f t t| t |d d d |f dksRJ t |d d |d	 d f dkseJ q&t |d	|jd  ssJ q&d S )
Nr   rH   r   r   r   r;   rI   r   rE   )rJ   rK   randnaranger   r&   r   rO   	enumeraterj   rP   r   r   rq   allallcloser   )r=   r5   r6   rZ   r\   icalibrated_classifierprobar7   r7   r8   test_calibration_less_classes  s   


 "(r  r5   r/      rH   r.   c                 C   s2   g d}G dd dt }t| }|| | dS )z;Test that calibration accepts n-dimensional arrays as input)rE   r   r   rE   rE   r   rE   rE   r   r   rE   r   r   rE   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)rJ   r   r   )r   r5   r6   r7   r7   r8   rO   	  s   zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitc                 S   s   | |jd djddS )Nr   r   rE   r   )reshaper   r   r   r7   r7   r8   r     s   zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionN)r   r   r   __doc__rO   r   r7   r7   r7   r8   MockTensorClassifier  s    r  N)r   r&   rO   )r5   r6   r  rz   r7   r7   r8    test_calibration_accepts_ndarray  s   	
r  c                  C   s,   dddddddddg} g d}| |fS )NNYadult)stateageTXVTchild)rE   r   rE   r7   )	dict_datatext_labelsr7   r7   r8   r    s   r  c                 C   s,   | \}}t dt fdt fg}|||S )N
vectorizerrZ   )r   r    r   rO   )r  r5   r6   pipeline_prefitr7   r7   r8   dict_data_pipeline!  s
   r  c                 C   sf   | \}}|}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
    r   ri   n_features_in_N)r&   rO   r   r   hasattrr   rP   )r  r  r5   r6   rZ   rm   r7   r7   r8   test_calibration_dict_pipeline*  s   	
r  zclf, cvrE   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   rH   r>   r   r1   r2   r   r3   r   ri   rE   )r   rO   r&   r   r   r  r   r   )rZ   rG   r5   r6   rm   r   r7   r7   r8   test_calibration_attributesD  s   	r  c                  C   s   t ddddd\} }tdd| |}t|dd	}d
}tjt|d || d d d df | W d    d S 1 s<w   Y  d S )Nr   rH   r>   r   r  rE   r  r   ri   zAX has 3 features, but LinearSVC is expecting 5 features as input.matchr   )r   r   rO   r&   rQ   rR   rS   )r5   r6   rZ   rm   msgr7   r7   r8   2test_calibration_inconsistent_prefit_n_features_in\  s   "r  c                  C   sV   t ddddd\} }tdd tdD d	d
}|| | t|dd}|| | d S )Nr   rH   r>   r   r  c                 S   s   g | ]}d t | t fqS )lr)strr   ).0r   r7   r7   r8   
<listcomp>n  s    z5test_calibration_votingclassifier.<locals>.<listcomp>r   soft)
estimatorsvotingr   )rk   rG   )r   r   rangerO   r&   )r5   r6   voterm   r7   r7   r8   !test_calibration_votingclassifierh  s   r&  c                   C   s
   t ddS )NT
return_X_y)r   r7   r7   r7   r8   	iris_datax  s   
r)  c                 C   s    | \}}||dk  ||dk  fS )Nr>   r7   )r)  r5   r6   r7   r7   r8   iris_data_binary}  s   r*  c           
      C   s   |\}}|\}}t  ||}d}tjt|d t||| W d    n1 s*w   Y  t ||}	d}tjt|d t|	|| W d    n1 sQw   Y  t }	tt	 t|	|| W d    d S 1 sqw   Y  d S )Nz)'estimator' should be a fitted classifierr  z/response method predict_proba is not defined in)
r   rO   rQ   rR   rS   r'   from_estimatorr   r   r   )
pyplotr)  r*  r5   r6   X_binaryy_binaryregr  rZ   r7   r7   r8   #test_calibration_display_validation  s    "r0  constructor_namer+  from_predictionsc                 C   s   |\}}t  }||| ||}|dkr9d}tjt|d t||| W d    d S 1 s2w   Y  d S d}tjt|d t|| W d    d S 1 sTw   Y  d S )Nr+  z"to be a binary classifier, but gotr  z-y should be a 1d array, got an array of shape)	r   rO   rP   rQ   rR   rS   r'   r+  r2  )r,  r)  r1  r5   r6   rZ   r   r  r7   r7   r8   #test_calibration_display_non_binary  s   
""r3  r   r   r   rM   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   alpharE   r   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rO   r'   r+  rP   r(   r   r   r   r   estimator_name
matplotlibrl   line_linesLine2D	get_alphaax_axesAxesfigure_figureFigure
get_xlabel
get_ylabel
get_legend	get_textsrq   get_text)r,  r*  r   r   r5   r6   r  vizr   r   r   mplexpected_legend_labelslegend_labelslabelsr7   r7   r8    test_calibration_display_compute  s4   
rL  c           	      C   sz   |\}}t t t }||| t|||}|jdg}|j 	 }t
|t
|ks.J |D ]
}| |v s:J q0d S )Nr5  )r   r   r   rO   r'   r+  r6  r<  rD  rE  rq   rF  )	r,  r*  r5   r6   rZ   rG  rI  rJ  rK  r7   r7   r8   $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   rE   rE   r   r   r   r   皙?r6  r5  )
rJ   r   r'   plotappendr<  rD  rE  rq   rF  )
r,  nameexpected_labelr   r   r   rG  rI  rJ  rK  r7   r7   r8   '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  r5  )
rJ   r   r'   r6  rT  r<  rD  rE  rq   rF  )	r,  r   r   r   rV  rG  rI  rJ  rK  r7   r7   r8   )test_calibration_display_label_class_plot  s   
rZ  c                 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 namerE   r+  rV  r   r5  another_namerY  )r   rO   rP   getattrr'   r6  closerT  r<  rD  rE  rq   rF  )r1  r,  r*  r5   r6   clf_namerZ   r   constructorparamsrG  rI  rJ  rK  r7   r7   r8   ,test_calibration_display_name_multiple_calls  s,   


ra  c           	      C   sj   |\}}t  ||}t ||}t|||}tj||||jd}|j d }|ddks3J d S )N)axrE   r5  )r   rO   r   r'   r+  r<  get_legend_handles_labelscount)	r,  r*  r5   r6   r  dtrG  viz2rK  r7   r7   r8   !test_calibration_display_ref_line*  s   rg  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.r/   spamr   eggsr>   dtyper   rA   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 explicitlyr  N)
rJ   rK   rL   r   randintrB   rQ   rR   rS   r(   )rh  rngy1y2err_msgr7   r7   r8   *test_calibration_curve_pos_label_error_str7  s   "rr  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   rE   rE   rE   rE   rE   rE   ri  eggrk  )	rw   r   g333333?rR  r   gffffff?r   r   r      r   )r   r   rE   rE   )r   	pos_labelrE   r   )r   r   r   rE   N)rJ   r   r(   r   )rh  r   r   
y_true_strr   r   _r7   r7   r8    test_calibration_curve_pos_labelG  s   rx  zpos_label, expected_pos_label))NrE   r   )rE   rE   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`.)ru  Nz,Mean predicted probability (Positive class: )z'Fraction of positives (Positive class: r5  )r   rO   r'   r+  rP   r(   r   r   r   r   r<  rB  rC  	__class__r   rD  rE  rq   rF  )r,  r*  ru  expected_pos_labelr5   r6   r  rG  r   r   r   rI  rJ  rK  r7   r7   r8   "test_calibration_display_pos_label\  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   r>   r   rE   rk  r:   r=   rG   rC   )r   r   fit_transformrJ   	ones_liker   r   rl  r   r&   r   rO   ziprj   r   rk   coef_rP   )r:   r=   r5   r6   rD   X_twicey_twicerk   calibrated_clf_without_weightscalibrated_clf_with_weightsest_with_weightsest_without_weightsy_pred_with_weightsy_pred_without_weightsr7   r7   r8   ?test_calibrated_classifier_cv_double_sample_weights_equivalence}  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&   rO   )r  r9   r5   r6   
fit_paramsrZ   pc_clfr7   r7   r8    test_calibration_with_fit_params  s   r  rD   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_weightrC   N)r)   r&   rO   )rD   r9   r5   r6   rZ   r  r7   r7   r8   2test_calibration_with_sample_weight_base_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 base_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 base_estimator.
    c                       s   e Zd Z fddZ  ZS )zUtest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeightc                    s"   d|vsJ t  j||fi |S )NrD   )superrO   )r   r5   r6   r  rz  r7   r8   rO     s   zYtest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeight.fit)r   r   r   rO   __classcell__r7   r7   r  r8   ClfWithoutSampleWeight  s    r  rC   N)rJ   r  r)   r&   rQ   r   UserWarningrO   )r9   r5   r6   rD   r  rZ   r  r7   r7   r8   5test_calibration_without_sample_weight_base_estimator  s   
"r  c                 C   s   | \}}d|dd i}t |d}t|}dtt d }tjt|d |j||fi | W d   dS 1 s:w   Y  dS )z]fit_params having different length than data should raise the
    correct error message.
    r  NrH   r  z>Found input variables with inconsistent numbers of samples: \[z, 5\]r  )r)   r&   r  r4   rQ   rR   rS   rO   )r9   r5   r6   r  rZ   r  r  r7   r7   r8   4test_calibration_with_fit_params_inconsistent_length  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 }t|| |dd	}t	|}|j
|||d
 |
|ddd |ddd  t|j|jD ]\}}	t|jj|	jj qe||}
||}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.Tr'  N(   r   Z   rE   r>   r}  rC   )r   r   r~  rJ   r   hstack
zeros_liker   r&   r   rO   r  rj   r   rk   r  rP   )r:   r=   r5   r6   rD   rk   r  r  r  r  r  r  r7   r7   r8   >test_calibrated_classifier_cv_zeros_sample_weights_equivalence  s6   
 

r  )zrQ   numpyrJ   numpy.testingr   scipyr   sklearn.baser   r   sklearn.dummyr   sklearn.model_selectionr   r   sklearn.utils._testingr	   r
   r   sklearn.utils.extmathr   sklearn.exceptionsr   sklearn.datasetsr   r   r   sklearn.preprocessingr   r   r   sklearn.naive_bayesr   sklearn.ensembler   r   r   sklearn.linear_modelr   r   sklearn.treer   sklearn.svmr   sklearn.pipeliner   r   r   sklearn.isotonicr   sklearn.feature_extractionr    sklearn.imputer!   sklearn.metricsr"   sklearn.calibrationr#   r$   r%   r&   r'   r(   sklearn.utils._mockingr)   r*   r4   fixturer9   markparametrizera   re   rh   ro   ru   r~   r   r$  r   r   r   r   r   r   r   r   r   r  rK   rL   r   r  r  r  r  paramr  r  r&  r)  r*  r0  r3  rL  rM  rX  rZ  ra  rg  r  objectrr  rx  r|  r  r  r   r  r  r  r  r7   r7   r7   r8   <module>   s    

;
	


=/
 












)

"


 0

