a
    h                 :   @   s  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	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mZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> d d	l?m@Z@ d d
lAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZL d dlMmNZN d dlOmPZP d dlQmRZRmSZSmTZT d dlUmVZVmWZWmXZXmYZYmZZZm[Z[ d dl\m]Z]m^Z^m_Z_ d dl`maZa d dlbmcZcmdZd e)e*e/e0e-e2e+eee7dde;e<ee1d de.e,ee1ddeeddeedZeeeeeddeeddedd e3ee3dde"e>ee>dde$e6e8e ee!ddee!d de(ee!d!d d"ee d!d#ee!d!dd"ee6d!d#ee8d!d#ee$d!d#ee!d$d d"ee d$d#ee!d$dd"ee6d$d#ee8d$d#ee$d$d#ee!d%d d"ee d%d#ee!d%dd"ee6d%d#ee8d%d#ee$d%d#ee!d&d d"ee d&d#ee!d&dd"ee6d&d#ee8d&d#ee$d&d#ed'+Zfd(d) Zge:eged*Zhee&e'ee'dde#ee9ee9d!d#ee9d&d#ee9d$d#ee9d%d+d,ee9d!d+d,ee9d%d-d,ee9d!d-d,ee9d d.eeed!d#eed&d#eed$d#e%e4ee=d/Ziej Zkeklei eklef eklee ekleh h d0Zmh d1ZnemoenZph d2Zqh d3Zrh d4Zsh d5Zth d6Zuh d7Zvh d8Zwh d9Zxh d:Zyh d;Zzh d<Z{h d=Z|d>d?hZ}d@dA Z~dBdC ZdDdE ZejdFeeydGdH ZejdFeezdIdJ ZejdFeeekep dKdL ZdMdN ZejdFeeekep dOdP ZejdFeeefep dQdR ZejdFeidSdT Zd dUge
je
jgfd dUge
je
jgfd dUge
je
jgfd dUge
jdUgfd dUge
jdUgfgZejdVeei ee ejdWedXdY ZejdVef ejdWee
jdUdgg dZfe
jdUdgg dZfg d[d\ ZejdVef d]d^ Zd_d` Zdadb ZejdcejdFeeekep eei ddde ZejdcejdFeexewB dfdg ZejdFeexdhdi ZejdFeexdjdk Zejdlejdme]dndo ZejdFeewdpdq ZejdFeeudrds ZejdFeeudtdu ZejdFeeuewdvdw Zdxdy Zdzd{ ZejdFeeqd|d} ZejdFeeqerB d~d ZejdFeeqdd Zdd ZejdFeeqdd Zdd ZejdFeeekeeee{ dd ZejdFeeekeee e{ em dd ZejdFeeekeee e{ ep dd ZejdFeewevB exB e{ dd Zdd ZejdFeewdh dd ZejdFeevexB dd ZejdFeeeiep dd Zejdefdd Zejdedfedfe dfee!dUddfe$dfe5dfe6dfe8dfe:dfg	ejdeegdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zeeeege eeege3eeege>eeege1egee1ddegee1ddege7eegeEege*eege/eege0eegeegeHege.egeBege,ege)ege+eegeCegeIegeDegeFegeGegeJegeKege;eege<eegeLegiZefddZejdeT ejde dd Zejdddgejdeekdd Zdd ZdS )    N)partial)	signature)chainpermutationsproduct)config_context)make_multilabel_classification)UndefinedMetricWarning),accuracy_scoreaverage_precision_scorebalanced_accuracy_scorebrier_score_losscohen_kappa_scoreconfusion_matrixcoverage_errord2_absolute_error_scored2_pinball_scored2_tweedie_score	dcg_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matthews_corrcoef	max_errormean_absolute_errormean_absolute_percentage_errormean_gamma_deviancemean_pinball_lossmean_poisson_deviancemean_squared_errormean_squared_log_errormean_tweedie_deviancemedian_absolute_errormultilabel_confusion_matrix
ndcg_scoreprecision_recall_curveprecision_scorer2_scorerecall_scoreroc_auc_score	roc_curveroot_mean_squared_errorroot_mean_squared_log_errortop_k_accuracy_scorezero_one_loss)_average_binary_score)additive_chi2_kernelchi2_kernelcosine_distancescosine_similarityeuclidean_distanceslinear_kernelpaired_cosine_distancespaired_euclidean_distancespolynomial_kernel
rbf_kernelsigmoid_kernel)LabelBinarizer)shuffle)_atol_for_type_convert_to_numpy)yield_namespace_device_dtype_combinations)_array_api_for_testsassert_allcloseassert_almost_equalassert_array_equalassert_array_lessignore_warnings)COO_CONTAINERSparse_version
sp_version)type_of_target)_num_samplescheck_random_stateZvariance_weighted)multioutput)powergffffff?)r    r!   r&   r'   r$   r)   r"   r   r.   r2   r3   mean_normal_deviancer%   r#   mean_compound_poisson_deviancer   r   r   T)adjustedF	normalizec                  O   s8   t | i |dt | i |jddd d tjf  S )Nfloat   axis)r   astypesumnpZnewaxis)argskwargs rc   ]/var/www/html/assistant/venv/lib/python3.9/site-packages/sklearn/metrics/tests/test_common.py<lambda>   s    "re   )Z
samplewise   )beta      ?weighted)averagerg   rj   micromacrosamples)+r
   r    adjusted_balanced_accuracy_scoreunnormalized_accuracy_scoreunnormalized_confusion_matrixnormalized_confusion_matrix(unnormalized_multilabel_confusion_matrix/unnormalized_multilabel_confusion_matrix_sampler   r5   unnormalized_zero_one_lossr   r-   r/   r   f2_score
f0.5_scorematthews_corrcoef_scoreweighted_f0.5_scoreweighted_f1_scoreweighted_f2_scoreweighted_precision_scoreweighted_recall_scoreweighted_jaccard_scoremicro_f0.5_scoremicro_f1_scoremicro_f2_scoremicro_precision_scoremicro_recall_scoremicro_jaccard_scoremacro_f0.5_scoremacro_f1_scoremacro_f2_scoremacro_precision_score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               
   O   sR   t | i |\}}}t|t| }t||tj|tjd|fdtjgd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   Zconstant)Z	pad_widthmodeZconstant_values)r,   lenr`   arraypadr^   float64nan)ra   rb   	precisionZrecallZ
thresholdsZpad_threshholdsrc   rc   rd   (precision_recall_curve_padded_thresholds   s    
r   )r1   r,   r   Zovr)rj   Zmulti_classZovo)Zmax_fpr)r   r   r   unnormalized_log_lossr   r   r0   weighted_roc_aucsamples_roc_aucmicro_roc_aucZovr_roc_aucZweighted_ovr_roc_aucovo_roc_aucweighted_ovo_roc_aucpartial_roc_aucr    weighted_average_precision_scoresamples_average_precision_scoremicro_average_precision_scorer   r+   r   r4   >   r   r   r   r+   r   rt   r   r   r   r   r   r   >   r   r   r-   r   r1   r,   r   r   r   r   rw   r0   r   r/   rv   >   r   r-   r   rw   r/   rv   >   r0   r   r   >   r   r   r-   r1   r,   r   r   r   r   rw   r   r/   r   rv   >    r   r   r,   rt   r|   rr   rw   r/   r1   r   r}   r   r{   r   r-   r   ry   rz   rq   r~   r   r   rv   r   r   r   rs   r   r   r   r   r   >   r
   r4   r5   >   r0   r   r   r   r+   r   r   r   r   r   r   r   r   r   r   r   >   r   r   r|   r5   r   r
   r   r   ru   r}   r   r{   r   ry   rz   rp   r~   r   r   r   r   rs   r   r   r   r   r   r   r   r   >   r'   r   r)   r"   r3   r.   r   r&   r   r$   r!   r2   >   r   r   r5   rU   r   r&   r!   r
   r'   r)   ru   r}   r   r   r3   rx   rp   r   r   r2   r   r   r   r   r   r$   r    r   >   r   r,   r   r|   rr   r   r%   r/   rw   ro   r"   r1   r   r   r   r{   r   r-   r.   ry   r#   rz   rq   rV   r~   rv   r   rs   r   r   r   >   r    r)   r   r   >   r%   r#   rV   r   r'   r3   c                 C   s2   t t|  | d }| |7 } ||7 }| |fS )zMake targets strictly positiver[   )absminy1y2offsetrc   rc   rd   _require_positive_targets?  s    r   c                 C   sJ   t t|  | d }| tj} |tj}| |7 } ||7 }| |fS )z$Make targets strictly larger than -1gGz?)r   r   r^   r`   r   r   rc   rc   rd   _require_log1p_targetsG  s    r   c                   C   s6   t tB ttB tB ttks J t t@ t ks2J d S N)SYMMETRIC_METRICSNOT_SYMMETRIC_METRICSsetTHRESHOLDED_METRICS"METRIC_UNDEFINED_BINARY_MULTICLASSALL_METRICSrc   rc   rc   rd   test_symmetry_consistencyQ  s    r   namec                 C   s   t d}|jdddd}|jdddd}| tv r@t||\}}n| tv rVt||\}}|jdddd}|jdddd}t|  }| tv r| tv rt	||||||d|  d qdsJ d	nt	||||||d|  d d S )
