
    >[g@                        d dl Z d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZmZmZmZ d dlmZmZmZ d dlmZ ej.                  j1                  d       Zej5                  d      Zej5                  d      Zeej;                  d	
      ddej<                  f   z  Zeej;                  d	
      ddej<                  f   z  Zdej>                  _         dej>                  _         ejB                  jE                  dg d      ejB                  jE                  dg d      ejB                  jE                  dd dg      d                      Z#ejB                  jE                  de      ejB                  jE                  dddg      ejB                  jE                  dg d      ejB                  jE                  dd dg      d                             Z$d Z%ejB                  jE                  de      d        Z&ejB                  jE                  dg d      ejB                  jE                  d e'd	d            d               Z(ejB                  jE                  dg d      d         Z)d! Z*d" Z+d# Z,d$ Z-d% Z.d& Z/d' Z0d( Z1ejB                  jE                  de      d)        Z2d* Z3d+ Z4d, Z5d- Z6d. Z7d/ Z8d0 Z9ejB                  jE                  d1e	e
eeg      d2        Z:d3 Z;y)4    N)make_classification)AdditiveChi2SamplerNystroemPolynomialCountSketch
RBFSamplerSkewedChi2Sampler)chi2_kernelkernel_metricspolynomial_kernel
rbf_kernel)assert_allcloseassert_array_almost_equalassert_array_equal)CSR_CONTAINERS),  2   size   axisFgamma)皙?r         @zdegree, n_components))r     )   r   )   i  coef0r   c                    t        t        t        | ||      }t        || ||d      }|j	                  t              }|j                  t              }t        j                  ||j                        }||z
  }	t        j                  t        j                  |	            dk  sJ t        j                  |	|	       t        j                  |	      dk  sJ t        j                  |	      dk  sJ y )N)r   degreer   *   )n_componentsr   r   r    random_state皙?outr   )r   XYr   fit_transform	transformnpdotTabsmeanmax)
r   r    r   r"   kernelps_transformX_transY_transkernel_approxerrors
             b/var/www/html/bid-api/venv/lib/python3.12/site-packages/sklearn/tests/test_kernel_approximation.pytest_polynomial_count_sketchr8   (   s     q!5uMF )!L ((+G$$Q'GFF7GII.M]"E66"''%.!T)))FF5e66%=C775>T!!!    csr_containerr         ?r    )r   r   r   c                 4   t        d| ||d      }|j                  t              }|j                  t              }t        d| ||d      }|j                   |t                    }|j                   |t                    }	t        ||       t        ||	       y)zZCheck that PolynomialCountSketch results are the same for dense and sparse
    input.
    r   r!   )r"   r   r    r   r#   N)r   r)   r'   r*   r(   r   )
r   r    r   r:   ps_denseXt_denseYt_dense	ps_sparse	Xt_sparse	Yt_sparses
             r7   )test_polynomial_count_sketch_dense_sparserC   E   s     %fEPRH %%a(H!!!$H%fEPRI ''a(89I##M!$45IHi(Hi(r9   c                 B    t        j                  | |j                        S )N)r+   r,   r-   )r'   r(   s     r7   _linear_kernelrE   ]   s    66!QSS>r9   c                 F   t         d d t        j                  d d f   j                         }t        t        j                  d d d d f   j                         }d|z  |z  ||z   z  }|j                  d      }t        d      }|j                  t               }|j                  t              }t        j                  ||j                        }t        ||d       |j                   | t                     }	|j                   | t                    }
t        ||	j                                t        ||
j                                t        j                         }d|d<   d}t        j                  t         |	      5  |j#                  |       d d d        y # 1 sw Y   y xY w)
Nr   r   r   sample_stepsr   r   r   z!Negative values in data passed tomatch)r'   r+   newaxiscopyr(   sumr   r)   r*   r,   r-   r   r   toarraypytestraises
ValueErrorfit)r:   X_Y_large_kernelr1   r*   r3   r4   r5   
X_sp_trans
Y_sp_transY_negmsgs                r7   test_additive_chi2_samplerr\   a   sH    
1bjj!		!	!	#B	
2::q!		!	!	#Br6B;"r'*L 1%F $3I%%a(G!!!$GFF7GII.MfmQ7((q)9:J$$]1%56Jw
 2 2 45w
 2 2 45 FFHEE$K
