o
    \i 1                     @   s8  d dl Zd dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZ d dl	mZ d dlmZ d dlmZmZ d d	lmZmZ d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z ej!"de ej!"de#d e$d!d"fd#d$ e%d"&ej$d"fd%d$ d&d$ d'fd(d$ d)d$ d*fe'g d+e'g d,d fe'ej(ej(d d-d.d/ge'g d0d fe'g d+ej'g d0ej$d1d*fgd2d3 Z)ej!"d4d!d5gd6d7 Z*d8d9 Z+ej!j,eed:k d;d<ej!"d=g d>d?d@ Z-ej!j,eed:k dAd<ej!"d=g d>dBdC Z.dS )D    N)assert_allcloseassert_array_equal)approx)config_context)_convert_to_numpyget_namespace)yield_namespace_device_dtype_combinationsdevice)_array_api_for_tests)
np_versionparse_version)_averaged_weighted_percentile_weighted_percentilec                  C   s>   t g d} t g d}t| |d}|t | ksJ d S )Nr                  )r   r   r   r   r   r   2   )nparrayr   medianyswscore r   /var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/lib/python3.10/site-packages/sklearn/utils/tests/test_stats.pytest_averaged_weighted_median   s   r    c                 C   sL   t j| }|jddd}t d}t||d}|t j|dddks$J d S )N   
   sizeaveraged_inverted_cdf)method)r   randomRandomStaterandintonesr   
percentile)global_random_seedrngr   r   r   r   r   r   !test_averaged_weighted_percentile   s
   
r.   c                  C   sH   t g d} t g d}d}t| ||}t| ||}||ks"J d S )N)r   r   r   )r   r   r   r   )r   r   r   r   )r   r   qscore_averagedr   r   r   r   %test_averaged_and_weighted_percentile&   s   r1   c                  C   sp   t jdt jd} d| dd< d| dd< d| d	< d
| d< t jdt jd}d|d	< t| |d}t|d
ks6J dS )zCCheck `weighted_percentile` on artificial data with obvious median.f   dtyper   Nr   r   ii r           )r   emptyfloat64r*   r   r   r   r   valuer   r   r   test_weighted_percentile1   s   r;   c                  C   sJ   t jdt jd} | d t jdt jd}t| |d}t|dks#J dS )z8Check `weighted_percentile` with all weights equal to 1.r2   r3   r6   r   r   Nr   r7   r8   fillr*   r   r   r   r   r   r   test_weighted_percentile_equal>   s
   
r>   c                  C   sT   t jdt jd} | d t jdt jd}|d t| |d}t|dks(J dS )z8Check `weighted_percentile` with all weights equal to 0.r2   r3   g      ?r6   r   Nr<   r9   r   r   r   $test_weighted_percentile_zero_weightG   s   

r?   c                  C   st   t g d} t g d}t| |d}t|dksJ t| |d}t|dks*J t| |d}t|dks8J d	S )
zCheck `weighted_percentile(percentile_rank=0)` behaves correctly.

    Ensures that (leading)zero-weight observations ignored when `percentile_rank=0`.
    See #20528 for details.
    r   r   r   r   r   r   r   r   r   r   r   d   r   N)r   r   r   r   r9   r   r   r   4test_weighted_percentile_zero_weight_zero_percentileQ   s   rB   c                 C   sN   t j| }|jddd}t |j}t |}t||}|t|ks%J dS )a  Checks `_weighted_percentile(percentile_rank=50)` is the same as `np.median`.

    `sample_weights` are all 1s and the number of samples is odd.
    When number of samples is odd, `_weighted_percentile` always falls on a single
    observation (not between 2 values, in which case the lower value would be taken)
    and is thus equal to `np.median`.
    For an even number of samples, this check will not always hold as (note that
    for some other percentile methods it will always hold). See #17370 for details.
    r"      r#   N)	r   r'   r(   r)   r*   shaper   r   r   )r,   r-   xweightsr   w_medianr   r   r   "test_weighted_median_equal_weightsc   s   


rH   c                 C   s\   t j| }|jddd}|jddd}t ||}t |}t||}|t|ks,J d S )Nr!   r"   r#   r   )	r   r'   r(   r)   choicerepeatr   r   r   )r,   r-   rE   rF   x_manualr   rG   r   r   r   $test_weighted_median_integer_weightsu   s   

rL   c                    s   t j| }|jddd}|jddd |jddd}t ||fjt } fddtj	d D }t
|| |jddd}t  |fjt}fddtj	d D }t
|| d S )	Nr"   r#   r   r!   c                    s"   g | ]}t d d |f  qS Nr   .0i)w1x_2dr   r   
<listcomp>   s   " z/test_weighted_percentile_2d.<locals>.<listcomp>r   c                    s.   g | ]}t d d |f  d d |f qS rM   rN   rO   )w_2drS   r   r   rT      s    ")r   r'   r(   r)   rI   vstackTr   rangerD   r   )r,   r-   x1x2rG   p_axis_0w2r   )rR   rU   rS   r   test_weighted_percentile_2d   s   


