o
    `^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 i de)de*de/de0de-de2de+dedee7ddde;de<dee1d d d!e.d"e,d#ee1d$d d%eed$d d&ed'eiZei d(ed)ed*eed+d,d-eed.d/d0ed1d2d3 d4e3d5ee3d+d6d7e"d8e>d9ee>d.d/d:e$d;e6d<e8d=e d>ee!d?d@dAee!dBd@i dCe(dDee!dEdBdFdGee dEdHdIee!dEd?dFdJee6dEdHdKee8dEdHdLee$dEdHdMee!dNdBdFdOee dNdHdPee!dNd?dFdQee6dNdHdRee8dNdHdSee$dNdHdTee!dUdBdFdVee dUdHdWee!dUd?dFdXee6dUdHee8dUdHee$dUdHee!dYdBdFee dYdHee!dYd?dFee6dYdHee8dYdHee$dYdHedZ	Zfd[d\ Zge:eged]Zhi d^ed_e&d`e'daee'd.d/dbe#dcedde9deee9dEdHdfee9dYdHdgee9dNdHdhee9dUdidjdkee9dEdidjdlee9dUdmdjdnee9dEdmdjdoee9dBdpdqedreedEdHeedYdHeedNdHe%e4ee=dsZiej Zkeklei eklef eklee ekleh h dtZmh duZnemoenZph dvZqh dwZrh dxZsh dyZth dzZuh d{Zvh d|Zwh d}Zxh d~Zyh dZzh dZ{h dZ|ddhZ}dd Z~dd Zdd Zejdeeydd Zejdeezdd Zejdeeekep dd Zdd Zejdeeekep dd Zejdeeefep dd Zejdeidd Zd dge
je
jgfd dge
je
jgfd dge
je
jgfd dge
jdgfd dge
jdgfgZejdeei ee ejdedd Zejdef ejdee
jdd?gg dfe
jdd?gg dfg dd Zejdef dd Zdd Zdd Zejdejdeeekep eei dd ZejdejdeexewB dd Zejdeexdd Zejdeexdd Zejdejde]dd Zejdeewdd Zejdeeudd Zejdeeudd Zejdeeuewdd Zdd Zdd Zejdeeqdd ZejdeeqerB dd ZejdeeqddÄ Zddń ZejdeeqddǄ ZddɄ Zejdeeekeeee{ dd˄ Zejdeeekeee e{ em dd̈́ Zejdeeekeee e{ ep ddτ ZejdeewevB exB e{ ddф Zddӄ Zejdeewd4h ddՄ ZejdeevexB ddׄ Zejdeeeiep ddل Zejdefdd܄ Zejded+fed+fe d.fee!dd@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 Zi e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_stater    r!   r&   r'   r$   r)   r"   r   r.   variance_weighted)multioutputr2   r3   mean_normal_deviance)powerr%   r#   mean_compound_poisson_deviancegffffff?r   r   r   r
   r    adjusted_balanced_accuracy_scoreT)adjustedunnormalized_accuracy_scoreF	normalizeunnormalized_confusion_matrixnormalized_confusion_matrixc                  O   s8   t | i |dt | i |jddd d tjf  S )Nfloat   axis)r   astypesumnpnewaxis)argskwargs ri   _/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/sklearn/metrics/tests/test_common.py<lambda>   s   "rk   (unnormalized_multilabel_confusion_matrix/unnormalized_multilabel_confusion_matrix_sample)
samplewiser   r5   unnormalized_zero_one_lossr   r-   r/   r   f2_score   )beta
f0.5_score      ?matthews_corrcoef_scoreweighted_f0.5_scoreweighted)averagerr   weighted_f1_scorerx   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               
   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   constant)	pad_widthmodeconstant_values)r,   lenre   arraypadrc   float64nan)rg   rh   	precisionrecall
