
     h                        d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlZ	d dl
mZ d dlZd dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dlm!Z! d dlm"Z" d dlm#Z# d dlm$Z$ d dlm%Z% d dlm&Z& d dlm'Z' d dlm(Z( d dlm)Z) d dlm*Z* d dlm+Z+ d dlm,Z, d dlm-Z- d d lm.Z. d d!lm/Z/ d d"lm0Z0 d d#lm1Z1 d d$lm2Z2 d d%lm3Z3 d d&lm4Z4 d d'lm5Z5 d d(lm6Z6 d d)lm7Z7 d d*lm8Z8 d d+lm9Z9 d d,lm:Z: d d-lm;Z; d d.lm<Z< d d/lm=Z= d d0lm>Z> d d1lm?Z? d d2lm@Z@ d d3lmAZA d d4lmBZB d d5lmCZC d d6lmDZD d d7lmEZE d d8lmFZF d d9lmGZG d d:lmHZH d d;lImJZJ e4e6e8e>e<e7e+ eeAd<=           ee9d >          e:e; ee9d?>           ee'd?>          e(e)d@ZKi dAe dBe"dC ee"dDE          dF ee dGH          dIe%dJdK dLe=dM ee=dDN          dOe.dPeEdQ eeEdGH          dRe0dSe@dTeBdUe,dV ee-dWX          dY ee-dZX          i d[e5d\ ee-d]dZ^          d_ ee,d]`          da ee-d]dW^          db ee@d]`          dc eeBd]`          dd ee0d]`          de ee-dfdZ^          dg ee,df`          dh ee-dfdW^          di ee@df`          dj eeBdf`          dk ee0df`          dl ee-dmdZ^          dn ee,dm`          do ee-dmdW^          dp ee@dm`           eeBdm`           ee0dm`           ee-dqdZ^           ee,dq`           ee-dqdW^           ee@dq`           eeBdq`           ee0dq`          e$dr	ZLds ZMeDeMe*dtZNi due&dve2dwe3dx ee3dGH          dye/dze#d{eCd| eeCd]`          d} eeCdq`          d~ eeCdf`          d eeCdmd          d eeCd]d          d eeCdmd          d eeCd]d          d eeCdZ          de!d ee!d]`           ee!dq`           ee!df`          e1eFeGeHdZO eP            ZQeQR                    eO           eQR                    eL           eQR                    eK           eQR                    eN           h dZSh dZTeSU                    eT          ZVh dZWh dZXh dZYh dZZh dZ[h dZ\h dZ]h dZ^h dZ_h dZ`h dZah dZbd Zcd Zdeje        f                    d ege_                    d             Zheje        f                    d ege`                    d             Zieje        f                    d eg ejeQ          eVz
                      d             Zked             Zleje        f                    d eg ejeQ          eVz
                      d             Zmeje        f                    d eg ejeL          eVz
                      d             Zneje        f                    deO          d             Zod dge	jp        e	jp        gfd dge	jq        e	jq        gfd dge	jq        e	jp        gfd dge	jp        dgfd dge	jq        dgfgZreje        f                    d eeOs                                eKs                                                    eje        f                    der          d                         Zteje        f                    deLs                                          eje        f                    dere	jq        ddWgg dfe	jp        ddWgg dfgz             d                         Zueje        f                    deLs                                          d             Zved             Zwed             Zxeje        f                    d eg ejeQ          eVz
   ejeO          z
                      d             Zyeje        f                    d ege^e]z                      d             Zzeje        f                    d ege^                    d             Z{eje        f                    d ege^                    d             Z|ed             Z}eje        f                    d ege]                    d             Z~eje        f                    d ege[                    d             Zeje        f                    d ege[                    d             Zeje        f                    d ege[                    e]                              d             Zed             Zd Zeje        f                    d egeW                    d             Zeje        f                    d egeWeXz                      d             Zeje        f                    d egeW                    d             Zd Zeje        f                    d egeW                    d             Zed             Zeje        f                    d eg ejeQ                               ejeK                    eaz
                      d             Zeje        f                    d eg ejeQ           ejeK          z
  eaz
  eSz
                      d             Zeje        f                    d eg ejeQ           ejeK          z
  eaz
  eVz
                      d             Zeje        f                    d ege]e\z  e^z  eaz
                      d             Zed             Zeje        f                    d ege]dLhz
                      d             Zeje        f                    d ege\e^z                      dÄ             Zeje        f                    d eg ejeO          eVz
                      dĄ             Zeje        f                    deL          dƄ             Zeje        f                    de!dDfe#dDfe,dGf ee-dX          dGfe0dGfe?dDfe@dGfeBdGfeDdDfg	          eje        f                    deeg          dɄ                         ZdS )    )partial)	signature)product)chain)permutationsN)make_multilabel_classification)LabelBinarizer)type_of_target)_num_samples)check_random_state)shuffle)assert_allclose)assert_almost_equal)assert_array_equal)assert_array_less)ignore_warnings)accuracy_score)average_precision_score)balanced_accuracy_score)brier_score_loss)cohen_kappa_score)confusion_matrix)coverage_error)d2_tweedie_score)d2_pinball_score)d2_absolute_error_score)	det_curve)explained_variance_score)f1_score)fbeta_score)hamming_loss)