r]   z#array_namespace, device, dtype_namezdata, weights, percentile*   r   r   c                 C   s
   |  dS Nr   randr-   r   r   r   <lambda>   s   
 rc   c                 C      |  ddS )Nr   r   r`   rb   r   r   r   rc          c                 C   s   |  dtjS r_   ra   astyper   float32rb   r   r   r   rc      s    K   c                 C   rd   Nr!   r   r`   rb   r   r   r   rc      re   c                 C   s   |  ddtjS rj   rf   rb   r   r   r   rc      s       r   r@   r   r   r   )r   r   r   r   r   r   r3   c                 C   s  |dkr zddl }W n	 ty   Y nw ||dkr td t||}|jd|d}	|jd|d}
|dkrJ||	|	|
|	krJtd|  t
j| }t|rX||n|}t|rb||n|}||}t|||}|j||d}|j||d}td	d
, t|||}t|t|ksJ t|d t|d ksJ t||d}W d   n1 sw   Y  |j|jksJ |j|jksJ t|| |dkr|j|j  krt
jksJ  J dS |jt
jksJ dS )zECheck `_weighted_percentile` gives consistent results with array API.array_api_strictr   Ndevice1zXarray_api_strict has bug when indexing with tuple of arrays on non-'CPU_DEVICE' devices.r   r	   zxp.nextafter is broken on T)array_api_dispatch)xprh   )rl   ImportErrorDevicepytestxfailr   zerosr*   all	nextafterr   r'   r(   callablerg   r   asarrayr   array_devicer   r   r4   rD   r   rh   r8   )r,   array_namespacer
   
dtype_namedatarF   r+   rl   ro   zerooner-   X_np
weights_np	result_npX_xp
weights_xp	result_xpresult_xp_npr   r   r   .test_weighted_percentile_array_api_consistency   sD   "


&r   sample_weight_ndimr   c                    s  t j|}|dd t j |j j dk < t  | dkr)|jddddn|jddd	dt d
} fddt	 jd D j
dkr^t  jd  jd  jd fddt	 jd D t fddt	 jd D }t|| dS )a  Test that calling _weighted_percentile on an array with nan values returns
    the same results as calling _weighted_percentile on a filtered version of the data.
    We test both with sample_weight of the same shape as the data and with
    one-dimensional sample_weight.rA   r"         ?r   r      )rA   r"   r#   )rA      c                    s&   g | ]} d d |f  |f qS rM   r   rP   col)array_with_nansnan_maskr   r   rT     s    z9test_weighted_percentile_nan_filtered.<locals>.<listcomp>r   c                    s&   g | ]} d d |f  |f qS rM   r   r   )r   sample_weightr   r   rT   
  s    c                    s    g | ]}t  | | d qS )r   rN   r   )filtered_arrayfiltered_weightsr   r   rT     s    N)r   r'   r(   ra   nanrD   isnanr)   r   rX   ndimrJ   reshaper   r   )r   r,   r-   resultsexpected_resultsr   )r   r   r   r   r   r   %test_weighted_percentile_nan_filtered   s0   

r   c               	   C   s~   t t jdgt jdgt jt jgt jt jgt jdgt jt jgg} t | }d}t| ||}t j|t t jdgdds=J dS )zCCheck that nans are ignored in general, except for all NaN columns.r   r   r   Z   T)	equal_nanN)r   r   r   	ones_liker   array_equal)r   rF   percentile_rankvaluesr   r   r   'test_weighted_percentile_all_nan_column  s   




$r   z2.0z2np.quantile only accepts weights since version 2.0)reasonr+   )B   r"   r   c                 C   sZ   t j|}|dd}|jdddd}t||| }t j|| d |ddd	}t|| d
S )z\Check that _weighted_percentile delivers equivalent results as np.quantile
    with weights.r"   rA   r   r   r"   rA   r#   r   inverted_cdfrF   axisr&   N)r   r'   r(   ra   r)   r   quantiler   )r+   r,   r-   r   r   percentile_weighted_percentilepercentile_numpy_quantiler   r   r   ,test_weighted_percentile_like_numpy_quantile0  s   	r   z5np.nanquantile only accepts weights since version 2.0c                 C   sp   t j|}|dd}t j||j|j dk < |jdddd}t||| }t j|| d |dd	d
}t	|| dS )z_Check that _weighted_percentile delivers equivalent results as np.nanquantile
    with weights.r"   rA   r   r   r   r   r#   r   r   r   N)
r   r'   r(   ra   r   rD   r)   r   nanquantiler   )r+   r,   r-   r   r   r   percentile_numpy_nanquantiler   r   r   /test_weighted_percentile_like_numpy_nanquantileG  s   	r   )/numpyr   rr   numpy.testingr   r   r   sklearn._configr   sklearn.utils._array_apir   r   r   r
   ry   sklearn.utils.estimator_checksr   sklearn.utils.fixesr   r   sklearn.utils.statsr   r   r    r.   r1   r;   r>   r?   rB   rH   rL   r]   markparametrizerh   int32r*   rg   r   r   r   r   r   skipifr   r   r   r   r   r   <module>   sp    		
(7
*

