a
    hF                    @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZm Z  d d	l!m"Z" d d
l#m$Z$ d dl%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+m,Z, d dl-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3m4Z4 eeegZ5d?ddZ6dd Z7dd Z8dd Z9dd Z:ej;<dddgdd Z=dd  Z>d!d" Z?d#d$ Z@d%d& ZAd'd( ZBd)d* ZCd+d, ZDd-d. ZEd/d0 ZFd1d2 ZGd3d4 ZHej;<d5eIg d6g d7feIg d6dfg d8g d9fg d8dfgd:d; ZJej;<d5eIg d<g d7feIg d=g d>fgd?d@ ZKej;<d5eIg dAdfg dBdfg dAg d7fg dBg d9fgdCdD ZLej;<dEg dFdGdH ZMdIdJ ZNej;<dKdLeIg dAg dMfdLeIg dBg dNfdOeIg d<dfdPeIg dBg dQfdReIg dAd dSgfdReIg dBdTdUgfdVeIg dAg dWfdVeIg dBg dXfdYeIg dZg d9fdYeIg dXg d9fdYeIg dWg d7fgej;<d[d\d]gd^d_ ZOej;<d`dadbd\dcfdddbd]dcfded\g dffdgd\dhdifdji fgdkdl ZPdmdn ZQej;<doe5dpdq ZRej;<doe5drds ZSej;Tdtej;<doeegej;<dudvdwgdxdy ZUej;<doe5dzd{ ZVej;<dddgd|d} ZWd~d ZXej;<dddgdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^dd Z_ej;<dg dg dd gd gfg dg dd gd gfg dg dd gd gfg dg dd gd gfg dg ddhgd gfg dg ddhgd gfg dg ddhgd gfg dg ddhgd gfg dg ddgd gfg dg dd gd gfg dg dd gd gfg dg dd gd gfg dg dg dg dfg dg dg dg dfg dg dg dg dfg dg dg dg dfg dg dg dg dfg dg dg dg dfg dg dg dg dfg dg dg dg dfgdd Z`ej;<ddSd gdhdhgdSgd gfd dSgdhdhgdSgd gfg dg ddhgd gfg dg ddhgd gfg dg dd gd gfg dg ddSgd gfg dg ddSgd gfg dg ddSgd gfgdd Zadd Zbej;<dg ddd Zcej;<dg dg dg dg dg dgdd Zdej;<dd dSgg ddfg dd dhgdfg dg ddfg dg ddfg dg ddfgdd Zedd Zfdd Zgdd Zhdd Zidd ZjddĄ ZkddƄ Zld@ddʄZmej;<degekejehfej;<deelfdd΄ ZnddЄ Zoej;<ddҡej;<ddԡej;<depdSddׄ Zqddل Zrddۄ Zsdd݄ Ztej;<dg dg dfg dg dgfg dgg dfgdd Zudd Zvej;<de0dd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dd Z~dd Zej;<dddgdd Zdd Zdd Zdd Zdd  Zej;<dg dWdSdfg dWddhfg dWddfgdd Zej;<deIg d	dSdSfeIg d
dSdhfeIg d
ddSfeIg ddSdSfeIg ddSdhfeIg dddSfgdd Zej;<deIg ddg dWfeIg ddhg dWfeIg ddhg dWfeIg ddg dfgej;<dddgdd Zdd Zej;<dg dWdSdfg dWddhfg dWddSfgdd Zej;<dg dWdfg dWdfgdd Zej;<d g d!g d"g d#g d$g d%gdd&fg dWg d"g d#g d$g d%gdd'fg d(g d"g d#g d$g d%gg dBd)fg d(g d"g d#g d$g d%gg dQd*fg d+g d"g d#g d$g d%gg dWd,fg d+g d"g d#g d$g d%gg d-d.fd dSgg d/g d0gdd1fgd2d3 Zej;<de0d4d5 Zej;<d6eeeegej;<d7g d8d9d: Zd;d< Zd=d> ZdS (A      N)stats)datasetssvm)make_multilabel_classification)UndefinedMetricWarning)LogisticRegression)accuracy_scoreaucaverage_precision_scorecoverage_error	dcg_score	det_curve%label_ranking_average_precision_scorelabel_ranking_loss
ndcg_scoreprecision_recall_curveroc_auc_score	roc_curvetop_k_accuracy_score)_dcg_sample_scores_ndcg_sample_scores)train_test_split)label_binarize)_sparse_random_matrix)_convert_containerassert_allcloseassert_almost_equalassert_array_almost_equalassert_array_equal)softmax)CSR_CONTAINERS)check_arraycheck_consistent_lengthcheck_random_stateFc                 C   s  | du rt  } | j}| j}|r:||dk  ||dk   }}|j\}}t|}td}|| || ||  }}t	|d }tj
d}tj|||d| f }tjdddd}	|	|d| |d| ||d }
|r|
ddd	f }
|	||d }||d }|||
fS )
zMake some classification predictions on a toy dataset using a SVC

    If binary is True restrict to a binary classification problem instead of a
    multiclass classification problem
    N   %   r      ZlinearT)ZkernelZprobabilityrandom_state   )r   Z	load_irisdatatargetshapenparanger#   shuffleintrandomRandomStateZc_Zrandnr   ZSVCfitpredict_probaZpredict)ZdatasetbinaryXy	n_samples
n_featuresprngZhalfclfy_scorey_predy_true r?   ^/var/www/html/assistant/venv/lib/python3.9/site-packages/sklearn/metrics/tests/test_ranking.pymake_prediction8   s*    


*rA   c                 C   sd   t | d }|| |k }|| |k }|dd|dd }t |dk}|tt|t|  S )zKAlternative implementation to check for correctness of
    `roc_auc_score`.r(   r   )r,   uniquereshapesumfloatlen)r>   r<   	pos_labelposnegZdiff_matrixZ	n_correctr?   r?   r@   _auch   s    rK   c           	      C   s   t | d }t | |k}t |ddd }|| }| | } d}tt|D ]P}| | |krPd}td|d D ]}| | |krr|d7 }qr||d  }||7 }qP|| S )a>  Alternative implementation to check for correctness of
    `average_precision_score`.

    Note that this implementation fails on some edge cases.
    For example, for constant predictions e.g. [0.5, 0.5, 0.5],
    y_true = [1, 0, 0] returns an average precision of 0.33...
    but y_true = [0, 0, 1] returns 1.0.
    r(   NrB   r         ?)r,   rC   rE   ZargsortrangerG   )	r>   r<   rH   Zn_posorderscoreiprecjr?   r?   r@   _average_precisionw   s    	

