
    -i2L              	       r   S r SSKrSSKrSSKrSSKJrJr  SSKJ	r	  SSK
Jr  SSKJrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  \" S5      r\" 5       r\R=                  \R>                  R@                  5      r!\RD                  \!   r#\R>                  \!   r$S\#RJ                  l&        S\$RJ                  l&        \RN                  " \(5      RR                  r*S r+S r,S r-S r.S r/S r0S r1\Rd                  Rg                  S/ SQ5      \Rd                  Rg                  S/ SQ5      \Rd                  Rg                  S/ SQ5      \Rd                  Rg                  S/ SQ5      S 5       5       5       5       r4S r5S r6\Rd                  Rg                  S/ SQ5      S  5       r7S! r8S" r9S# r:S$ r;S% r<S& r=\Rd                  Rg                  S'S\R|                  " S(5      4S)\R|                  " S*5      4S+\R~                  " S,5      4/5      S- 5       r@\Rd                  Rg                  SSS./5      S/ 5       rAg)0zL
Testing for Neighborhood Component Analysis module (sklearn.neighbors.nca)
    N)assert_array_almost_equalassert_array_equal)
check_grad)clone)	load_iris
make_blobsmake_classification)ConvergenceWarning)pairwise_distances)NeighborhoodComponentsAnalysis)LabelEncoder)check_random_state)validate_dataFc                  R   [         R                  " SS/SS/SS/SS//5      n [         R                  " / SQ5      n[        SSSS9nUR                  X5        UR	                  U 5      n[        [        U5      R                  5       SS2S4   [         R                  " / S	Q5      5        g)
zTest on a simple example.

Puts four points in the input space where the opposite labels points are
next to each other. After transform the samples from the same class
should be next to each other.

r         )r   r   r   r   identity*   )n_componentsinitrandom_stateN)r      r   r   )nparrayr   fit	transformr   r   argsort)XyncaX_ts       S/var/www/html/venv/lib/python3.13/site-packages/sklearn/neighbors/tests/test_nca.pytest_simple_exampler#   $   s     	1a&1a&1a&1a&12A
A
(ZbC GGAM
--
C)#.668A>@VW    c                     [         R                  R                  S5      n SnU R                  SU5      n[         R                  " X"R                  SS9[         R                  SS24   /5      n/ SQn " S S	5      nU" X45      n[        SUR                  S
9nUR                  X45      n[        U5        [        XS   -
  S5        [        UR                  S-   5      S:  d   eg)a  Test on a toy example of three points that should collapse

We build a simple example: two points from the same class and a point from
a different class in the middle of them. On this simple example, the new
(transformed) points should all collapse into one single point. Indeed, the
objective is 2/(1 + exp(d/2)), with d the euclidean distance between the
two samples from the same class. This is maximized for d=0 (because d>=0),
with an objective equal to 1 (loss=-1.).

r      r   r   )axisN)r   r   r   c                        \ rS rSrS rS rSrg)4test_toy_example_collapse_points.<locals>.LossStorerG   c                 ^   [         R                  U l        [        5       U l        [         R                  U R                  l        [        U R                  XSS9u  U l        n[        5       R                  U5      nUS S 2[         R                  4   U[         R                  S S 24   :H  U l        g Nr   )ensure_min_samples)r   inflossr   fake_ncan_iter_r   r   r   fit_transformnewaxissame_class_maskselfr   r   s      r"   __init__=test_toy_example_collapse_points.<locals>.LossStorer.__init__H   sx    DI:<DM$&FFDMM!%dmmQaPIDFA,,Q/A#$Q

]#3qQ7G#GD r$   c                 v    U R                   R                  XR                  U R                  S5      u  U l        ng)z*Stores the last value of the loss functiong      N)r0   _loss_grad_lbfgsr   r4   r/   )r6   transformationn_iter_s       r"   callback=test_toy_example_collapse_points.<locals>.LossStorer.callbackQ   s-    ==99(<(<dLDIqr$   )r   r0   r/   r4   N__name__
__module____qualname____firstlineno__r7   r>   __static_attributes__ r$   r"   
LossStorerr)   G   s    	H	r$   rG   )r   r>           r   g|=)r   randomRandomStaterandnvstackmeanr3   r   r>   r2   printr   absr/   )	rng	input_dim
two_pointsr   r   rG   loss_storerr    r!   s	            r"    test_toy_example_collapse_pointsrT   6   s     ))


