
     h              
          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# 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l/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= d dl>m?Z? d dlm@Z@ dZA e jB        d          d             ZCe jD        E                    dd d!g          e jD        E                    d"d#d$g          d%                         ZFd& ZGe jD        E                    d"d#d$g          d'             ZHe jD        E                    dd d!g          e jD        E                    d"d#d$g          d(                         ZIe jD        E                    dd d!g          e jD        E                    d"d#d$g          d)                         ZJe jD        E                    dd d!g          e jD        E                    d"d#d$g          e jD        E                    d* eKd+                    d,                                     ZLd- ZMd. ZNe jD        E                    dd d!g          d/             ZOd0 ZPd1 ZQd2 ZRe jD        E                    d"d#d$g          d3             ZSe jD        E                    d"d#d$g          d4             ZTe jD        E                    d"d#d$g          d5             ZUe jD        E                    d6ejV        W                    d7          X                    d8d9d+          ejV        W                    d7          X                    d8d9d+d:          g          d;             ZYe jB        d<             ZZe jB        d=             Z[d> Z\e jD        E                    d? e j]         e*d@A          d+           e j]         e*d@A          dB          g          dC             Z^dD Z_dE Z` e jB        d          dF             Za e jB        d          dG             ZbdH Zce jD        E                    dIdJdKg          dL             Zde jD        E                    dMd9dNg          e jD        E                    dOdPdQg          dR                         ZedS Zfe jD        E                    dTdUdVg          dW             ZgdX Zhe jD        E                    dIdJdKg          dY             ZidZ Zje jD        E                    d[ekelg          d\             Zme jD        E                    d[ekelg          d]             Zne jD        E                    d^g d_          d`             Zoe jD        E                    dd d!g          e jD        E                    d"d#d$g          da                         Zpe jD        E                    dbdcddg          de             Zqe jD        E                    dfdggeAz   ejr        eA          g          dh             Zsdi Ztdj Zue jD        E                    dd d!g          e jD        E                    d"d#d$g          dk                         Zvdl Zwdm ZxdS )n    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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                  >    t          t          dd          \  } }| |fS )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLES)Xys     Z/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/tests/test_calibration.pydatar9   4   s#    qrRRRDAqa4K    methodsigmoidisotonicensembleTFc                    t           dz  }| \  }}t          j                            d                              |j                  }||                                z  }|d |         |d |         |d |         }	}}||d          ||d          }}
t          d                              |||	          }|	                    |
          d d df         }t          ||j        dz   |	          }t          j        t                    5  |                    ||           d d d            n# 1 swxY w Y   ||
ft          j        |          t          j        |
          ffD ]\  }}t          ||d
|          }|                    |||	           |	                    |          d d df         }t!          ||          t!          ||          k    sJ |                    ||dz   |	           |	                    |          d d df         }t#          ||           |                    |d|z  dz
  |	           |	                    |          d d df         }t#          ||           |                    ||dz   dz  |	           |	                    |          d d df         }|dk    rt#          |d|z
             ct!          ||          t!          |dz   dz  |          k    sJ d S )N   r0   seedsizeTforce_alphasample_weight   cvr>      r;   rK   r>   r<   )r5   nprandomRandomStateuniformrD   minr   fitpredict_probar&   pytestraises
ValueErrorr   
csr_matrixr"   r
   )r9   r;   r>   r2   r6   r7   rH   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_relabeleds                      r8   test_calibrationre   :   s    QIDAqI))r)22:::GGMLA "#:I:*9*}ZiZ?XhWGyzz]AijjMFF D
)
)
)
-
-gwh
-
W
WC$$V,,QQQT2L$SQVaZ(KKKG	z	"	"  Aq              
 
&		7	#	#V%6v%>%>?& # #!k )VHUUU 	L'BBB"00==aaadC  558H$9
 9
 
 
 
 

 	L'A+XFFF%,%:%:;%G%G1%M"!"24NOOO 	L!g+/JJJ%,%:%:;%G%G1%M"!"24NOOO 	L7Q;!"38LLL%,%:%:;%G%G1%M"Y%&6<V8VWWWW $FL99<L!q "<= =     C# #s   
D--D14D1c                     | \  }}t          d          }|                    ||           |j        d         j        }t	          |t
                    sJ d S )Nr@   rK   r   )r&   rS   calibrated_classifiers_	estimator
