
    0Ph$Q                        d dl Z d dlZd dlZd dlZd dl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 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 Z d Z!d Z"d Z#ej$        %                    de          d             Z&ej$        %                    dg d          ej$        %                    dddg          d                         Z'd Z(d Z)d Z*d Z+ej$        %                    dg d          d             Z,d Z-d Z.d  Z/d! Z0ej$        %                    dg d          d"             Z1ej$        %                    d#g d$          d%             Z2d& Z3d' Z4d( Z5d) Z6d* Z7d+ Z8dS ),    N)	load_iris
make_blobsmake_circles)PCA	KernelPCA)NotFittedError)
Perceptron)
rbf_kernel)GridSearchCV)Pipeline)StandardScaler)assert_allcloseassert_array_almost_equalassert_array_equal)CSR_CONTAINERS)_check_psd_eigenvaluesc                     t           j                            d          } |                     d          }|                     d          }d }dD ]}ddd|fD ] }t	          |           }t          d	|||
          }|                    |          }|                    |                              |          }	t          t          j
        |          t          j
        |	                     |j        dk    sJ |                    |          }
|
j        d         |j        d         k    sJ |r'|                    |
          }|j        |j        k    sJ dS )zNominal test for all solvers and all known kernels + a custom one

    It tests
     - that fit_transform is equivalent to fit+transform
     - that the shapes of transforms and inverse transforms are correct
    r            r   c                 `    |i k    sJ t          j        | |                                          S )N)npminimumsum)xykwargss      k/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/decomposition/tests/test_kernel_pca.py	histogramz"test_kernel_pca.<locals>.histogram#   s-    ||||z!Q##%%%    autodensearpack
randomizedlinearrbfpolyr   )kerneleigen_solverfit_inverse_transform   N)r   randomRandomStaterandom_samplecallabler   fit_transformfit	transformr   abssizeshapeinverse_transform)rngX_fitX_predr!   r,   r+   invkpcaX_fit_transformedX_fit_transformed2X_pred_transformedX_pred2s               r    test_kernel_pcarC      s    )


"
"Cf%%Ev&&F& & &
 B 5 5	: 	5 	5F v&&&C &|SV  D !% 2 25 9 9!%%!:!:5!A!A%())262D+E+E   %)Q.... "&!7!7%+A.2C2I!2LLLLL  5001CDD}44445	55 5r"   c                      t          ddd          } d}t          j        t          |          5  |                     t
          j                            dd                     ddd           dS # 1 swxY w Y   dS )zCheck that kPCA raises an error if the parameters are invalid

    Tests fitting inverse transform with a precomputed kernel raises a
    ValueError.
    
   Tprecomputed)n_componentsr-   r+   z6Cannot fit_inverse_transform with a precomputed kernelmatchN)r   pytestraises
ValueErrorr4   r   r/   randn)	estimatorerr_mss     r    "test_kernel_pca_invalid_parametersrP   F   s     tM  I FF	z	0	0	0 / /bioob"--.../ / / / / / / / / / / / / / / / / /s   4A11A58A5c                  d   t           j                            d          } |                     dd          }t	          |                               |          }|                    |          }|                                }d|dddf<   |                    |          }t          ||           dS )zCheck robustness to mutations in the original training array

    Test that after fitting a kPCA model, it stays independent of any
    mutation of the values of the original data object by relying on an
    internal copy.
    r   rE   )random_statei  N)	r   r/   r0   randr   r4   r5   copyr   )stateXr>   transformed1X_copytransformed2s         r    $test_kernel_pca_consistent_transformrZ   T   s     I!!!$$E

2rA%(((,,Q//D>>!$$LVVXXFAaaadG>>&))LlL99999r"   c            
         t           j                            d          } |                     dd          }d}|D ]}t          j        d          }t          d          D ]6}t          d||           }|                    |          d         ||ddf<   7t          |t          j	        |dddf         d          
                    dd                     dS )	zTest that Kernel PCA produces deterministic output

    Tests that the same inputs and random state produce the same output.
    r   rE   )r&   r%   )   r   r\   r   )rG   r,   rR   N)r   r/   r0   rS   zerosranger   r3   r   tilereshape)r:   rV   r,   solvertransformed_Xir>   s          r    $test_kernel_pca_deterministic_outputrd   g   s    
 )


"
"CRA&L X X))r 	; 	;A!&sSSSD"&"4"4Q"7"7":M!QQQ$rw}QT/BB'G'G'O'OPRTU'V'VWWWWX Xr"   csr_containerc           	         t           j                            d          } | |                    d                    } | |                    d                    }dD ]}dD ]}t	          d||dd          }|                    |          }|                    |                              |          }t          t          j	        |          t          j	        |                     |                    |          }	|	j
        d	         |j
        d	         k    sJ t          j        t                    5  |                    |	           d
d
d
           n# 1 swxY w Y   
d
S )zTest that kPCA works on a sparse data input.

    Same test as ``test_kernel_pca except inverse_transform`` since it's not
    implemented for sparse matrices.
    r   r   r   )r$   r&   r'   )r(   r)   r*   r   F)r+   r,   r-   rR   r.   N)r   r/   r0   r1   r   r3   r4   r5   r   r6   r8   rJ   rK   r   r9   )
re   r:   r;   r<   r,   r+   r>   r?   r@   rA   s
             r    test_kernel_pca_sparserg   x   s    )


"
"CM#++F3344E]3,,V4455F8 ; ;/ 	; 	;F)&+  D !% 2 25 9 9!%%!:!:5!A!A%())262D+E+E  
 "&!7!7%+A.2C2I!2LLLLL
 ~.. ; ;&&'9:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ;-	;; ;s   7EE Era   r#   
n_featuresr   rE   c           
          t           j                            d          }|                    d|f          }|                    d|f          }| dk    rdnd}t	          t          j        t          ||                               |                              |                    t          j        t          || dk    r| nd	
                              |                              |                               dS )zTest that kPCA with linear kernel is equivalent to PCA for all solvers.

    KernelPCA with linear kernel should produce the same output as PCA.
    r   r   r   r&      r   )r,   r%   full)
svd_solverN)
r   r/   r0   r1   r   r6   r   r4   r5   r   )ra   rh   r:   r;   r<   n_compss         r    test_kernel_pca_linear_kernelrn      s     )


"
"Cq*o..E://F X%%aa1G
yv666::5AAKKFSSTT
f.?.?FFVLLLSZZYv	
 	
    r"   c                  <   t           j                            d          } |                     d          }|                     d          }dD ]O}dD ]J}t	          ||          }|                    |                              |          j        }|d|fk    sJ KPdS )	zTest that `n_components` is correctly taken into account for projections

    For all solvers this tests that the output has the correct shape depending
    on the selected number of components.
    r   r   r   r%   r&   r'   )r.   r   r   rG   r,   r   N)r   r/   r0   r1   r   r4   r5   r8   )r:   r;   r<   r,   cr>   r8   s          r    test_kernel_pca_n_componentsrs      s     )


"
"Cf%%Ev&&F9 # # 	# 	#A!,GGGDHHUOO--f55;EQF?????		## #r"   c                  j   t          j        ddgddgddgg          } t                      }|                    |           }|j        dk    sJ t          d          }|                    |           }|j        dk    sJ t          dd          }|                    |           }|j        dk    sJ d	S )
zCheck that the ``remove_zero_eig`` parameter works correctly.

    Tests that the null-space (Zero) eigenvalues are removed when
    remove_zero_eig=True, whereas they are not by default.
          ?r.   )rj   r   r   rG   )rj   r   T)rG   remove_zero_eigN)r   arrayr   r3   r8   )rV   r>   Xts      r    test_remove_zero_eigrz      s     	9a.1a&1i.9::A ;;D			A		B8v!$$$D			A		B8v!T:::D			A		B8vr"   c                  &   t          j        ddgddgg          } t          j                    5  t          j        dt
                     t          j        d          5  t          ddd	          }|                    |           	                    |           }|
                    |           }t          t          j        |          t          j        |                     d
d
d
           n# 1 swxY w Y   d
d
d
           d
S # 1 swxY w Y   d
S )zNon-regression test for issue #12141 (PR #12143)

    This test checks that fit().transform() returns the same result as
    fit_transform() in case of non-removed zero eigenvalue.
    r.   r   errorwarn)allr   Fr%   )rG   rw   r,   N)r   rx   warningscatch_warningssimplefilterRuntimeWarningerrstater   r4   r5   r3   r   r6   )r;   kABs       r    test_leave_zero_eigr      st    Hq!fq!f%&&E 
	 	"	" < <
 	g~666[V$$$ 	< 	<q%gVVVAe&&u--A&&A%bfQii;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<< < < < < < < < < < < < < < < < < <s6   0DBC."D.C2	2D5C2	6DD
D
c                     t           j                            d          } |                     d          }|                     d          }dD ]}t	          d|d                              |                              |          }t	          d|dd                              t          j        ||j                                                t          j        ||j                            }t	          d|dd          	                    t          j        ||j                            }t	          d|dd                              t          j        ||j                                                t          j        ||j                            }t          t          j        |          t          j        |                     t          t          j        |          t          j        |                     d	S )
z?Test that kPCA works with a precomputed kernel, for all solversr   r   r   rp   r   r,   rR   rF   )r,   r+   rR   N)r   r/   r0   r1   r   r4   r5   dotTr3   r   r6   )r:   r;   r<   r,   X_kpcaX_kpca2X_kpca_trainX_kpca_train2s           r    test_kernel_pca_precomputedr      s   
)


"
"Cf%%Ev&&F9 O OalCCCSZZYv 	 ]QR   Suw''((Yrvfeg..// 	 !LQ
 
 

-ueg..
/
/ 	
 ]QR   Suw''((YrveUW--.. 	 	""&.."&//BBB!"&"6"6}8M8MNNNN;O Or"   c                 .   ddgddgg}t          d| dd          }|                    |           dd	gd	dgg}t          d| dd          }|                    |           t          |j        |j                   t          |j        |j                   d
S )zCheck that the kernel centerer works.

    Tests that a non symmetric precomputed kernel is actually accepted
    because the kernel centerer does its job correctly.
    r.   r   rj   (   rF   r   )r+   r,   rG   rR   	   iN)r   r4   r   eigenvectors_eigenvalues_)ra   Kr>   Kckpca_cs        r    )test_kernel_pca_precomputed_non_symmetricr     s     Q!RA6PQ  D 	HHQKKK b'B7	B6PQ  F JJrNNN t)6+?@@@t(&*=>>>>>r"   c                  P   t          dddd          \  } }t          dd          }t          d	|fd
t          d          fg          }t	          dt          j        dd          z            }t          |d|          }|                    | |           |j	        dk    sJ dS )zCheck that kPCA works as expected in a grid search pipeline

    Test if we can do a grid-search to find parameters to separate
    circles with a perceptron model.
      333333?皙?r   	n_samplesfactornoiserR   r)   r   r+   rG   
kernel_pcar	   r   max_iter       @)kernel_pca__gammarj   cv
param_gridr.   N)
r   r   r   r	   dictr   aranger   r4   best_score_)rV   r   r>   pipeliner   grid_searchs         r    test_gridsearch_pipeliner   :  s     #cANNNDAqE222D,-jRS>T>T>T/UVWWHryQ/?/?(?@@@JxA*EEEKOOAq"a''''''r"   c                  l   t          dddd          \  } }t          dd          }t          d	|fd
t          d          fg          }t	          t          j        dd                    }t          |d|          }t          | d          }|	                    ||           |j
        dk    sJ dS )zCheck that kPCA works as expected in a grid search pipeline (2)

    Test if we can do a grid-search to find parameters to separate
    circles with a perceptron model. This test uses a precomputed kernel.
    r   r   r   r   r   rF   r   r   r   r	   r   r   r.   )Perceptron__max_iterrj   r   r   )gammaN)r   r   r   r	   r   r   r   r   r
   r4   r   )rV   r   r>   r   r   r   X_kernels          r    $test_gridsearch_pipeline_precomputedr   I  s     #cANNNDAqM:::D,-jRS>T>T>T/UVWWH29Q??;;;JxA*EEEK!3'''HOOHa   "a''''''r"   c                     t          dddd          \  } }t          d                              | |                              | |          }|dk     sJ t	          d	d
dd          }|                    |           }t          d                              ||                              ||          }|dk    sJ dS )a  Check that kPCA projects in a space where nested circles are separable

    Tests that 2D nested circles become separable with a perceptron when
    projected in the first 2 kPCA using an RBF kernel, while raw samples
    are not directly separable in the original space.
    r   r   r   r   r   r   r   g?r)   r   Tr   )r+   rG   r-   r   ru   N)r   r	   r4   scorer   r3   )rV   r   train_scorer>   r   s        r    test_nested_circlesr   Y  s     #cANNNDAq a(((,,Q2288A>>K 1D  D ""F a(((,,VQ77==faHHK#r"   c                  
   ddgddgddgg} t          ddd	          }|                    |            |j                                        dk    sJ t	          j        |j        t          |j                  k              sJ d
S )z}Check that ``_check_psd_eigenvalues`` is correctly called in kPCA

    Non-regression test for issue #12140 (PR #12145).
    r   r.   gw̫   @g:0yE>r   r(   r   T)r+   rG   r-   N)r   r4   r   minr   r~   r   rV   r>   s     r    test_kernel_conditioningr   u  s     Q(D!Ha=1AH1DQQQDHHQKKK   ""a''''6$#'=d>O'P'PPQQQQQQQr"   c           	         g dg dg dg dg dg dg dg dg}t          d	| d
          }t          j        t          d          5  |                    |           ddd           n# 1 swxY w Y   t          d	| d          }| dk    rKt          j        t          d          5  |                    |           ddd           dS # 1 swxY w Y   dS |                    |           dS )a  Check how KernelPCA works with non-PSD kernels depending on n_components

    Tests for all methods what happens with a non PSD gram matrix (this
    can happen in an isomap scenario, or with custom kernel functions, or
    maybe with ill-posed datasets).

    When ``n_component`` is large enough to capture a negative eigenvalue, an
    error should be raised. Otherwise, KernelPCA should run without error
    since the negative eigenvalues are not selected.
    )gQ@      p=
# @p=
ף@r   r   
ףp=
(\))r   gQ      @ףp=
r   r   Q(\@)r   r   g(\.@Q @r   r   333333&{G:7)r   r   r         @333333r   RQ?)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   gq=
ףp@     #@)r   r   r   r   r   r   r   g(\u5@rF      )r+   r,   rG   z*There are significant negative eigenvaluesrH   Nr   r'   )r   rJ   rK   rL   r4   )ra   r   r>   s      r    test_precomputed_kernel_not_psdr     s     	<;;<<<;;;:::::::::;;;===		A MQOOOD	z)U	V	V	V                 MQOOOD ]J
 
 
 	 	 HHQKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s$   A**A.1A.(CCCrG   )r   rE   r\   c                    d\  }}t          ||z   ddd          \  }}|d|ddf         ||dddf         }}t          | dd                              |                              |          }t          | d	d                              |                              |          }t	          t          j        |          t          j        |                     t          | d
d                              |                              |          }	t	          t          j        |	          t          j        |                     dS )zGCheck that 'dense' 'arpack' & 'randomized' solvers give similar results)i  d   r   r   r   r   Nr%   r   r&   r'   )r   r   r4   r5   r   r   r6   )
rG   n_trainn_testrV   _r;   r<   ref_preda_predr_preds
             r    #test_kernel_pca_solvers_equivalencer     s?   
 !OGVV#S1  DAq hwhkNAghhkN6E 	,W1EEE	U	6		  	,XAFFF	U	6		  bfVnnbfX.>.>??? 	,\JJJ	U	6		  bfVnnbfX.>.>?????r"   c                  8   t          ddd          ^} }t          dddd	          }|                    |           }|                    |          }t          j                            | |z
            t          j                            |           z  d
k     sJ dS )zTest if the reconstruction is a good approximation.

    Note that in general it is not possible to get an arbitrarily good
    reconstruction because of kernel centering that does not
    preserve all the information of the original data.
    r   r   r   r   rh   rR   r\   r)   TgMbP?)rG   r+   r-   alpha皙?N)r   r   r3   r9   r   linalgnorm)rV   r   r>   X_trans	X_reconsts        r    0test_kernel_pca_inverse_transform_reconstructionr     s     CCCEAT  D   ##G&&w//I9>>!i-((29>>!+<+<<tCCCCCCr"   c                  @   t           j                            d                              dd          } t	                      }|                    |            t          j        t                    5  |	                    |            d d d            d S # 1 swxY w Y   d S )N   r   rj   )
r   r/   rM   r`   r   r4   rJ   rK   r   r9   r   s     r    &test_kernel_pca_raise_not_fitted_errorr     s    
	##Aq))A;;DHHQKKK	~	&	& " "q!!!" " " " " " " " " " " " " " " " " "s   0BBBc                  t   t          dg dg dgdd          \  } }t                                          |           } | |                                 z  } t	                      }|                    |           j        |                    |                     t          j                            j        k    sJ dS )zTest that the decomposition is similar for 32 and 64 bits data

    Non regression test for
    https://github.com/scikit-learn/scikit-learn/issues/18146
       )r   r   r   )r.   r.   r.   r   r   )r   centersrR   cluster_stdN)	r   r   r3   r   r   r8   astyper   float32)rV   r   r>   s      r    test_32_64_decomposition_shaper     s     yyy)))41RU  DAq 	&&q))ALA ;;Da  &$*<*<QXXbj=Q=Q*R*R*XXXXXXXr"   c                      t          ddd          ^} }t          d                              |           }|                                }t	          d t          d          D             |           dS )	z&Check feature names out for KernelPCA.r   r   r   r   r   rv   c                     g | ]}d | S )	kernelpca ).0rc   s     r    