hinge_loss)jaccard_score)%label_ranking_average_precision_score)label_ranking_loss)log_loss)	max_error)matthews_corrcoef)mean_absolute_error)mean_absolute_percentage_error)mean_squared_error)mean_tweedie_deviance)mean_poisson_deviance)mean_gamma_deviance)median_absolute_error)multilabel_confusion_matrix)mean_pinball_loss)precision_recall_curve)precision_score)r2_score)recall_score)roc_auc_score)	roc_curve)zero_one_loss)
ndcg_score)	dcg_score)top_k_accuracy_score)_average_binary_scorevariance_weighted)multioutput)powergffffff?)r'   r)   r+   r1   r/   r*   r   r4   mean_normal_deviancer-   r.   mean_compound_poisson_deviancer   r   r   r   r    adjusted_balanced_accuracy_scoreT)adjustedunnormalized_accuracy_scoreF	normalizeunnormalized_confusion_matrixnormalized_confusion_matrixc                      t          | i |                    d          t          | i |                    d          d d t          j        f         z  S )Nfloat   axis)r   astypesumnpnewaxis)argskwargss     ]/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/metrics/tests/test_common.py<lambda>rU      s[    $)&))0099
D
+F
+
+
/
/Q
/
7
72:
F	G     (unnormalized_multilabel_confusion_matrix/unnormalized_multilabel_confusion_matrix_sample)
samplewiser!   r8   unnormalized_zero_one_lossr#   r3   r5   r   f2_score   )beta
f0.5_scoreg      ?matthews_corrcoef_scoreweighted_f0.5_scoreweighted)averager]   weighted_f1_scorerb   weighted_f2_scoreweighted_precision_scoreweighted_recall_scoreweighted_jaccard_scoremicro_f0.5_scoremicromicro_f1_scoremicro_f2_scoremicro_precision_scoremicro_recall_scoremicro_jaccard_scoremacro_f0.5_scoremacromacro_f1_scoremacro_f2_scoremacro_precision_scoresamples)	macro_recall_scoremacro_jaccard_scoresamples_f0.5_scoresamples_f1_scoresamples_f2_scoresamples_precision_scoresamples_recall_scoresamples_jaccard_scorer   c            
         t          | i |\  }}}t          |          t          |          z
  }t          j        ||t          j        |                    t          j                  d|fdt          j        g          g          S )a'  
    The dimensions of precision-recall pairs and the threshold array as
    returned by the precision_recall_curve do not match. See
    func:`sklearn.metrics.precision_recall_curve`

    This prevents implicit conversion of return value triple to an higher
    dimensional np.array of dtype('float64') (it will be of dtype('object)
    instead). This again is needed for assert_array_equal to work correctly.

    As a workaround we pad the threshold array with NaN values to match
    the dimension of precision and recall arrays respectively.
    r   constant)	pad_widthmodeconstant_values)r2   lenrP   arraypadrN   float64nan)rR   rS   	precisionrecall