isinstancer   )r9   r6   r7   	calib_clfbase_ests        r8   "test_calibration_default_estimatorrm   w   s[    DAq&!,,,IMM!Q03=Hh	*******r:   c                    | \  }}d}t          |          }t          ||          }t          |j        t                     sJ |j        j        |k    sJ |                    ||           |r|nd}t          |j                  |k    sJ d S )NrL   )n_splitsrJ   rI   )r   r&   rj   rK   ro   rS   lenrh   )r9   r>   r6   r7   splitskfoldrk   expected_n_clfs           r8   test_calibration_cv_splitterrt      s     DAqF6"""E&%(CCCIilE*****< F****MM!Q'.VVQNy011^CCCCCCr:   c                 >   t           dz  }| \  }}t          j                            d                              t          |                    }|d |         |d |         |d |         }	}}||d          }
t          d          }t          |||          }|                    |||	           |	                    |
          }|                    ||           |	                    |
          }t          j
                            ||z
            }|dk    sJ d S )	Nr@   r0   rA   rC   r4   )r;   r>   rG   皙?)r5   rN   rO   rP   rQ   rp   r   r&   rS   rT   linalgnorm)r9   r;   r>   r2   r6   r7   rH   rY   rZ   r[   r\   ri   calibrated_clfprobs_with_swprobs_without_swdiffs                   r8   test_sample_weightr~      s     QIDAqI))r)22::A:GGM!":I:*9*}ZiZ?XhWGyzz]Fr***I+IfxXXXNwx@@@"0088M w(((%33F;;9>>-*::;;D#::::::r:   c                 v   | \  }}t          ||d          \  }}}}t          d          }	t          |	|d|          }
|
                    ||           |
                    |          }t          |	|d|          }|                    ||           |                    |          }t          ||           dS )zTest parallel calibrationr0   rv   r@   )r;   n_jobsr>   rI   N)r	   r   r&   rS   rT   r   )r9   r;   r>   r6   r7   rY   r\   rZ   r]   ri   cal_clf_parallelprobs_parallelcal_clf_sequentialprobs_sequentials                 r8   test_parallel_executionr      s     DAq'712'N'N'N$GVWfr***I-&X   '***%33F;;N/&X   7G,,,)77??N$455555r:   rB   r@   c                    d }t          d          }t          dd|dd          \  }}d	||d	k    <   t          j        |          j        d
         }|d d d	         |d d d	         }	}|dd d	         |dd d	         }}
|                    ||	           t          || d|          }|                    ||	           |                    |
          }t          t          j	        |d          t          j
        t          |
                               d|                    |
|          cxk     rdk     sn J |                    |
|          d|                    |
|          z  k    sJ  ||t          |                    |
                    |          } ||||          }|d|z  k     sJ t          dd          }|                    ||	           |                    |
          } ||||          }t          || d|          }|                    ||	           |                    |
          } ||||          }|d|z  k     sJ d S )Nc                     t          j        |          |          }t          j        ||z
  dz            |j        d         z  S )Nr@   r   )rN   eyesumshape)y_true
proba_pred	n_classesY_onehots       r8   multiclass_brierz5test_calibration_multiclass.<locals>.multiclass_brier   s<    6)$$V,vx*,233hnQ6GGGr:      rv   i  d   
         .@r2   r3   r4   centerscluster_stdr@   r   rI   rL   rM   axis?gffffff?)r   g?   r0   )n_estimatorsr4   )r   r   rN   uniquer   rS   r&   rT   r   r   onesrp   scorer   decision_functionr   )r;   r>   rB   r   r^   r6   r7   r   rY   rZ   r\   r]   r`   probasuncalibrated_briercalibrated_brier	clf_probscal_clf_probss                     r8   test_calibration_multiclassr      s   H H H 
#
#
#C#D"RV  DAq Aa!eH	!"1%I1vq1vWGqt!tWa1gFFGGGW$SAQQQGKK!!!""6**FBF6***BGCKK,@,@AAA
 #))FF++2222d222222 ==((4#))FF2K2K+KKKKK
 *)--f5566)   (')LLLc$666666 !br
B
B
BCGGGW!!&))I))&)yQQQ$SAQQQGKK!!!))&11M''SSSc$66666666r:   c                  0    G d d          } t          ddddd          \  }}t                                          ||          } |             }t          ||g|j                  }|                    |          }t          |d	|j        z             d S )
Nc                       e Zd Zd ZdS )9test_calibration_zero_probability.<locals>.ZeroCalibratorc                 @    t          j        |j        d                   S )Nr   )rN   zerosr   selfr6   s     r8   predictzAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predict
  s    8AGAJ'''r:   N)__name__
__module____qualname__r    r:   r8   ZeroCalibratorr     s#        	( 	( 	( 	( 	(r:   r   2   r   r   r   r   )ri   calibratorsclasses      ?)r   r   rS   r#   classes_rT   r   
n_classes_)r   r6   r7   r^   
calibratorr`   r   s          r8   !test_calibration_zero_probabilityr     s    
( ( ( ( ( ( ( (
 !RT  DAq 



1
%
%C!!J#J<  G ""1%%F FC#.011111r:   c                     d} t          d| z  dd          \  }}t          j                            d                              |j                  }||                                z  }|d|          |d|          |d|          }}}|| d	| z           || d	| z           || d	| z           }	}}|d	| z  d         |d	| z  d         }}
t          d
          }t          |d          }t          j
        t                    5  |                    ||           ddd           n# 1 swxY w Y   |                    |||           |                    |
          dddf         }||
ft          j        |          t          j        |
          ffD ]\  }}dD ]}t          ||d          }|	dfD ]}|                    |||           |                    |          }|                    |          }|dddf         }t#          |t          j        ddg          t          j        |d                              t)          ||          t)          ||          k    sJ ̌dS )z*Test calibration for prefitted classifiersr      r/   r0   r1   rA   rC   Nr@   TrE   prefitrg   rI   )r=   r<   )r;   rK   rG   r   r   )r   rN   rO   rP   rQ   rD   rR   r   r&   rU   rV   r   rS   rT   r   rX   r   r   arrayargmaxr"   )r2   r6   r7   rH   rY   rZ   r[   X_caliby_calibsw_calibr\   r]   r^   	unfit_clfr_   this_X_calibrb   r;   r`   swy_proby_predrc   s                          r8   test_calibration_prefitr     s   IY1SUVVVDAqI))r)22:::GGMLA "#:I:*9*}ZiZ?XhWG	)a)m
