
    ה9iV              
          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	m
Z
 d dlmZ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 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'm(Z( d dl)m*Z*m+Z+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZBmCZCmDZDmEZEmFZF d dlGmHZH d dlImJZJ d dlKmLZLmMZMmNZNmOZOmPZP d dlQmRZR d dlSmTZT d dlUmVZV dZW ej                  d      d         ZYd! ZZej                  j                  d"eT      ej                  j                  d#d$d%g      ej                  j                  d&d'd(g      d)                      Z]d* Z^ej                  j                  d&d'd(g      d+        Z_d, Z`ej                  j                  d#g d-      ej                  j                  d&d'd(g      d.               Zaej                  j                  ej                  j                  d#g d-      ej                  j                  d&d'd(g      d/                      Zcej                  j                  d#d$d%g      ej                  j                  d&d'd(g      ej                  j                  d0 edd1            d2                      Zed3 Zfej                  j                  d"eT      ej                  j                  d#g d-      d4               Zgej                  j                  d#d5gd$ e       fd% e%d67      fd8 e       fg      d9        Zhd: Ziej                  j                  d;g d<      ej                  j                  d&d'd(g      d=               Zjd> Zkd? Zlej                  j                  d#g d-      ej                  j                  d&d'd(g      d@               Zmej                  j                  d#g d-      ej                  j                  d&d'd(g      dA               Znej                  j                  d&d'd(g      dB        Zoej                  j                  dCej                  j                  dD      j                  dEdFd1      ej                  j                  dD      j                  dEdFd1dG      g      dH        Zsej                  dI        Ztej                  dJ        ZudK ZvdL ZwdM ZxdN Zy ej                  d      dO        Zz ej                  d      dP        Z{ej                  j                  dQdFdRg      ej                  j                  dSdTdUg      dV               Z|dW Z}ej                  j                  dXdYdZg      d[        Z~d\ Zej                  j                  d]d^d_g      d`        Zda Zej                  j                  dbeeg      dc        Zej                  j                  dbeeg      dd        Zej                  j                  dedfd1dgdhdfd1dgdig      dj        Zej                  j                  dkg dl      dm        Zej                  j                  d#g d-      ej                  j                  d&d'd(g      dn               Zej                  j                  dodpdqg      dr        Zej                  j                  dsdtgeWz   ej                  eW      g      du        Zdv Zdw Zdx Zdy Zej                  j                  dzd'd(g      ej                  j                  d#g d-      d{               Zd| Zej                  j                  d&d(d'g      ej                  j                  dzd(d'g      ej                  j                  d} eF       eC~      d                      Zej                  j                  d&d(d'g      ej                  j                  dzd(d'g      ej                  j                  d} eF       eC~      d                      Zy)    N)assert_allclose)config_context)BaseEstimatorClassifierMixinclone)CalibratedClassifierCVCalibrationDisplay_CalibratedClassifier_sigmoid_calibration_SigmoidCalibration_TemperatureScalingcalibration_curve)	load_iris
make_blobsmake_classification)LinearDiscriminantAnalysis)DummyClassifier)RandomForestClassifierVotingClassifier)DictVectorizer)FrozenEstimator)SimpleImputer)IsotonicRegression)LogisticRegressionSGDClassifier)accuracy_scorebrier_score_losslog_lossroc_auc_score)KFoldLeaveOneOutcheck_cvcross_val_predictcross_val_scoretrain_test_split)MultinomialNB)Pipelinemake_pipeline)LabelEncoderStandardScaler)	LinearSVC)DecisionTreeClassifier)_convert_to_numpy_get_namespace_device_dtype_idsdeviceget_namespace)yield_namespace_device_dtype_combinations)CheckingClassifier)get_tags)_array_api_for_tests_convert_containerassert_almost_equalassert_array_almost_equalassert_array_equal)softmax)CSR_CONTAINERS)check_is_fitted   module)scopec                  4    t        t        dd      \  } }| |fS )N   *   	n_samples
n_featuresrandom_state)r   	N_SAMPLESXys     h/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/sklearn/tests/test_calibration.pydatarK   I   s    qrRDAqa4K    c                     | \  }}d}t        j                  t              5  t        |      j	                  ||       d d d        y # 1 sw Y   y xY w)Nz%not sigmoid, isotonic, or temperaturemethod)pytestraises
ValueErrorr   fit)rK   rH   rI   invalid_methods       rJ   test_calibration_method_raisesrU   O   sJ    DAq<N	z	" @n599!Q?@ @ @s   AAcsr_containerrO   sigmoidisotonicensembleTFc                    t         dz  }| \  }}t        j                  j                  d      j	                  |j
                        }||j                         z
  }|d | |d | |d | }
}	}||d  ||d  }}t               j                  ||	|
      }|j                  |      d d df   }t        ||j
                  dz   |      }t        j                  t              5  |j                  ||       d d d        ||f ||       ||      ffD ]>  \  }}t        ||d|	      }|j                  ||	|
       |j                  |      d d df   }t        ||      t        ||      kD  sJ |j                  ||	dz   |
       |j                  |      d d df   }t        ||       |j                  |d|	z  dz
  |
       |j                  |      d d df   }t        ||       |j                  ||	dz   dz  |
       |j                  |      d d df   }|d
k(  rt        |d|z
         t        ||      t        |dz   dz  |      kD  r?J  y # 1 sw Y   axY w)N   rA   seedsizesample_weight   cvrY      rO   rd   rY   rW   )rF   nprandomRandomStateuniformr_   minr&   rS   predict_probar   rP   rQ   rR   r   r7   )rK   rO   rV   rY   rC   rH   rI   ra   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                       rJ   test_calibrationry   X   s   
 QIDAqII))r)2:::GM	AEEGA "#:I*9}Zi?XhWGyz]AijMFF /