-C	z	-e 
.	-	-s   <FF method)rT   r)   r*   rH      c                     t        |      } t        ||       t               d}t        ||      } t        ||       t               |j                  |k(  sJ y)zsCheck that the input sample step doesn't raise an error
    and that sample interval doesn't change after fit.
    rG   g      ?)rH   sample_intervalN)r   getattrr'   r`   )r]   rH   transformerr`   s       r7   'test_additive_chi2_sampler_sample_stepsrc      s[     &<@K GK #O%!'K !GK #&&/999r9   c                     t        d      }t        j                  d      }t        j                  t
        |      5   t        ||       t               ddd       y# 1 sw Y   yxY w)z8Check that we raise a ValueError on invalid sample_stepsr^   rG   zHIf sample_steps is not in [1, 2, 3], you need to provide sample_intervalrK   N)r   reescaperQ   rR   rS   ra   r'   )r]   rb   r[   s      r7   -test_additive_chi2_sampler_wrong_sample_stepsrg      sL     &15K
))RC 
z	-$V$Q' 
.	-	-s   AA&c                     d} t         j                         }|  dz  |d<   t        | z   d d t        j                  d d f   }|| z   t        j                  d d d d f   }t        j
                  |      dz  t        j
                  |      dz  z   t        j
                  d      z   t        j
                  ||z         z
  }t        j                  |j                  d            }t        | dd      }|j                  t              }|j                  |      }t        j                  ||j                        }	t        ||	d	       t        j                  |      j                         sJ d
       t        j                  |	      j                         sJ d       |j                         }
|  dz  |
d<   d}t!        j"                  t$        |      5  |j                  |
       d d d        y # 1 sw Y   y xY w)NgQ?g       @rJ   r   r     r!   )
skewednessr"   r#   r   zNaNs found in the Gram matrixz)NaNs found in the approximate Gram matrixz2X may not contain entries smaller than -skewednessrK   )r(   rN   r'   r+   rM   logexprO   r   r)   r*   r,   r-   r   isfiniteallrQ   rR   rS   )crV   X_cY_c
log_kernelr1   r*   r3   r4   r5   rZ   r[   s               r7   test_skewed_chi2_samplerrs      s    	A 
BrCxBtH q5!RZZ"
#C62::q!#
$C
 
s	rvvc{S01BFF3K?"&&sBSS  VVJNNN*+F "QTPRSI%%a(G!!"%GFF7GII.MfmQ7;;v""$E&EE$;;}%))+X-XX+ GGIE"s(E$K
>C	z	-E" 
.	-	-s   G))G2c                  x   t               } t        j                         }d|d<   t        j                  t
        d      5  | j                  |       ddd       t        j                  t
        d      5  | j                  t               | j                  |       ddd       y# 1 sw Y   TxY w# 1 sw Y   yxY w)zEnsures correct error messagerI   rJ   zX in AdditiveChi2SamplerrK   N)r   r'   rN   rQ   rR   rS   rT   r*   )rb   X_negs     r7   %test_additive_chi2_sampler_exceptionsrv      s    %'KFFHEE$K	z)C	D 
E	z)C	De$ 
E	D 
E	D	D	Ds   B$4'B0$B-0B9c                     d} t        t        t        |       }t        | dd      }|j	                  t              }|j                  t              }t        j                  ||j                        }||z
  }t        j                  t        j                  |            dk  sJ t        j                  ||       t        j                  |      dk  sJ t        j                  |      d	k  sJ y )
Ng      $@r   ri   r!   )r   r"   r#   g{Gz?r%   r   r$   )r   r'   r(   r   r)   r*   r+   r,   r-   r.   r/   r0   )r   r1   rbf_transformr3   r4   r5   r6   s          r7   test_rbf_samplerrz      s     E1E*F UBOM))!,G%%a(GFF7GII.M]"E66"''%.!T)))FF5e66%=C775>T!!!r9   c                     t               }t        j                  ddgddgddgg|       }|j                  |       |j                  j
                  | k(  sJ |j                  j
                  | k(  sJ y	zRCheck that the fitted attributes are stored accordingly to the
    data type of X.r   r   r   r^         dtypeN)r   r+   arrayrT   random_offset_r   random_weights_)global_dtyperbfr'   s      r7   (test_rbf_sampler_fitted_attributes_dtyper      sm     ,C
1a&1a&1a&)>AGGAJ##|333$$444r9   c                     t        d      } t        j                  ddgddgddggt        j                  	      }| j	                  |       t        d      }t        j                  ddgddgddggt        j
                  	      }|j	                  |       t        | j                  |j                         t        | j                  |j                         y