Nr   rf      sizer      z%s is not symmetricerr_msgFz This case is currently unhandled)
rR   randintMETRICS_REQUIRE_POSITIVE_Yr   METRICS_WITH_LOG1P_Yr   r   METRIC_UNDEFINED_BINARYMULTILABELS_METRICSrH   )r   random_statey_truey_predZ
y_true_binZ
y_pred_binmetricrc   rc   rd   test_symmetric_metric]  s.    r   c                 C   s   t d}|jdddd}|jdddd}| tv r>t||\}}t|  }tt2 t|||||| t	d|  W d    n1 s0    Y  d S )Nr   rf   r   r   z%s seems to be symmetric)
rR   r   r   r   r   pytestraisesAssertionErrorrJ   
ValueError)r   r   r   r   r   rc   rc   rd   test_not_symmetric_metric  s    r   c                 C   s   t d}|jdddd}|jdddd}| tv r@t||\}}n| tv rVt||\}}t||dd\}}t 6 t|  }t	||||||d|  d W d    n1 s0    Y  d S )Nr   rf   r   r   r    %s is not sample order invariantr   )
rR   r   r   r   r   r   rC   rL   r   rH   )r   r   r   r   y_true_shuffley_pred_shuffler   rc   rc   rd   test_sample_order_invariance  s    r   c            	      C   s  t d} | jdddd}| jdddd}| j|jd}||jddd }t|||dd\}}}tD ]*}t| }t||||||d	| d
 qbt	D ]*}t| }t||||||d	| d
 qt