#$	)a)m
#$i!i-/0 WG
 q9}'1y=??);FF D
)
)
)C&sx888I	~	&	& ( (gw'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( GGGWh'''$$V,,QQQT2L 
&		7	#	#V%6v%>%>?&  !k . 	 	F,SHMMMG& 	 	L'DDD ..{;; 55#)!!!Q$< "628QF+;+;BIfST<U<U<U+VWWW'==@P,A A     			 s   D''D+.D+c                    | \  }}t          d          }t          ||dd          }|                    ||           |                    |          }t	          |||dd          }|dk    rt          d	
          }nt                      }|                    ||           |                    ||           |                    |          }	|                    |	          }
t          |d d df         |
           d S )Nr   rv   r   FrM   r   )rK   r;   r=   clip)out_of_boundsrI   )
r   r&   rS   rT   r   r   r$   r   r   r   )r9   r;   r6   r7   r^   r`   
cal_probasunbiased_predsr   clf_dfmanual_probass              r8   test_calibration_ensemble_falser   K  s    DAq

#
#
#C$SANNNGKK1&&q))J 'sAqQ?RSSSN'f===

(**
NN>1%%%GGAqMMM""1%%F&&v..MJqqq!t$m44444r:   c                  |   t          j        g d          } t          j        g d          }t          j        ddg          }t          |t          | |          d           ddt          j        |d         | z  |d         z             z   z  }t                                          | |                              |           }t          ||d	           t          j	        t                    5  t                                          t          j        | | f          |           d
d
d
           d
S # 1 swxY w Y   d
S )z0Test calibration values with Platt sigmoid model)rL   r   )rI   r   gj=ɿgY90(?r   r   r   rI   r/   N)rN   r   r
   r%   expr$   rS   r   rU   rV   rW   vstack)exFexYAB_lin_libsvmlin_probsk_probs        r8   test_sigmoid_calibrationr   d  sc   
(<<<
 
 C
(;;;

CH24GHIIMm-A#s-K-KQOOOcBF=#3c#9M!<L#LMMMNH!##''S1199#>>Gh333 
z	"	" > >!!")S#J"7"7===> > > > > > > > > > > > > > > > > >s   -7D11D58D5c                     t          j        g d          } t          j        g d          }t          | |d          \  }}t          |          t          |          k    sJ t          |          dk    sJ t	          |ddg           t	          |ddg           t          j        t                    5  t          dgd	g           d
d
d
           n# 1 swxY w Y   t          j        g d          }t          j        g d          }t          ||dd          \  }}t          |          t          |          k    sJ t          |          dk    sJ t	          |ddg           t	          |ddg           t          j        t                    5  t          ||d           d
d
d
           d
S # 1 swxY w Y   d
S )z Check calibration_curve functionr   r   r   rI   rI   rI           rw   皙?皙??r   r@   n_binsr   rI   rw   r   gN)r   r   r   r   rI   rI   )r   rw   r         ?r   r   quantiler   strategygUUUUUU?r   
percentile)r   )rN   r   r(   rp   r   rU   rV   rW   )r   r   	prob_true	prob_predy_true2y_pred2prob_true_quantileprob_pred_quantiles           r8   test_calibration_curver   u  sx   X((())FX44455F,VVAFFFIyy>>S^^++++y>>Q	Aq6***	C:... 
z	"	" ' '1#v&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' h)))**Gh55566G->Z. . .** !""c*<&=&=====!""a''''*QJ777*S#J777 
z	"	" C C'7\BBBBC C C C C C C C C C C C C C C C C Cs$   6CCCF66F:=F:c                     t          j        g d          } t          j        g d          }t          j        t                    5  t          | |dd           ddd           n# 1 swxY w Y   t          j        t                    5  t          | |dz  dd          \  }}t          | |d          \  }}t          ||           t          ||           ddd           dS # 1 swxY w Y   dS )	z6Tests the `normalize` parameter of `calibration_curve`r   r   r@   F)r   	normalizeNTr   )rN   r   rU   warnsFutureWarningr(   r   )r   r   prob_true_unnormalizedprob_pred_unnormalizedr   r   s         r8   .test_calibration_curve_with_unnormalized_probar     s   X((())FX44455F 
m	$	$ E E&&eDDDDE E E E E E E E E E E E E E E 
m	$	$ ? ?9JFQJqD:
 :
 :
6 6  1JJJ	9I'=>>>I'=>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s%   A&&A*-A*
AC&&C*-C*c                 ,   t          ddddd          \  }}t          j        |d<   t          dt	                      fdt          d	
          fg          }t          |dd|           }|                    ||           |                    |           dS )z$Test that calibration can accept nanr   r@   r   r0   )r2   r3   n_informativen_redundantr4   r   r   imputerrfrI   )r   r=   )rK   r;   r>   N)	r   rN   nanr   r!   r   r&   rS   r   )r>   r6   r7   r^   clf_cs        r8   test_calibration_nan_imputerr     s     !QS  DAq fAdG

]__	%.DRS.T.T.T'UV C #31Z(SSSE	IIaOOO	MM!r:   c                 l   d}t          dd|          \  }}t          dd          }t          |dt                      | 	          }|                    ||           |                    |          }t          |                    d
          t          j	        |j
        d                              d S )Nr@   r   rL   )r2   r3   r   r   r   Cr4   r<   rM   rI   r   r   )r   r   r&   r   rS   rT   r
   r   rN   r   r   )r>   num_classesr6   r7   r^   clf_probprobss          r8   test_calibration_prob_sumr    s     K[QQQDAq
c
*
*
*C%I+--(  H LLA""1%%EeiiQi//Q1H1HIIIIIr:   c           	         t           j                            dd          }t          j        d          }t	          dd          }t          |dt                      |           }|                    ||           t          |j	                  D ]\  }}|
                    |          }| rt          |d d |f         t          j        t          |                               t          j        |d d d |f         dk              sJ t          j        |d d |d	z   d f         dk              sJ t          j        |d	|j        d         z            sJ d S )
Nr   rL   r   r   r  r<   rM   r   rI   )rN   rO   randnaranger   r&   r   rS   	enumeraterh   rT   r   r   rp   allallcloser   )r>   r6   r7   r^   r`   icalibrated_classifierprobas           r8   test_calibration_less_classesr    sf    		AA
	"A