rS   c                 C   sd   t | |\}}}tt|}tt|}d}tdt|D ]$}||| || ||d    7 }q:|S )ao  A second alternative implementation of average precision that closely
    follows the Wikipedia article's definition (see References). This should
    give identical results as `average_precision_score` for all inputs.

    References
    ----------
    .. [1] `Wikipedia entry for the Average precision
       <https://en.wikipedia.org/wiki/Average_precision>`_
    r   r(   )r   listreversedrM   rG   )r>   r<   	precisionrecall	thresholdZaverage_precisionrP   r?   r?   r@   _average_precision_slow   s    
"rY   c                 C   s^   dd }|| ||\}}t ||}d}|}d||  ||  }	|| }
dd||	 |
|	    S )zcAlternative implementation to check for correctness of `roc_auc_score`
    with `max_fpr` set.
    c                 S   s   t | |\}}}|||k }t||}|||k }t||k}|d }	||	 || g}
||	 || g}t|t||
|}||fS )Nr(   )r   r,   appendargmaxinterp)r>   	y_predictmax_fprfprtpr_new_fprnew_tprZidx_outZidx_inZx_interpZy_interpr?   r?   r@   _partial_roc   s    z,_partial_roc_auc_score.<locals>._partial_rocr         ?r(   )r	   )r>   r]   r^   rd   rb   rc   Zpartial_aucZfpr1Zfpr2Zmin_areaZmax_arear?   r?   r@   _partial_roc_auc_score   s    
rf   dropTc           	      C   sz   t dd\}}}t||}t||| d\}}}t||}t||dd t|t|| |j|jksfJ |j|jksvJ d S )NTr4   Zdrop_intermediater$   decimal)rA   rK   r   r	   r   r   r   r+   )	rg   r>   ra   r<   Zexpected_aucr_   r`   
thresholdsroc_aucr?   r?   r@   test_roc_curve   s    

rn   c                  C   s   t jd} t dgd dgd  }| jddd}t||dd\}}}|d dksXJ |d	 dkshJ |j|jksxJ |j|jksJ d S )
Nr   2   r(      d   sizeTri   rB   )r,   r0   r1   arrayrandintr   r+   )r:   r>   r=   r_   r`   Zthrr?   r?   r@   test_roc_curve_end_points   s    rv   c            
      C   s   t dd\} }}t| |\}}}g }|D ]2}t||k| @ }t| }	|d| |	  q(t||dd |j|jkszJ |j|jksJ d S )NTrh   rL   r$   rj   )rA   r   r,   rE   rZ   r   r+   )
r>   ra   r<   r_   r`   rl   Ztpr_correctttpr9   r?   r?   r@   test_roc_returns_consistency   s    
ry   c                  C   sH   t dd\} }}tt t| | W d    n1 s:0    Y  d S )NFrh   )rA   pytestraises
ValueErrorr   )r>   ra   r<   r?   r?   r@   test_roc_curve_multi   s    r}   c                  C   s`   t dd\} }}t| |d \}}}t||}t|ddd |j|jksLJ |j|jks\J d S )NTrh   re   ?r$   rj   )rA   r   r	   r   r+   )r>   ra   r<   r_   r`   rl   rm   r?   r?   r@   test_roc_curve_confidence   s    
r   c                  C   s  t dd\} }}t| j}t| |\}}}t||}t|ddd |j|jksTJ |j|jksdJ t| j}t| |\}}}t||}t|ddd |j|jksJ |j|jksJ t| |\}}}t||}t|ddd |j|jksJ |j|jksJ d S )NTrh   re   r$   rj   g(\?)rA   r,   onesr+   r   r	   r   zeros)r>   predr<   Ztrivial_predr_   r`   rl   rm   r?   r?   r@   test_roc_curve_hard  s$    