z?Check the equivalence of the results with 32 and 64 bits input.r!   )r#   r   r   r   r^   r}   r~   r   N)	r   r+   r   float32rT   float64r   r   r   )rbf32X32rbf64X64s       r7   "test_rbf_sampler_dtype_equivalencer      s    B'E
((QFQFQF+2::
>C	IIcNB'E
((QFQFQF+2::
>C	IIcNE((%*>*>?E))5+@+@Ar9   c                      dgdggddg}} t        d      }|j                  | |       |j                  t        j                  d      k(  sJ y)	z4Check the inner value computed when `gamma='scale'`.g        r;   r   r   scalerx   r^   N)r   rT   _gammarQ   approx)r'   yr   s      r7   test_rbf_sampler_gamma_scaler     sI    EC5>Aq6qA
7
#CGGAqM::q))))r9   c                     t               }t        j                  ddgddgddgg|       }|j                  |       |j                  j
                  | k(  sJ |j                  j
                  | k(  sJ yr|   )r   r+   r   rT   r   r   r   )r   skewed_chi2_samplerr'   s      r7   0test_skewed_chi2_sampler_fitted_attributes_dtyper     sr     ,-
1a&1a&1a&)>AA--33|CCC..44DDDr9   c                     t        d      } t        j                  ddgddgddggt        j                  	      }| j	                  |       t        d      }t        j                  ddgddgddggt        j
                  	      }|j	                  |       t        | j                  |j                         t        | j                  |j                         y
r   )	r   r+   r   r   rT   r   r   r   r   )skewed_chi2_sampler_32X_32skewed_chi2_sampler_64X_64s       r7   *test_skewed_chi2_sampler_dtype_equivalencer     s    .B?88aVaVaV,BJJ?Dt$.B?88aVaVaV,BJJ?Dt$--/E/T/T ..0F0V0Vr9   c                 j   ddgddgddgg}t               j                  |      j                  |       t               j                  |      j                  |       t	               j                  |      j                  |        | |      }t	               j                  |      j                  |       y )Nr   r   r   r^   r}   r~   )r   rT   r*   r   r   )r:   r'   s     r7   test_input_validationr   -  s     Q!Q!Q Aa **1-A((+LQ!!!$aALQ!!!$r9   c                     t         j                  j                  d      } | j                  d      }t	        |j
                  d         j                  |      }t        |      }t        t        j                  ||j                        |       t	        d|       }|j                  |      j                  |      }|j
                  |j
                  d   dfk(  sJ t	        dt        |       }|j                  |      j                  |      }|j
                  |j
                  d   dfk(  sJ t               }|D ]P  }t	        d||       }|j                  |      j                  |      }|j
                  |j
                  d   dfk(  rPJ  y )Nr   
   r^   r   r"   r   r"   r#   )r"   r1   r#   )r+   randomRandomStateuniformr   shaper)   r   r   r,   r-   rT   r*   rE   r
   )rndr'   X_transformedKtranskernels_availablekerns          r7   test_nystroem_approximationr   :  sN   
))


"C!A !''!*5CCAFM1Abff]MOODaH!#6EIIaL**1-M1771:q/111 !NMEIIaL**1-M1771:q/111 '(!a3G		!..q1""qwwqz1o555 "r9   c                     t         j                  j                  d      } | j                  d      }t	        d      }|j                  |      }t        |d       }t        j                  ||j                        }t        ||       t	        dd      }|j                  |      }t        |d	      }t        j                  ||j                        }t        ||       y )
Nr!   r   r   r   r   rx   chi2r1   r"   r   )r+   r   r   r   r   r)   r   r,   r-   r   r	   )r   r'   nystroemr   r   K2s         r7    test_nystroem_default_parametersr   U  s    
))


#C!A R(H**1-M1D!A	}	/Ba$ vB7H**1-MAQA	}	/Ba$r9   c                     t         j                  j                  d      } | j                  dd      }t        j                  |gdz        }d}t        ||j                  d         j                  |      }|j                  |      }t        ||      }t        |t        j                  ||j                               t        j                  t        j                  t                    sJ y )Nr   r      r   d   )r   r"   rx   )r+   r   r   randvstackr   r   rT   r*   r   r   r,   r-   rn   rm   r(   )rngr'   r   Nr   r   s         r7   test_nystroem_singular_kernelr   i  s    
))


"CRA
		1#'AEu1771:6::1=AKKNM1E"Aa}!GH66"++a.!!!r9   c                  0   t         j                  j                  d      } | j                  d      }t	        |dd      }t        d|j                  d   dd	      }|j                  |      }t        t        j                  ||j                        |       y )
N%   r   r   g@r   r    r   
polynomialr   )r1   r"   r    r   )r+   r   r   r   r   r   r   r)   r   r,   r-   )r   r'   r   r   r   s        r7    test_nystroem_poly_kernel_paramsr   y  sz    
))