c
*
*
*C$I+--(  G KK1$-g.M$N$N 
: 
:  %33A66 	:uQQQT{BHSVV,<,<===6%2A2,*+++++6%1q577
+a/000000 ;ua%+a.&8999999
: 
:r:   r6   r0      rL   r/   c                     g d} G d dt                     }t           |                      }|                    | |           dS )z;Test that calibration accepts n-dimensional arrays as input)rI   r   r   rI   rI   r   rI   rI   r   r   rI   r   r   rI   r   c                       e Zd ZdZd Zd ZdS )>test_calibration_accepts_ndarray.<locals>.MockTensorClassifierz*A toy estimator that accepts tensor inputsc                 8    t          j        |          | _        | S )N)rN   r   r   )r   r6   r7   s      r8   rS   zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fit  s    IaLLDMKr:   c                 l    |                     |j        d         d                              d          S )Nr   r   rI   r   )reshaper   r   r   s     r8   r   zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_function  s-    99QWQZ,,00a0888r:   N)r   r   r   __doc__rS   r   r   r:   r8   MockTensorClassifierr    s8        88	 	 		9 	9 	9 	9 	9r:   r  N)r   r&   rS   )r6   r7   r  rz   s       r8    test_calibration_accepts_ndarrayr    sn     	655A	9 	9 	9 	9 	9} 	9 	9 	9 ,,@,@,B,BCCNq!r:   c                  .    dddddddddg} g d}| |fS )NNYadult)stateageTXVTchild)rI   r   rI   r   )	dict_datatext_labelss     r8   r$  r$    sB     w''w''w''I
 ))Kk!!r:   c                     | \  }}t          dt                      fdt                      fg          }|                    ||          S )N
vectorizerr^   )r   r    r   rS   )r$  r6   r7   pipeline_prefits       r8   dict_data_pipeliner)    sQ    DAq
((	)E3I3K3K+LM O q!$$$r:   c                 2   | \  }}|}t          |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   rg   n_features_in_N)r&   rS   r   r   hasattrr   rT   )r$  r)  r6   r7   r^   rk   s         r8   test_calibration_dict_pipeliner-    s     DAq
C&sx888IMM!Qy)3<888 s,-----y"233333 aAr:   zclf, cvrI   r  r   c                    t          dddd          \  }}|dk    r|                     ||          } t          | |          }|                    ||           |dk    r.t          |j        | j                   |j        | j        k    sJ d S t                                          |          j        }t          |j        |           |j        |j        d         k    sJ d S )	Nr   rL   r@   r   r2   r3   r   r4   r   rg   rI   )r   rS   r&   r   r   r+  r   r   )r^   rK   r6   r7   rk   r   s         r8   test_calibration_attributesr1  +  s     QUVWWWDAq	X~~ggamm&sr222IMM!Q	X~~9-s|<<<'3+=======..$$Q''09-w777'171:555555r:   c                  L   t          dddd          \  } }t          d                              | |          }t          |d	          }d
}t	          j        t          |          5  |                    | d d d df         |           d d d            d S # 1 swxY w Y   d S )Nr   rL   r@   r   r0  rI   r.  r   rg   zAX has 3 features, but LinearSVC is expecting 5 features as input.matchr   )r   r   rS   r&   rU   rV   rW   )r6   r7   r^   rk   msgs        r8   2test_calibration_inconsistent_prefit_n_features_inr6  C  s     QUVWWWDAq
a...

