U
    2dx                     @   s   d Z ddlZddlZddlZddlmZmZ ddl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	dgd
d	d	gdd	dgdd	dggZdd	ddgZdd Zdd Zdd Zdd Zdd Zdd ZdS )zs
Tests for chi2, currently the only feature selection function designed
specifically to work with sparse matrices.
    N)
coo_matrix
csr_matrixSelectKBestchi2)
_chisquare)assert_array_almost_equal)assert_array_equal      	      c                 C   s   t t| dS )zMake k-best chi2 selectorkr   r    r   M/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/feature_selection/tests/test_chi2.pymkchi2   s    r   c                  C   s  t ddtt} t ddtt} t| jdddg t| tttd d dgf  t ddtt} tt	| jddddg t
ttjd}t dd|t} tt	| jddddg | |}t|j|jd dg | }t dd|t }t|| d S )Nr   r   T)indicesr   r
   )Zdtype)r   ZfitXyr	   Zget_supportZ	transformnparraysortedr   Zfloat64shapeZtoarrayfit_transformr   )r   ZXspZXtransZXtrans2r   r   r   	test_chi2   s    $
r   c                  C   s   t t} tdd| t d S )Nr
   r   )r   r   r   r   r   )ZXcoor   r   r   test_chi2_coo5   s    r   c               
   C   sX   ddgddggddg } }| t | t| fD ]$} tt t| | W 5 Q R X q.d S )Nr   r   g#Bǻ)r   r   r   pytestZraises
ValueErrorr   )r   r   r   r   r   test_chi2_negative=   s    r   c               	   C   s   t jddP} t d tddgddggddg\}}| D ]}dt|kr:td| q:W 5 Q R X t|dtjg t|d tj d S )NT)recordalwaysr   r   zdivide by zerozFound unexpected warning %s)	warningscatch_warningssimplefilterr   reprAssertionErrorr	   r   nan)Zwarnedchipwr   r   r   test_chi2_unused_featureE   s    
r+   c                  C   sd   t ddgddgg} t ddgddgg}tj| |\}}t| |\}}t|| t|| d S )Ng       @g      ?g      ?)r   r   scipystatsZ	chisquarer   r   )ZobsexpZchi_scpZp_scpZchi_ourZp_ourr   r   r   test_chisquareR   s    
r/   )__doc__r"   Znumpyr   r   Zscipy.sparser   r   Zscipy.statsr,   Zsklearn.feature_selectionr   r   Z/sklearn.feature_selection._univariate_selectionr   Zsklearn.utils._testingr   r	   r   r   r   r   r   r   r+   r/   r   r   r   r   <module>   s"   $