gwh

GC$$V,QT2L$SQVVaZ(KG	z	" Aq
 
&	w	v!67& #!k )VHU 	L'B"00=adC  58H$9
 
 	
 

 	L'A+XF%,%:%:;%G1%M"!"24NO 	L!g+/J%,%:%:;%G1%M"!"24NO 	L7Q;!"38L%,%:%:;%G1%M"Y%&6<V8VW $FL9<L!q "<=   C#	 s   IIc                     | \  }}t        d      }|j                  ||       |j                  d   j                  }t	        |t
              sJ y )Nr[   rd   r   )r   rS   calibrated_classifiers_	estimator
isinstancer+   )rK   rH   rI   	calib_clfbase_ests        rJ   "test_calibration_default_estimatorr      sI    DAq&!,IMM!Q003==Hh	***rL   c                    | \  }}d}t        |      }t        ||      }t        |j                  t               sJ |j                  j                  |k(  sJ |j                  ||       |r|nd}t        |j                        |k(  sJ y )Nre   n_splitsrc   rb   )r    r   r~   rd   r   rS   lenr|   )rK   rY   rH   rI   splitskfoldr   expected_n_clfs           rJ   test_calibration_cv_splitterr      s     DAqF6"E&%(CIillE***<<  F***MM!Q'VQNy001^CCCrL   c                 r   | \  }}t        d      }t        |d      }t        j                  t        d      5  |j                  ||       d d d        t        t               d      }t        j                  t        d      5  |j                  ||       d d d        y # 1 sw Y   UxY w# 1 sw Y   y xY w)Ne   r   Trc   z$Requesting 101-fold cross-validationmatchz!LeaveOneOut cross-validation does)r    r   rP   rQ   rR   rS   r!   )rK   rH   rI   r   r   s        rJ   test_calibration_cv_nfoldr      s    DAq3E&%$?I	z)O	P a '+-$GI	z)L	M a 	  s   B!B-!B*-B6)rW   rX   temperaturec                    t         dz  }| \  }}t        j                  j                  d      j	                  t        |            }|d | |d | |d | }	}}||d  }
t        d      }t        |||      }|j                  |||	       |j                  |
      }|j                  ||       |j                  |
      }t        j                  j                  ||z
        }|dkD  sJ y )	Nr[   rA   r\   r^   rE   )rO   rY   r`   皙?)rF   rg   rh   ri   rj   r   r+   r   rS   rl   linalgnorm)rK   rO   rY   rC   rH   rI   ra   rm   rn   ro   rp   r}   calibrated_clfprobs_with_swprobs_without_swdiffs                   rJ   test_sample_weightr      s     QIDAqII))r)2::A:GM!":I*9}Zi?XhWGyz]Fr*I+IfxXNwx@"008M w(%33F;99>>-*::;D#::rL   c                 P   | \  }}t        ||d      \  }}}}t        t               t        d            }	t	        |	|d|      }
|
j                  ||       |
j                  |      }t	        |	|d|      }|j                  ||       |j                  |      }t        ||       y)zTest parallel calibrationrA   r   r[   )rO   n_jobsrY   rb   N)r%   r(   r*   r+   r   rS   rl   r   )rK   rO   rY   rH   rI   rm   rp   rn   rq   r}   cal_clf_parallelprobs_parallelcal_clf_sequentialprobs_sequentials                 rJ   test_parallel_executionr      s    
 DAq'712'N$GVWfn.	r0JKI-&X '*%33F;N/&X 7G,)77?N$45rL   r]   r[   c                    d }t        d      }t        dd|dd      \  }}d	||d	kD  <   t        j                  |      j                  d
   }|d d d	   |d d d	   }	}|dd d	   |dd d	   }}
|j                  ||	       t        || d|      }|j                  ||	       |j                  |
      }t        t        j                  |d      t        j                  t        |
                   d|j                  |
|      cxk  rdk  sJ  J |j                  |
|      d|j                  |
|      z  kD  sJ  ||t        |j                  |
            |      } ||||      }|d|z  k  sJ t        dd      }|j                  ||	       |j                  |
      } ||||      }t        || d|      }|j                  ||	       |j                  |
      } ||||      }|d|z  k  sJ y )Nc                     t        j                  |      |    }t        j                  ||z
  dz        |j                  d   z  S )Nr[   r   )rg   eyesumshape)y_true
proba_pred	n_classesY_onehots       rJ   multiclass_brierz5test_calibration_multiclass.<locals>.multiclass_brier   s<    66)$V,vvx*,23hnnQ6GGGrL      r     d   
         .@rC   rD   rE   centerscluster_stdr[   r   rb   re   rf   axis?gffffff?)r   g?   rA   )n_estimatorsrE   )r+   r   rg   uniquer   rS   r   rl   r   r   onesr   scorer9   decision_functionr   )rO   rY   r]   r   rr   rH   rI   r   rm   rn   rp   rq   rt   probasuncalibrated_briercalibrated_brier	clf_probscal_clf_probss                     rJ   test_calibration_multiclassr      s   H 
#C#D"RVDAq Aa!eH		!""1%I1vq1vWGqt!tWa1gFFGGGW$SAQGKK!""6*FBFF6*BGGCK,@A
 #))FF+2d22222 ==(4#))FF2K+KKKK
 *--f56) ()Lc$66666 !br
BCGGGW!!&)I)&)yQ$SAQGKK!))&1M'Sc$66666rL   c                      G d d      } t        ddddd      \  }}t               j                  ||      } |        }t        ||g|j                        }|j                  |      }t        |d	|j                  z         y )
Nc                       e Zd Zd Zy)9test_calibration_zero_probability.<locals>.ZeroCalibratorc                 F    t        j                  |j                  d         S )Nr   )rg   zerosr   selfrH   s     rJ   predictzAtest_calibration_zero_probability.<locals>.ZeroCalibrator.predict:  s    88AGGAJ''rL   N)__name__
__module____qualname__r    rL   rJ   ZeroCalibratorr   8  s    	(rL   r   2   r   r   r   r   )r}   calibratorsclasses      ?)r   r   rS   r
   classes_rl   r   
n_classes_)r   rH   rI   rr   
calibratorrt   r   s          rJ   !test_calibration_zero_probabilityr   3  s    
( (
 !RTDAq 


1
%C!J#J<G ""1%F FC#..01rL   c           
      >   d}t        d|z  dd      \  }}t        j                  j                  d      j	                  |j
                        }||j                         z  }|d| |d| |d| }}}||d	|z   ||d	|z   ||d	|z   }}
}	|d	|z  d |d	|z  d }}t               }|j                  |||       |j                  |      ddd
f   }|	|f | |	       | |      ffD ]  \  }}t        t        |      |      }|dfD ]  }|j                  ||
|       |j                  |      }|j                  |      }|ddd
f   }t        |t        j                  dd
g      t        j                  |d
                t!        ||      t!        ||      kD  rJ   y)z'Test calibration for frozen classifiersr      r@   rA   rB   r\   r^   Nr[   rb   rN   r`   r   r   )r   rg   rh   ri   rj   r_   rk   r&   rS   rl   r   r   r   r8   arrayargmaxr   )rV   rO   rC   rH   rI   ra   rm   rn   ro   X_caliby_calibsw_calibrp   rq   rr   rs   this_X_calibrv   cal_clf_frozenswy_prob_frozeny_pred_frozenprob_pos_cal_clf_frozens                          rJ   test_calibration_frozenr   L  s    IY1SUVDAqII))r)2:::GMLA "#:I*9}Zi?XhWG	)a)m$	)a)m$i!i-0 WG
 q9}'1y=?);FF /CGGGWh'$$V,QT2L 