Q
"
"C&sx888I
MC	z	-	-	- # #a2A2h"""# # # # # # # # # # # # # # # # # #s   )#BB Bc                      t          dddd          \  } }t          d t          d          D             d	          }|                    | |           t	          |d
          }|                    | |           d S )Nr   rL   r@   r   r0  c                 N    g | ]"}d t          |          z   t                      f#S )lr)strr   ).0r  s     r8   
<listcomp>z5test_calibration_votingclassifier.<locals>.<listcomp>U  s.    LLLaTCFF]$6$8$89LLLr:   r   soft)
estimatorsvotingr   )ri   rK   )r   r   rangerS   r&   )r6   r7   voterk   s       r8   !test_calibration_votingclassifierrB  O  s     QUVWWWDAqLL588LLL  D 	HHQNNN&(CCCIMM!Qr:   c                  "    t          d          S )NT
return_X_y)r   r   r:   r8   	iris_datarF  _  s    %%%%r:   c                 <    | \  }}||dk              ||dk              fS )Nr@   r   )rF  r6   r7   s      r8   iris_data_binaryrH  d  s&    DAqQU8Qq1uXr:   c                 z   |\  }}|\  }}t                                          ||          }d}t          j        t          |          5  t          j        |||           d d d            n# 1 swxY w Y   t                                          ||          }	d}t          j        t          |          5  t          j        |	||           d d d            n# 1 swxY w Y   t                      }	t          j        t                    5  t          j        |	||           d d d            d S # 1 swxY w Y   d S )Nz)'estimator' should be a fitted classifierr3  z/response method predict_proba is not defined in)
r   rS   rU   rV   rW   r'   from_estimatorr   r   r   )
pyplotrF  rH  r6   r7   X_binaryy_binaryregr5  r^   s
             r8   #test_calibration_display_validationrO  j  s   DAq)Hh



 
 A
&
&C
5C	z	-	-	- 5 5)#q!4445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ++//!Q

C
;C	z	-	-	- 5 5)#q!4445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 

C	~	&	& 5 5)#q!4445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s6   
A--A14A17CC!CD00D47D4constructor_namerJ  from_predictionsc                    |\  }}t                      }|                    ||           |                    |          }|dk    rNd}t          j        t
          |          5  t          j        |||           d d d            d S # 1 swxY w Y   d S d}t          j        t
          |          5  t          j        ||           d d d            d S # 1 swxY w Y   d S )NrJ  z"to be a binary classifier, but gotr3  z-y should be a 1d array, got an array of shape)	r   rS   rT   rU   rV   rW   r'   rJ  rQ  )rK  rF  rP  r6   r7   r^   r   r5  s           r8   #test_calibration_display_non_binaryrS  }  sl   DAq
 
"
"CGGAqMMMq!!F+++2]:S111 	9 	9-c1a888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 >]:S111 	; 	;/6:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s$   "BB
B
0CCCr   r   r   rQ   r   c                    |\  }}t                                          ||          }t          j        |||||d          }|                    |          d d df         }t          ||||          \  }	}
t          |j        |	           t          |j        |
           t          |j	        |           |j
        dk    sJ dd l}t          |j        |j        j                  sJ |j                                        dk    sJ t          |j        |j        j                  sJ t          |j        |j        j                  sJ |j                                        dk    sJ |j                                        dk    sJ dd	g}|j                                                                        }t7          |          t7          |          k    sJ |D ]}|                                |v sJ d S )
Nr   )r   r   alpharI   r   r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rS   r'   rJ  rT   r(   r   r   r   r   estimator_name
matplotlibrj   line_linesLine2D	get_alphaax_axesAxesfigure_figureFigure
get_xlabel
get_ylabel
get_legend	get_textsrp   get_text)rK  rH  r   r   r6   r7   r9  vizr   r   r   mplexpected_legend_labelslegend_labelslabelss                  r8    test_calibration_display_computerm    s%    DAq				!	!!Q	'	'B