#CI1i(J
		:A6rzz1}EFGAA   Q"K
(b;CWCW
XC


A
!C	#JcFlC0{!#$u,,,r$   c                   ^^^	 [         R                  R                  U 5      n[        U S9u  mnUR	                  UR                  STR                  S   S-   5      TR                  S   5      n[        5       m	ST	l        USS2[         R                  4   U[         R                  SS24   :H  mUUU	4S jnUUU	4S jn[        XEUR                  5       5      nU[        R                  " SSS	9:X  d   eg)
zrTest gradient of loss function

Assert that the gradient is almost equal to its finite differences
approximation.
)r   r   r   Nc                 0   > TR                  U TT5      S   $ )Nr   r:   Mr   maskr    s    r"   fun$test_finite_differences.<locals>.funn       ##Aq$/22r$   c                 0   > TR                  U TT5      S   $ )Nr   rW   rX   s    r"   grad%test_finite_differences.<locals>.gradq   r]   r$   rH   -C6?)rO   )r   rI   rJ   r	   rK   randintshaper   r1   r3   r   ravelpytestapprox)
global_random_seedrP   r   rY   r[   r_   diffr   rZ   r    s
          @@@r"   test_finite_differencesri   `   s     ))

 2
3C,>?DAq		#++aa0!''!*=A
(
*CCKQ

]qQ//D33 c+D6==$////r$   c                     [         R                  " S5      R                  SS5      n / SQn[        n[         R                  R                  S5      nUR                  SS5      nSUR                  S    S	UR                  S
    S3n[        R                  " [        [        R                  " U5      S9   U" US9R                  X5        S S S 5        SnSU SU R                  S
    S3n[        R                  " [        [        R                  " U5      S9   U" US9R                  X5        S S S 5        g ! , (       d  f       Nr= f! , (       d  f       g = f)N      r   r   r   r   r   r   r&   The output dimensionality (r   ]) of the given linear transformation `init` cannot be greater than its input dimensionality (r   ).matchr   
   DThe preferred dimensionality of the projected space `n_components` (8) cannot be greater than the given data dimensionality ()!r   )r   arangereshaper   rI   rJ   randrc   re   raises
ValueErrorreescaper   )r   r   NCArP   r   msgr   s          r"   test_params_validationr   y   s   
		"a#AA
(C
))


#C88Aq>D
%djjm_ 5226**Q-	D 
 
z3	81  
9L	'. )//0wwqzl"	> 
 
z3	8&**10 
9	8 
9	8 
9	8s   2D4E4
E
Ec                  z   [         R                  " S5      R                  SS5      n / SQn[         R                  " SS/SS//5      n[        R
                  " [        5         [        US9R                  X5        S S S 5        [         R                  " SS/SS/SS	//5      n[        R
                  " [        5         [        US9R                  X5        S S S 5        [         R                  " S
5      R                  SS5      n[        US9R                  X5        g ! , (       d  f       N= f! , (       d  f       N^= f)Nrk   rl   r   rm   r   r   rs   r&      	   )	r   ry   rz   r   re   r|   r}   r   r   )r   r   r;   s      r"   test_transformation_dimensionsr      s    
		"a#AA XX1v1v./N	z	"&N;??E 
#
 XX1v1v1v67N	z	"&N;??E 