thresholdspad_threshholdss         rT   (precision_recall_curve_padded_thresholdsr      s     %;D$KF$K$K!Ivz)nns:6O8F!!"*--o.!#	  		
  rV   )r7   r2   r   r   r%   r&   unnormalized_log_lossr"   r   r6   weighted_roc_aucsamples_roc_aucmicro_roc_aucovr_roc_aucovr)rb   multi_classweighted_ovr_roc_aucovo_roc_aucovoweighted_ovo_roc_aucpartial_roc_auc)max_fprr    weighted_average_precision_score)samples_average_precision_scoremicro_average_precision_scorer$   r9   r:   r;   >   r:   r9   r   ry   rz   r%   r|   r}   r{   r$   rX   rx   >   r   r[   r   r7   r5   r#   r   r6   r   r3   r   r   r   r2   r   r   r   r   r^   >   r   r[   r5   r#   r3   r^   >   r6   r   r   >   r   r[   r   r7   r5   r#   r3   r   r2   r   r   r   r   r^   >    r   r[   r   r7   r5   r#   rr   rs   rk   rl   r3   r   rc   re   rv   rn   rw   ro   rt   rm   rg   r2   rh   rf   rH   rG   rW   rX   r^   rp   ri   r`   >   r8   r   r;   >   r&   r:   r9   r   r6   r   r   r   r   r%   r   r   r   r   r   r$   >   r!   r8   r   rr   rs   rk   rl   ry   rz   rc   re   rv   rn   rw   ro   r|   rt   rm   r}   rg   rh   r{   rf   rZ   rD   rW   rp   ri   rx   r`   >	   r4   r   r1   r+   r)   r/   r   r   r*   >   r   r'   r!   r#   r8   r   rr   rk   rl   r   r1   r+   rn   rw   r)   ro   r@   r/   rm   r}   rg   r_   rZ   rD   ri   >   r[   r4   r   r7   r"   r5   rs   r3   r   r   rc   re   rv   r.   rt   r-   r2   rh   r   r   r   rf   rH   rG   r*   rA   rB   rW   r^   rp   r`   >   r'   r   r   r/   >   r   r.   r-   rA   c                     t          t          |                                 |                                                    dz   }| |z  } ||z  }| |fS )zMake targets strictly positiverK   )absmin)y1y2offsets      rT   _require_positive_targetsr     sI    RVVXXrvvxx(())A-F&LB&LBr6MrV   c                      t           t          z  t          t                    z  t          z  t          t
                    k    sJ t           t          z  t                      k    sJ d S N)SYMMETRIC_METRICSNOT_SYMMETRIC_METRICSsetTHRESHOLDED_METRICS"METRIC_UNDEFINED_BINARY_MULTICLASSALL_METRICS rV   rT   test_symmetry_consistencyr   '  sn     	
	 
!
"
"	# -	- 
[		     55#%%??????rV   namec                    t          d          }|                    ddd          }|                    ddd          }| t          v rt          ||          \  }}|                    ddd          }|                    ddd          }t          |          }| t
          v r>| t          v r+t           |||           |||          d| z             d S J d	            t           |||           |||          d| z             d S )
Nr   r\      sizer      z%s is not symmetricerr_msgFz This case is currently unhandled)r   randintMETRICS_REQUIRE_POSITIVE_Yr   r   METRIC_UNDEFINED_BINARYMULTILABELS_METRICSr   )r   random_statey_truey_pred
y_true_bin
y_pred_binmetrics          rT   test_symmetric_metricr   4  sX    &a((L!!!QU!33F!!!QU!33F)))266BB%%a%::J%%a%::JF&&&&&&z:..z:..-4      =<<<5F66""F66"")D0	
 	
 	
 	
 	
 	
rV   c                    t          d          }|                    ddd          }|                    ddd          }| t          v rt          ||          \  }}t          |          }t          j        t                    5  t           |||           |||                     t          d| z            # 1 swxY w Y   d S )Nr   r\   r   r   z%s seems to be symmetric)
r   r   r   r   r   pytestraisesAssertionErrorr   
ValueError)r   r   r   r   r   s        rT   test_not_symmetric_metricr   S  s     &a((L!!!QU!33F!!!QU!33F)))266BBF 
~	&	& < <66&&1166&&3I3IJJJ3d:;;;< < < < < < < < < <s   7B99B= B=c                    t          d          }|                    ddd          }|                    ddd          }| t          v rt          ||          \  }}t	          ||d          \  }}t                      5  t          |          }t           |||           |||          d| z             d d d            d S # 1 swxY w Y   d S )Nr   r\   r   r   r    %s is not sample order invariantr   )r   r   r   r   r   r   r   r   )r   r   r   r   y_true_shuffley_pred_shuffler   s          rT   test_sample_order_invariancer   e  s4    &a((L!!!QU!33F!!!QU!33F)))266BB%,VV!%L%L%L"NN			 
 
T"F66""F>>226=	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   ?7CC
Cc                     t          d          } |                     ddd          }|                     ddd          }|                     |j                  }t	          |||d          \  }}}t
          D ]8}t          |         }t           |||           |||          d|z             9t          D ]8}t          |         }t           |||           |||          d|z             9t          D ]a}t          |         }t           |||           |||          d|z             t           |||           |||          d|z             bd S )Nr   r\   r   r   r   r   r   )
r   r   normalshaper   r   r   r   THRESHOLDED_MULTILABEL_METRICSMULTIOUTPUT_METRICS)	r   r   r   y_scorer   r   y_score_shuffler   r   s	            rT   7test_sample_order_invariance_multilabel_and_multioutputr   {  s   %a((L !!!QX!66F!!!QX!66F!!v|!44G6=a7 7 73NNO $ 
 
T"F66""F>>226=	
 	
 	
 	
 	
 / 
 
T"F67##F>?336=	
 	
 	
 	
 	
 $ 
 
T"F67##F>?336=	
 	
 	
 	

 	F66""F>>226=	
 	
 	
 	
 	

 
rV   c                 z   t          d          }|                    ddd          }|                    ddd          }| t          v rt          ||          \  }}t	          |          }t	          |          }t          j        |          t          j        |          }}t          |j        d           t          |j        d           t          j	        |d          }t          j	        |d          }	t          j	        |d          }
t          j	        |d          }t                      5  t          |          } |||          }t           |||          |d| z  	           t           |||          |d
| z  	           t           |||	          |d| z  	           t           |||          |d| z  	           t           |||          |d| z  	           t           |||	          |d| z  	           t           |||          |d| z  	           t           |||	          |d| z  	           t           |||          |d| z  	           t          j        t                    5   |||           d d d            n# 1 swxY w Y   t          j        t                    5   ||
|           d d d            n# 1 swxY w Y   t          j        t                    5   |||           d d d            n# 1 swxY w Y   t          j        t                    5   ||
|           d d d            n# 1 swxY w Y   t          j        t                    5   |||           d d d            n# 1 swxY w Y   t          j        t                    5   ||
|	           d d d            n# 1 swxY w Y   | t           t"          z  t$          z  vr=t          j        t                    5   ||
|           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   r\   r   r   rK   )rK   )rK   r   z,%s is not representation invariant with listr   z3%s is not representation invariant with np-array-1dz7%s is not representation invariant with np-array-columnz@%s is not representation invariant with mix np-array-1d and listzK%s is not representation invariant with mix np-array-1d and np-array-columnzD%s is not representation invariant with mix list and np-array-column)r   r   r   r   listrP   r   r   ndimreshaper   r   r   r   r   r   r   r   r   )r   r   r   r   y1_listy2_listy1_1dy2_1d	y1_column	y2_columny1_rowy2_rowr   measures                 rT   &test_format_invariance_with_1d_vectorsr     s    &a((L			a		/	/B			a		/	/B)))*2r22B2hhG2hhG8B<<"5Euz1%%%uz1%%%
5'**I
5'**IZw''FZw''F			 `' `'T"&R..F7G$$BTI	
 	
 	
 	
 	F5%  IDP	
 	
 	
 	
 	F9i((MPTT	
 	
 	
 	
 	F5'""V	
 	
 	
 	
 	F7E""V	
 	
 	
 	
 	F5)$$2 		
 	
 	
 	
 	F9e$$2 		
 	
 	
 	
 	F7I&&V	
 	
 	
 	
 	F9g&&V	
 	
 	
 	
 ]:&& 	" 	"F5&!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"]:&& 	" 	"F65!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"]:&& 	$ 	$F7F###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$]:&& 	$ 	$F67###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$]:&& 	& 	&F9f%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&]:&& 	& 	&F69%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&
 "@@CVV
 
 z** ' 'vv&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' '`' `' `' `' `' `' `' `' `' `' `' `' `' `' `' `' `' `'s  .E
P08JP0J	P0J	P05KP0K	P0K	P02L?P0L	P0L	P0/M<P0M	P0M	P0,N9P0N			P0N		P0)O6P0O	P0	O	
5P0?PP0P	P0P	 P00P47P4c                    t          d          }|                    ddd          }|                    ddd          }t          j        ddg          |         }t          j        ddg          |         }d}ddg}t	                      5  t
          |          } |||          }	|}
| t          v rt          |
|          }
 |
||          }t          |	|d	                    |           	            |
|
                    d
          |
                    d
                    }t          |	|d	                    |           	           | t          v rt          |