&	w	v!67& !k 00DVTT" 	B|WBG*88EM*22;?M&3AqD&9#rxxA/		-a0PQ $FL9<L/=   	rL   r   clip)out_of_boundsr   c                    | \  }}t        d      }t        ||dd      }|j                  ||       |j                  |      }t	        |||dd      }|j                  ||       |j                  ||       |j                  |      }	|j                  |	      }
|dk(  r*|
j                  d	k(  r|
j                  d
   d	k(  r	|
d d d
f   }
t        |d d d
f   |
       y )Nr   r   r   Frf   r   )rd   rO   r   r[   rb   )
r+   r   rS   rl   r#   r   r   ndimr   r   )rK   rO   r   rH   rI   rr   rt   
cal_probasunbiased_predsclf_dfmanual_probass              rJ   test_calibration_ensemble_falser   y  s     DAq

#C$SANGKK1&&q)J 'sAqQ?RSNNN>1%GGAqM""1%F&&v.M!#-*=*=a*@A*E)!Q$/MJq!t$m4rL   c                  0   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               j                  | |      j                  |       }t        ||d	       t        j                  t              5  t               j                  t        j                  | | f      |       d
d
d
       y
# 1 sw Y   y
xY w)z0Test calibration values with Platt sigmoid model)re   r   )rb   r   gj=ɿgY90(?r   r   r   rb   r@   N)rg   r   r7   r   expr   rS   r   rP   rQ   rR   vstack)exFexYAB_lin_libsvmlin_probsk_probs        rJ   test_sigmoid_calibrationr     s    
((<
 C
((;
CHH24GHIMm-A#s-KQOcBFF=#3c#9M!<L#LMMNH!#''S199#>Gh3 
z	" >!!"))S#J"7=> > >s   0DDr   )r[   r   re   c           
         t        dddd| ddd      \  }}t        ||d      \  }}}}t        t        j                  d	d
d      }|j                  ||       t        t        |      dd|      j                  ||      }	|	j                  }
|
D ]G  }t        |j                        dk(  sJ |j                  d   }t        |       |j                  dkD  rGJ  |s|j                  |      }|	j                  |      }t        ||      t        ||      k(  sJ |j                  |      }|	j                  |      }t!        ||      t!        ||      k  sJ | dk(  r|dddf   }|dddf   }t#        t%        ||d      t%        ||d             |j                  |      }t'               j                  ||      }t#        |j                  ddd       yy)z%Check temperature scaling calibration  r   r   rb          @rA   rC   rD   n_informativen_redundantr   n_clusters_per_class	class_seprE   r   g:0yE>r<   )Ctolmax_iterrE   r   r   rd   rO   rY   r[   Novr)multi_classr   ư>)atolrtol)r   r%   r   rg   infrS   r   r   r|   r   r   r;   beta_r   r   rl   r   r   r   r   )r   rY   rH   rI   rm   X_calrn   y_calrr   rt   calibrated_classifierscalibrated_classifierr   y_pred
y_pred_caly_scoresy_scores_caly_scores_traintss                      rJ   test_temperature_scalingr    s    	DAq &6a%L"GUGU
rvv4#A
NCGGGW$=8	c%  %<<!7 	$ (445:::*66q9

#!###	$ U#__U+
eZ0N5&4QQQQ $$U+,,U3|,0IIII
 >1~H'1-L%u=%5A	
 **73 "&&~w?#Dq95 rL   c                    t        j                  d      j                  |       }|j                  dd      }t         j                  j                  dd|j                  d         }t               j                  ||      }t               j                  ||      }t        |      t        |      k(  sJ |j                  |      }|j                  |      }t        ||       y )Nr   r   rb   r   r[   r^   )rg   arangeastypereshaperh   randintr   r   rS   r3   r   r   )global_dtyperH   X_2drI   r  ts_2dy_pred1y_pred2s           rJ   )test_temperature_scaling_input_validationr!    s    
		"\*A99RD
		!QQWWQZ0A			"	"1a	(B!%%dA.EB<8E?***jjmGmmD!GGW%rL   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
       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
       y
# 1 sw Y   xY w# 1 sw Y   y
xY w)z Check calibration_curve function)r   r   r   rb   rb   rb   )        r   皙?皙??r   r[   n_binsr   rb   r   r&  gN)r   r   r   r   rb   rb   )r#  r   r$        ?r&  r   quantiler(  strategygUUUUUU?r%  
percentile)r,  )rg   r   r   r   r6   rP   rQ   rR   )r   r  	prob_true	prob_predy_true2r   prob_true_quantileprob_pred_quantiles           rJ   test_calibration_curver3    si   XX()FXX45F,VVAFIyy>S^+++y>Q	Aq6*	C:. 
z	" '1#v&' hh)*Ghh56G->Z.** !"c*<&====!"a'''*QJ7*S#J7 
z	" C'7\BC C!' ' C Cs   E+E7+E47F c                     t        ddddd      \  }}t        j                  |d<   t        dt	               fdt        d	
      fg      }t        |d| |      }|j                  ||       |j                  |       y)z$Test that calibration can accept nanr   r[   r   rA   )rC   rD   r   r   rE   r   r   imputerrfrb   )r   r  N)	r   rg   nanr'   r   r   r   rS   r   )rO   rY   rH   rI   rr   clf_cs         rJ   test_calibration_nan_imputerr:  "  s}     !QSDAq ffAdG

]_	%.DRS.T'UVC #31VhOE	IIaO	MM!rL   c                     t        ddd      \  }}g d}t        dd      }t        || t        d	
      |      }|j	                  ||       t        |j                  |      j                  d      d       y )Nr   re   r[   )rC   rD   r   )