r   c                  C   s  g d} g d}d}t jt|d  t| |\}}}W d    n1 sH0    Y  t|tt|tj |j	|j	kszJ |j	|j	ksJ d}t jt|d* tdd | D |\}}}W d    n1 s0    Y  t|tt|tj |j	|j	ksJ |j	|j	ksJ d S )N)
r(   r(   r(   r(   r(   r(   r(   r(   r(   r(   )
r   r(   r   r(   r   r(   r   r(   r   r(   INo negative samples in y_true, false positive value should be meaninglessmatchHNo positive samples in y_true, true positive value should be meaninglessc                 S   s   g | ]}d | qS )r(   r?   ).0xr?   r?   r@   
<listcomp>5      z,test_roc_curve_one_label.<locals>.<listcomp>)
rz   warnsr   r   r   r,   fullrG   nanr+   )r>   r=   expected_messager_   r`   rl   r?   r?   r@   test_roc_curve_one_label!  s     .8r   c                  C   s  ddg} ddg}t | |\}}}t| |}t|g d t|g d t|d ddg} ddg}t | |\}}}t| |}t|g d t|g d t|d ddg} ddg}t | |\}}}t| |}t|ddg t|ddg t|d ddg} ddg}t | |\}}}t| |}t|g d t|g d t|d ddg} ddg}t | |\}}}t| |}t|ddg t|ddg t|d ddg} dd	g}d
}tjt|d  t | |\}}}W d    n1 s0    Y  t|g d t|tjtjtjg d}tjt|d t| |}W d    n1 s<0    Y  t	
|sVJ ddg} dd	g}d}tjt|d  t | |\}}}W d    n1 s0    Y  t|tjtjtjg t|g d d}tjt|d t| |}W d    n1 s0    Y  t	
|sJ tddgddgg} tddgddgg}tjt|d t| |dd W d    n1 s|0    Y  tjt|d t| |dd W d    n1 s0    Y  tt| |ddd tt| |ddd tddgddgg} tddgddgg}tjt|d t| |dd W d    n1 sL0    Y  tjt|d t| |dd W d    n1 s0    Y  tt| |ddd tt| |ddd tddgddgg} tddgddgg}tt| |ddd tt| |ddd tt| |ddd tt| |ddd tddgddgg} tddgddgg}tt| |ddd tt| |ddd tt| |ddd tt| |ddd d S )Nr   r(   r   r   r(   r   r(   r(   rL           re         ?      ?r   r   r   re   rL   OOnly one class is present in y_true. ROC AUC score is not defined in that case.r   macroaverageweightedsamplesmicro)r   r   r   r   rz   r   r   r,   r   mathisnanrt   )r>   r<   r`   r_   ra   rm   r   r	   r?   r?   r@   test_roc_curve_toydata<  s    









0*0*....r   c                  C   sx   g d} g d}t | |dd\}}}t|tjdddg g d} g d	}t | |dd\}}}t|tjdd
dddg d S )Nr   r   r   r   r(   r(   r   皙?re   333333?ffffff?rL   Tri   rL   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~   rL   r~   r   )r   r   r,   inf)r>   r<   r`   r_   rl   r?   r?   r@    test_roc_curve_drop_intermediate  s    r   c                  C   sh   g d} g d}t dd}t| ||d\}}}t |dk  dksJJ t |dk  dksdJ d S )Nr   r   r(   r(   r(   )r   r   333333?皙?re   r      sample_weightr   )r,   repeatr   diffrE   )r>   r<   r   r_   r`   ra   r?   r?   r@   !test_roc_curve_fpr_tpr_increasing  s    r   c                  C   s   ddg} ddg}t t| |d ddg} ddg}t t| |d g d} g d}t t| |d ddg} ddg}t t| |d g d} g d}t t| |d d S )Nr   r(   re   r(   r   r   r   r   re   r(   )r   r	   )r   r6   r?   r?   r@   test_auc  s    r   c                  C   s   t t" tg dddg W d    n1 s20    Y  t t tdgdg W d    n1 sj0    Y  g d} g d}dt| }t jtt|d t| | W d    n1 s0    Y  d S )	Nr   r   r   r   )r$   r(   rp      )r            z+x is neither increasing nor decreasing : {}r   )	rz   r{   r|   r	   formatr,   rt   reescape)r   r6   error_messager?   r?   r@   test_auc_errors  s    0,r   zy_true, labels)r   r(   r   r$   r   r(   r$   )abr   c)r   r   r   c                 C   sZ  t g dg dg dg dg}tg dg d}tg dg d}|| d	 }tg d
g d}tg dg d}|| d	 }tddgddg}	tddgddg}
|	|
 d	 }|| | d }tt| ||dd| |||g}g d}t j||d}tt| ||ddd| d}tjt|d" t| ||dd d W d    n1 sL0    Y  d S )Nr   r   r   r   r   r   ffffff?re   333333?r   r   r   r(   r   r(   )r   r   r   r   r(   r   )r   r   re   r$   r(   r(   r   )r   r   r   r   )r   r   r   r(   r   r   r   r   r   rp   ovolabelsmulti_class)r   r   re   )weightsr   r   r   r   z6average=None is not implemented for multi_class='ovo'.r   )r,   rt   r   r   r   rz   r{   NotImplementedError)r>   r   y_scoresscore_01score_10Zaverage_score_01Zscore_02Zscore_20Zaverage_score_02Zscore_12Zscore_21Zaverage_score_12Zovo_unweighted_scoreZpair_scoresZ
prevalenceZovo_weighted_scorer   r?   r?   r@   #test_multiclass_ovo_roc_auc_toydata  s:    