D ]H}t| }t||||||d	| d
 t||||||d	| d
 qd S )Nr   rf   r   r   r[   Tr]   Zkeepdimsr   r   r   )rR   r   uniformshaper_   rC   r   r   rH   THRESHOLDED_MULTILABEL_METRICSMULTIOUTPUT_METRICS)	r   r   r   y_scorer   r   Zy_score_shuffler   r   rc   rc   rd   7test_sample_order_invariance_multilabel_and_multioutput  sD    r   c              	   C   s  t d}|jdddd}|jdddd}| tv r@t||\}}n| tv rVt||\}}t|}t|}t|t| }}t	|j
d t	|j
d t|d}t|d}	t|d}
t|d}t  t|  }|||}t||||d|  d	 t||||d
|  d	 t|||	|d|  d	 t||||d|  d	 t||||d|  d	 t|||	|d|  d	 t||||d|  d	 t|||	|d|  d	 t||||d|  d	 tt ||| W d    n1 s0    Y  tt ||
| W d    n1 s0    Y  tt ||| W d    n1 sP0    Y  tt ||
| W d    n1 s0    Y  tt ||| W d    n1 s0    Y  tt ||
|	 W d    n1 s0    Y  | ttB tB vrd| v r\tt& t||
|s:J W d    n1 sP0    Y  n6tt ||
| W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr   rf   r   r   r[   )r[   )r[   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-columnZroc_auc)rR   r   r   r   r   r   listr`   r   rJ   ndimreshaperL   r   rH   r   r   r   r   r   r   Zwarnsr	   mathisnan)r   r   r   r   Zy1_listZy2_listZy1_1dZy2_1dZ	y1_columnZ	y2_columnZy1_rowZy2_rowr   measurerc   rc   rd   &test_format_invariance_with_1d_vectors  s    



	
******

8r   c                 C   sT  t d}|jdddd}|jdddd}tddg| }tddg| }d}ddg}t  t|  }|||}	|}
| tv rt|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 | tv r0t|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 W d    n1 sF0    Y  d S )Nr   rf   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)rR   r   r`   r   rL   CLASSIFICATION_METRICSMETRICS_WITH_POS_LABELr   rJ   formatr^   METRICS_WITH_LABELS)r   r   r   r   y1_strZy2_strpos_label_strZ
labels_strr   measure_with_number
metric_strmeasure_with_strmeasure_with_strobjrc   rc   rd   7test_classification_invariance_string_vs_numbers_labelsW  sN    