|          }
 |
||          }t          |	|d	                    |           	            |
|
                    d
          |
                    d
                    }t          |	|d	                    |           	           d d d            d S # 1 swxY w Y   d S )Nr   r\   r   r   eggsspam	pos_label+{0} failed string vs number invariance testr   O2{0} failed string object vs number invariance test)labelsz,{0} failed string vs number  invariance test)r   r   rP   r   r   CLASSIFICATION_METRICSMETRICS_WITH_POS_LABELr   r   formatrN   METRICS_WITH_LABELS)r   r   r   r   y1_stry2_strpos_label_str
labels_strr   measure_with_number
metric_strmeasure_with_strmeasure_with_strobjs                rT   7test_classification_invariance_string_vs_numbers_labelsr     s   
 &a((L			a		/	/B			a		/	/BXvv&''+FXvv&''+FM&!J			 & &'-$fRnn 
))) }EEEJ%:ff55AHHNN	
 	
 	
 	
 )js););V]]3=O=OPPHOOPTUU	
 	
 	
 	
 &&& J???J)z&&99# FMMdSS    #-*V]]3-?-?sASAS"T"T##FMMdSS   E& & & & & & & & & & & & & & & & & &s   E G99G= G=c                    t          d          }|                    ddd          }|                    ddd          }t          j        ddg          |         }d}t	                      5  t
          |          }| t          vr|}| t          v rt          ||          } |||          } |||          }	t          ||	d
                    |           	            ||                    d
          |          }
t          ||
d
                    |           	           nt          j        t                    5   |||           d d d            n# 1 swxY w Y   t          j        t                    5   ||                    d
          |           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   r\   r   r   r   r   r   r   r   r   r   )r   r   rP   r   r   r   r   r   r   r   r   rN   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   s              rT   4test_thresholded_invariance_string_vs_numbers_labelsr   W  s    &a((L			a		/	/B			a		/	/BXvv&''+FM			 / /$T*...J---$Z=III
"(&R..)z&"55# ELLTRR    #-*V]]3-?-?"D"D##LSS      z** # #vr"""# # # # # # # # # # # # # # #z** / /v}}S))2.../ / / / / / / / / / / / / / /9/ / / / / / / / / / / / / / / / / /s[   ,CF;:EF;E	F;E	F;7 F#F;#F'	'F;*F'	+F;;F?F?rK   r   zy_true, y_scorec                     | t           k    r|g}|g}t          j        t          d          5   | ||           d d d            d S # 1 swxY w Y   d S )Nzcontains (NaN|infinity)match)r   r   r   r   )r   r   r   s      rT   )test_regression_thresholded_inf_nan_inputr    s     )	z)C	D	D	D    vw                                   s   AAA)rK   r\      c                    t          j        |                                          s.d}t          j        |                                          rd}n0d}n-d}t          j        |                                          rd}nd}d| d| }t          j        t          |          5   | ||           ddd           dS # 1 swxY w Y   dS )	z{check that classification metrics raise a message mentioning the
    occurrence of non-finite values in the target vectors.r   NaNzinfinity or a value too larger   zInput z
 contains r   N)rP   isfiniteallisnananyr   r   r   )r   r   r   
input_nameunexpected_valuer   s         rT   !test_classification_inf_nan_inputr    s*    ;v""$$ ?
8F!! 	?$>
8G  "" 	?$>?z??-=??G	z	1	1	1    vw                                   s   %B??CCc                     g dg d}}d}t          j        t          |          5   | ||           ddd           dS # 1 swxY w Y   dS )zocheck that classification metrics raise a message of mixed type data
    with continuous/binary target vectors.)abr  )g?g?333333?zJClassification metrics can't handle a mix of binary and continuous targetsr   N)r   r   r   )r   r   r   r   s       rT   +test_classification_binary_continuous_inputr    s     &ooGFT  
z	1	1	1    vw                                   s   A  AAc                     t           |          }| t          v rddg}nddg}t          |d          D ]\  }} ||g|g           d S )NrK   r\   r   repeat)r   r   r   )r   r   valuesijs        rT   check_single_sampler    ss     F )))QQq)))  1sQC rV   c                     t           |          }t          ddgd          D ]=\  }}}} |t          j        ||gg          t          j        ||gg                     >d S )Nr   rK      r  )r   r   rP   r   )r   r   r  r  kls         rT   check_single_sample_multioutputr    ss    Fq!fQ/// 7 7
1arx!Q!!28aVH#5#566667 7rV   c                 $    t          |            d S r   )r  r   s    rT   test_single_sampler    s     rV   c                 $    t          |            d S r   )r  r  s    rT   test_single_sample_multioutputr!    s    #D)))))rV   c                    t          j        g dg dg dg          }t          j        ddgddgddgg          }t          |          }t          j        t
                    5   |||           d d d            d S # 1 swxY w Y   d S )N)rK   r   r   rK   )r   rK   rK   rK   )rK   rK   r   rK   r   rK   rP   r   r   r   r   r   )r   r   r   r   s       rT   (test_multioutput_number_of_output_differr$    s    X|||\\\<<<@AAFX1v1v1v.//FF	z	"	"  vv                 s   !A;;A?A?c           	         t          d          }|                    ddd          }|                    ddd          }t          |          } |||          }t          d          D ]U}|                    |j        d                   }t           ||d d |f         |d d |f                   |d| z             Vd S )	Nr   r\   )r      r   r  rK   z'%s is not dimension shuffling invariantr   )r   uniformr   rangepermutationr   r   )r   r   r   r   r   error_perms           rT   =test_multioutput_regression_invariance_to_dimension_shufflingr-    s     &a((L!!!QW!55F!!!QW!55FFF66""E1XX 
 
