U
    3d?                     @   sL  d dl Z d dlZd dlmZ d dl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 d d
lmZ d dlmZ d dlmZ d dl
mZmZmZ ejd ZejddZejddZeejddddejf  Zeejddddejf  Zej ddddgej ddddgej dd dgdd Z!ej dddgej ddddgej dd dgd d! Z"d"d# Z#d$d% Z$d&d' Z%d(d) Z&d*d+ Z'd,d- Z(d.d/ Z)d0d1 Z*d2d3 Z+d4d5 Z,d6d7 Z-d8d9 Z.d:d; Z/d<d= Z0d>d? Z1d@dA Z2dBdC Z3dDdE Z4ej dFeeeegdGdH Z5dIdJ Z6dS )K    N)
csr_matrix)assert_array_equal)assert_array_almost_equal)assert_allclose)kernel_metrics)
RBFSampler)AdditiveChi2Sampler)SkewedChi2Sampler)Nystroem)PolynomialCountSketch)make_classification)polynomial_kernel
rbf_kernelchi2_kernel),  2   size   Zaxisgamma皙?g      @zdegree, n_components)r     )   r   )   i  coef0c           
      C   s   t tt| ||d}t|| ||dd}|t}|t}t||j}|| }	t	t
|	dksfttj	|	|	d t|	dkstt
|	dkstd S )N)r   degreer   *   )n_componentsr   r   r   random_state皙?outr   )r   XYr   fit_transform	transformnpdotTabsmeanAssertionErrormax)
r   r   r   r   kernelZps_transformX_transY_transkernel_approxerror r3   K/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/tests/test_kernel_approximation.pytest_polynomial_count_sketch   s     

r5         ?r   r   r   c           	      C   sl   t d| ||dd}|t}|t}t d| ||dd}|tt}|tt}t|| t|| dS )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   Zps_denseZXt_denseZYt_denseZ	ps_sparseZ	Xt_sparseZ	Yt_sparser3   r3   r4   )test_polynomial_count_sketch_dense_sparse9   s(        

    
r7   c                 C   s   t | |jS )N)r'   r(   r)   )r#   r$   r3   r3   r4   _linear_kernelP   s    r8   c               	   C   s  t d d tjd d f } ttjd d d d f }d|  | | |  }|jdd}tdd}|t }|t}t||j	}t
||d |tt }|tt}	t||j t||	j t }
d|
d< d}tjt|d	 ||
 W 5 Q R X td
d}td}tjt|d	 |t  W 5 Q R X dddg}|D ]:}t|d}|jd ksXt|t  |jd k	s:tq:d}td
|d}|j|kst|t  |j|kstd S )Nr   r   r   sample_stepsr   r   r   z!Negative values in data passed tomatch   zHIf sample_steps is not in [1, 2, 3], you need to provide sample_intervalg333333?)r:   sample_interval)r#   r'   newaxisr$   sumr   r%   r&   r(   r)   r   r   r   Acopypytestraises
ValueErrorreescapefitr@   r,   Zsample_interval_)ZX_ZY_Zlarge_kernelr.   r&   r/   r0   r1   Z
X_sp_transZ
Y_sp_transY_negmsgZsample_steps_availabler:   r@   r3   r3   r4   test_additive_chi2_samplerT   sF    







rM   c               	   C   s:  d} |  d t d< t|  d d tjd d f }t |  tjd d d d f }t|d t|d  td t||  }t|jdd}t| ddd}|t}|	t }t
||j}t||d	 t| std
t| stdt  }	|  d |	d< d}
tjt|
d |	|	 W 5 Q R X d S )NgQ?g       @r<   r   r     r   )Z
skewednessr   r   r   zNaNs found in the Gram matrixz)NaNs found in the approximate Gram matrixz2X may not contain entries smaller than -skewednessr=   )r$   r#   r'   rA   logexprB   r	   r%   r&   r(   r)   r   isfiniteallr,   rD   rE   rF   rG   )cZX_cZY_cZ
log_kernelr.   r&   r/   r0   r1   rK   rL   r3   r3   r4   test_skewed_chi2_sampler   s&    2