r   c              	   C   sP  t d}|jdddd}|jdddd}tddg| }d}t  t|  }| tvr|}| tv rnt||d}|||}|||}	t	||	d
| d	 ||d
|}
t	||
d
| d	 nptt ||| W d    n1 s0    Y  tt  ||d
| W d    n1 s"0    Y  W d    n1 sB0    Y  d S )Nr   rf   r   r   r   r   r   r   r   r   r   )rR   r   r`   r   rL   r   r   r   r   rJ   r   r^   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   rc   rc   rd   4test_thresholded_invariance_string_vs_numbers_labels  s<    

	(r   r[   r   zy_true, y_scorec                 C   sP   | t kr|g}|g}tjtdd | || W d    n1 sB0    Y  d S )Nzcontains (NaN|infinity)match)r   r   r   r   )r   r   r   rc   rc   rd   )test_regression_thresholded_inf_nan_input  s
    r   )r[   rf      c                 C   s   t | s,d}t | r&d}qHd}nd}t | rDd}nd}d| d| }tjt|d | || W d   n1 s0    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)r`   isfiniteallr   anyr   r   r   )r   r   r   Z
input_nameZunexpected_valuer   rc   rc   rd   !test_classification_inf_nan_input  s    r   c                 C   sR   g dg d }}d}t jt|d | || W d   n1 sD0    Y  dS )zocheck that classification metrics raise a message of mixed type data
    with continuous/binary target vectors.)abr   皙?皙?333333?zJClassification metrics can't handle a mix of binary and continuous targetsr   N)r   r   r   )r   r   r   r   rc   rc   rd   +test_classification_binary_continuous_input  s
    r  c                 C   s\   t |  }| tv rddg}n| tv r,ddg}nddg}t|ddD ]\}}||g|g q@d S )Nr[   rf   gffffffr   repeat)r   r   r   r   )r   r   valuesijrc   rc   rd   check_single_sample  s    

r  c                 C   sL   t |  }tddgddD ].\}}}}|t||ggt||gg qd S )Nr   r[      r  )r   r   r`   r   )r   r   r  r  klrc   rc   rd   check_single_sample_multioutput  s    r  ignorec                 C   s   t |  d S r   )r  r   rc   rc   rd   test_single_sample  s    r  c                 C   s   t |  d S r   )r  r  rc   rc   rd   test_single_sample_multioutput'  s    r  c                 C   sx   t g dg dg dg}t ddgddgddgg}t|  }tt ||| W d    n1 sj0    Y  d S )N)r[   r   r   r[   )r   r[   r[   r[   )r[   r[   r   r[   r   r[   r`   r   r   r   r   r   )r   r   r   r   rc   rc   rd   (test_multioutput_number_of_output_differ-  s
    r  c                 C   s   t d}|jdddd}|jdddd}t|  }|||}tdD ]D}||jd }t||d d |f |d d |f |d|  d qBd S )	Nr   rf   )r      r   r   r[   z'%s is not dimension shuffling invariantr   )rR   r   r   rangeZpermutationr   rH   )r   r   r   r   r   error_permrc   rc   rd   =test_multioutput_regression_invariance_to_dimension_shuffling7  s    
 r  z1ignore::sklearn.exceptions.UndefinedMetricWarningcoo_containerc                 C   s  d}d}t d|d|dd\}}t d|d|dd\}}t|dg| gg}t|dg| gg}| |}| |}t|}t|}	dd |D }