rb   rb   rb   rb   rb   r   r   r   r   r   r   r   )r  rE   r   r   rf   rb   r   )r   r+   r   r    rS   r   rl   r   )rO   rY   rH   _rI   rr   clf_probs          rJ   test_calibration_prob_sumr>  2  sr    
 QGDAq&A
c
*C%Fua08H LLAH**1-11q193?rL   c           	         t         j                  j                  dd      }g dg dz   g dz   }t        d      }t	        |dt        d	      | 
      }|j                  ||       | rt        j                  d      }t        ddgdd	g      D ]v  \  }}|j                  |   j                  |      }t        |d d |f   t        j                  t        |                   t        j                  |d d ||k7  f   dkD        rvJ  y |j                  d   j                  |      }t        |j!                  d      t        j"                  |j$                  d                y )N   re   )r   r   r   rb   )rb   rb   r[   r[   )r[   r   r   r   r   r   rW   r   rf      r   r[   rb   r   )rg   rh   randnr,   r   r    rS   r  zipr|   rl   r8   r   r   allr7   r   r   r   )	rY   rH   rI   rr   rt   r   calib_iclass_iprobas	            rJ   test_calibration_less_classesrH  B  s/    			AA|#l2A
 a
0C$I%(XG KK1))A, #QFQF 3 	<GW33G<JJ1MEuQZ0"((3q62BC66%7g#5 56:;;;	< //2@@C!%)))"3RWWU[[^5LMrL   rH   rA      re   r@   c                 x    g d} G d dt         t              }t         |             }|j                  | |       y)z;Test that calibration accepts n-dimensional arrays as input)rb   r   r   rb   rb   r   rb   rb   r   r   rb   r   r   rb   r   c                       e Zd ZdZd Zd Zy)>test_calibration_accepts_ndarray.<locals>.MockTensorClassifierz*A toy estimator that accepts tensor inputsc                 :    t        j                  |      | _        | S N)rg   r   r   )r   rH   rI   s      rJ   rS   zBtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.fitn  s    IIaLDMKrL   c                 `    |j                  |j                  d   d      j                  d      S )Nr   r   rb   r   )r  r   r   r   s     rJ   r   zPtest_calibration_accepts_ndarray.<locals>.MockTensorClassifier.decision_functionr  s)    99QWWQZ,00a088rL   N)r   r   r   __doc__rS   r   r   rL   rJ   MockTensorClassifierrL  k  s    8		9rL   rQ  N)r   r   r   rS   )rH   rI   rQ  r   s       rJ    test_calibration_accepts_ndarrayrR  `  s7     	6A	9 	9 ,,@,BCNq!rL   c                  >    dddddddddddddddg} g d	}| |fS )
NNYadult)stateageTXVTchildCTBR)rb   r   rb   rb   r   r   )	dict_datatext_labelss     rJ   r]  r]  {  sE     w'w'w'w'w'I "Kk!!rL   c                 r    | \  }}t        dt               fdt               fg      }|j                  ||      S )N
vectorizerrr   )r'   r   r   rS   )r]  rH   rI   pipeline_prefits       rJ   dict_data_pipelinerb    sC    DAq
(	)E3I3K+LMO q!$$rL   c                    | \  }}|}t        t        |      d      }|j                  ||       t        |j                  |j                         t        |d      rJ t        |d      rJ |j                  |       |j                  |       y)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[   r{   n_features_in_N)r   r   rS   r8   r   hasattrr   rl   )r]  rb  rH   rI   rr   r   s         rJ   test_calibration_dict_pipelinerf    s     DAq
C&s';BIMM!Qy))3<<8 s,---y"2333 aArL   c                  (   t        dddd      \  } }t        t        d      d      }|j                  | |       t	               j                  |      j
                  }t        |j
                  |       |j                  | j                  d   k(  sJ y )	Nr   re   r[   r   rC   rD   r   rE   rb   r  r{   )	r   r   r+   rS   r)   r   r8   rd  r   )rH   rI   r   r   s       rJ   test_calibration_attributesrj    sy    QUVWDAq&y1~!<IMM!Qn  #,,Gy))73##qwwqz111rL   c                  "   t        dddd      \  } }t        d      j                  | |      }t        t	        |            }d}t        j                  t        |	      5  |j                  | d d d d