''Q88F6!!!T'?F111d7O44=F	
 	
 	
 	
 	

 
rV   c                     d} d}t          d| d|d          \  }}t          d| d|d          \  }}t          j        |dg| z  gg          }t          j        |dg| z  gg          }t          j        |          }t          j        |          }t          |          }t          |          }d |D             }	d |D             }
t          D ]}t          |         }t          |t                    rd	|_
        ||_         |||          }t           |||          |d
|z             t           ||	|
          |d|z             t           |||          |d|z             d S )Nr  2   rK   r   T
n_features	n_classesr   	n_samplesallow_unlabeledc                 ,    g | ]}t          |          S r   r   .0r  s     rT   
<listcomp>z=test_multilabel_representation_invariance.<locals>.<listcomp>-      GGG!d1ggGGGrV   c                 ,    g | ]}t          |          S r   r6  r7  s     rT   r9  z=test_multilabel_representation_invariance.<locals>.<listcomp>.  r:  rV   tmpzO%s failed representation invariance between dense and sparse indicator formats.r   z\%s failed representation invariance  between dense array and list of list indicator formats.zW%s failed representation invariance  between dense and list of array indicator formats.)r   rP   vstacksp
coo_matrixr   r   r   
isinstancer   
__module____name__r   r   )r2  r3  r+  r   r   y1_sparse_indicatory2_sparse_indicatory1_list_array_indicatory2_list_array_indicatory1_list_list_indicatory2_list_list_indicatorr   r   r   s                 rT   )test_multilabel_representation_invariancerI    s
    II*  EAr +  EAr 
B!y)*	+	+B	B!y)*	+	+B-++-++"2hh"2hhGG/FGGGGG/FGGG# '
 '
T" fg&& 	# %F"FO&R.. 	F&(;<<6 		
 	
 	
 	
 	F)+ABB% 		
 		
 		
 		
 	F*,CDD% 		
 		
 		
 		
 		
='
 '
rV   c           	          dgdgddggg dg gdgt          j        g ddggd          g}t          |          }|D ]?}t          j        t
                    5   |||           d d d            n# 1 swxY w Y   @d S )NrK   r\   r   )r   r\   )r   rK   r   objectdtyper#  )r   multilabel_sequencesr   seqs       rT   +test_raise_value_error_multilabel_sequencesrP  Z  s     qcAq6		
"q!fX... F#  ]:&& 	 	F3	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   A..A2	5A2	c                    d}d}t          d          }|                    d||f          }|                    d||f          }|                    |j                  }t          |          }| t
          v r|n|} |||d          }	 |||d          }
t          d|	z  dd	
           t          |	|
|z  d|  
           d S )Nr\   r   r   r   TrE   F      0We failed to test correctly the normalize optionr   Failed with )r   r   r   r   r   r   r   r   r   r2  r3  r   r   r   r   metricspredmeasure_normalizedmeasure_not_normalizeds              rT   +test_normalize_option_binary_classificationrZ  k  s    II%a((L!!!Yi\!BBF!!!Yi\!BBF!!v|!44G$G11177vD >>>$WVTUCCC!!	B    *%t%%     rV   c                    d}d}t          d          }|                    d||f          }|                    d||f          }|                    ||f          }t          |          }| t          v r|n|} |||d          }	 |||d          }
t          d|	z  dd	
           t          |	|
|z  d|  
           d S )Nr  r   r   r   TrE   FrR  rS  r   rT  )r   r   r'  r   r   r   r   rU  s              rT   /test_normalize_option_multiclass_classificationr\    s    II%a((L!!!Yi\!BBF!!!Yi\!BBF""I(>"??G$G11177vD >>>$WVTUCCC!!	B    *%t%%     rV   c                    d}d}t          d          }t          d|dd|          \  }}t          d|dd|          \  }}|                    |j                  }|dg|z  z  }|dg|z  z  }t          |          }| t
          v r|n|}	 |||	d          }
 |||	d	          }t          d