d	d |	D }tD ]t}t| }t|trd
|_||_	|||}t
||||d| d t||
||d| d t|||	|d| d qd S )Nr	  2   r[   r   T
n_features	n_classesr   	n_samplesallow_unlabeledc                 S   s   g | ]}t |qS rc   r   .0r   rc   rc   rd   
<listcomp>j      z=test_multilabel_representation_invariance.<locals>.<listcomp>c                 S   s   g | ]}t |qS rc   r   r!  rc   rc   rd   r#  k  r$  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   r`   vstackr   r   r   
isinstancer   
__module____name__rH   rI   )r  r  r  r  r   r   Zy1_sparse_indicatorZy2_sparse_indicatorZy1_list_array_indicatorZy2_list_array_indicatorZy1_list_list_indicatorZy2_list_list_indicatorr   r   r   rc   rc   rd   )test_multilabel_representation_invarianceJ  sf    

	

	
r*  c              	   C   s   dgdgddggg dg gdgt jg ddggddg}t|  }|D ]8}tt ||| W d    qB1 sp0    Y  qBd S )Nr[   rf   r   )rc   rf   )r   r[   rc   objectdtyper  )r   Zmultilabel_sequencesr   seqrc   rc   rd   +test_raise_value_error_multilabel_sequences  s    r/  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j|jd}t|  }| tv rV|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )Nrf   r   r   r   TrX   F      0We failed to test correctly the normalize optionr   Failed with )rR   r   normalr   r   r   rK   rH   r   r  r  r   r   r   r   metricspredmeasure_normalizedmeasure_not_normalizedrc   rc   rd   +test_normalize_option_binary_classification  s(    r9  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j||fd}t|  }| tv rX|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )Nr	  r   r   r   TrX   Fr0  r1  r   r2  )rR   r   r   r   r   rK   rH   r4  rc   rc   rd   /test_normalize_option_multiclass_classification  s(    r:  c                 C   s   d}d}t d}td|dd|d\}}td|dd|d\}}|j|jd}|dg| 7 }|dg| 7 }t|  }| tv rz|n|}	|||	dd}
|||	d	d}td
|
 ddd t|
|| d|  d d S )Nr	  d   r   r[   T)r  r  r   r  r  r   rX   Fr0  r1  r   r2  )rR   r   r   r   r   r   rK   rH   )r   r  r  r   r  r   r   r   r5  r6  r7  r8  rc   rc   rd   /test_normalize_option_multilabel_classification  sD    

r<  c                    s  j \}} ||d d}t| fddt|D   ||dd}	t|	     ||dd}
t|
t| tjdtd}t|dkr ||dd}t|tj||d	 n ||dd}t|d |r ||d
d}t|t fddt|D  t	
t  ||dd W d    n1 s@0    Y  t	
t  ||dd W d    n1 sz0    Y  d S )Nrk   c                    s.   g | ]&} d d |f d d |f qS r   rc   r"  r  r   y_pred_binarizey_true_binarizerc   rd   r#    s   z$_check_averaging.<locals>.<listcomp>rl   rm   r   )r]   r-  ri   )weightsrn   c                    s   g | ]} | | qS rc   rc   r=  r>  rc   rd   r#  =  s   unknowngarbage)r   rH   r  Zravelr`   meanr_   intrj   r   r   r   )r   r   r   r@  r?  is_multilabelr  r  Zlabel_measureZmicro_measureZmacro_measurerA  Zweighted_measureZsample_measurerc   r>  rd   _check_averaging  sD    
	

.rG  c                 C   sZ   t |d}t|  }| tv r2t|||||| n$| tv rNt|||||| ntdd S )NZ
multilabelz2Metric is not recorded as having an average option)rP   
startswithr   METRICS_WITH_AVERAGINGrG  "THRESHOLDED_METRICS_WITH_AVERAGINGr   )r   r   r@  r   r?  r   rF  r   rc   rc   rd   check_averagingJ  s    rK  c           
      C   sz   d\}}t d}|jd||fd}|jd||fd}|j||fd}t |}||}||}	t| ||||	| d S )N)r  r   r   r   )rR   r   r   rB   fitZ	transformrK  )
r   r  r  r   r   r   r   Zlbr@  r?  rc   rc   rd   test_averaging_multiclass[  s    

rM  c           
      C   sh   d\}}t d|d|dd\}}|d d }|dd  }tdjd|fd}|}|}	t| ||||	| d S )	N)(   r  r[   r  Fr  r   r   r   )r   rR   r3  rK  )
r   r  r  r  yr   r   r   r@  r?  rc   rc   rd   test_averaging_multilabelj  s    
rP  c                 C   s<   t d}t d}t d}|}|}t| ||||| d S Nr   r   )r`   zerosrK  r   r   r   r   r@  r?  rc   rc   rd   $test_averaging_multilabel_all_zeroes  s    


rU  c                  C   s>   t d} t d}| }|}ddd}t|| |||dd d S )NrR  rm   c                 S   s   t t| ||S r   )r6   r-   )r   r   rj   rc   rc   rd   re     s   z=test_averaging_binary_multilabel_all_zeroes.<locals>.<lambda>T)rF  )rm   )r`   rS  rG  )r   r   r@  r?  Zbinary_metricrc   rc   rd   +test_averaging_binary_multilabel_all_zeroes  s    