+
Aq(#  C a  A&F,	6&8  Iy CM9---CM9---CJ'''!55555 ci!1222229  C''''cgsx}-----ck3:#4555557#SSSSS7#NNNNN24JKG&&((2244M}%;!<!<<<<< ; ;  $::::::; ;r:   c                    |\  }}t          t                      t                                }|                    ||           t	          j        |||          }|j        dg}|j                                        	                                }t          |          t          |          k    sJ |D ]}|                                |v sJ d S )NrV  )r   r   r   rS   r'   rJ  rW  r]  re  rf  rp   rg  )	rK  rH  r6   r7   r^   rh  rj  rk  rl  s	            r8   $test_plot_calibration_curve_pipelinero    s    DAq
((*<*>*>
?
?CGGAqMMM

+CA
6
6C!02HIG&&((2244M}%;!<!<<<<< ; ;  $::::::; ;r:   zname, expected_label)N_line1)my_estrq  c                    t          j        g d          }t          j        g d          }t          j        g           }t          ||||          }|                                 |g n|g}|                    d           |j                                                                        }t          |          t          |          k    sJ |D ]}	|		                                |v sJ d S )Nr   rI   rI   r   r   r   r   皙?rW  rV  )
rN   r   r'   plotappendr]  re  rf  rp   rg  )
rK  nameexpected_labelr   r   r   rh  rj  rk  rl  s
             r8   'test_calibration_display_default_labelsr{    s     &&I---..IXb\\F
Y	6$
O
O
OCHHJJJ#'<RRdV!!"8999G&&((2244M}%;!<!<<<<< ; ;  $::::::; ;r:   c                    t          j        g d          }t          j        g d          }t          j        g           }d}t          ||||          }|j        |k    sJ d}|                    |           |dg}|j                                                                        }t          |          t          |          k    sJ |D ]}|	                                |v sJ d S )Nrs  rt  zname onerv  zname twory  rV  )
rN   r   r'   rW  rw  r]  re  rf  rp   rg  )	rK  r   r   r   ry  rh  rj  rk  rl  s	            r8   )test_calibration_display_label_class_plotr~    s    &&I---..IXb\\FD
Y	6$
O
O
OC%%%%DHH$H"$:;G&&((2244M}%;!<!<<<<< ; ;  $::::::; ;r:   c                    |\  }}d}t                                          ||          }|                    |          d d df         }t          t          |           }| dk    r|||fn||f}	 ||	d|i}
|
j        |k    sJ |                    d           |
                                 |dg}|
j        	                                
                                }t          |          t          |          k    sJ |D ]}|                                |v sJ |                    d           d}|
                    |           t          |          t          |          k    sJ |D ]}|                                |v sJ d S )	Nzmy hand-crafted namerI   rJ  ry  r  rV  another_namer}  )r   rS   rT   getattrr'   rW  closerw  r]  re  rf  rp   rg  )rP  rK  rH  r6   r7   clf_namer^   r   constructorparamsrh  rj  rk  rl  s                 r8   ,test_calibration_display_name_multiple_callsr    s    DAq%H



"
"1a
(
(Cq!!!!!Q$'F,.>??K,0@@@c1a[[q&kF
+v
-H
-
-C))))
LLHHJJJ&(>?G&&((2244M}%;!<!<<<<< ; ;  $::::::
LLHHH(H}%;!<!<<<<< ; ;  $::::::; ;r:   c                 r   |\  }}t                                          ||          }t                                          ||          }t          j        |||          }t          j        ||||j                  }|j                                        d         }|                    d          dk    sJ d S )N)axrI   rV  )r   rS   r   r'   rJ  r]  get_legend_handles_labelscount)	rK  rH  r6   r7   r9  dtrh  viz2rl  s	            r8   !test_calibration_display_ref_liner    s    DAq				!	!!Q	'	'B		!	!	%	%a	+	+B

+B1
5
5C,RA#'BBBDX//11!4F<<.//1444444r:   dtype_y_strc                 L   t           j                            d          }t          j        dgdz  dgdz  z   |           }|                    dd|j                  }d	}t          j        t          |
          5  t          ||           ddd           dS # 1 swxY w Y   dS )zKCheck error message when a `pos_label` is not specified with `str` targets.r0   spamr   eggsr@   dtyper   rC   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 explicitlyr3  N)
rN   rO   rP   r   randintrD   rU   rV   rW   r(   )r  rngy1y2err_msgs        r8   *test_calibration_curve_pos_label_error_strr    s     )


#
#C	6(Q,&A-[	A	A	AB	Q	(	(B	$ 
 
z	1	1	1 " ""b!!!" " " " " " " " " " " " " " " " " "s   ;BB Bc                    t          j        g d          }t          j        ddg|           }||         }t          j        g d          }t          ||d          \  }}t          |g d           t          ||dd	          \  }}t          |g d           t          |d
|z
  dd	          \  }}t          |g d           t          |d