|
z  dd           t          |
||z  d|             d S )Nr  d   r   rK   T)r1  r2  r   r4  r3  r   rE   FrR  rS  r   rT  )r   r   r'  r   r   r   r   r   )r   r2  r3  r   r+  r   r   r   rV  rW  rX  rY  s               rT   /test_normalize_option_multilabel_classificationr_    sT   
 II%a((L /  IAv /  IAv """55G qcIoF
qcIoF$G11177vD >>>$WVTUCCC!!	B    *%t%%     rV   c           	      ,    j         \  }}  ||d           }t          | fdt          |          D                          ||d          }	t          |	                                                                                         ||d          }
t          |
t	          j        |                     t	          j        dt                    }t	          j        |          dk    r3  ||d          }t          |t	          j        ||                     n  ||d          }t          |d           |rK  ||d	          }t          |t	          j         fd
t          |          D                                  t          j
        t                    5    ||d           d d d            n# 1 swxY w Y   t          j
        t                    5    ||d           d d d            d S # 1 swxY w Y   d S )Nrd   c                 P    g | ]"} d d |f         d d |f                   #S r   r   r8  r  r   y_pred_binarizey_true_binarizes     rT   r9  z$_check_averaging.<locals>.<listcomp>  sO     	
 	
 	
 F?111a4(/!!!Q$*?@@	
 	
 	
rV   rj   rq   r   )rM   rM  ra   )weightsru   c                 @    g | ]} |         |                   S r   r   rb  s     rT   r9  z$_check_averaging.<locals>.<listcomp>  s?        F?1-q/ABB  rV   unknowngarbage)r   r   r(  ravelrP   meanrO   intrb   r   r   r   )r   r   r   rd  rc  is_multilabelr3  r2  label_measuremicro_measuremacro_measurere  weighted_measuresample_measures   `  ``         rT   _check_averagingrr    s    +0Iy F664888M	
 	
 	
 	
 	
 	
9%%	
 	
 	
   F667;;;Mvvo33557L7L7N7NOO  
 F667;;;MM27=#9#9::: f_1C888G	vg!!6&&*EEE("*]G*T*T*TUUUU!6&&*EEE(!,,,  

	BBBG     "9--   	
 	
 	
 
z	"	" 2 2vvy11112 2 2 2 2 2 2 2 2 2 2 2 2 2 2	z	"	" 2 2vvy11112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s$   .G		GG-H		HHc                     t          |                              d          }t          |          }| t          v rt	          ||||||           d S | t
          v rt	          ||||||           d S t          d          )N
multilabelz2Metric is not recorded as having an average option)r
   
startswithr   METRICS_WITH_AVERAGINGrr  "THRESHOLDED_METRICS_WITH_AVERAGINGr   )r   r   rd  r   rc  r   rl  r   s           rT   check_averagingrx    s    "6**55lCCMF%%%FFO_m	
 	
 	
 	
 	
 
3	3	3FG_g}	
 	
 	
 	
 	
 MNNNrV   c                    d\  }}t          d          }|                    d||f          }|                    d||f          }|                    ||f          }t                                          |          }|                    |          }|                    |          }	t          | ||||	|           d S )N)r/  r  r   r   )r   r   r'  r	   fit	transformrx  )
r   r3  r2  r   r   r   r   lbrd  rc  s
             rT   test_averaging_multiclassr}    s     Iy%a((L!!!Yi\!BBF!!!Yi\!BBF""I(>"??G					f	%	%Bll6**Oll6**OD&/6?GTTTTTrV   c                     d\  }}t          d|d|d          \  }}|d d         }|dd          }t          d                              d|f          }|}|}	t          | ||||	|           d S )	N)(   r&  rK   r&  Fr0  r   r   r   )r   r   r   rx  )
r   r3  r2  r+  yr   r   r   rd  rc  s
             rT   test_averaging_multilabelr  .  s     !Iy)  DAq ssVFrssVF ##**Y*@@GOOD&/6?GTTTTTrV   c                     t          j        d          }t          j        d          }t          j        d          }|}|}t          | |||||           d S Nr   r  )rP   zerosrx  r   r   r   r   rd  rc  s         rT   $test_averaging_multilabel_all_zeroesr  C  sW    XgFXgFhwGOOD&/6?GTTTTTrV   c                      t          j        d          } t          j        d          }| }|}dd}t          || |||d           d S )Nr  rq   c                 0    t          t          | ||          S r   )r<   r3   )r   r   rb   s      rT   rU   z=test_averaging_binary_multilabel_all_zeroes.<locals>.<lambda>T  s    =R'> > rV   T)rl  )rq   )rP   r  rr  )r   r   rd  rc  binary_metrics        rT   +test_averaging_binary_multilabel_all_zeroesr  N  sp    XgFXgFOO  M      rV   c                     t          j        d          }t          j        d          }t          j        d          }|}|}t          | |||||           d S r  )rP   onesrx  r  s         rT   "test_averaging_multilabel_all_onesr  a  sW    WWFWWFggGOOD&/6?GTTTTTrV   c                    t           j                            d          }|                    ddt	          |                    }| dk    rt          |d          n|} |||d           }t          | |||t          j        t	          |                              d	| z  
            ||||          }t          j	        t                    5  t          ||           t          d|d|d|           # 1 swxY w Y    ||||                                          }t          ||d|d|d| 
            |t          j        ||d          t          j        ||d          d           }	t          ||	d| z  
           |dd d         }
t          j        |          }d|d d d<   |dd d         }|dd d         } ||||
          } ||||          }t          ||d|d|d| 
           |                     d          s)dD ]&}t          | |||||z            d| z  
           'd                    t#          |          t#          |          t#          |          dz            }t          j	        t          |          5   |||t          j        ||g                     d d d            d S # 1 swxY w Y   d S )Nr   rK   
   r   r;   )r  )sample_weight)r   zAFor %s sample_weight=None is not equivalent to sample_weight=onesr   z>Unweighted and weighted scores are unexpectedly almost equal (z) and (z) for zFWeighted scores for array and list sample_weight input are not equal (z != rL   z.Weighting %s is not equal to repeating samplesr\   zUZeroing weights does not give the same result as removing the corresponding samples (unnormalized)r\   r  z/%s sample_weight is not invariant under scalingzJFound input variables with inconsistent numbers of samples: \[{}, {}, {}\]r   )rP   randomRandomStater   r   r   r   r  r   r   r   r   tolistr  copyru  r   r   hstack)r   r   r   r   rngr  unweighted_scoreweighted_scoreweighted_score_listrepeat_weighted_scoresample_weight_subsetsample_weight_zeroed	y1_subset	y2_subsetweighted_score_subsetweighted_score_zeroedscalingerror_messages                     rT   check_sample_weight_invariancer  l  sE   
)


"
"CKK2CGGK44M &*-C%C%CWVq!!!!F vb"D999r2RW3r77%;%;%;<<<S
    VB-@@@N 
~	&	& 
 
(.999j )((...$$@
 
 	

 
 
 
 
 
 
 
 !&R}7K7K7M7MNNN
 >>...	6    #F
	"m!,,,
	"m!,,,  
 @4G    )A.7=11 !114a4I14a4I"F9,@   #F2r9MNNN
 !  "7"7"7	?    ??>**    	 	Gr2]W-DEEEIDP    	##)6l2..]0K0Ka0O$
 $
  
z	7	7	7 P Pr2RY}/M%N%NOOOOP P P P P P P P P P P P P P P P P Ps$   )C==DD#KKKc                     d}t          d          }|                    |f          }|                    |f          }t          |          }t          | |||           d S )Nr/  r   r   )r   random_sampler   r  )r   r3  r   r   r   r   s         rT   (test_regression_sample_weight_invariancer    sj     I%a((L''i\'::F''i\'::FF"4@@@@@rV   c                 2   d}t          d          }|                    dd|f          }|                    dd|f          }|                    |f          }t          |          }| t          v rt          | |||           d S t          | |||           d S )Nr/  r   r\   r   )r   r   r  r   r   r  )r   r3  r   r   r   r   r   s          rT   $test_binary_sample_weight_invariancer    s     I%a((L!!!Qi\!::F!!!Qi\!::F((yl(;;GF"""&tVVWEEEEE&tVVVDDDDDrV   c                    d}t          d          }|                    dd|f          }|                    dd|f          }|                    |df          }t          |          }| t          v rVt          j        |           }||                    d                              dd          z  }t          | |||           d S t          | |||           d S )Nr/  r   r&  r   r   rL   rK   )
r   r   r  r   r   rP   exprO   r   r  )	r   r3  r   r   r   r   r   tempy_score_norms	            rT   (test_multiclass_sample_weight_invariancer    s     I%a((L!!!Qi\!::F!!!Qi\!::F((y!n(==GF"""vwhdhhBh//77A>>>&tVV\JJJJJ&tVVVDDDDDrV   c                    t          d          }t          ddddd          \  }}t          ddddd          \  }}t          j        ||g          }t          j        ||g          }|                    dd|j                  }t          |          }| t          v rt          | |||           d S t          | |||           d S )	Nr   rK   r  r/  Fr0  r  r   )	r   r   rP   r=  r   r   r   r   r  )	r   r   r+  yaybr   r   r   r   s	            rT   (test_multilabel_sample_weight_invariancer    s     &a((L*bRW  EAr +bRW  EAr YBx  FYBx  F""1afl";;GF"""&tVVWEEEEE&tVVVDDDDDrV   c                     t          j        g dg dg          } t          j        g dg dg          }t          j        g d          }t          j        g d          }t          j        g d          }t          j        |d          \  }}t          D ]e}||g| |gfD ]Z\  }}	|t          vr|	j        d	k    rt          |         }
 |