rV  c                 C   s<   t d}t d}t d}|}|}t| ||||| d S rQ  )r`   onesrK  rT  rc   rc   rd   "test_averaging_multilabel_all_ones  s    


rX  c                 C   s<  t jd}|jddt|d}| dkr4t|ddn|}|||d d}t||||t jt|ddd	|  d
 ||||d}t	t
, t|| td||| f W d    n1 s0    Y  |||| d}t||d||| f d
 |t j||ddt j||ddd d}	t||	d|  d
 |dd d }
t |}d|d d d< |dd d }|dd d }||||
d}||||d}t||d||| f d
 | dsdD ]&}t|||||| dd|  d
 qdt|t|t|d }tj	t|d( |||t ||gd W d    n1 s.0    Y  d S )Nr   r[   
   r   r4   )r
  )sample_weight)r   zAFor %s sample_weight=None is not equivalent to sample_weight=onesr   zQUnweighted and weighted scores are unexpectedly almost equal (%s) and (%s) for %szVWeighted scores for array and list sample_weight input are not equal (%s != %s) for %sr\   z.Weighting %s is not equal to repeating samplesrf   zeZeroing weights does not give the same result as removing the corresponding samples (%s != %s) for %sZunnormalized)rf   r  z/%s sample_weight is not invariant under scalingzJFound input variables with inconsistent numbers of samples: \[{}, {}, {}\]r   )r`   randomRandomStater   r   r   rH   rW  r   r   r   r   tolistr  copyrH  r   rQ   Zhstack)r   r   r   r   rngrZ  Zunweighted_scoreZweighted_scoreZweighted_score_listZrepeat_weighted_scoreZsample_weight_subsetZsample_weight_zeroedZ	y1_subsetZ	y2_subsetZweighted_score_subsetZweighted_score_zeroedZscalingerror_messagerc   rc   rd   check_sample_weight_invariance  s    
"


	ra  c                 C   sB   d}t d}|j|fd}|j|fd}t|  }t| ||| d S )Nr  r   r   )rR   random_sampler   ra  )r   r  r   r   r   r   rc   rc   rd   (test_regression_sample_weight_invariance	  s    rc  c                 C   sp   d}t d}|jdd|fd}|jdd|fd}|j|fd}t|  }| tv r^t| ||| nt| ||| d S )Nr  r   rf   r   )rR   r   rb  r   r   ra  )r   r  r   r   r   r   r   rc   rc   rd   $test_binary_sample_weight_invariance  s    rd  c           	      C   s   d}t d}|jdd|fd}|jdd|fd}|j|dfd}t|  }| tv rt| }||jdddd }t	| ||| nt	| ||| d S )Nr  r   r  r   r   r\   r[   )
