
    sg                        d Z ddlZ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mZ ddgddgddgd	d	gd	d
gd
d	ggZg dZddgd
d
gdd
ggZg dZg dZg dZd	dgdd	gdd	ggZ ej*                         Zej.                  j1                  d	      Zej5                  ej6                  j8                        Zej<                  e   e_        ej6                  e   e_        ej>                  jA                  de      d        Z!d Z"d Z#d Z$d Z%d Z&d Z'ej>                  jA                  de      d        Z(d Z)d Z*d Z+ej>                  jA                  dg d      d         Z,ej>                  jA                  d!ejZ                  gez         d"        Z.y)#z*
Testing for the nearest centroid module.
    N)datasets)NearestCentroid)assert_allcloseassert_array_almost_equalassert_array_equal)CSR_CONTAINERS      )r
   r
   r
   r   r   r      )r
   r   r   )i@   P   gj=gHKbi):g68csr_containerc                 `    | t               } | t              }t               }|j                  t         t               t        |j                  t              t               t        |j                  t              t               t        |j                  t              t               t        d      }|j                  t         t               t        |j                  t              t               t        |j                  t              t               t        |j                  t              t               t        d      }|j                  t         t               t        |j                  t              t               t        |j                  t              t               t        |j                  t              t               t        ddg      }|j                  t         t               t        |j                  t              t               t               }|j                  |t               t        |j                  |      t               t               }|j                  |t               t        |j                  t              t               t               }|j                  t         t               t        |j                  |      t               t               }|j                  |j                         t               t        |j                  |j                               t               y )Nuniformpriors	empiricalg      ?g      ?)XTr   fityr   predicttrue_resultr   decision_functiontrue_discriminant_scorespredict_proba
true_probatrue_result_prior1tocootolil)r   X_csrT_csrclfs       `/var/www/html/venv/lib/python3.12/site-packages/sklearn/neighbors/tests/test_nearest_centroid.pytest_classification_toyr'   %   s    !E!E 
CGGAqMs{{1~{3c33A68PQc//2J? 
+CGGAqMs{{1~{3c33A68PQc//2J?

-CGGAqMs{{1~{3c33A68PQc//2J? $
.CGGAqMs{{1~'9: 
CGGE1s{{5);7 
CGGE1s{{1~{3 
CGGAqMs{{5);7 
CGGEKKM1s{{5;;=1;?    c                  8   dD ]  } t        |       j                  t        j                  t        j                        }t        j                  |j                  t        j                        t        j                  k(        }|dkD  rJ dt        |      z           y )N	euclidean	manhattanmetricg?Failed with score = 	r   r   irisdatatargetnpmeanr   str)r.   r%   scores      r&   	test_irisr8   Y   sq    , @V,00DKKHDII.$++=>s{?2SZ??{@r(   c                  L   dD ]  } dD ]  }t        | |      }|j                  t        j                  t        j                        }t        j                  |j                  t        j                        t        j                  k(        }|dkD  rJ dt        |      z            y )Nr*   )N皙?g      ?)r.   shrink_thresholdg?r/   r0   )r.   r;   r%   r7   s       r&   test_iris_shrinkager<   a   s    , D 0 	D!BRSC''$))T[[1CGGCKK		2dkkABE3;C 6U CC;		DDr(   c                     dd l } t               }|j                  t        j                  t        j
                         |j                  t        j                  t        j
                        }| j                  |      }| j                  |      }t        |      |j                  k(  sJ |j                  t        j                  t        j
                        }t        ||d       y )Nr   z>Failed to generate same score after pickling (classification).)pickler   r   r1   r2   r3   r7   dumpsloadstype	__class__r   )r>   objr7   sobj2score2s         r&   test_picklerG   k   s     
CGGDIIt{{#IIdii-ESA<<?D:&&&ZZ		4;;/FHr(   c                  @   t        j                  ddgddgddgddgddgg      } t        j                  g d      }t        d      }|j                  | |       t        j                  d	d
gddgg      }t         j                  j                  |j                  |       y )Nr   r   r         )r   r   r   r   r   r:   r;   g;4,F]?gCMX?gJGp@gU@)r4   arrayr   r   testingr   
centroids_)r   r   r%   expected_results       r&   test_shrinkage_correctrP   ~   s     	1a&1a&1a&1a&1a&9:A
!A
3
/CGGAqMhhI 688LMNOJJ((Ir(   c                     t        d      } t        j                  t              }d||dk(  <   | j	                  t
        |       | j                  }| j	                  t
        t               t        || j                         y )Ng{Gz?rK   r   r
   )r   r4   asarrayr   r   r   rN   r   )r%   y_indcentroid_encodeds      r&   "test_shrinkage_threshold_decoded_yrU      s[    