|z
  dd	          \  }}t          |g d           dS )z8Check the behaviour when passing explicitly `pos_label`.)	r   r   r   rI   rI   rI   rI   rI   rI   r  eggr  )	rw   r   g333333?ru  r   gffffff?r   r   r      r   )r   r   rI   rI   )r   	pos_labelrI   r   )r   r   r   rI   N)rN   r   r(   r   )r  r   r   
y_true_strr   r   _s          r8    test_calibration_curve_pos_labelr  .  s    X11122Fhk:::GJXDDDEEF %VVA>>>LIqI~~~...$ZUSSSLIqI~~~...$VQZQOOOLIqI~~~...$ZVAQWXXXLIqI~~~.....r:   zpos_label, expected_pos_label))NrI   r   )rI   rI   c                    |\  }}t                                          ||          }t          j        ||||          }|                    |          dd|f         }t          |||          \  }	}
t          |j        |	           t          |j        |
           t          |j	        |           |j
                                        d| dk    sJ |j
                                        d| dk    sJ |j        j        dg}|j
                                                                        }t#          |          t#          |          k    sJ |D ]}|                                |v sJ dS )z?Check the behaviour of `pos_label` in the `CalibrationDisplay`.)r  Nz,Mean predicted probability (Positive class: )z'Fraction of positives (Positive class: rV  )r   rS   r'   rJ  rT   r(   r   r   r   r   r]  rc  rd  	__class__r   re  rf  rp   rg  )rK  rH  r  expected_pos_labelr6   r7   r9  rh  r   r   r   rj  rk  rl  s                 r8   "test_calibration_display_pos_labelr  C  s   
 DAq				!	!!Q	'	'B