f   |       d d d        y # 1 sw Y   y xY w)Nr   re   r[   r   rh  rb   ri  zAX has 3 features, but LinearSVC is expecting 5 features as input.r   r   )r   r+   rS   r   r   rP   rQ   rR   )rH   rI   rr   r   msgs        rJ   2test_calibration_inconsistent_prefit_n_features_inrm    s     QUVWDAq
a.

Q
"C&s';<I
MC	z	- #a2A2h"# # #s    BBc            	         t        dddd      \  } }t        t        d      D cg c]  }dt        |      z   t	               f c}d	      }|j                  | |       t        t        |      
      }|j                  | |       y c c}w )Nr   re   r[   r   rh  r   lrsoft)
estimatorsvotingr}   )r   r   rangestrr   rS   r   r   )rH   rI   ivoter   s        rJ   !test_calibration_votingclassifierrx    s{     QUVWDAqCH8LaTCF]$6$89LD 	HHQN&1FGIMM!Q Ms   Bc                      t        d      S )NT
return_X_y)r   r   rL   rJ   	iris_datar|    s    %%rL   c                 ,    | \  }}||dk     ||dk     fS )Nr[   r   )r|  rH   rI   s      rJ   iris_data_binaryr~    s&    DAqQU8Qq1uXrL   r(  r   r,  rj   r*  c                    |\  }}t               j                  ||      }t        j                  |||||d      }|j	                  |      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                  j!                         dk(  sJ t        |j"                  |j$                  j&                        sJ t        |j(                  |j*                  j,                        sJ |j"                  j/                         dk(  sJ |j"                  j1                         dk(  sJ dd	g}|j"                  j3                         j5                         }t7        |      t7        |      k(  sJ |D ]  }|j9                         |v rJ  y )
Nr%  )r(  r,  alpharb   r+  r   r   z.Mean predicted probability (Positive class: 1)z)Fraction of positives (Positive class: 1)Perfectly calibrated)r   rS   r	   from_estimatorrl   r   r   r.  r/  y_probestimator_name
matplotlibr~   line_linesLine2D	get_alphaax_axesAxesfigure_figureFigure
get_xlabel
get_ylabel
get_legend	get_textsr   get_text)pyplotr~  r(  r,  rH   rI   ro  vizr  r.  r/  mplexpected_legend_labelslegend_labelslabelss                  rJ    test_calibration_display_computer    s    DAq			!	!!Q	'B

+
+
Aq(#C a A&F,	6&8Iy CMM9-CMM9-CJJ'!5555 cii!1!122299 C'''cggsxx}}---ckk3::#4#455577#SSSS77#NNNN24JKGG&&(224M}%;!<<<< ; $::::;rL   c                 l   |\  }}t        t               t                     }|j                  ||       t	        j
                  |||      }|j                  dg}|j                  j                         j                         }t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  y )Nr  )r(   r*   r   rS   r	   r  r  r  r  r  r   r  )	r  r~  rH   rI   rr   r  r  r  r  s	            rJ   $test_plot_calibration_curve_pipeliner    s    DAq
(*<*>
?CGGAqM

+
+CA
6C!002HIGG&&(224M}%;!<<<< ; $::::;rL   zname, expected_label)N_line1)my_estr  c                    t        j                  g d      }t        j                  g d      }t        j                  g       }t        ||||      }|j                          |g n|g}|j	                  d       |j
                  j                         j                         }t        |      t        |      k(  sJ |D ]  }	|	j                         |v rJ  y )Nr   rb   rb   r   r$  r%  r%  皙?r  r  )
rg   r   r	   plotappendr  r  r  r   r  )
r  nameexpected_labelr.  r/  r  r  r  r  r  s
             rJ   'test_calibration_display_default_labelsr    s     &I-.IXXb\F
Y	6$
OCHHJ#'<RdV!!"89GG&&(224M}%;!<<<< ; $::::;rL   c                    t        j                  g d      }t        j                  g d      }t        j                  g       }d}t        ||||      }|j                  |k(  sJ d}|j	                  |       |dg}|j
                  j                         j                         }t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  y )Nr  r  zname oner  zname twor  r  )
rg   r   r	   r  r  r  r  r  r   r  )	r  r.  r/  r  r  r  r  r  r  s	            rJ   )test_calibration_display_label_class_plotr  )  s     &I-.IXXb\FD
Y	6$
OC%%%DHH$H"$:;GG&&(224M}%;!<<<< ; $::::;rL   constructor_namer  from_predictionsc                    |\  }}d}t               j                  ||      }|j                  |      d d df   }t        t        |       }| dk(  r|||fn||f}	 ||	d|i}
|
j
                  |k(  sJ |j                  d       |
j                          |dg}|
j                  j                         j                         }t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  |j                  d       d}|
j                  |       t        |      t        |      k(  sJ |D ]  }|j                         |v rJ  y )	Nzmy hand-crafted namerb   r  r  rD  r  another_namer  )r   rS   rl   getattrr	   r  closer  r  r  r  r   r  )r  r  r~  rH   rI   clf_namerr   r  constructorparamsr  r  r  r  s                 rJ   ,test_calibration_display_name_multiple_callsr  =  sc    DAq%H