rT   c               	   C   sl   t  } t }d|d< tjtdd | | W 5 Q R X tjtdd | t | | W 5 Q R X dS )zEnsures correct error messager;   r<   zX in AdditiveChi2Sampler.fitr=   z"X in AdditiveChi2Sampler.transformN)r   r#   rD   rE   rF   rG   rJ   r&   )ZtransformerZX_negr3   r3   r4   %test_additive_chi2_sampler_exceptions   s    
rU   c                  C   s   d} t tt| d}t| ddd}|t}|t}t||j}|| }t	t
|dksbttj	||d t|dkstt
|d	kstd S )
Ng      $@r   rN   r   )r   r   r   g{Gz?r!   r   r    )r   r#   r$   r   r%   r&   r'   r(   r)   r*   r+   r,   r-   )r   r.   Zrbf_transformr/   r0   r1   r2   r3   r3   r4   test_rbf_sampler   s    

rW   c                 C   sT   t  }tjddgddgddgg| d}|| |jj| ks@t|jj| ksPtdS 	zRCheck that the fitted attributes are stored accordingly to the
    data type of X.r   r   r   r?         dtypeN)r   r'   arrayrJ   random_offset_r\   r,   random_weights_)global_dtyperbfr#   r3   r3   r4   (test_rbf_sampler_fitted_attributes_dtype   s
     
rb   c                  C   s   t dd} tjddgddgddggtjd	}| | t dd}tjddgddgddggtjd	}|| t| j|j t| j|j d
S z?Check the equivalence of the results with 32 and 64 bits input.r   )r   r   r   r   r?   rY   rZ   r[   N)	r   r'   r]   float32rJ   float64r   r^   r_   )Zrbf32ZX32Zrbf64ZX64r3   r3   r4   "test_rbf_sampler_dtype_equivalence   s    
"

"
rf   c                  C   sD   dgdggddg } }t dd}|| | |jtdks@tdS )	z4Check the inner value computed when `gamma='scale'`.g        r6   r   r   ZscalerV   r?   N)r   rJ   Z_gammarE   Zapproxr,   )r#   yra   r3   r3   r4   test_rbf_sampler_gamma_scale   s    
rh   c                 C   sT   t  }tjddgddgddgg| d}|| |jj| ks@t|jj| ksPtdS rX   )r	   r'   r]   rJ   r^   r\   r,   r_   )r`   Zskewed_chi2_samplerr#   r3   r3   r4   0test_skewed_chi2_sampler_fitted_attributes_dtype   s
     
ri   c                  C   s   t dd} tjddgddgddggtjd	}| | t dd}tjddgddgddggtjd	}|| t| j|j t| j|j d
S rc   )	r	   r'   r]   rd   rJ   re   r   r^   r_   )Zskewed_chi2_sampler_32ZX_32Zskewed_chi2_sampler_64ZX_64r3   r3   r4   *test_skewed_chi2_sampler_dtype_equivalence
  s    
"

"
  rj   c                  C   sj   ddgddgddgg} t  | |  t | |  t | |  t| } t | |  d S )Nr   r   r   r?   rY   rZ   )r   rJ   r&   r	   r   r   )r#   r3   r3   r4   test_input_validation  s    rk   c                  C   s   t jd} | jdd}t|jd d|}t|}tt 	||j
| td| d}|||}|j|jd dfks~ttdt| d}|||}|j|jd dfkstt }|D ]:}td|| d}|||}|j|jd dfkstqd S )Nr   
   r?   r   r   r   r   r   )r   r.   r   )r'   randomRandomStateuniformr
   shaper%   r   r   r(   r)   rJ   r&   r,   r8   r   )rndr#   X_transformedKZtransZkernels_availablekernr3   r3   r4   test_nystroem_approximation(  s     rx   c                  C   s   t jd} | jdd}tdd}||}t|d d}t ||j}t	|| tddd}||}t
|d	d}t ||j}t	|| d S )
Nr   rl   r   rm   rn   rV   Zchi2r.   r   r   )r'   rp   rq   rr   r
   r%   r   r(   r)   r   r   )rt   r#   nystroemru   rv   ZK2r3   r3   r4    test_nystroem_default_parametersC  s    



r{   c                  C   s   t jd} | dd}t |gd }d}t||jd d|}||}t	||d}t
|t ||j t t tstd S )Nr   rm      r   d   )r   r   rV   )r'   rp   rq   ZrandZvstackr
   rs   rJ   r&   r   r   r(   r)   rR   rQ   r$   r,   )rngr#   r   Nru   rv   r3   r3   r4   test_nystroem_singular_kernelW  s    