#C!A!Cs3A!''!*SH **1-Mbff]MOODaHr9   c                     t         j                  j                  d      } d}| j                  |df      }d }g }t	        |      }t        ||dz
  d|i      j                  |       t        |      ||dz
  z  d	z  k(  sJ d
}ddiddidd	if}|D ]L  }t        dt        |dz
  d|}t        j                  t        |      5  |j                  |       d d d        N y # 1 sw Y   YxY w)Nr!   r   r^   r   c                 l    |j                  d       t        j                  | |      j                         S )z&Histogram kernel that writes to a log.r   )appendr+   minimumrO   )xr   rk   s      r7   logging_histogram_kernelz8test_nystroem_callable.<locals>.logging_histogram_kernel  s'    

1zz!Q##%%r9   r   rk   )r1   r"   kernel_paramsr   -Don't pass gamma, coef0 or degree to Nystroemr   r   r    r   rK    )r+   r   r   r   listr   rT   lenrE   rQ   rR   rS   )	r   	n_samplesr'   r   
kernel_logr[   paramsparamnys	            r7   test_nystroem_callabler     s    
))


#CI)Q(A&
 JQA'!mj) 
c!fz?i9q=9A==== :ClWaL8Q-8FS^9q=SUS]]:S1FF1I 21 11s   C  C)	c                     t         j                  j                  d      } | j                  d      }t	        |dd      }t        d|j                  d   	      }|j                  |      }t        t        j                  ||j                        |       d
}ddiddiddif}|D ]R  }t        dd|j                  d   d	|}t        j                  t        |      5  |j                  |       d d d        T y # 1 sw Y   _xY w)N   r   r   r   r   r   precomputedr   r   r   r   r   r   r    rK   r   )r+   r   r   r   r   r   r   r)   r   r,   r-   rQ   rR   rS   rT   )	r   r'   r   r   r   r[   r   r   r   s	            r7    test_nystroem_precomputed_kernelr     s     ))


#C!A!AS1A}1771:FH**1-Mbff]MOODaH :ClWaL8Q-8FM]MuM]]:S1FF1I 21 11s   C::D	c                      t        dd      \  } }t        dd      }|j                  |        |j                  j                  dk(  sJ y)	zCheck that `component_indices_` corresponds to the subset of
    training points used to construct the feature map.
    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/20474
    r   r   )r   
n_featuresr   r   r   )r   N)r   r   rT   component_indices_r   )r'   _feature_map_nystroems      r7   test_nystroem_component_indicesr     sO     <DAq# Q2288EAAAr9   	Estimatorc                 *    |        j                  t              }|j                  t              }|j                         }| j                  j                         }t        |j                  d         D cg c]  }| | 
 }}t        ||       yc c}w )zCheck get_feature_names_outr   N)	rT   r'   r*   get_feature_names_out__name__lowerranger   r   )r   estr3   	names_out
class_nameiexpected_namess          r7   test_get_feature_names_outr     s    
 +//!
CmmAG))+I##))+J27a8H2IJ2IQQC(2INJy.1 Ks   4Bc                     t         j                  j                  d      } | j                  d      }t	        d      j                  |      }g d}g d}|j                  |      }|D cg c]  }d	| 	 }}t        ||       y
c c}w )z4Check get_feature_names_out for AdditiveChi2Sampler.r   )r   r   r   r   rG   )f0f1f2)f0_sqrtf1_sqrtf2_sqrtf0_cos1f1_cos1f2_cos1f0_sin1f1_sin1f2_sin1f0_cos2f1_cos2f2_cos2f0_sin2f1_sin2f2_sin2)input_featuresadditivechi2sampler_N)r+   r   r   random_sampler   rT   r   r   )r   r'   chi2_samplerinput_namessuffixesr   suffixr   s           r7   .test_additivechi2sampler_get_feature_names_outr    s    
))


"Cx(A&A6::1=L$KH$ 22+2NIDLMH&,VH5HNMy.1 Ns   +B)<re   numpyr+   rQ   sklearn.datasetsr   sklearn.kernel_approximationr   r   r   r   r   sklearn.metrics.pairwiser	   r
   r   r   sklearn.utils._testingr   r   r   sklearn.utils.fixesr   r   r   r   r   r'   r(   rO   rM   flags	writeablemarkparametrizer8   rC   rE   r\   r   rc   rg   rs   rv   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r9   r7   <module>r     s   	   0   
 / 	iiA9%9% QUUU]1bjj=! ! QUUU]1bjj=! !    -0/1PQ1c(+" , R 1"4 .93*-9-1c(+) , . . :)( .9! :!H #HIq!5: 6 J:  #HI( J(&#R	%"&
5B*
E$ .9	% :	%66%(" 
I:(B '5FQ222r9   