"
"1a
(Cq!!Q$'F,.>?K,0@@c1a[q&kF
v
-H
-C)))
LLHHJ&(>?GG&&(224M}%;!<<<< ; $::::; LLHHH(H}%;!<<<< ; $::::;rL   c                 P   |\  }}t               j                  ||      }t               j                  ||      }t        j                  |||      }t        j                  ||||j
                        }|j
                  j                         d   }|j                  d      dk(  sJ y )N)axrb   r  )r   rS   r,   r	   r  r  get_legend_handles_labelscount)	r  r~  rH   rI   ro  dtr  viz2r  s	            rJ   !test_calibration_display_ref_liner  `  s    DAq			!	!!Q	'B		!	%	%a	+B

+
+B1
5C,,RA#''BDXX//1!4F<<./1444rL   dtype_y_strc                 >   t         j                  j                  d      }t        j                  dgdz  dgdz  z   |       }|j	                  dd|j
                        }d	}t        j                  t        |
      5  t        ||       ddd       y# 1 sw Y   yxY w)zKCheck error message when a `pos_label` is not specified with `str` targets.rA   spamr   eggsr[   dtyper   r^   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)
rg   rh   ri   r   r  r_   rP   rQ   rR   r   )r  rngy1y2err_msgs        rJ   *test_calibration_curve_pos_label_error_strr  m  s     ))


#C	6(Q,&A-[	AB	Q	(B	$ 
 
z	1 ""b!" " "s   =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       y)z8Check the behaviour when passing explicitly `pos_label`.)	r   r   r   rb   rb   rb   rb   rb   rb   r  eggr  )	r   r$  g333333?r  r   gffffff?r%  r&  r   rA  r'  )r   r)  rb   rb   )r(  	pos_labelrb   r   )r   r   r)  rb   N)rg   r   r   r   )r  r   r   
y_true_strr  r.  r<  s          rJ    test_calibration_curve_pos_labelr  }  s     XX12Fhhk:GJXXDEF %VVA>LIqI~.$ZUSLIqI~.$VQZQOLIqI~.$ZVAQWXLIqI~.rL   kwargsred-.)clwls)color	linewidth	linestylec                 ,   |\  }}t               j                  ||      }t        j                  |||fi |}|j                  j                         dk(  sJ |j                  j                         dk(  sJ |j                  j                         dk(  sJ y)z*Check that matplotlib aliases are handled.r  r[   r  N)r   rS   r	   r  r  	get_colorget_linewidthget_linestyle)r  r~  r  rH   rI   ro  r  s          rJ   test_calibration_display_kwargsr    s     DAq			!	!!Q	'B

+
+B1
?
?C99 E)))99""$)))99""$,,,rL   zpos_label, expected_pos_label))Nrb   r5  )rb   rb   c                    |\  }}t               j                  ||      }t        j                  ||||      }|j	                  |      dd|f   }t        |||      \  }	}
t        |j                  |	       t        |j                  |
       t        |j                  |       |j                  j                         d| dk(  sJ |j                  j                         d| dk(  sJ |j                  j                  dg}|j                  j                         j!                         }t#        |      t#        |      k(  sJ |D ]  }|j%                         |v rJ  y)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   rS   r	   r  rl   r   r   r.  r/  r  r  r  r  	__class__r   r  r  r   r  )r  r~  r  expected_pos_labelrH   rI   ro  r  r  r.  r/  r  r  r  s                 rJ   "test_calibration_display_pos_labelr    sc   
 DAq			!	!!Q	'B

+
+B1	
JCa $6!67F,Q)LIyCMM9-CMM9-CJJ' 	9:L9MQO	P	P 	45G4HJ	K	K !ll335KLGG&&(224M}%;!<<<< ; $::::;rL   c                 r   t        d      \  }}t               j                  |      }|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        |      }	|	j                  |||
       |j                  ||       t        |	j                  |j                        D ]9  \  }
}t        |
j                  j                   |j                  j                          ; |	j#                  |      }|j#                  |      }t        ||       y)zrCheck that passing repeating twice the dataset `X` is equivalent to
    passing a `sample_weight` with a factor 2.Trz  Nr   r[   r   rb   r  )rO   rY   rd   r`   )r   r*   fit_transformrg   	ones_liker   r   r  r   r   r   rS   rC  r|   r   r}   coef_rl   )rO   rY   rH   rI   ra   X_twicey_twicer}   calibrated_clf_without_weightscalibrated_clf_with_weightsest_with_weightsest_without_weightsy_pred_with_weightsy_pred_without_weightss                 rJ   ?test_calibrated_classifier_cv_double_sample_weights_equivalencer    s   
 %DAq&&q)ATc7AdsGqALLOa'M hh
Q
3177CGGCaCFOGADqD!GhhqwwqzA~QWW5GGCaCLGADqDM"$I%;	&" #((F"G##Aq#F"&&w8 25#;;&>>2 
-- 	&&,,))//	
	
 6CCAF;II!L')?@rL   fit_params_typelistr   c                     |\  }}t        ||       t        ||       d}t        ddg      }t        |      } |j                  ||fi | y)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)r5   r2   r   rS   )r  rK   rH   rI   
fit_paramsrr   pc_clfs          rJ    test_calibration_with_fit_paramsr    sW     DAq?3?3J
 #s
<C#C(FFJJq!"z"rL   ra   r   c                 d    |\  }}t        d      }t        |      }|j                  |||        y)zMTests that sample_weight is passed to the underlying base
    estimator.
    T)expected_sample_weightr`   N)r2   r   rS   )ra   rK   rH   rI   rr   r  s         rJ   -test_calibration_with_sample_weight_estimatorr    s3     DAq
D
9C#C(F
JJq!=J1rL   c                     | \  }}t        j                  |      } G d dt              } |       }t        |      }t	        j
                  t              5  |j                  |||       ddd       y# 1 sw Y   yxY w)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 )Ptest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeightc                 2    d|vsJ t        |   ||fi |S )Nra   superrS   )r   rH   rI   r  r  s       rJ   rS   zTtest_calibration_without_sample_weight_estimator.<locals>.ClfWithoutSampleWeight.fit(  s'    "*4447;q!2z22rL   r   r   r   rS   __classcell__r  s   @rJ   ClfWithoutSampleWeightr  '  s    	3 	3rL   r  r`   N)rg   r  r2   r   rP   warnsUserWarningrS   )rK   rH   rI   ra   r  rr   r  s          rJ   0test_calibration_without_sample_weight_estimatorr    sn     DAqLLOM3!3 3
 !