r   c                  C   s^   t jd} | jdd}t|ddd}td|jd ddd	}||}tt 	||j
| d S )
N%   rl   r   g@r   r   r   Z
polynomialr   )r.   r   r   r   )r'   rp   rq   rr   r   r
   rs   r%   r   r(   r)   )rt   r#   rv   rz   ru   r3   r3   r4    test_nystroem_poly_kernel_paramsg  s       
r   c            	   
   C   s   t jd} d}| j|dfd}dd }g }t|}t||d d|id	| t|||d  d
 ksltd}ddiddidd
if}|D ]@}tf t	|d d|}t
jt|d || W 5 Q R X qd S )Nr   rm   r?   r   c                 S   s   | d t| | S )z&Histogram kernel that writes to a log.r   )appendr'   ZminimumrB   )xrg   rO   r3   r3   r4   logging_histogram_kernelz  s    
z8test_nystroem_callable.<locals>.logging_histogram_kernelr   rO   )r.   r   Zkernel_paramsr   -Don't pass gamma, coef0 or degree to Nystroemr   r   r   ry   r=   )r'   rp   rq   rr   listr
   rJ   lenr,   r8   rE   rF   rG   )	rt   	n_samplesr#   r   Z
kernel_logrL   paramsparamnyr3   r3   r4   test_nystroem_callablet  s(    r   c            	   
   C   s   t jd} | jdd}t|ddd}td|jd d	}||}tt 	||j
| d
}ddiddiddif}|D ]B}tf d|jd d	|}tjt|d || W 5 Q R X qtd S )N   rl   r   r   r   r   Zprecomputedr   ry   r   r   r   r   r   r=   )r'   rp   rq   rr   r   r
   rs   r%   r   r(   r)   rE   rF   rG   rJ   )	rt   r#   rv   rz   ru   rL   r   r   r   r3   r3   r4    test_nystroem_precomputed_kernel  s    
r   c                  C   s:   t ddd\} }tddd}||  |jjdks6tdS )	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   Z
n_featuresrm   r   ro   )rm   N)r   r
   rJ   Zcomponent_indices_rs   r,   )r#   _Zfeature_map_nystroemr3   r3   r4   test_nystroem_component_indices  s    
r   	Estimatorc                    sR   |   t}|t}| }| j   fddt|jd D }t|| dS )zCheck get_feature_names_outc                    s   g | ]}  | qS r3   r3   ).0i
class_namer3   r4   
<listcomp>  s     z.test_get_feature_names_out.<locals>.<listcomp>r   N)	rJ   r#   r&   get_feature_names_out__name__lowerrangers   r   )r   Zestr/   	names_outexpected_namesr3   r   r4   test_get_feature_names_out  s    

r   c                  C   s|   t jd} | jdd}tdd|}dddg}d	d
dddddddddddddg}|j|d}dd |D }t|| dS )z4Check get_feature_names_out for AdditiveChi2Sampler.r   )r   r   r   r   r9   Zf0f1f2Zf0_sqrtZf1_sqrtZf2_sqrtZf0_cos1Zf1_cos1Zf2_cos1Zf0_sin1Zf1_sin1Zf2_sin1Zf0_cos2Zf1_cos2Zf2_cos2Zf0_sin2Zf1_sin2Zf2_sin2)Zinput_featuresc                 S   s   g | ]}d | qS )Zadditivechi2sampler_r3   )r   suffixr3   r3   r4   r     s     zBtest_additivechi2sampler_get_feature_names_out.<locals>.<listcomp>N)r'   rp   rq   random_sampler   rJ   r   r   )r~   r#   Zchi2_samplerZinput_namessuffixesr   r   r3   r3   r4   .test_additivechi2sampler_get_feature_names_out  s.    
r   )7rH   Znumpyr'   Zscipy.sparser   rE   Zsklearn.utils._testingr   r   r   Zsklearn.metrics.pairwiser   Zsklearn.kernel_approximationr   r   r	   r
   r   Zsklearn.datasetsr   r   r   r   rp   rq   r~   r   r#   r$   rB   rA   markZparametrizer5   r7   r8   rM   rT   rU   rW   rb   rf   rh   ri   rj   rk   rx   r{   r   r   r   r   r   r   r   r3   r3   r3   r4   <module>   sf   ?( 