||	|d 
          } |
||	d           }t          |||                    [fd S )N)rK   rK   r   r   )r   r   rK   rK   )r   rK   rK   r   )r   rK   r\   )r   r\   r  )r  r   rK   r\   T)return_inverserK   )r   rb   rd   )rP   r   uniquerv  r   r   r   r   )y_true_multilabely_pred_multilabely_true_multiclassy_pred_multiclassr   r+  inverse_labelsr   r   r   r   score_labelsscores                rT   test_no_averaging_labelsr  +  sI    ,,,!=>>,,,!=>>++++Xlll##F	&>>>A~& D D 12 12
 	D 	DNFF ...6;?? &F!6&&NNNLF664888E|U>-BCCCC	DD DrV   c                 p   t          d          }d\  }}|                    dd||f          }|                    dd||f          }t          |          } |||          }t          t	          |          |          D ]6}|d d |f         }	|d d |f         }
 ||
|	          }t          ||           7d S )Nr   r   r  r\   r   )r   r   r   r   r(  r   r   r   r3  r2  r   r   r   r  r,  y_score_permy_true_permcurrent_scores               rT   -test_multilabel_label_permutations_invariancer  E  s     &a((L Iy!!!Qi-C!DDF""1ay).D"EEGFF67##EU9--y99 2 2qqq$w'QQQWo{L99E=11112 2rV   c                 >   t          d          }d\  }}|                    dd||f          }|                    |j                  }d||                    d          dk    df<   d||                    d          dk    df<   t
          |          } |||          }t          t          |          |          D ]`}|d d |f         }	|d d |f         }
 ||
|	          }|t          k    rt          j
        |          sJ |dk    sJ Pt          ||           ad S )Nr   r  r\   r   rK   r  g    .A)r   r   r   r   rO   r   r   r(  r*   rP   r  r   r  s               rT   ?test_thresholded_multilabel_multioutput_permutations_invariancer  Z  sU    &a((L Iy!!!Qi-C!DDF!!v|!44G %&F6::a==Aq !$%F6::a==Aq !FF67##EU9--y99 6 6qqq$w'QQQWo{L99333;}----- 3&&&&&  }55556 6rV   c                 x   d\  }}t          d          }|                    ||          }t          j        |           }||                    d                              dd          z  }|                    d||          }t          |          } |||          }t          t          |          |          D ]~}	t          j
        |t                    }
t          j        |          |
t          |	          <   |d d |
f         }t          j        |	|          } |||          }t          ||           d S )N)r^  r  r   r   rL   rK   r   rL  )r   randrP   r  rO   r   r   r   r   r(  r  rk  aranger   taker   )r   r3  r2  r   r   r  r   r   r  r,  inverse_permr  r  r  s                 rT   .test_thresholded_metric_permutation_invariancer  {  s>    "Iy%a((L	955G67(DTXX2X&&..r1555G!!!YY!??FFF67##EU9--y99 2 2x	555#%9Y#7#7T$ZZ qqq,/gdF++{L99E=11112 2rV   metric_namec                 b   t           j                            d          }t          j        dgdz  dgdz  z   t                    }|                    dd|j                  }d	}t          j        t          |
          5  t          |          ||           d d d            d S # 1 swxY w Y   d S )N*   r   r  r   r\   rL  r   r   z7Labels in y_true and y_pred should be of the same type.r   )rP   r  r  r   rK  r   r   r   r   	TypeErrorr   )r  r  r   r   r   s        rT   "test_metrics_consistent_type_errorr    s     )


#
#C	6(Q,&A-V	<	<	<B	Q	(	(BGG	y	0	0	0 4 4{+B3334 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4s    B$$B(+B(zmetric, y_pred_thresholddtype_y_strc                    t           j                            d          }t          j        dgdz  dgdz  z   |          }|                    dd|j                  }|st          j        ddg|          |         }d	}d
}t          |           j        d         j        }|dk    r|n|}	t          j
        t          |	          5   | ||           d d d            d S # 1 swxY w Y   d S )Nr  r   r  r   r\   rL  r   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 explicitzHpos_label=1 is not a valid label. It should be one of \['eggs', 'spam'\]r   rK   r   )rP   r  r  r   r   r   r   
parametersdefaultr   r   r   )
r   y_pred_thresholdr  r  r   r   err_msg_pos_label_Noneerr_msg_pos_label_1pos_label_defaultr   s
             rT    test_metrics_pos_label_error_strr    sK   $ )


#
#C	6(Q,&A-[	A	A	AB	Q	(	(B ?Xvv&k:::2>	"  	X  "&))4[AI%6!%;%;!!AWG	z	1	1	1  r2                 s   C  C$'C$)	functoolsr   inspectr   	itertoolsr   r   r   numpyrP   scipy.sparsesparser>  r   sklearn.datasetsr   sklearn.preprocessingr	   sklearn.utils.multiclassr
   sklearn.utils.validationr   r   sklearn.utilsr   sklearn.utils._testingr   r   r   r   r   sklearn.metricsr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   sklearn.metrics._baser<   REGRESSION_METRICSr   r   CURVE_METRICSr   dictr   updater   METRIC_UNDEFINED_MULTICLASSunionr   rv  rw  r   r   METRICS_WITH_NORMALIZE_OPTIONr   r   r   r   r   METRICS_WITHOUT_SAMPLE_WEIGHTr   r   r   markparametrizesortedr   r   r   r   r   r   r   r   infr   invalids_nan_infr  r  r  r  r  r  r  r!  r$  r-  rI  rP  rZ  r\  intersectionr_  rr  rx  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  strrK  r  r   rV   rT   <module>r     s                           " " " " " "            ; ; ; ; ; ; 0 0 0 0 0 0 3 3 3 3 3 3 1 1 1 1 1 1 7 7 7 7 7 7 ! ! ! ! ! ! 2 2 2 2 2 2 6 6 6 6 6 6 5 5 5 5 5 5 4 4 4 4 4 4 2 2 2 2 2 2 * * * * * * 3 3 3 3 3 3 3 3 3 3 3 3 , , , , , , - - - - - - , , , , , , * * * * * * , , , , , , , , , , , , 3 3 3 3 3 3 % % % % % % 4 4 4 4 4 4 $ $ $ $ $ $ ' ' ' ' ' ' ( ( ( ( ( ( & & & & & & ) ) ) ) ) ) A A A A A A . . . . . . $ $ $ $ $ $ % % % % % % - - - - - - / / / / / / : : : : : : . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 / / / / / / 1 1 1 1 1 1 7 7 7 7 7 7 - - - - - - 2 2 2 2 2 2 + + + + + + $ $ $ $ $ $ ( ( ( ( ( ( ) ) ) ) ) ) % % % % % % ) ) ) ) ) ) & & & & & & % % % % % % 0 0 0 0 0 0 7 7 7 7 7 7J .,*2&D 8.ABBB#G$9CCC2.&-g.C3&O&O&O 0<<<(6  $6n666 '0GRV(W(W(W6 "77>U#K#K#K	6 $%56 " $ $6 /0K6 6ww#8 8 86$ L%6& ]'6( !''-5"I"I"I)6, ]-6. /60 L162 364 !,,,566 ''+C00076 68 096: 77;
MMM;6< :>>>=6> jqIII?6@  L L LA6B WW\:FFFC6D ggmZHHHE6F W3GGGG6H ggh888I6J ggk7CCCK6L WW_gFFFM6N '',@@@O6P 77='BBBQ6R W3GGGS6T ggh888U6V ggk7CCCW6X WW_gFFFY6 6Z "',@@@"7='BBB!'+ysKKK)<<<YQGGG&w	JJJ#GL)DDD$W]IFFF*k6 6 6 r  @ F  n ,    WWX???	 
 *  (  ]  zBBB  ww}i@@@  WW]G<<<  77='uMMM  GGzu    77='uMMM   GGzu  ! & ww}c:::' ( 6) * ') ) )+ 0 (/w( ( ( &-W-Dg%V%V%V-R0?      D dff   & ' ' '   ) * * *   % & & &   = ! ! !       2 &=%B%B& & "
   & & & "   (! ! ! H! ! ! " " " (   D
 
 
    B      H! ! !      
@ 
@ 
@ (9!:!:;;
 
 <;
< (=!>!>??< < @?<" 
FF33{##&HHII 
 
 
& '
 '
 '
T 
FF33{##&HHII s' s' s'l 
FF33-..1SSTT 2 2 2j !455'/ '/ 65'/V VbfbfVbfbfVbfbfVbfa[Vbfa[  ee'..002D2K2K2M2MNN  *,<==    >=   #9#@#@#B#BCC
 &!Q#
&!Q#	
 
   
 
 DC , #9#@#@#B#BCC    DC      7 7 7 

FK -	- #!
"
"		# 	 	 	 	 (;>Q(Q!R!RSS* * TS* (;!<!<==  >= (;!<!<==
 
 >=
$ H
 H
 H
V (;!<!<==  >=  (E!F!FGG  HG8 (E!F!FGG  HG8 
FF0==>QRRSS , , ,^ 32 32 32lO O O" (>!?!?@@U U A@U 
FF),NNOO U U U$ (>!?!?@@U U A@U  & (>!?!?@@U U A@U ^P ^P ^PB 

FK%%cc*<&=&=>>
'	(  A A A 

FK
# 
!
!	"
'	( "	"  E E E 

FK
# 
!
!	"
'	( -	-  E E E" 

F	=	=@S	S
'	(  E E E( D D D2 
FF&*T)UUVV 2 2 2$ 
FF14GGHH 6 6 6< 
FF33*++.PPQQ 2 2 2* (>??	4 	4 @?	4 	 $'	4 	5	1	%	%	%u-		&	% 	u	D
  f66  76   rV   