r   )r   r$   r   r$   )r   dr   r   )r   r   r   c                 C   s   t g dg dg dg dg}tg dg d}tg dg d}|| d	 }tt| ||d
d| tt| ||d
dd| 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   r   r   r$   r   r   r   r   r,   rt   r   r   )r>   r   r   r   r   Z	ovo_scorer?   r?   r@   *test_multiclass_ovo_roc_auc_toydata_binary'  s    
r   )r   r(   r$   r$   )r   r   r   r   c                 C   s   t g dg dg dg dg}tg d|d d df }tg d|d d df }tg d	|d d d
f }tt| |d|d d|||g || | d }tt| |d|d| |d |d  |d  }tt| |d|dd| d S )NrL   r   r   )r   re   r   )r   r   r   )r   r   r   r(   r   r   r   r   )r   r(   r   r   r(   r   r   r(   r(   r$   ovr)r   r   r   g      @)r   r   r   re   r   r   )r>   r   r   Zout_0Zout_1Zout_2Zresult_unweightedZresult_weightedr?   r?   r@   #test_multiclass_ovr_roc_auc_toydataK  s,    
r   zmulti_class, average))r   r   )r   r   )r   r   c                 C   s   t g d}g dg dg dg dg}tt||| |dd g dg dg dg dg}t||| |ddk stJ dt d	 }t||| |dtd
ksJ d S )N)rp   r(   r$   r   )r   r   r   rL   )r   rL   r   r   )r   r   rL   r   )r   皙?r   r   r   r   rL   r   )r   r   re   )r,   rt   r   r   r   rz   approx)r   r   r>   Z	y_perfectZy_imperfectZy_chancer?   r?   r@   0test_perfect_imperfect_chance_multiclass_roc_aucv  s2    	r   c           	         s   |  t jjg dd d}t fdd|D }t|g dd}t| | \}}}t||}t	||dd	d
}|t
|ksJ d S )N)       @rL   re     )rs   r'   c                    s"   g | ]}t jjd | d qS )r(   )nr9   r'   )r   Zmultinomialrvsr[   )r   Zy_pred_iseedr?   r@   r     s   z3test_micro_averaged_ovr_roc_auc.<locals>.<listcomp>r   )classesr   r   r   )r   Z	dirichletr   r,   asarrayr   r   Zravelr	   r   rz   r   )	global_random_seedr=   r>   Zy_onehotr_   r`   ra   Zroc_auc_by_handZroc_auc_autor?   r   r@   test_micro_averaged_ovr_roc_auc  s    

r   zmsg, y_true, labelsz!Parameter 'labels' must be uniquer   r$   r   )r   r   r   zKNumber of classes in y_true not equal to the number of columns in 'y_score'z"Parameter 'labels' must be ordered)r   r   r   zMNumber of given labels, 2, not equal to the number of columns in 'y_score', 3r(   r   r   zMNumber of given labels, 4, not equal to the number of columns in 'y_score', 3)r   r(   r$   rp   )r   r   r   r   z2'y_true' contains labels not in parameter 'labels')r   r   r   er   r   r   c                 C   sd   t g dg dg dg dg}tjt| d  t||||d W d    n1 sV0    Y  d S )Nr   r   r   r   r   r   )r,   rt   rz   r{   r|   r   )msgr>   r   r   r   r?   r?   r@   *test_roc_auc_score_multiclass_labels_error  s
    Ir   zmsg, kwargszLaverage must be one of \('macro', 'weighted', None\) for multiclass problemsr   )r   r   zUaverage must be one of \('micro', 'macro', 'weighted', None\) for multiclass problemszksample_weight is not supported for multiclass one-vs-one ROC AUC, 'sample_weight' must be None in this case)r   r   z|Partial AUC computation not available in multiclass setting, 'max_fpr' must be set to `None`, received `max_fpr=0.5` insteadre   )r   r^   z'multi_class must be in \('ovo', 'ovr'\)c                 C   sp   t d}|dd}t|}|jdddd}tjt| d" t||fi | W d    n1 sb0    Y  d S )N     rp   r   rr   r   )r#   randr   ru   rz   r{   r|   r   )r   kwargsr:   r<   Zy_probr>   r?   r?   r@   #test_roc_auc_score_multiclass_error  s    'r   c                  C   s   t d} | d}tjddd}d}tjt|d t|| W d    n1 sR0    Y  tjddd}tjt|d t|| W d    n1 s0    Y  tj	dddd}tjt|d t|| W d    n1 s0    Y  d S )Nr   
   r/   Zdtyper   r   rB   )
r#   r   r,   r   rz   r   r   r   r   r   )r:   r=   r>   warn_messager?   r?   r@   test_auc_score_non_binary_class4  s    
((r   
curve_funcc                 C   sb   t d}|jdddd}|d}d}tjt|d | || W d    n1 sT0    Y  d S )Nr   r   rp   r   rr   z"multiclass format is not supportedr   )r#   ru   r   rz   r{   r|   )r   r:   r>   r=   r   r?   r?   r@   &test_binary_clf_curve_multiclass_errorI  s    
r   c                 C   s   d}t jt|d, | tjddgddddg W d    n1 sD0    Y  t jt|d, | tjddgtdddg W d    n1 s0    Y  g d	}| g d
|}| g d|}t||D ]\}}tj|| qd S )Nzy_true takes value in {'a', 'b'} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitly.r   r   r   z<U1r   r   rL   )r   rL   r   gzG?)r   r(   r(   r   )r   rL   rL   r   )	rz   r{   r|   r,   rt   objectzipZtestingr   )r   r   r=   Z	int_curveZfloat_curveZint_curve_partZfloat_curve_partr?   r?   r@   (test_binary_clf_curve_implicit_pos_labelS  s    ::r  z.ignore:Support for labels represented as byteslabels_typerT   rt   c                 C   sR   t ddg|}d}tjt|d | |ddg W d    n1 sD0    Y  d S )N   a   bzy_true takes value in {b'a', b'b'} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitly.r   r   rL   )r   rz   r{   r|   )r   r  r   r   r?   r?   r@   .test_binary_clf_curve_implicit_bytes_pos_labelm  s
    r  c                 C   sn   g d}g d}g d}| |||d}| |d d |d d |d d d}t ||D ]\}}t|| qVd S )Nr   )r   r   r   r   re   )r(   r(   r(   re   r   r   rB   )r  r   )r   r>   r<   r   Zresult_1Zresult_2Zarr_1Zarr_2r?   r?   r@   (test_binary_clf_curve_zero_sample_weight}  s    &r  c           
      C   sF  t dd\}}}t|||  t|dd  |dd  | d\}}}|d dksPJ |d |dd   kslJ d|t|dk< | }t|||  t|| g d}g d	}	t||	| d\}}}| rt|g d
 t|g d t|g d n*t|g d t|g d t|g d	 |j	|j	ks,J |j	|j	d ksBJ d S )NTrh   r(   ri   r   rL   rB   r(   r   r   r(   )r(   r$   rp   r   )re   QUU?rL   rL   )rL   re   re   r   )r(   r$   r   )re   r	  re   rL   rL   )rL   re   re   re   r   )
rA   _test_precision_recall_curver   meanr,   wherecopyr   r   rs   )
rg   r>   ra   r<   r9   rrw   Zy_true_copyr   Zpredict_probasr?   r?   r@   test_precision_recall_curve  s*    $
r  c                 C   s   t | ||d\}}}t| |}t|dd t|t| | tt| ||dd |j|jks^J |j|jd ksrJ t | t||d\}}}|j|jksJ |j|jd ksJ d S )Nri   grh|?rp   r$   rj   r(   )	r   rY   r   r
   r   rS   rs   r,   
zeros_like)r>   r<   rg   r9   r  rl   Zprecision_recall_aucr?   r?   r@   r
    s     

r
  c              	   C   s  t jdd ddg}ddg}t||| d\}}}t||}t|g d t|g d t|d ddg}ddg}t||| d\}}}t||}t|g d	 t|g d
 t|d ddg}ddg}t||| d\}}}t||}t|ddg t|ddg t|d ddg}ddg}t||| d\}}}t||}t|g d t|g d t|d ddg}ddg}t||| d\}}}t||}t|ddg t|ddg t|d ddg}ddg}tjtdd$ t||| d\}}}W d    n1 s0    Y  tjtdd t||}W d    n1 s80    Y  t	|g d t	|g d t	|d ddg}ddg}t||| d\}}}tt||d t|g d t|g d t 
ddgddgg}t 
ddgddgg}tjtdd$ t	t||ddd W d    n1 s0    Y  tjtdd$ t	t||ddd W d    n1 sb0    Y  t	t||ddd t	t||ddd t 
ddgddgg}t 
ddgddgg}tjtdd$ t	t||ddd W d    n1 s0    Y  tjtdd$ t	t||ddd W d    n1 s>0    Y  t	t||ddd t	t||ddd t 
ddgddgg}t 
ddgddgg}tt||ddd tt||ddd tt||ddd tt||ddd t 
ddgddgg}t 
ddgddgg}tjtdd$ t	t||ddd W d    n1 sR0    Y  t	t||ddd tjtdd$ t	t||ddd W d    n1 s0    Y  tjtdd$ t	t||ddd W d    n1 s0    Y  t 
ddgddgg}t 
ddgddgg}t	t||ddd t	t||ddd t	t||ddd t	t||ddd t 
ddgddgg}t 
ddgddgg}tt||ddd tt||ddd tt||ddd tt||ddd W d    n1 s0    Y  t jdd t 
ddgddgg}t 
ddgddgg}tjtdd$ t	t||ddd W d    n1 s0    Y  W d    n1 s0    Y  d S )Nraise)allr   r(   ri   )re   r(   r(   r   rL   )re   r   rL   r   re   r   r   r   z!No positive class found in y_truer   r   )rL   rL   rL   )r(   re   r   r   r   r   r   r   ignore)r,   Zerrstater   r
   r   r   rz   r   UserWarningr   rt   )rg   r>   r<   r9   r  ra   Zauc_prcr?   r?   r@   #test_precision_recall_curve_toydata  s   









4*
$$$$$$$$r  c                  C   s   g d} g d}t | |dd\}}}t|g d g d} g d}t | |dd\}}}t|g d g d	} g d
}t | |dd\}}}t|ddg g d} g d
}t | |dd\}}}t|g d
 dS )z9Check the behaviour of the `drop_intermediate` parameter.r   r   Tri   )r   r   rL   r   r   )r   r   r   r   r~   rL   r   r   r   r   )r   r   r   r   r   r   r(   r(   r(   r(   N)r   r   )r>   r<   rV   rW   rl   r?   r?   r@   -test_precision_recall_curve_drop_intermediated  s0    r  c                  C   s<   t jdtd} d| d d d< t d}t| |dks8J d S )Nrq   r   r(   r   r   )r,   r   r/   r   r
   r>   r<   r?   r?   r@   &test_average_precision_constant_values  s    
r  c                  C   s`   t ddg} t ddg}d}tjt|d t| |dd W d    n1 sR0    Y  d S )Nr   r(   z>pos_label=2 is not a valid label. It should be one of \[0, 1\]r   r$   rH   r,   rt   rz   r{   r|   r
   r>   r=   err_msgr?   r?   r@   4test_average_precision_score_binary_pos_label_errors  s
    r  c                  C   s   t ddgddgddgddgg} t ddgddgddgddgg}d}tjt|d t| |dd	 W d    n1 sz0    Y  d S )