thresholdspad_threshholdsri   ri   rj   (precision_recall_curve_padded_thresholds   s   
r   )r1   r,   r   r   r   r   unnormalized_log_lossr   r   r0   weighted_roc_aucsamples_roc_aucmicro_roc_aucovr_roc_aucovr)rx   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   r+   r   r4   >   r   r+   r   r   r   r   r   r   r   r   rm   r   >   r   rp   r   r1   r/   r   r   r0   r   r-   r   r   r   r,   rs   >   r   rp   r/   r   r-   rs   >   r0   r   r   >   r   rp   r   r1   r/   r   r-   r   r,   r   r   r   r   rs   >    r   rp   r   r1   r/   r   r   r   r   r   r-   r   ry   r{   r   r   r   r   r   r   r}   r,   r~   r|   r^   r]   rl   rm   rs   r   r   rv   >   r5   r
   r4   >   r   r   r+   r   r0   r   r   r   r   r   r   r   r   r   r   r   >   r   r5   r
   r   r   r   r   r   r   ry   r{   r   r   r   r   r   r   r   r   r}   r~   r   r|   ro   rZ   rl   r   r   r   rv   >   r.   r   r$   r&   r!   r)   r'   r   r2   r   r3   r"   >   r   r    r   r   r5   r
   r   r   r   r   r$   r&   r   r   r!   r   rU   r)   r   r   r}   r'   ru   r2   ro   r3   rZ   r   >   rp   r.   r   r1   r   r/   r   r-   r   r   ry   r{   r   r#   r   r%   r,   r~   r   r   r   r|   r^   r]   r"   rW   rX   rl   rs   r   rv   >   r    r   r   r)   >   r   r#   r%   rW   c                 C   s2   t t|  | d }| |7 } ||7 }| |fS )zMake targets strictly positiver`   )absminy1y2offsetri   ri   rj   _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   rc   re   r   r   ri   ri   rj   _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sJ d S N)SYMMETRIC_METRICSNOT_SYMMETRIC_METRICSsetTHRESHOLDED_METRICS"METRIC_UNDEFINED_BINARY_MULTICLASSALL_METRICSri   ri   ri   rj   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 r+t||\}}|jdddd}|jdddd}t|  }| tv r\| tv rXt	||||||d|  d d S J d	t	||||||d|  d d S )
Nr   rq      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_pred
y_true_bin
y_pred_binmetricri   ri   rj   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 t|||||| t	d|  1 s=w   Y  d S )Nr   rq   r   r   z%s seems to be symmetric)
rR   r   r   r   r   pytestraisesAssertionErrorrJ   
ValueError)r   r   r   r   r   ri   ri   rj   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 r+t||\}}t||dd\}}t  t|  }t	||||||d|  d W d    d S 1 sVw   Y  d S )Nr   rq   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   ri   ri   rj   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
 q1t	D ]}t| }t||||||d	| d
 qIt
D ]$}t| }t||||||d	| d
 t||||||d	| d
 qad S )Nr   rq   r   r   r`   Trb   keepdimsr   r   r   )rR   r   uniformshaperd   rC   r   r   rH   THRESHOLDED_MULTILABEL_METRICSMULTIOUTPUT_METRICS)	r   r   r   y_scorer   r   y_score_shuffler   r   ri   ri   rj   7test_sample_order_invariance_multilabel_and_multioutput  sF   r   c              	   C   s  t d}|jdddd}|jdddd}| tv r t||\}}n| tv r+t||\}}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 sw   Y  tt ||
| W d    n	1 sw   Y  tt ||| W d    n	1 s'w   Y  tt ||
| W d    n	1 sBw   Y  tt ||| W d    n	1 s]w   Y  tt ||
|	 W d    n	1 sxw   Y  | ttB tB vrd| v rtt t||
|sJ W d    n	1 sw   Y  n+tt ||
| W d    n!1 sw   Y  W d    d S W d    d S W d    d S W d    d S 1 sw   Y  d S )Nr   rq   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-columnroc_auc)rR   r   r   r   r   r   listre   r   rJ   ndimreshaperL   r   rH   r   r   r   r   r   r   warnsr	   mathisnan)r   r   r   r   y1_listy2_listy1_1dy2_1d	y1_column	y2_columny1_rowy2_rowr   measureri   ri   rj   &test_format_invariance_with_1d_vectors  s   



	


 \ e$r   c                 C   sb  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 x t|  }|||}	|}
| tv rEt|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 | tv rt|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 W d    d S W d    d S 1 sw   Y  d S )Nr   rq   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   re   r   rL   CLASSIFICATION_METRICSMETRICS_WITH_POS_LABELr   rJ   formatrc   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_strobjri   ri   rj   7test_classification_invariance_string_vs_numbers_labelsW  sT   


"r  c              	   C   sn  t d}|jdddd}|jdddd}tddg| }d}t  t|  }| tvr^|}| tv r7t||d}|||}|||}	t	||	d
| d	 ||d
|}
t	||
d
| d	 n?tt ||| W d    n1 ssw   Y  tt ||d
| W d    n1 sw   Y  W d    d S W d    d S W d    d S 1 sw   Y  d S )Nr   rq   r   r   r   r   r   r   r   r  r  )rR   r   re   r   rL   r   r   r  r   rJ   r  rc   r   r   r   )r   r   r   r   r  r
  r   r  r  r  r  ri   ri   rj   4test_thresholded_invariance_string_vs_numbers_labels  sJ   

	"r  r`   r   zy_true, y_scorec                 C   sR   | t kr