<listcomp>z5test_kernel_pca_feature_names_out.<locals>.<listcomp>  s    :::AA:::r"   N)r   r   r4   get_feature_names_outr   r^   )rV   r   r>   namess       r    !test_kernel_pca_feature_names_outr     st    CCCEA!$$$((++D&&((E::q:::EBBBBBr"   c                     t           j                            d          } |                     d          }d| ddd}d|j        d         z  }t          d
dd	i|                    |          }t          d
d|i|                    |          }|j        |k    sJ |j        |k    sJ |                    |	                    |                    }|                    |	                    |                    }t          ||           d	S )z]Check that gamma is set correctly when not provided.

    Non-regression test for #26280
    r   r   r   Tr)   )rG   rR   r-   r+   r.   r   Nr   )r   r/   r0   r1   r8   r   r4   gamma_r9   r5   r   )r:   rV   r   expected_gammakpca1kpca2X1_reconX2_recons           r    %test_kernel_pca_inverse_correct_gammar     s   
 )


"
"C&!!A !%	 F ^N++D+F++//22E55N5f5599!<<E<>))))<>))))&&uq'9'9::H&&uq'9'9::HHh'''''r"   c                      t          j        d           t          dd          \  } }t          j        d          5  t          dd                              |            ddd           dS # 1 swxY w Y   dS )	zCheck that KernelPCA works with pandas output when the solver is arpack.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/27579
    pandasT)as_frame