"C#C(F	k	" 6

1a}
56 6 6s   A33A<c           
           G d dt               } t         |             j                  | dt        j                  t        | d         dz         i y)z[Check that CalibratedClassifierCV does not enforce sample alignment
    for fit parameters.c                         e Zd Zd fd	Z xZS )Jtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifierc                 0    |J t         |   |||      S )Nr`   r  )r   rH   rI   ra   	fit_paramr  s        rJ   rS   zNtest_calibration_with_non_sample_aligned_fit_param.<locals>.TestClassifier.fit8  s$    (((7;q!=;AArL   )NNr   r  s   @rJ   TestClassifierr	  7  s    	B 	BrL   r  rs  r  rb   N)r   r   rS   rg   r   r   )rK   r  s     rJ   2test_calibration_with_non_sample_aligned_fit_paramr  3  sM    B+ B
 ;^%56::	T!W!12rL   c           	         d}d}t         j                  j                  |       j                  |      }t        j                  dgt        ||z        z  dg|t        ||z        z
  z  z         }d|j                  d      z  |z   }t        d|d	
      }|j                  ||      }|D ]Y  \  }}	||   ||   }}
||	   }t        d|       }|j                  |
|       |j                  |      }|dkD  j                         rYJ  t        t        d|       d      }t        |||d      }t        t        d|       d      }t        |||d      }t        ||       y)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?r   r^   rb   r   g     j@)r   rb   NT)rd   rI   
classifiersquared_hinge)lossrE   g     @rW   rN   roc_auc)scoringrX   )rg   rh   default_rngnormalr   intr  r"   splitr   rS   r   anyr   r$   r   )global_random_seedprobnrandom_noiserI   rH   rd   indicestraintestrm   rn   rp   sgd_clfpredictionsclf_sigmoidscore_sigmoidclf_isotonicscore_isotonics                      rJ   @test_calibrated_classifier_cv_works_with_large_confidence_scoresr&  A  sq    DA99(();<CCCKL
!s1t8}$sa#a$h-.?'@@AAaii  </A 
TQ4	0Bhhq!nG )tU8QuX4_CUVGW%//7c!&&((() )?9KLK $KAyIM *?9KLL %\1aKN M>2rL   c                 x   t         j                  j                  |       }d}|j                  dd|      }|j	                  ddd      }d}t        |||	      \  }}d
}t        |||	      \  }	}
t        ||      \  }}d}t        ||	|       t        |	||       t        ||
|       t        |
||       y )Nr\   r   r   r[   r^   )lowhighr_   r   )r!  rI   max_abs_prediction_thresholdr   )r!  rI   r  )r  )rg   rh   ri   r  rj   r   r   )r  rE   r  rI   predictions_smallthreshold_1a1b1threshold_2a2b2a3b3r  s                 rJ   5test_sigmoid_calibration_max_abs_prediction_thresholdr5  s  s    99((.@(ALAQ*A %,,!#,F K!%
%0FB K!%
%0FB "%
FB DB&B&B&B&rL   use_sample_weightc                 F   |r)t        j                  | d   t         j                        }nd} G d dt              } |       }t	        ||      } |j
                  | d|i   |       j
                  | d|i}t	        t        |      |      } |j
                  | d|i y)z|Check that CalibratedClassifierCV works with float32 predict proba.

    Non-regression test for gh-28245 and gh-28247.
    rb   r  Nc                        e Zd Z fdZ xZS )4test_float32_predict_proba.<locals>.DummyClassifer32c                 \    t         |   |      j                  t        j                        S rN  )r  rl   r  rg   float32)r   rH   r  s     rJ   rl   zBtest_float32_predict_proba.<locals>.DummyClassifer32.predict_proba  s"    7(+222::>>rL   )r   r   r   rl   r  r  s   @rJ   DummyClassifer32r9    s    	? 	?rL   r<  rN   ra   )rg   r  float64r   r   rS   r   )rK   r6  rO   ra   r<  modelr   s          rJ   test_float32_predict_probar?    s      T!WBJJ??? ? E'f=JJNND66 #""DFFE'(>vNJJNND66rL   c                      t         j                  j                  d      } dgdz  dgdz  z   }t        d      j	                  | |       y)	zlCheck that CalibratedClassifierCV works with string targets.

    non-regression test for issue #28841.
    )   r   r^   r  r   r  r   r{   N)rg   rh   r  r   rS   rG   s     rJ   (test_error_less_class_samples_than_foldsrB    sF    
 			g&A	
cURZAa $$Q*rL   z$array_namespace, device_, dtype_name)idsc           
      l   t        ||      }t        dddddddd      \  }}t        ||d	      \  }}	}
}|j                  |      }|
j                  |      }
|j	                  ||
      }|j	                  |
|
      }|	j                  |      }	|j                  |      }|j	                  |	|
      }|j	                  ||
      }|rt        j                  |      }d|ddd<   nd}t               }|j                  ||
       t        t        |      dd|       j                  |	||      }|j                  d   j                  d   }|j                  |      }t        d      5  t               }|j                  ||       t        t        |      dd|       j                  |||      }|j                  d   j                  d   }|dk(  rdnd}t        |j                         d   j"                  |j"                  k(  sJ |j                   j$                  |j$                  k(  sJ t'        |j                         t'        |      k(  sJ t)        t+        |j                   |      |j                   |       |j                  |      }t)        t+        ||      |       ddd       y# 1 sw Y   yxY w)zaCheck that `CalibratedClassifierCV` with temperature scaling is compatible
    with the array APIr   r   r   re   rb   r   rA   r   r   r/   r[   Nr   r   r  r`   Tarray_api_dispatchr;  MbP?Hz>xpr	  )r4   r   r%   r  asarrayrg   r  r   rS   r   r   r|   r   r   r   r0   r  r   r  r/   r   r-   )rY   r6  array_namespacedevice_