rR   r   rb  r   r   r`   expr_   r   ra  )	r   r  r   r   r   r   r   tempZy_score_normrc   rc   rd   (test_multiclass_sample_weight_invariance1  s    rg  c           	      C   s   t d}tdddddd\}}tdddddd\}}t||g}t||g}|j|jd}||jddd	 }t|  }| tv rt	| ||| nt	| ||| d S )
Nr   r[   rY  r  Fr  r   Tr   )
rR   r   r`   r&  r   r   r_   r   r   ra  )	r   r   r  ZyaZybr   r   r   r   rc   rc   rd   (test_multilabel_sample_weight_invarianceK  s    	



rh  c                  C   s   t g dg dg} t g dg dg}t g d}t g d}t g d}t j|dd\}}tD ]f}||g| |gfD ]P\}}	|tvr|	jd	krqt| }
|
||	|d d
}|
||	d d}t|||  qqld S )N)r[   r[   r   r   r   r   r[   r[   )r   r[   r[   r   )r   r[   rf   )r   rf   r   )r   r   r[   rf   T)Zreturn_inverser[   )r   rj   rk   )r`   r   uniquerI  r   r   r   rJ   )Zy_true_multilabelZy_pred_multilabelZy_true_multiclassZy_pred_multiclassr   r  Zinverse_labelsr   r   r   r   Zscore_labelsscorerc   rc   rd   test_no_averaging_labelsi  s     rl  rs   c                 C   s   t d}d\}}|jdd||fd}|jdd||fd}t|  }|||}tt||D ]8}|d d |f }	|d d |f }
||
|	}t|| qXd S )Nr   r   r	  rf   r   )rR   r   r   r   r  rI   r   r   r  r  r   r   r   rk  r  y_score_permy_true_permcurrent_scorerc   rc   rd   -test_multilabel_label_permutations_invariance  s    

rr  c                 C   s   t d}d\}}|jdd||fd}|j|jd}||jddd }d||ddkdf< d||ddkdf< t|  }|||}tt||D ]\}|d d |f }	|d d |f }
||
|	}|tkrt	
|sJ |d	ksJ qt|| qd S )
Nr   rm  rf   r   r[   Tr   r	  g    .A)rR   r   r   r   r_   r   r   r  r"   r`   r   rI   rn  rc   rc   rd   ?test_thresholded_multilabel_multioutput_permutations_invariance  s"    

rs  c                 C   s   d\}}t d}|||}t| }||jdddd }|jd||d}t|  }|||}tt	||D ]T}	tj
|td}
t||
t|	< |d d |
f }t|	|}|||}t|| qpd S )N)r;  r   r   r   r\   r[   r   r,  )rR   Zrandr`   re  r_   r   r   r   r   r  rS  rE  Zaranger   ZtakerI   )r   r  r  r   r   rf  r   r   rk  r  Zinverse_permro  rp  rq  rc   rc   rd   .test_thresholded_metric_permutation_invariance  s    

rt  metric_namec                 C   s   t jd}t jdgd dgd  td}|jdd|jd}d	}tjt	|d
 t
|  || W d    n1 sr0    Y  d S )N*   r   r   r   rf   r,  r   r   z7Labels in y_true and y_pred should be of the same type.r   )r`   r[  r\  r   r+  r   r   r   r   	TypeErrorr   )ru  r_  r   r   r   rc   rc   rd   "test_metrics_consistent_type_error  s    rx  zmetric, y_pred_thresholddtype_y_strc           
      C   s   t jd}t jdgd dgd  |d}|jdd|jd}|sVt jddg|d| }d	}d
}t| jd j}|dkrz|n|}	t	j
t|	d | || W d    n1 s0    Y  d S )Nrv  r   r   r   rf   r,  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   r[   r   )r`   r[  r\  r   r   r   r   
parametersdefaultr   r   r   )
r   Zy_pred_thresholdry  r_  r   r   Zerr_msg_pos_label_NoneZerr_msg_pos_label_1Zpos_label_defaultr   rc   rc   rd    test_metrics_pos_label_error_str  s    r|  c                 K   s  t ||}|j||d}|j||d}	| ||fi |}
|dd ur\|j|d |d|d< |d}t|tjr|j||d|d< zt| t|	 d}W n ty   d}Y n0 |r*| ||	fi |}t||
t|d | ||	fi |}t||
t|d | ||fi |}t||
t|d t	dd@ | ||	fi |}tt
||||
t|d W d    n1 s|0    Y  d S )N)devicerZ  rS   TF)Zatol)Zarray_api_dispatch)rG   Zasarraygetr'  r`   Zndarrayrw  rH   rD   r   rE   )r   array_namespacer}  
dtype_namea_npb_npmetric_kwargsZxpZa_xpZb_xpZ	metric_nprS   Znumpy_as_array_worksZ	metric_xpZmetric_xp_mixed_1Zmetric_xp_mixed_2rc   rc   rd   check_array_api_metric	  sV    





r  c              	   C   s^   t g d}t g d}t| |||||d d t jg d|d}t| ||||||d d S )Nri  )r   r[   r   r[   r  r  rZ          r          @      ?r,  r`   r   r  r   r  r}  r  	y_true_np	y_pred_nprZ  rc   rc   rd   ,check_array_api_binary_classification_metricH  s*    
r  c           
      C   s   t g d}t g d}ddi}t| |d}|D ]R}t| |||f||d d| t jg d|d}	t| |||f|||	d| q4d S )	N)r   r[   rf   r   )r   r[   r   rf   rj   rl   rm   ri   r   paramsr  r  r,  r`   r   (_get_metric_kwargs_for_array_api_testingr  
r   r  r}  r  r  r  Zadditional_paramsmetric_kwargs_combinationsr  rZ  rc   rc   rd   0check_array_api_multiclass_classification_metrice  sD    r  c           
      C   s   t jddgddgddgg|d}t jddgddgddgg|d}ddi}t| |d}|D ]R}t| |||f||d d| t jg d|d}	t| |||f|||	d| qXd S )	Nr[   r   r,  rj   r  r  r  )r  r   r  r  r  rc   rc   rd   0check_array_api_multilabel_classification_metric  sD      r  c           	      C   s   t | tr| jjn| j}|dkr6ttdk r6td tj	g d|d}tj	g d|d}i }t