4
0CJJqMEE%2+GGAu~~GGAqM'8r(   c                     t         j                  j                  d      } | j                  dd      }| j	                  ddd      }| j                  d      }t        d      }|j                  ||       |j                  |      }t        d      }||z   }|j                  ||       |j                  |      }t        ||       y )Nr   2   r   r:   rK   )	r4   randomRandomStaterandrandintr   r   r   r   )rngr   r   noiser%   y_initX_noisey_translates           r&   test_predict_translated_datara      s     ))


"CRAAq"AHHRLE
3
/CGGAqM[[^F
3
/C%iGGGGQ++g&Kv{+r(   c                      | t               }t        d      }|j                  t         t               |j                  }|j                  |t               t        |j                  |       t        |ddgddgg       y )Nr,   r-   r
   r   )r   r   r   r   rN   r   )r   r#   r%   dense_centroids       r&   test_manhattan_metricrd      sa     !E

-CGGAqM^^NGGE1s~~~6~R1a&'9:r(   c                     t        j                  d      } d| d d df<   d| d d df<   t        j                  d      }d|d<   t        d      }t	        j
                  t              5  |j                  | |       d d d        y # 1 sw Y   y xY w)	N)
   r   g$b@r   g_LOчr   rf   r:   rK   )r4   emptyzerosr   pytestraises
ValueErrorr   )r   r   r%   s      r&   test_features_zero_varrl      sz     	AAadGAadG
"AAaD
3
/C	z	" 1  s   'BBc                      t        ddg      } t        j                  t        d      5  | j	                  t
        t               ddd       y# 1 sw Y   yxY w)zGCheck that we raise an error when the user-defined priors are negative.r	      r   zpriors must be non-negativematchN)r   ri   rj   rk   r   r   r   )r%   s    r&   test_negative_priors_errorrq      sA    
"a
)C	z)F	G 1  s   AAc                  R   ddg} t        |       }t        j                  t        d      5  |j	                  t
        t               ddd       t        |j                  t        j                  |       t        j                  |       j                         z         y# 1 sw Y   WxY w)zfCheck that we raise a warning and normalize the user-defined priors when they
    don't sum to 1.
    r   rn   r   zAThe priors do not sum to 1. Normalizing such that it sums to one.ro   N)r   ri   warnsUserWarningr   r   r   r   class_prior_r4   rR   sum)r   r%   s     r&   test_warn_non_normalized_priorsrw      s     VF

(C	Q
  	1	 C$$bjj&82::f;M;Q;Q;S&ST s   BB&response_method)r   r   predict_log_probac                     t        d      j                  t        t              }t	        j
                  t              5   t        ||       t               ddd       y# 1 sw Y   yxY w)z{Check that we raise an AttributeError with Manhattan metric when trying
    to call a non-thresholded response method.
    r,   r-   N)	r   r   r   r   ri   rj   AttributeErrorgetattrr   )rx   r%   s     r&   (test_method_not_available_with_manhattanr}      sM     
-
1
1!Q
7C	~	& )%_%a() ) )s   AA'array_constructorc                    t        j                  t        t              df      }|dddfxx   dz  cc<    | |      }t	               }t        j                  t        d      5  |j                  |t               ddd       y# 1 sw Y   yxY w)zHCheck that we raise an error when the variance for all features is zero.r   Nr   zAll features have zero variancero   )	r4   oneslenr   r   ri   rj   rk   r   )r~   r   r%   s      r&   test_error_zero_variancesr      sn     	QAadGqLG!A

C	z)J	K 1  s   #BB)/__doc__numpyr4   ri   sklearnr   sklearn.neighborsr   sklearn.utils._testingr   r   r   sklearn.utils.fixesr   r   r   r   r   r    r   r   	load_irisr1   rX   rY   r\   permutationr3   sizepermr2   markparametrizer'   r8   r<   rG   rP   rU   ra   rd   rl   rq   rw   r}   rL   r    r(   r&   <module>r      s      - 
 / 	"XBx"bAq6Aq6Aq6:"X1v1v ( +a 0;2BC

 xiiA
t{{''(IIdO	kk$ .90@ :0@f@D&J9," .9	; :	;U R)) ,rxxj>.IJ Kr(   