|g}|g}tjtdd | || W d    d S 1 s"w   Y  d S )Nzcontains (NaN|infinity)match)r   r   r   r   )r   r   r   ri   ri   rj   )test_regression_thresholded_inf_nan_input  s   "r  )r`   rq      c                 C   s   t | sd}t | rd}nd}nd}t | r"d}nd}d| d| }tjt|d | || W d   dS 1 sDw   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)re   isfiniteallr   anyr   r   r   )r   r   r   
input_nameunexpected_valuer   ri   ri   rj   !test_classification_inf_nan_input  s   "r  c                 C   sT   g dg d}}d}t jt|d | || W d   dS 1 s#w   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   ri   ri   rj   +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`   rq   gffffffr   repeat)r   r   r   r   )r   r   valuesijri   ri   rj   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   re   r   )r   r   r'  r(  klri   ri   rj   check_single_sample_multioutput  s   $r-  ignorec                 C      t |  d S r   )r)  r   ri   ri   rj   test_single_sample  s   r1  c                 C   r/  r   )r-  r0  ri   ri   rj   test_single_sample_multioutput'  s   r2  c                 C   sz   t g dg dg dg}t ddgddgddgg}t|  }tt ||| W d    d S 1 s6w   Y  d S )N)r`   r   r   r`   )r   r`   r`   r`   )r`   r`   r   r`   r   r`   re   r   r   r   r   r   )r   r   r   r   ri   ri   rj   (test_multioutput_number_of_output_differ-  s   "r4  c                 C   s   t d}|jdddd}|jdddd}t|  }|||}tdD ]"}||jd }t||d d |f |d d |f |d|  d q!d S )	Nr   rq   )r      r   r  r`   z'%s is not dimension shuffling invariantr   )rR   r   r   rangepermutationr   rH   )r   r   r   r   r   error_permri   ri   rj   =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rad
|_||_	|||}t
||||d| d t||
||d| d t|||	|d| d qPd S )Nr*  2   r`   r   T
n_features	n_classesr   	n_samplesallow_unlabeledc                 S      g | ]}t |qS ri   r   .0r  ri   ri   rj   
<listcomp>j      z=test_multilabel_representation_invariance.<locals>.<listcomp>c                 S   rC  ri   rD  rE  ri   ri   rj   rG  k  rH  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   re   vstackr   r   r   
isinstancer   
__module____name__rH   rI   )r<  r@  rA  r9  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   ri   ri   rj   )test_multilabel_representation_invarianceJ  sh   

	

	
rT  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 ]}tt ||| W d    n1 s8w   Y  q!d S )Nr`   rq   r   )ri   rq   )r   r`   ri   objectdtyper3  )r   multilabel_sequencesr   seqri   ri   rj   +test_raise_value_error_multilabel_sequences  s   rZ  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j|jd}t|  }| tv r+|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )Nrq   r   r   r   Tr[   F      0We failed to test correctly the normalize optionr   Failed with )rR   r   normalr   r   r   rK   rH   r   r@  rA  r   r   r   r   metricspredmeasure_normalizedmeasure_not_normalizedri   ri   rj   +test_normalize_option_binary_classification  s(   
rd  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j||fd}t|  }| tv r,|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )Nr*  r   r   r   Tr[   Fr[  r\  r   r]  )rR   r   r   r   r   rK   rH   r_  ri   ri   rj   /test_normalize_option_multiclass_classification  s(   
re  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 r=|n|}	|||	dd}
|||	d	d}td
|
 ddd t|
|| d|  d d S )Nr*  d   r   r`   T)r?  r@  r   rB  rA  r   r[   Fr[  r\  r   r]  )rR   r   r   r   r   r   rK   rH   )r   r@  rA  r   r9  r   r   r   r`  ra  rb  rc  ri   ri   rj   /test_normalize_option_multilabel_classification  sD   


rg  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 sw   Y  t	
t  ||dd W d    d S 1 sw   Y  d S )Nrz   c                    s.   g | ]} d d |f d d |f qS r   ri   rF  r'  r   y_pred_binarizey_true_binarizeri   rj   rG    s     z$_check_averaging.<locals>.<listcomp>r   r   r   )rb   rW  rw   )weightsr   c                    s   g | ]} | | qS ri   ri   rh  ri  ri   rj   rG  =  s    unknowngarbage)r   rH   r6  ravelre   meanrd   intrx   r   r   r   )r   r   r   rk  rj  is_multilabelrA  r@  label_measuremicro_measuremacro_measurerl  weighted_measuresample_measureri   ri  rj   _check_averaging  sH   
	

"rx  c                 C   sZ   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)rP   
startswithr   METRICS_WITH_AVERAGINGrx  "THRESHOLDED_METRICS_WITH_AVERAGINGr   )r   r   rk  r   rj  r   rr  r   ri   ri   rj   check_averagingJ  s   r}  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   fit	transformr}  )
r   rA  r@  r   r   r   r   lbrk  rj  ri   ri   rj   test_averaging_multiclass[  s   

r  c           
      C   sh   d\}}t d|d|dd\}}|d d }|dd  }tdjd|fd}|}|}	t| ||||	| d S )	N)(   r5  r`   r5  Fr>  r   r   r   )r   rR   r^  r}  )