+B1	
J
J
JCa  $6!67F,Q)LLLIyCM9---CM9---CJ''' 	O:LOOO	P 	P 	P 	P 	J5GJJJ	K 	K 	K 	K !l35KLG&&((2244M}%;!<!<<<<< ; ;  $::::::; ;r:   c                    t          d          \  }}t                                          |          }|dd         |dd         }}t          j        |          dz  }t          j        |j        d         dz  |j        d         f|j                  }||dddddf<   ||dddddf<   t          j        |j        d         dz  |j                  }||ddd<   ||ddd<   t                      }t          || |d	          }t          |          }	|	                    |||
           |                    ||           t          |	j        |j                  D ])\  }
}t          |
j        j        |j        j                   *|	                    |          }|                    |          }t          ||           dS )zrCheck that passing repeating twice the dataset `X` is equivalent to
    passing a `sample_weight` with a factor 2.TrD  Nr   r@   r   rI   r  r;   r>   rK   rG   )r   r   fit_transformrN   	ones_liker   r   r  r   r&   r   rS   ziprh   r   ri   coef_rT   )r;   r>   r6   r7   rH   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_weightss                 r8   ?test_calibrated_classifier_cv_double_sample_weights_equivalencer  d  s   
 %%%DAq&&q))ATcT7AdsdGqALOOa'M h
Q
317CCCGGCCaCFOGADqD!!!GhqwqzA~QW555GGCCaCLGADqDM"$$I%;	& & &" #((F"G"G##Aq#FFF"&&w888 25#;&>2 2 
 
-- 	&,)/	
 	
 	
 	
 6CCAFF;II!LL')?@@@@@r:   fit_params_typelistr   c                     |\  }}t          ||           t          ||           d}t          ddg          }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&   rS   )r  r9   r6   r7   
fit_paramsr^   pc_clfs          r8    test_calibration_with_fit_paramsr    sx     DAq?33?33 J
 #s
<
<
<C#C((FFJq!""z"""""r:   rH   r   c                 ~    |\  }}t          d          }t          |          }|                    |||            dS )zMTests that sample_weight is passed to the underlying base
    estimator.
    T)expected_sample_weightrG   N)r)   r&   rS   )rH   r9   r6   r7   r^   r  s         r8   2test_calibration_with_sample_weight_base_estimatorr    sH     DAq
D
9
9
9C#C((F
JJq!=J11111r:   c                 $   | \  }}t          j        |          } G d dt                    } |            }t          |          }t	          j        t                    5  |                    |||           ddd           dS # 1 swxY w 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Z xZS )Utest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeightc                 F    d|vsJ  t                      j        ||fi |S )NrH   )superrS   )r   r6   r7   r  r  s       r8   rS   zYtest_calibration_without_sample_weight_base_estimator.<locals>.ClfWithoutSampleWeight.fit  s4    "*4444577;q!22z222r:   )r   r   r   rS   __classcell__)r  s   @r8   ClfWithoutSampleWeightr    s8        	3 	3 	3 	3 	3 	3 	3 	3 	3r:   r  rG   N)rN   r  r)   r&   rU   r   UserWarningrS   )r9   r6   r7   rH   r  r^   r  s          r8   5test_calibration_without_sample_weight_base_estimatorr    s     DAqLOOM3 3 3 3 3!3 3 3 3
 !
 
"
"C#C((F	k	"	" 6 6

1a}
5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6s   BB	B	c                     | \  }}d|dd         i}t          |          }t          |          }dt          t                    z   dz   }t	          j        t          |          5   |j        ||fi | ddd           dS # 1 swxY w Y   dS )z]fit_params having different length than data should raise the
    correct error message.
    r  NrL   r  z>Found input variables with inconsistent numbers of samples: \[z, 5\]r3  )r)   r&   r:  r5   rU   rV   rW   rS   )r9   r6   r7   r  r^   r  r5  s          r8   4test_calibration_with_fit_params_inconsistent_lengthr    s     DAqq!uJ

<
<
<C#C((F	Y	(*2	3  
z	-	-	- ' '
1a&&:&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's   &BB
Bc                    t          d          \  }}t                                          |          }t          j        |dd         |dd         f          }t          j        |dd         |dd         f          }t          j        |          }d|ddd<   t                      }t          || |d	          }t          |          }|
                    |||
           |
                    |ddd         |ddd                    t          |j        |j                  D ])\  }}	t          |j        j        |	j        j                   *|                    |          }
|                    |          }t          |
|           dS )z|Check that passing removing some sample from the dataset `X` is
    equivalent to passing a `sample_weight` with a factor 0.TrD  N(   r   Z   rI   r@   r  rG   )r   r   r  rN   r   hstack
zeros_liker   r&   r   rS   r  rh   r   ri   r  rT   )r;   r>   r6   r7   rH   ri   r  r  r  r  r  r  s               r8   >test_calibrated_classifier_cv_zeros_sample_weights_equivalencer    s   
 %%%DAq&&q))A 		1SbS61RU8$%%A
	1SbS61RU8$%%AM!$$MM##A#"$$I%;	& & &" #((F"G"G##Aq#FFF"&&q1vq1v666 25#;&>2 2 
 
-- 	&,)/	
 	
 	
 	
 6CCAFF;II!LL')?@@@@@r:   c                     t          t                      t                                }t          j        t          d          5   |j        |   ddd           dS # 1 swxY w Y   dS )zUCheck that we raise an error is a user set both `base_estimator` and
    `estimator`.)base_estimatorri   z%Both `base_estimator` and `estimator`r3  N)r&   r   rU   rV   rW   rS   )r9   r  s     r8   /test_calibrated_classifier_error_base_estimatorr    s     3)++7I7K7K   
z)P	Q	Q	Q ) )!!4(() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )s   AA!$A!c                     t          t                                }d}t          j        t          |          5   |j        |   ddd           dS # 1 swxY w Y   dS )zPCheck that we raise a warning regarding the deprecation of
    `base_estimator`.)r  z+`base_estimator` was renamed to `estimator`r3  N)r&   r   rU   r   r   rS   )r9   r  warn_msgs      r8   5test_calibrated_classifier_deprecation_base_estimatorr    s     3BTBVBVWWW<H	m8	4	4	4 ) )!!4(() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )s   AAA)yrU   numpyrN   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   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*   r5   fixturer9   markparametrizere   rm   rt   r~   r   r@  r   r   r   r   r   r   r   r   r  r  rO   rP   r	  r  r$  r)  r-  paramr1  r6  rB  rF  rH  rO  rS  rm  ro  r{  r~  r  r  r:  objectr  r  r  r  r  r   r  r  r  r  r  r  r   r:   r8   <module>r     s
        ) ) ) ) ) )       - - - - - - - - ) ) ) ) ) ) A A A A A A A A         
 * ) ) ) ) ) - - - - - - G G G G G G G G G G . . . . . . < < < < < < < < - - - - - -        F E E E E E E E / / / / / / ! ! ! ! ! ! 4 4 4 4 4 4 4 4 0 0 0 0 0 0 / / / / / / 5 5 5 5 5 5 ( ( ( ( ( ( , , , , , ,                6 5 5 5 5 5 5 5 5 5 5 5 	 h   
 Iz#:;;dE]338 8 43 <;8v+ + + dE]33D D 43D Iz#:;;dE]33  43 <;, Iz#:;;dE]336 6 43 <;6, Iz#:;;dE]33 q**:7 :7 +* 43 <;
:7z2 2 22, , ,^ Iz#:;;5 5 <;50> > >"C C C@? ? ?& dE]33  43 dE]33J J 43J dE]33: : 43:4 
	b!!''Aq11
	b!!''Aq!44   ( " " " % % %  4 YY^^^Q''YY^^^X.. 6 6 6"	# 	# 	#    h& &  & h   
5 5 5& +.>@R-STT; ; UT;  Ar7++i%<==&; &; >= ,+&;R; ; ; -/CD ; ; ; ; ; ;( +.>@R-STT; ; UT;D
5 
5 
5 f66" " 76" f66/ / 76/( 8:U:U:UVV; ; WV;@ Iz#:;;dE]33-A -A 43 <;-A` *VW,=>># # ?>#$ 			 2 2 26 6 6,' ' '" Iz#:;;dE]33'A 'A 43 <;'AV) ) )) ) ) ) )r:   