dtype_namerK  rH   rI   rm   r  rn   r  
X_train_xp
y_train_xpX_cal_xpy_cal_xpra   clf_np
cal_clf_npcalibrator_nppred_npclf_xp
cal_clf_xpcalibrator_xpr	  pred_xps                             rJ   -test_temperature_scaling_array_api_compliancer]    s    
ow	7B	DAq &6a%L"GUGUnnZ(GnnZ(GGG4JGG4JLL$ELL$Ezz%z0Hzz%z0HU+add')F
JJw 'Amh	c%mc4  66q9EEaHM  )G	4	0 D+-

:z*+F#-(

#h#
> 	 #::1=II!L!Y.tD]001!4==LLL""((HNN:::m))*fX.>>>>m11b9	

 $$Z0)'b97C%D D Ds   'D:J**J3c           
         t        ||      }t        dddddddd      \  }}t        j                  g d	      }|j	                  |      }||   }	|j                  ||
      }
|rt        j
                  |      }d|ddd<   nd}t        t               dd|       j                  ||	|      }|j                  d   j                  d   }|j                  |      }t        d      5  t        t               dd|       j                  |
|	|      }|j                  d   j                  d   }|dk(  rdnd}t        |j                        d   j                  |j                  k(  sJ |j                  j                   |
j                   k(  sJ t#        |j                        t#        |
      k(  sJ t%        t'        |j                  |      |j                  |       |j                  |
      }t)        ||       ddd       y# 1 sw Y   yxY w)zCheck that `CalibratedClassifierCV` with temperature scaling is compatible
    with the array API when `y` is an ndarray of strings and the estimator is not
    fit beforehand (i.e. it is fit within `CalibratedClassifierCV`).
    r   r   r   re   rb   r   rA   r   )r  r  r  derE  r[   Nr   r   )r}   rd   rO   rY   r`   TrF  r;  rH  rI  rJ  rL  )r4   r   rg   rM  r  r  r   r   rS   r|   r   r   r   r0   r  r   r  r/   r   r-   r8   )rY   r6  rN  rO  rP  rK  rH   rI   str_mappingy_strX_xpra   rV  rW  rX  rZ  r[  r	  r\  s                      rJ   Btest_temperature_scaling_array_api_with_str_y_estimator_not_prefitrd    s	   & 
ow	7B	DAq **67K	ANE::a:(DQadd',.	
 
c!U-c0  66q9EEaHM  #G	4	0 -+02 	

 #dE#
7 	 #::1=II!L!Y.tD]001!4==LLL""((DJJ666m))*fTl:::m11b9	

 $$T*7G,'- - -s   'DHH)numpyrg   rP   numpy.testingr   sklearnr   sklearn.baser   r   r   sklearn.calibrationr   r	   r
   r   r   r   r   sklearn.datasetsr   r   r   sklearn.discriminant_analysisr   sklearn.dummyr   sklearn.ensembler   r   sklearn.feature_extractionr   sklearn.frozenr   sklearn.imputer   sklearn.isotonicr   sklearn.linear_modelr   r   sklearn.metricsr   r   r   r   sklearn.model_selectionr    r!   r"   r#   r$   r%   sklearn.naive_bayesr&   sklearn.pipeliner'   r(   sklearn.preprocessingr)   r*   sklearn.svmr+   sklearn.treer,   sklearn.utils._array_apir-   r.   r/   r0   r1   sklearn.utils._mockingr2   sklearn.utils._tagsr3   sklearn.utils._testingr4   r5   r6   r7   r8   sklearn.utils.extmathr9   sklearn.utils.fixesr:   sklearn.utils.validationr;   rF   fixturerK   rU   markparametrizery   r   r   r   r   thread_unsafer   rt  r   r   r   r   r   r  r!  r3  r:  r>  rH  rh   ri   rB  rR  r]  rb  rf  rj  rm  rx  r|  r~  r  r  r  r  r  r  ru  objectr  r  r  r  r  r  r   r  r  r  r&  r5  r?  rB  r]  rd  r   rL   rJ   <module>r     s_     ) " > >   H G D ) 6 * ( / B   . 4 > ! /  6 (  * . 4	 h  
@ .9Iz#:;dE]38 4 < :8v+ dE]3D 4D #IJdE]3 4 K0 #IJdE]36 4 K 6, Iz#:;dE]3 q*:7 + 4 <
:7z22 .9#IJ( K :(V |	')*	'f=>	+-.554>"  	5M;:		;:|&"C> #IJdE]3 4 K #IJdE]3@ 4 K@ dE]3N 4N: 
		b!''Aq1
		b!''Aq!4( 	" 	" % %42	#  h&  & h  
 Ar7+i%<=&; > ,&;R; -/CD;; ;( +.>@R-ST; U;D
5 f6" 7" f6/ 7/( 1D)ad;	-	- 8:UV; W;@ #IJdE]3-A 4 K-A` *VW,=># ?#$ 			226,/3d&'R ,tUm<#IJ7 K =7D+ eT]3,udm<*-/'  
=D = 4=D@ eT]3,udm<*-/'  
>- = 4>-rL   