r   rA  r@  r9  yr   r   r   rk  rj  ri   ri   rj   test_averaging_multilabelj  s   
r  c                 C   <   t d}t d}t d}|}|}t| ||||| d S Nr   r  )re   zerosr}  r   r   r   r   rk  rj  ri   ri   rj   $test_averaging_multilabel_all_zeroes     


r  c                  C   s>   t d} t d}| }|}ddd}t|| |||dd d S )Nr  r   c                 S   s   t t| ||S r   )r6   r-   )r   r   rx   ri   ri   rj   rk     s    z=test_averaging_binary_multilabel_all_zeroes.<locals>.<lambda>T)rr  )r   )re   r  rx  )r   r   rk  rj  binary_metricri   ri   rj   +test_averaging_binary_multilabel_all_zeroes  s   



r  c                 C   r  r  )re   onesr}  r  ri   ri   rj   "test_averaging_multilabel_all_ones  r  r  c                 C   s,  t jd}|jddt|d}| dkrt|ddn|}|||d d}t||||t jt|ddd	|  d
 ||||d}t	t
 t|| td||| f 1 sUw   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    d S 1 sw   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 %sra   z.Weighting %s is not equal to repeating samplesrq   zeZeroing weights does not give the same result as removing the corresponding samples (%s != %s) for %sunnormalized)rq   r"  z/%s sample_weight is not invariant under scalingzJFound input variables with inconsistent numbers of samples: \[{}, {}, {}\]r  )re   randomRandomStater   r   r   rH   r  r   r   r   r   tolistr%  copyrz  r  rQ   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_messageri   ri   rj   check_sample_weight_invariance  s   
	


	$r  c                 C   sB   d}t d}|j|fd}|j|fd}t|  }t| ||| d S )Nr=  r   r   )rR   random_sampler   r  )r   rA  r   r   r   r   ri   ri   rj   (test_regression_sample_weight_invariance	  s   r  c                 C   sr   d}t d}|jdd|fd}|jdd|fd}|j|fd}t|  }| tv r0t| ||| d S t| ||| d S )Nr=  r   rq   r   )rR   r   r  r   r   r  )r   rA  r   r   r   r   r   ri   ri   rj   $test_binary_sample_weight_invariance  s   r  c           	      C   s   d}t d}|jdd|fd}|jdd|fd}|j|dfd}t|  }| tv rCt| }||jdddd }t	| ||| d S t	| ||| d S )Nr=  r   r5  r   r   ra   r`   )
rR   r   r  r   r   re   exprd   r   r  )	r   rA  r   r   r   r   r   tempy_score_normri   ri   rj   (test_multiclass_sample_weight_invariance1  s   r  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 rIt	| ||| d S t	| ||| d S )
Nr   r`   r  r=  Fr>  r   Tr   )
rR   r   re   rJ  r   r   rd   r   r   r  )	r   r   r9  yaybr   r   r   r   ri   ri   rj   (test_multilabel_sample_weight_invarianceK  s   	



r  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 ]3}||g| |gfD ](\}}	|tvrN|	jd	krNq@t| }
|
||	|d d
}|
||	d d}t|||  q@q6d S )N)r`   r`   r   r   r   r   r`   r`   )r   r`   r`   r   )r   r`   rq   )r   rq   r  )r  r   r`   rq   T)return_inverser`   )r  rx   rz   )re   r   uniquer{  r   r   r   rJ   )y_true_multilabely_pred_multilabely_true_multiclassy_pred_multiclassr  r9  inverse_labelsr   r   r   r   score_labelsscoreri   ri   rj   test_no_averaging_labelsi  s$   r  c                 C   s   t d}d\}}|jdd||fd}|jdd||fd}t|  }|||}tt||D ]}|d d |f }	|d d |f }
||
|	}t|| q,d S )Nr   r   r*  rq   r   )rR   r   r   r   r6  rI   r   r   rA  r@  r   r   r   r  r:  y_score_permy_true_permcurrent_scoreri   ri   rj   -test_multilabel_label_permutations_invariance  s   

r  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rqt	
|sjJ |d	kspJ qHt|| qHd S )
Nr   r  rq   r   r`   Tr   r*  g    .A)rR   r   r   r   rd   r   r   r6  r"   re   r  rI   r  ri   ri   rj   ?test_thresholded_multilabel_multioutput_permutations_invariance  s$   