Nr(   r   r~   r   r   r   znParameter pos_label is fixed to 1 for multilabel-indicator y_true. Do not set pos_label or set pos_label to 1.r   r  r  r  r?   r?   r@   8test_average_precision_score_multilabel_pos_label_errors  s    ""r   c               	   C   s   t g d} t g dg dg dg dg dg dg}d}tjt|d t| |dd	 W d    n1 sr0    Y  d S )
N)r   r(   r$   r   r(   r$   )re   r   r   )r   re   r   )r   r   r   )r   r   re   zdParameter pos_label is fixed to 1 for multiclass y_true. Do not set pos_label or set pos_label to 1.r   rp   r  r  r  r?   r?   r@   8test_average_precision_score_multiclass_pos_label_errors  s    r!  c                  C   s   t dd\} }}t| |}t| d| }t| d| }t| |d }||ksPJ ||ks\J ||kshJ t| |}t| d| }t| d| }	t| |d }
||ksJ ||	ksJ ||
ksJ d S )NTrh   rq   gư>r   )rA   r   r
   )r>   ra   r<   rm   Zroc_auc_scaled_upZroc_auc_scaled_downZroc_auc_shiftedZpr_aucZpr_auc_scaled_upZpr_auc_scaled_downZpr_auc_shiftedr?   r?   r@   test_score_scale_invariance  s    