return_X_y)transform_outputr   r&   rq   N)rJ   importorskipr   sklearnconfig_contextr   r3   )rV   r   s     r    test_kernel_pca_pandas_outputr  -  s     !!!dt444DAq			:	:	: J Jqx888FFqIIIJ J J J J J J J J J J J J J J J J Js   %A00A47A4)9r   numpyr   rJ   r  sklearn.datasetsr   r   r   sklearn.decompositionr   r   sklearn.exceptionsr   sklearn.linear_modelr	   sklearn.metrics.pairwiser
   sklearn.model_selectionr   sklearn.pipeliner   sklearn.preprocessingr   sklearn.utils._testingr   r   r   sklearn.utils.fixesr   sklearn.utils.validationr   rC   rP   rZ   rd   markparametrizerg   rn   rs   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r"   r    <module>r     s          @ @ @ @ @ @ @ @ @ @ 0 0 0 0 0 0 0 0 - - - - - - + + + + + + / / / / / / 0 0 0 0 0 0 % % % % % % 0 0 0 0 0 0         
 / . . . . . ; ; ; ; ; ;+5 +5 +5\/ / /: : :&X X X" .99"; "; :9";J #L#L#LMM2w//  0/ NM0# # #$  ,< < <2#O #O #OL #L#L#LMM? ? NM?4( ( (( ( (   8R R R  #L#L#LMM0 0 NM0f 55!@ !@ 65!@HD D D"" " "Y Y Y"C C C( ( (8	J 	J 	J 	J 	Jr"   