| j}d|v rxd |d< t| |||f||d| d|v rtj	g d	|d|d< t| |||f||d| d S )
Nr%   z1.14.0zJmean_poisson_deviance's dependency `xlogy` is available as of scipy 1.14.0)r  r   r  g      @r,  )rh   rh   rf   rf   rZ  r  r  )r   r        ?rh   )r'  r   funcr)  rO   rN   r   skipr`   r   r   rz  r  )	r   r  r}  r  	func_namer  r  r  Zmetric_paramsrc   rc   rd   !check_array_api_regression_metric  sJ    


r  c                 C   s   t jg dg dg|d}t jg dg dg|d}t| |||||d d t jddg|d}t| ||||||d t| |||||t jg d	|dd
 t| |||||dd
 d S )N)r[   r   rf   )r[   rf   rf   r,  )r[   r	  r	  )r[   r[   r[   r  r   r  )r   r  ffffff?)r  r  rS   Z
raw_valuesr  r  rc   rc   rd   -check_array_api_regression_metric_multioutput  sN    


r  c                 C   s   t jg dg dg|d}t jg dg dg|d}i }dt| jv rrd|d< t| |||f||d| d	|d< t| |||f||d| d S )
Nr   )皙?rh   333333?r,  )r   r  r  )rh   r  r  Zdense_outputFr  T)r`   r   r   rz  r  )r   r  r}  r  ZX_npZY_npr  rc   rc   rd   check_array_api_metric_pairwise  s8    	r  g      r  c                 c   s*   |   D ]\}}|D ]}||fV  qqd S r   )items)Zmetric_checkersr   Zcheckerscheckerrc   rc   rd   !yield_metric_checker_combinationst  s    r  z#array_namespace, device, dtype_namezmetric, check_funcc                 C   s   || ||| d S r   rc   )r   r  r}  r  Z
check_funcrc   rc   rd   test_array_api_compliancez  s    r  df_lib_nameZpandasZpolarsc                 C   s|   t |}|g d}|g d}t|  }z|| | }W n" tyf   t |  d Y n0 t|||| d S )N)r  r  r   r  )r  r  r  r  z can not deal with 1d inputs)r   ZimportorskipZSeriesr   Zto_numpyr   r  rH   )ru  r  Zdf_libr   r   r   Zexpected_metricrc   rc   rd   test_metrics_dataframe_series  s    
r  c           	      C   sb   i g}|  D ]N\}}|t| jvr&qg }|D ](}|D ]}| }|||< || q6q.|}q|S )zHelper function to enable specifying a variety of additional params and
    their corresponding values, so that they can be passed to a metric function
    when testing for array api compliance.)r  r   rz  r^  append)	r   r  r  paramr  Znew_combinationsrb   valueZ
new_kwargsrc   rc   rd   r    s    r  )r   	functoolsr   inspectr   	itertoolsr   r   r   numpyr`   r   Zsklearn._configr   Zsklearn.datasetsr   Zsklearn.exceptionsr	   Z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&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   Zsklearn.metrics._baser6   Zsklearn.metrics.pairwiser7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   Zsklearn.preprocessingrB   Zsklearn.utilsrC   Zsklearn.utils._array_apirD   rE   rF   Zsklearn.utils._testingrG   rH   rI   rJ   rK   rL   Zsklearn.utils.fixesrM   rN   rO   Zsklearn.utils.multiclassrP   Zsklearn.utils.validationrQ   rR   ZREGRESSION_METRICSr   r   ZCURVE_METRICSr   dictr   updater   ZMETRIC_UNDEFINED_MULTICLASSunionr   rI  rJ  r   r   ZMETRICS_WITH_NORMALIZE_OPTIONr   r   r   r   r   ZMETRICS_WITHOUT_SAMPLE_WEIGHTr   r   r   r   r   markZparametrizesortedr   r   r   r   r   r   r   r   infr   Zinvalids_nan_infr  r   r   r  r  r  filterwarningsr  r  r  r  r*  r/  r9  r:  intersectionr<  rG  rK  rM  rP  rU  rV  rX  ra  rc  rd  rg  rh  rl  rr  rs  rt  rx  strr+  r|  r  r  r  r  r  r  r  Zarray_api_metric_checkersr  r  r  r  rc   rc   rc   rd   <module>   s  .4 $
























9 





"




$"$$	

!

-
~
5
+		




	

K



/6





a

	
	




!

?''*1H