r"  z(y_true,y_score,expected_fpr,expected_fnrr   r   )r   r   re   )re   r   r(   r   re   r   r   r   r   r   )r(   r(   re   r   r   )r   re   re   c                 C   s(   t | |\}}}t|| t|| d S Nr   r   r>   r<   Zexpected_fprZexpected_fnrr_   fnrra   r?   r?   r@   test_det_curve_toydata  s    
r(  r   re   re   c                 C   s(   t | |\}}}t|| t|| d S r$  r%  r&  r?   r?   r@   test_det_curve_tie_handling  s    
r*  c                   C   s*   t tg dg dtg dg d d S )Nr   r   r   )r   r   re   re   r(   r(   )r   r   r?   r?   r?   r@   test_det_curve_sanity_check  s    r+  r<   )r   r   re   r   r(   c                 C   sF   t g dtd| d\}}}t|dg t|dg t|| g d S )N)r   r(   r   r(   r   r(   r   r  r(   r   )r   r,   r   r   )r<   r_   r'  rX   r?   r?   r@   test_det_curve_constant_scores  s    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(   c                 C   s.   t | | d\}}}t|dg t|dg d S )Nr  r   r%  )r>   r_   r'  ra   r?   r?   r@   test_det_curve_perfect_scores   s    r-  zy_true, y_pred, err_msgzinconsistent numbers of samplesr   r   r   z#Only one class is present in y_truer(   r(   r(   )cancerr0  
not cancer)r   r   r   zpos_label is not specifiedc                 C   s<   t jt|d t| | W d    n1 s.0    Y  d S )Nr   )rz   r{   r|   r   r  r?   r?   r@   test_det_curve_bad_input1  s    r2  c            	      C   s   dgd dgd  } t g d}d| }t| |dd\}}}t| |dd\}}}|d td	kshJ |d td
ks~J t||d d d  t||d d d  d S )Nr0  rp   r1  r   )
r   r   r   r   r   r   re   r   r   r~   r(   r  r   r   r   rB   )r,   rt   r   rz   r   r   )	r>   Zy_pred_pos_not_cancerZy_pred_pos_cancerZfpr_pos_cancerZfnr_pos_cancerZth_pos_cancerZfpr_pos_not_cancerZfnr_pos_not_cancerZth_pos_not_cancerr?   r?   r@   test_det_curve_pos_labelE  s"    r3  c                 C   s  t | ddggddggd t | ddggddggd t | ddggddggd t | g dgg dgd t | g dgg dgd t | g d	gg dgd t | g d
gg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | g d	gg dgd t | g d
gg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | g d	gg dgd t | g d
gg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | ddggddggd t | ddggddggd t | ddggddggd t | g dgg dgd t | g dgg dgd t | g d	gg dgd t | g d
gg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd t | g dgg dgd d S )Nr   r(   r   r   re   r   r#  r   r   r   gUUUUUU?r   g?r   g?r   re   r   r/  re   r   r   r)  )re   re   re   UUUUUU?)r(   r(   r(   r   )re   re   re   re   )r   
lrap_scorer?   r?   r@   check_lrap_toy_  s^    r9  c                 C   s   t d}tddD ]}|jd|fd}t|}td|f}| ||dksPJ | ||dksbJ td|f}| ||dksJ | ||dksJ qt| dgdgdgdggdgdgdgdggd d S )Nr   r$   r   r(   rr   rL   re   )r#   rM   uniformr,   r  r   r   r   )r8  r'   n_labelsr<   Zy_score_tiesr>   r?   r?   r@   !check_zero_or_all_relevant_labels  s    
*r<  c                 C   s  t t" | g dg d W d    n1 s20    Y  t t0 | g dg dg dg dg W d    n1 s|0    Y  t t0 | g dg dg dg dg W d    n1 s0    Y  t t* | ddgddggddg W d    n1 s0    Y  t t, | ddgddggddgg W d    n1 sT0    Y  t t. | ddgddggdgdgg W d    n1 s0    Y  t t, | ddggddgddgg W d    n1 s0    Y  t t. | dgdggddgddgg W d    n1 s00    Y  t t. | ddgddggdgdgg W d    n1 sz0    Y  d S )	Nr   )r   r   r   r   )r   r   r   )r   r   r   )r   r   r   r   r(   )rz   r{   r|   r7  r?   r?   r@   check_lrap_error_raised  s(    0>":<><>r=  c              	   C   sz   t ddD ]j}td|f}t d|D ]L}t || D ]:}td|f}d|d||| f< t| ||||  q6q&q
d S )Nr$   r   r(   r   )rM   r,   r   r   r   )r8  r;  r<   
n_relevantrI   r>   r?   r?   r@   check_lrap_only_ties  s    r?  c              	      s   t ddD ]}|t|d|fd  }td|f}d|d< d|d< t| ||d| d d  t d|D ]` t |  D ]Ntd|f}d|d  f< t| ||t fddt  D  q~qnq
d S )	Nr$   r   r(   )r   r   )r   rB   r   c                 3   s&   | ]}|d  | d     V  qdS )r(   Nr?   r   r  r>  rI   r?   r@   	<genexpr>  s   z>check_lrap_without_tie_and_increasing_score.<locals>.<genexpr>)rM   r,   r-   rD   r   r   rE   )r8  r;  r<   r>   r?   rA  r@   +check_lrap_without_tie_and_increasing_score  s     rC  c                    s
  t | | t| } t|}| j\}}t|f}t|D ]}tj|| dd\}}|j}|| tj|d d	 }	|	 | | 
 d }
|
jdks|
j|krd||< q8d||< |
D ]4 t fdd|
D }||  |   7  < q||  |
j  < q8| S )	z8Simple implementation of label ranking average precisionT)Zreturn_inverser(   )Z	minlengthr   r   c                 3   s   | ]}|   kV  qd S r$  r?   r@  labelZrankr?   r@   rB    r   z_my_lrap.<locals>.<genexpr>)r"   r!   r+   r,   emptyrM   rC   rs   ZbincountZcumsumZnonzerorE   r  )r>   r<   r7   r;  rO   rP   Zunique_rankZinv_rankZn_ranksZ	corr_rankZrelevantZn_ranked_abover?   rD  r@   _my_lrap  s*    

rG  r   r   c           	      C   s   t dd|||d\}}t|jd |jd |d}t|drB| }t||}t||}t|| t|}|j	||fd}t||}t||}t|| d S )Nr(   F)r8   Zallow_unlabeledr'   	n_classesr7   r   )Zn_componentsr8   r'   toarrayrr   )
r   r   r+   hasattrrI  r   rG  r   r#   r:  )	r8  rH  r7   r'   ra   r>   r<   Z
score_lrapZscore_my_lrapr?   r?   r@   %check_alternative_lrap_implementation  s,    
	





rK  checkfuncc                 C   s   | | d S r$  r?   )rL  rM  r?   r?   r@   test_label_ranking_avp;  s    rN  c                   C   s   t t d S r$  )r=  r   r?   r?   r?   r@   test_lrap_error_raisedI  s    rO  r7   )r(   r$   r   r   rH  )r$   r   r   r'   c                 C   s   t t|| | d S r$  )rK  r   )r7   rH  r'   r?   r?   r@   $test_alternative_lrap_implementationM  s    rP  c                  C   s   t jg dg dg dgtd} t g dg dg dg}t g d}t g d	}tt| ||d
t || t |  d S )Nr   r  r  r   )r   r   r   r   )r   r   r   r   r   r   r   r   )re   r   rL   )rL   rL   r   r   )r,   rt   boolr   r   rE   )r>   r<   Zsamplewise_lrapsr   r?   r?   r@   &test_lrap_sample_weighting_zero_labelsV  s     rS  c                   C   s  t tddggddggd t tddggddggd t tddggddggd t tddggddggd t tg dgg dgd t tg dgg dgd t tg d	gg dgd t tg d
gg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg d	gg dgd t tg d
gg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg d	gg dgd t tg d
gg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg d	g dgg dg dgd t tg d	g dg d
gg dg dg dgd t tg d	g dg d
gg dg dg dgd d S )Nr   r(   r   r   r$   r.  r#  r   r   r   r   rp   r   r   r/  r4  r5  r   g      $@r   r(   rp   r   r   r   rU  r   g@rp   r(   rp   r   r   r?   r?   r?   r@   test_coverage_errork  sX     ((rZ  c                   C   sT  t tddggddggd t tddggddggd t tddggddggd t tddggddggd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg d	gg dgd t tg d
gg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd d S )Nr   re   r(   r$   r.  r)  r   r   r   r   rp   r   r   r/  rY  r?   r?   r?   r@   test_coverage_tie_handling  s    r[  zy_true, y_scorec                 C   s<   t jtdd t| | W d    n1 s.0    Y  d S )Nz'Expected 2D array, got 1D array insteadr   )rz   r{   r|   r   r  r?   r?   r@   test_coverage_1d_error_message  s    r\  c                   C   sT  t tddggddggd t tddggddggd t tg dgg dgd t tg dgg dgd t tg d	gg dgd t tg d
gg dgd t tg dgg dgd t tg dgg dgd t tddggddggd t tddggddggd t tddggddggd t tddggddggd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dg dgg dg dgd t tg dg dg d	gg dg dg dgd t tg dg dg d	gg dg dg dgd d S )Nr   r(   r   r   r   r#  r   re   r   r   rL   r   r   r.  r5  r/  r)  rT  rV  rW  r   rX  r   r   r?   r?   r?   r@   test_label_ranking_loss  s@     ((r^  csr_containerc                 C   s6   t t| tg dg dgg dg dgd d S )Nr   r   rW  rX  re   )r   r   r,   rt   )r_  r?   r?   r@   test_label_ranking_loss_sparse  s    &r`  c                   C   s  t t* tddgddggddg W d    n1 s:0    Y  t t, tddgddggddgg W d    n1 s0    Y  t t. tddgddggdgdgg W d    n1 s0    Y  t t, tddggddgddgg W d    n1 s0    Y  t t. tdgdggddgddgg W d    n1 sZ0    Y  t t. tddgddggdgdgg W d    n1 s0    Y  d S )Nr   r(   )rz   r{   r|   r   r?   r?   r?   r@   $test_ranking_appropriate_input_shape  s    8:<<>ra  c                   C   s   t tddggddggd t tddggddggd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg dgg dgd t tg d	gg dgd t tg d
gg dgd d S )Nr(   r   re   r   r)  r   r   r   r   r   r]  r?   r?   r?   r@   test_ranking_loss_ties_handling  s    rb  c                  C   sH   t ddd\} }| d }t|| tjdd\}}t|| d S Nr   r   )r'   rH  r(   )r$   rq   r   )r   _test_dcg_score_forr,   r0   r1   random_samplera   r>   r<   r?   r?   r@   test_dcg_score  s
    