# YYq\))!Q/N"7;;AA 
#	" 
#	"s   !D;D,
D),
D:c                  .   [         R                  R                  S5      n [         R                  " S5      R	                  SS5      n/ SQnU R                  UR                  S   S-
  S5      nUR                  S   n[        X4S9nSU S	UR                  S
    S3n[        R                  " [        [        R                  " U5      S9   UR                  X5        S S S 5        UR                  S   S-   n[        X4S9nSU SUR                  S    S3n[        R                  " [        [        R                  " U5      S9   UR                  X5        S S S 5        [        SSS9nUR                  X5        g ! , (       d  f       N= f! , (       d  f       N;= f)Nr   rk   rl   r   rm   r   r   r   ru   V) does not match the output dimensionality of the given linear transformation `init` (r   rw   rq   r   rv   r   )r   r   )r   rI   rJ   ry   rz   r{   rc   r   re   r|   r}   r~   r   r   )rP   r   r   r   r   r    r   s          r"   test_n_componentsr      sR   
))


#C
		"a#AA88AGGAJNA&D 771:L
(d
NC	'. )::a=/	%  
z3	8 
9 771:>L
(d
NC	'. )**+''!*R	9 
 
z3	8 
9 )aj
ICGGAM! 
9	8 
9	8s   E5?F5
F
Fc                  &   [         R                  R                  S5      n [        SSSSS9u  p[	        SS9nUR                  X5        [	        S	S9nUR                  X5        [	        S
S9nUR                  X5        [	        SS9nUR                  X5        [	        SS9nUR                  X5        U R                  UR                  S   UR                  S   5      n[	        US9nUR                  X5        U R                  UR                  S   UR                  S   S-   5      n[	        US9nSUR                  S    SUR                  S    S3n	[        R                  " [        [        R                  " U	5      S9   UR                  X5        S S S 5        U R                  UR                  S   S-   UR                  S   5      n[	        US9nSUR                  S    SUR                  S    S3n	[        R                  " [        [        R                  " U	5      S9   UR                  X5        S S S 5        U R                  UR                  S   UR                  S   5      nUR                  S   S-
  n
[	        XS9nSU
 SUR                  S    S3n	[        R                  " [        [        R                  " U	5      S9   UR                  X5        S S S 5        g ! , (       d  f       GNV= f! , (       d  f       N= f! , (       d  f       g = f)Nr      r   r&   r   	n_samplescenters
n_featuresr   r   rs   rI   autopcaldar   zThe input dimensionality (zc) of the given linear transformation `init` must match the dimensionality of the given inputs `X` (rp   rq   rn   ro   r   r   ru   r   rw   )r   rI   rJ   r   r   r   r{   rc   re   r|   r}   r~   r   )rP   r   r   r    
nca_randomnca_autonca_pcanca_ldar   r   r   s              r"   test_init_transformationr      s   
))


#CA!!LDA )j
9CGGAM 0X>JNN1 .6:HLL -%8GKK -%8GKK88AGGAJ
+D
(d
3CGGAM 88AGGAJ
Q/D
(d
3C
$TZZ]O 4334771:,b	B 
 
z3	8 
9 88AGGAJNAGGAJ/D
(d
3C
%djjm_ 5226**Q-	D 
 
z3	8 
9 88AGGAJ
+D771:>L
(d
NC	++7. 9))-Ar	;  
z3	8 
9	81 
9	8 
9	8 
9	8s$   >K K1L
K.1
K?
Lr   )r   r&         r   	n_classes)r&   r   r   r   c                 V   [         R                  R                  S5      n[        SUSUS9nX :  a  g UR	                  X5      n[         R
                  " [        U5      X-  S-   5      S U  nX1:  a  g [        U5      nUR                  Xg5        U[        US-
  U5      ::  a  [        U5      R                  SS9n	O@U[        X5      :  a  [        U5      R                  SS9n	O[        U5      R                  SS9n	U	R                  Xg5        [        UR                  U	R                  5        g )	Nr   r   r   )r   r   max_iterr   r   rs   r   r   )r   rI   rJ   r   rK   tileranger   r   min
set_paramsr   components_)
r   r   r   r   rP   nca_baser   r   r    	nca_others
             r"   test_auto_initr   
  s    ))


#C-,H  IIi,GGE)$i&<q&@A*9M$ /CGGAMs9q=*==!(O66E6B	J ::!(O66E6B	!(O66J6G	MM!%cooy7L7LMr$   c            	         [        SSSSSSS9u  p[        SSS9nUR                  X5        [        SSSSSSS9u  p1SUR                  S	    S
UR                  R                  S	    S3n[
        R                  " [        [        R                  " U5      S9   UR                  X15        S S S 5        g ! , (       d  f       g = f)Nr   r&   rl   r   )r   r   r   n_redundantn_informativer   T)
warm_startr   zThe new inputs dimensionality (r   zT) does not match the input dimensionality of the previously learned transformation (rp   rq   )
r	   r   r   rc   r   re   r|   r}   r~   r   )r   r   r    X_less_featuresr   s        r"   test_warm_start_validationr   -  s    DA )D1
ECGGAM,O */*?*?*B)C D??0034B	8 
 
z3	8# 
9	8	8s   B11
B?c                  Z   [        SSS9n U R                  [        [        5        U R                  nSU l        U R                  [        [        5        U R                  n[        SSS9nUR                  [        [        5        UR                  nSUl        UR                  [        [        5        UR                  n[        R                  " [        R                  " X!-
  5      5      n[        R                  " [        R                  " XT-
  5      5      nUS:  d   S5       eXv:  d   S5       eg )	NTr   )r   r   r   Fg      @zVTransformer changed significantly after one iteration even though it was warm-started.zfCold-started transformer changed less significantly than warm-started transformer after one iteration.)	r   r   	iris_datairis_targetr   r   r   sumrO   )nca_warmtransformation_warmtransformation_warm_plus_onenca_coldtransformation_coldtransformation_cold_plus_one	diff_warm	diff_colds           r"   test_warm_start_effectivenessr   K  s     .ANHLLK("..HLLK(#+#7#7 -QOHLLK("..HLLK(#+#7#7 rvv:PQRIrvv:PQRIs? 	5?
   	+ r$   	init_name)r   r   r   rI   precomputedc                 .   [         R                  R                  S5      n[        SSSSS9u  p4SnSU-   S	U-   S
.nU S:X  a-  UR	                  UR
                  S   UR
                  S   5      nOU n[        SUS9nUR                  X45        UR                  5       u  p[        R                  " SU	5      nU S
;   a(  [        R                  " X`   US   5      (       d   eUSS  nUS   S:X  d   eSR                  SSS5      nUS   SR                  U5      :X  d   eUS   SR                  S[        U5      -  5      :X  d   eUSS  H!  n[        R                  " SU5      (       a  M!   e   [        R                  " SUS   5      (       d   eUS   S:X  d   eg )Nr   r   r   r&   r   r   z... done in \ *\d+\.\d{2}szFinding principal componentsz&Finding most discriminative components)r   r   r   r   )verboser   z
+z [NeighborhoodComponentsAnalysis]z{:>10} {:>20} {:>10}	IterationzObjective ValuezTime(s)z#[NeighborhoodComponentsAnalysis] {}r   -r   zH\[NeighborhoodComponentsAnalysis\] *\d+ *\d\.\d{6}e[+|-]\d+\ *\d+\.\d{2}z@\[NeighborhoodComponentsAnalysis\] Training took\ *\d+\.\d{2}s\. )r   rI   rJ   r   rK   rc   r   r   
readouterrr~   splitrr   formatlen)r   capsysrP   r   r   regexp_initmsgsr   r    outr=   linesheaderlines                 r"   test_verboser   k  s    ))


#CA!!LDA/K-;7+ED M!yyQWWQZ0
(
>CGGAM FC HHUC E N"xxq2222ab	89999#**;8I9UF8<CCFKKKK8<CCC#f+DUVVVVa xx%
 
 	
 
  88Kb	    9??r$   c                     [        5       nUR                  [        [        5        U R	                  5       u  p#US:X  d   eg )Nr   )r   r   r   r   r   )r   r    r   r=   s       r"   test_no_verboser     s4    
(
*CGGI{# FC"99r$   c                     [         R                  5       n [        R                  5       nSn[        R                  " X:H  5      u  nSX'   X!US   '   [        SS9nUR                  X5        [        R                  " US:H  5      u  n[        R                  " US:H  5      u  nSX'   SXS   '   SX'   SXS   '   [        SS9nUR                  X5        [        R                  " US:H  5      u  n[        R                  " US:H  5      u  n[        R                  " US:H  5      u  nXS   US   US   /   n XS   US   US   /   n[        SSS9nUR                  X5        [        XR                  U 5      5        g )Nr   r   r   r   )r   r   )r   r   )	r   copyr   r   wherer   r   r   r   )r   r   singleton_classind_singletonr    ind_1ind_2ind_0s           r"   test_singleton_classr     s`   AA Oxx 45]A)mA
("
5CGGAM xxQHUxxQHUAHAAhKAHAAhK
("
5CGGAM xxQHUxxQHUxxQHU	8U1XuQx
()A	8U1XuQx
()A
(j2
FCGGAMq--*+r$   c                      [         [        S:H     n [        [        S:H     n[        SU R                  S   SS9nUR	                  X5        [        XR                  U 5      5        g )Nr   r   r   r   )r   r   r   )r   r   r   rc   r   r   r   )r   r   r    s      r"   test_one_classr     sW    +"#AK1$%A
(!''!*:C GGAMq--*+r$   c                    ^ SmU4S jn[        TUSS9nUR                  [        [        5        U R	                  5       u  p4SR                  TS-
  5      U;   d   eg )Nrt   c                    > U R                   [        R                   S   S-  4:X  d   eTU-
  n[        SR                  U5      5        g )Nr   r   {} iterations remaining...)rc   r   rN   r   )r;   r<   rem_iterr   s      r"   my_cbtest_callback.<locals>.my_cb  sF    ##	(:a(?'AAAAf$*11(;<r$   r   )r   r>   r   r   )r   r   r   r   r   r   )r   r   r    r   r=   r   s        @r"   test_callbackr     s[    H= )(UTU
VCGGI{# FC (..x!|<CCCr$   c                      [         n [        n " S S5      nU" X5      nUR                  n[        SUS9nUR	                  X5        UR
                  R                  U R                  S   S-  :X  d   eg)z4Test that the transformation has the expected shape.c                        \ rS rSrS rS rSrg)@test_expected_transformation_shape.<locals>.TransformationStoreri  c                 4   [        5       U l        [        R                  U R                  l        [        U R                  XSS9u  U l        n[        5       R                  U5      nUS S 2[        R                  4   U[        R                  S S 24   :H  U l
        g r,   )r   r0   r   r.   r1   r   r   r   r2   r3   r4   r5   s      r"   r7   Itest_expected_transformation_shape.<locals>.TransformationStorer.__init__  so     ;<DM$&FFDMM!%dmmQaPIDFA,,Q/A#$Q

]#3qQ7G#GD r$   c                     Xl         g)zKStores the last value of the transformation taken as input by
the optimizerN)r;   )r6   r;   r<   s      r"   r>   Itest_expected_transformation_shape.<locals>.TransformationStorer.callback  s
     #1r$   )r   r0   r4   r;   Nr@   rF   r$   r"   TransformationStorerr     s    	H	1r$   r   r&   )r   r>   r   r   N)r   r   r>   r   r   r;   sizerc   )r   r   r   transformation_storercbr    s         r"   "test_expected_transformation_shaper     sk    AA1 1 16		'	'B
(!b
ACGGAM //44
aGGGr$   c                  (   [        SSS9n U R                  R                  nSR                  U5      n[        R
                  " [        [        R                  " U5      S9   U R                  [        [        5        S S S 5        g ! , (       d  f       g = f)Nr   r   )r   r   z[{}] NCA did not convergerq   )r   	__class__rA   r   re   warnsr
   r~   r   r   r   r   )r    cls_namer   s      r"   test_convergence_warningr     s_    
(!Q
?C}}%%H
%
,
,X
6C	(		#	?	;' 
@	?	?s   B
Bzparam, valuer   r   d   tolra   c                 V    [        S0 X0D6n[        n[        nUR                  X45        g )NrF   )r   r   r   r   )paramvaluer    r   r   s        r"   test_parameters_valid_typesr     s(     )
:E>
:CAAGGAMr$   r   c                 j   [         n[        n[        U S9R                  X5      nUR	                  5       nUR
                  R                  R                  5       nU b  U nOUR                  S   n[        R                  " [        U5       Vs/ s H  ou U 3PM
     sn[        S9n[        XH5        gs  snf )zCheck `get_feature_names_out` for `NeighborhoodComponentsAnalysis`.

Non-regression test for:
https://github.com/scikit-learn/scikit-learn/issues/28293
rx   Nr   )dtype)r   r   r   r   get_feature_names_outr   rA   lowerrc   r   r   r   objectr   )	r   r   r   est	names_outclass_name_lowerexpected_n_featuresiexpected_names_outs	            r"   test_nca_feature_names_outr    s     	AA
(l
C
G
G
MC))+I}}--335*ggaj+01D+EF+EaaS	!+EF
 y5	 	Gs   B0)B__doc__r~   numpyr   re   numpy.testingr   r   scipy.optimizer   sklearnr   sklearn.datasetsr   r   r	   sklearn.exceptionsr
   sklearn.metricsr   sklearn.neighborsr   sklearn.preprocessingr   sklearn.utilsr   sklearn.utils.validationr   rP   irispermutationtargetr   permdatar   r   flags	writeablefinfofloatepsEPSr#   rT   ri   r   r   r   r   markparametrizer   r   r   r   r   r   r   r   r   r   int32float32r   r  rF   r$   r"   <module>r      s   
   G %  G G 1 . < . , 2{
t{{''(IIdO	kk$!	 #   hhuoX$'-T0212B* F=@ m4}5j17N 8 2 6 5N>$<@ D))X!,H,D"H6( 	!%	RXXc]#	

6"# $36 46r$   