r  c                 C   s   d\}}t d}|||}t| }||jdddd }|jd||d}t|  }|||}tt	||D ]*}	tj
|td}
t||
t|	< |d d |
f }t|	|}|||}t|| q8d S )N)rf  r  r   r   ra   r`   r   rV  )rR   randre   r  rd   r   r   r   r   r6  r  rq  aranger   takerI   )r   rA  r@  r   r   r  r   r   r  r:  inverse_permr  r  r  ri   ri   rj   .test_thresholded_metric_permutation_invariance  s    

r  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    d S 1 s:w   Y  d S )N*   r   r  r   rq   rV  r   r   z7Labels in y_true and y_pred should be of the same type.r  )re   r  r  r   rU  r   r   r   r   	TypeErrorr  )r  r  r   r   r   ri   ri   rj   "test_metrics_consistent_type_error  s   "r  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}|s+t jddg|d| }d	}d
}t| jd j}|dkr=|n|}	t	j
t|	d | || W d    d S 1 sWw   Y  d S )Nr  r   r  r   rq   rV  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  )re   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   ri   ri   rj    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rB|j||d|d< zt| t|	 d}W n ty[   d}Y nw |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    d S 1 sw   Y  d S )N)devicer  rT   TF)atol)array_api_dispatch)rG   asarraygetrK  re   ndarrayr  rH   rD   r   rE   )r   array_namespacer  
dtype_namea_npb_npmetric_kwargsxpa_xpb_xp	metric_nprT   numpy_as_array_works	metric_xpmetric_xp_mixed_1metric_xp_mixed_2ri   ri   rj   check_array_api_metric	  sZ   




"r  c              	   C   s^   t g d}t g d}t| |||||d d t jg d|d}t| ||||||d d S )Nr  )r   r`   r   r`   r  r  r          r          @      ?rV  re   r   r  r   r  r  r  	y_true_np	y_pred_npr  ri   ri   rj   ,check_array_api_binary_classification_metricH  s*   

r  c           
      C   s   t g d}t g d}ddi}t| |d}|D ])}t| |||f||d d| t jg d|d}	t| |||f|||	d| qd S )	N)r   r`   rq   r  )r   r`   r   rq   rx   r   r   rw   r   paramsr  r  rV  re   r   (_get_metric_kwargs_for_array_api_testingr  
r   r  r  r  r  r  additional_paramsmetric_kwargs_combinationsr  r  ri   ri   rj   0check_array_api_multiclass_classification_metrice  sF   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 ])}t| |||f||d d| t jg d|d}	t| |||f|||	d| q,d S )	Nr`   r   rV  rx   r  r  r  )r  r   r  r  r  ri   ri   rj   0check_array_api_multilabel_classification_metric  sF     r  c           	      C   s   t | tr	| jjn| j}|dkrttdk rtd tj	g d|d}tj	g d|d}i }t
| j}d|v r<d |d< t| |||f||d| d|v ritj	g d	|d|d< t| |||f||d| d S d S )
Nr%   z1.14.0zJmean_poisson_deviance's dependency `xlogy` is available as of scipy 1.14.0)r  r   r  g      @rV  )rt   rt   rq   rq   r  r  r  )r   r        ?rt   )rK  r   funcrM  rO   rN   r   skipre   r   r   r  r  )	r   r  r  r  	func_namer  r  r  metric_paramsri   ri   rj   !check_array_api_regression_metric  sL   



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  rq   )r`   rq   rq   rV  )r`   r*  r*  )r`   r`   r`   r  r   r  )r   r"  ffffff?)r  r  rT   
raw_valuesr  r  ri   ri   rj   -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 r9d|d< t| |||f||d| d	|d< t| |||f||d| d S )
Nr  )皙?rt   333333?rV  )r!  r"  r  )rt   r  r  dense_outputFr  T)re   r   r   r  r  )r   r  r  r  X_npY_npr  ri   ri   rj   check_array_api_metric_pairwise  s8   	
r
  g      r  c                 c   s,    |   D ]\}}|D ]}||fV  qqd S r   )items)metric_checkersr   checkerscheckerri   ri   rj   !yield_metric_checker_combinationst  s   r  z#array_namespace, device, dtype_namezmetric, check_funcc                 C   s   || ||| d S r   ri   )r   r  r  r  
check_funcri   ri   rj   test_array_api_compliancez  s   r  df_lib_namepandaspolarsc                 C   s|   t |}|g d}|g d}t|  }z|| | }W n ty3   t |  d Y nw t|||| d S )N)r  r  r   r  )r  r  r  r  z can not deal with 1d inputs)r   importorskipSeriesr   to_numpyr   r  rH   )r  r  df_libr   r   r   expected_metricri   ri   rj   test_metrics_dataframe_series  s   
r  c           	      C   sb   i g}|  D ]'\}}|t| jvrqg }|D ]}|D ]}| }|||< || qq|}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   r  r  append)	r   r  r  paramr&  new_combinationsrh   value
new_kwargsri   ri   rj   r    s   r  )r   	functoolsr   inspectr   	itertoolsr   r   r   numpyre   r   sklearn._configr   sklearn.datasetsr   sklearn.exceptionsr	   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   sklearn.metrics._baser6   sklearn.metrics.pairwiser7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   sklearn.preprocessingrB   sklearn.utilsrC   sklearn.utils._array_apirD   rE   rF   sklearn.utils._testingrG   rH   rI   rJ   rK   rL   sklearn.utils.fixesrM   rN   rO   sklearn.utils.multiclassrP   sklearn.utils.validationrQ   rR   REGRESSION_METRICSr  r   CURVE_METRICSr   dictr   updater   METRIC_UNDEFINED_MULTICLASSunionr   r{  r|  r  r  METRICS_WITH_NORMALIZE_OPTIONr   r   r   r   r   METRICS_WITHOUT_SAMPLE_WEIGHTr   r   r   r   r   markparametrizesortedr   r   r   r   r   r   r  r  infr   invalids_nan_infr&  r  r  r#  r)  r-  filterwarningsr1  r2  r4  r;  rT  rZ  rd  re  intersectionrg  rx  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  strrU  r  r  r  r  r  r  r  r
  array_api_metric_checkersr  r  r  r  ri   ri   ri   rj   <module>   s   .4 #	
	
 !"#$%&'()*+,
-




9 	

"




$"$$	

!

-
~
5
+		




	

K



/6





a


	
	



!

?''*1!%),-./0H