rg  c                 C   s   t t | jd d }t| | }t| |}||k s>J t| | dd|k sXJ |j| jd fksnJ |j| jd fksJ |tt | d d d d df | j	ddksJ d S )Nr(   r$   r   kr   rB   Zaxis)
r,   log2r-   r+   r   r  rz   r   sortrE   )r>   r<   ZdiscountidealrO   r?   r?   r@   rd    s    

rd  c               	   C   sN  t t dg} t | j}t| |}t| |dd}dt t dd }|t|	 | 
  gkslJ |t|| d d d d df  	 gksJ d|dd	d f< t| |}t| |dd}|t|| d d d d df  	 gksJ |t|d d 	 | dd	d f 
  |dd  	 | dd d	f 
   gksJJ d S )
Nr   Tignore_tiesr(   r$   r   rB   r   rp   )r,   r   r-   r   r+   r   rk  rz   r   rE   r  )r>   r<   ZdcgZdcg_ignore_tiesZ	discountsr?   r?   r@   test_dcg_ties%  s"    
 .
.""rp  c               	   C   s>   t dd} t| | dddtt| | dddks:J d S )N   )r$   r   rp   T)ri  ro  )r,   r-   rD   r   rz   r   )r   r?   r?   r@   test_ndcg_ignore_ties_with_k9  s    rr  c                  C   s`   t g dg} t g dg}d}tjt|d t| | W d   n1 sR0    Y  dS )zDCheck `ndcg_score` exception when `y_true` contains negative values.)g{Gzg(\gGz޿g(\?gQ?)gQ?gףp=
?r   gQ?gHzG?z7ndcg_score should not be used on negative y_true valuesr   N)r,   rt   rz   r{   r|   r   )r>   r<   r   r?   r?   r@    test_ndcg_negative_ndarray_error@  s
    rs  c                  C   s   t ddd} | t jdjdd| jd }t| |}t| |dd	}|t	|ksZJ |t	d
kslJ |d7 }t| |t	d
ksJ d S )NF   r   r   r   皙ɿr   rr   Trn  rL   r   )
r,   r-   rD   r0   r1   r:  r+   r   rz   r   )r>   r<   ZndcgZndcg_no_tiesr?   r?   r@   test_ndcg_invariantI  s    
rv  ro  c              
   C   s"  dt dd d  }t t dddd}|t jdjdd	|jd
 }t||| dt	
dt t dd ksxJ t||| dt	
dt t dd ksJ t||| dt	
dt t dd ksJ t||d| dt	
dt t dd ksJ t||| dt	
dt t dd  ks:J t||| dt	
dt t dd  ksnJ dt d }dt t dd  }t||| dt	
|t d ksJ t||| dt	
t dksJ t||| dt	
|ks J t||| dt	
dksJ d S )Nrp   r   r   r   rB   )r   r(   r   ru  r   rr   rn  r$   r(   r   )Zlog_basero  )r   r   	   rL   )r,   eyeZtiler-   r0   r1   r:  r+   r   rz   r   rk  r   log10r   r  r   r   rE   )ro  r>   r<   Zy_score_noisyZexpected_dcg_scorer?   r?   r@   test_ndcg_toy_examplesT  s^    


rz  c                  C   sH   d} t jt| d" tdggdgg W d   n1 s:0    Y  dS )zhCheck that we raise an informative error message when trying to
    compute NDCG with a single document.zTComputing NDCG is only meaningful when there is more than 1 document. Got 1 instead.r   r(   N)rz   r{   r|   r   )r  r?   r?   r@   test_ndcg_error_single_document{  s    r{  c                  C   sH   t ddd\} }| d }t|| tjdd\}}t|| d S rc  )r   _test_ndcg_score_forr,   r0   r1   re  rf  r?   r?   r@   test_ndcg_score  s
    

r}  c                 C   s   t | | }t | |}||k s$J | dkjdd}||  tt|  ksXJ || tt| ksxJ ||  tt| ||  t| | |   ksJ || tt| ksJ |j	| j	d fksJ |j	| j	d fksJ d S )Nr   r(   rj  )
r   r  rz   r   r,   r   rE   r   r   r+   )r>   r<   rm  rO   Zall_zeror?   r?   r@   r|    s    

$ 
 r|  c                  C   s  t g d} t| | dddks$J t| | dddks:J tt" t| | ddsXJ W d    n1 sl0    Y  tt" t| | ddsJ W d    n1 s0    Y  tt" t| | ddsJ W d    n1 s0    Y  t g d}t| |dd}t| |}||ks"J t| |d	dd
ks:J tdd\} }}t dddD ]"}tt| ||dt	| || qXd S )Nr   r(   )r^   gMbP?gg?r   )r   r   r   g{Gz?r   re   Trh   g-C6?r   )
r,   rt   r   rz   r{   r|   rA   Zlinspacer   rf   )r>   r   Zroc_auc_with_max_fpr_oneZunconstrained_roc_aucr=   ra   r^   r?   r?   r@   test_partial_roc_auc_score  s(    000

r~  zy_true, k, true_scorer   r$   rp   r   c                 C   sF   t g dg dg dg dg}t| ||d}|t|ksBJ d S )NrQ  r   r   r   r   r   r   r   r   r   r   r   r   rh  r,   rt   r   rz   r   )r>   ri  
true_scorer<   rO   r?   r?   r@   test_top_k_accuracy_score  s    	r  zy_score, k, true_score)rB   rB   r(   r(   )rB   r(   rB   r(   )r   r   r   r   )r   r   r   r   c                 C   s   g d}|   dkr$|  dkr$dnd}|dkr@| |ktjn|}t|| |d}t||}||  krvt|ks|n J d S )Nr   r   r(   re   rh  )	minmaxZastyper,   Zint64r   r   rz   r   )r<   ri  r  r>   rX   r=   rO   Z	score_accr?   r?   r@    test_top_k_accuracy_score_binary  s     
r  zy_true, true_score, labels)r   r(   r(   r$   )r   r(   r(   r(   r  )r   r   r   r   )r   r   r   r   labels_as_ndarrayc                 C   sV   |rt |}t g dg dg dg dg}t| |d|d}|t|ksRJ dS )z,Test when labels and y_score are multiclass.rQ  r  r  r  r$   ri  r   N)r,   r   rt   r   rz   r   )r>   r  r   r  r<   rO   r?   r?   r@   0test_top_k_accuracy_score_multiclass_with_labels  s    
	r  c                     s   t jddddd\ t dd\} }}}tdd| | t| |f||fD ]<\  fddtddD }tt	|dksTJ qTd S )	Nr   r   r   )rH  r7   Zn_informativer'   )r'   c                    s    g | ]}t  |d qS )rh  )r   r3   )r   ri  r5   r;   r6   r?   r@   r     s   z8test_top_k_accuracy_score_increasing.<locals>.<listcomp>r$   )
r   Zmake_classificationr   r   r2   r  rM   r,   r  r   )ZX_trainZX_testZy_trainZy_testZscoresr?   r  r@   $test_top_k_accuracy_score_increasing  s    

r  c                 C   sB   t g dg dg dg dg}t| ||dt|ks>J d S )N)r   r   r   r   )r(   r   r   r   )r   r   rp   rp   r  rh  r  )r>   ri  r  r<   r?   r?   r@   test_top_k_accuracy_score_ties  s    
r  z	y_true, kr   c                 C   sr   t g dg dg dg dg}d}tjt|d t| ||d}W d    n1 sX0    Y  |dksnJ d S )	NrQ  )r   r   r   r   )r   r   r   r   )r   r   r   r   zu'k' \(\d+\) greater than or equal to 'n_classes' \(\d+\) will result in a perfect score and is therefore meaningless.r   rh  r(   )r,   rt   rz   r   r   r   )r>   ri  r<   r   rO   r?   r?   r@   !test_top_k_accuracy_score_warning)  s    	,r  zy_true, y_score, labels, msg)r   g=
ףp=?r(   r$   )r   r   r   )r   r   r   r   )r   re   r   z9y type must be 'binary' or 'multiclass', got 'continuous'zZNumber of classes in 'y_true' \(4\) not equal to the number of classes in 'y_score' \(3\).)r   r   r   r   z"Parameter 'labels' must be unique.z#Parameter 'labels' must be ordered.)r   r   r(   r$   zSNumber of given labels \(4\) not equal to the number of classes in 'y_score' \(3\).rV  z3'y_true' contains labels not in parameter 'labels'.)re   r   r   )r   r   r   z}`y_true` is binary while y_score is 2d with 3 classes. If `y_true` does not contain all the labels, `labels` must be providedc                 C   sB   t jt|d  t| |d|d W d    n1 s40    Y  d S )Nr   r$   r  )rz   r{   r|   r   )r>   r<   r   r   r?   r?   r@   test_top_k_accuracy_score_errorB  s    Sr  c                 C   sJ   | g dg dg}t g dg dg}t||}|tdksFJ d S )Nr   r   )re   r~   r   r6  )r,   rt   r   rz   r   )r_  r>   r<   resultr?   r?   r@   Otest_label_ranking_avg_precision_score_should_allow_csr_matrix_for_y_true_input  s    
r  metricr   ))FT)r   r(   )r   rL   )zeroonec                 C   s   t jd}d|d  }}|j||dd}||}| |||d}t|tr\t |rJ n@|\}}	}
t | rxJ t |	 rJ t |
 rJ dS )zCheck that the metric works with different types of `pos_label`.

    We can expect `pos_label` to be a bool, an integer, a float, a string.
    No error should be raised for those types.
    *   r   rB   T)rs   replacer  N)	r,   r0   r1   choicer   
isinstancerF   r   any)r  r   r:   r7   rH   r>   Zy_probar  Zmetric_1Zmetric_2rl   r?   r?   r@   #test_ranking_metric_pos_label_types  s    


r  c                 C   sL   t j| }|jdddd}|d}t||\}}}t |d sHJ dS )zCheck that thresholds do not exceed 1.0 when `y_score` is a probability
    estimate.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/26193
    r   r$   r   rr   N)r,   r0   r1   ru   r   r   isinf)r   r:   r>   r<   ra   rl   r?   r?   r@   (test_roc_curve_with_probablity_estimates  s
    
r  c                  C   s   t dd\} }}d}tjt|d t| |d W d   n1 sD0    Y  d}tjt|d t| ||d W d   n1 s0    Y  dS )	z)Check the message for future deprecation.Trh   z)probas_pred was deprecated in version 1.5r   )probas_predNz4`probas_pred` and `y_score` cannot be both specified)r  r<   )rA   rz   r   FutureWarningr   r{   r|   )r>   ra   r<   Zwarn_msg	error_msgr?   r?   r@   /test_precision_recall_curve_deprecation_warning  s    $r  )NF)r   r   r   )r   r   numpyr,   rz   Zscipyr   Zsklearnr   r   Zsklearn.datasetsr   Zsklearn.exceptionsr   Zsklearn.linear_modelr   Zsklearn.metricsr   r	   r
   r   r   r   r   r   r   r   r   r   r   Zsklearn.metrics._rankingr   r   Zsklearn.model_selectionr   Zsklearn.preprocessingr   Zsklearn.random_projectionr   Zsklearn.utils._testingr   r   r   r   r   Zsklearn.utils.extmathr   Zsklearn.utils.fixesr    Zsklearn.utils.validationr!   r"   r#   ZCURVE_FUNCSrA   rK   rS   rY   rf   markZparametrizern   rv   ry   r}   r   r   r   r   r   r   r   r   rt   r   r   r   r   r   r   r   r   r   r  filterwarningsr  r  r  r
  r  r  r  r  r   r!  r"  r(  r*  r+  r,  r-  r2  r3  r9  r<  r=  r?  rC  rG  rK  rN  rO  rM   rP  rS  rZ  r[  r\  r^  r`  ra  rb  rg  rd  rp  rr  rs  rv  rz  r{  r}  r|  r~  r  r  r  r  r  r  r  r  r  r  r  r?   r?   r?   r@   <module>   s  <
0
r
	
2

	
"
"	


	G				#

	




 $"	






;'
 	7
+
		
&
	
R