B
    (b                 @   s   d 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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_raises)assert_array_almost_equal)assert_array_equal)clean_warning_registry      	      c             C   s   t t| dS )zMake k-best chi2 selector)k)r   r   )r    r   X/home/dcms/DCMS/lib/python3.7/site-packages/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   float64shapeZtoarrayfit_transform)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_coo9   s    r   c              C   sJ   ddgddggddg } }x*| t | t| fD ]} ttt| | q0W d S )Nr   r   g#Bǻ)r   r   r   r   
ValueErrorr   )r   r   r   r   r   test_chi2_negativeA   s    r   c           	   C   s   t   tjddT} td tddgddggddg\}}x$| D ]}dt|krBtd| qBW W d 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)
r
   warningscatch_warningssimplefilterr   reprAssertionErrorr	   r   nan)Zwarnedchipwr   r   r   test_chi2_unused_featureH   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_chisquareV   s    


r/   )__doc__r"   numpyr   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   r   r   <module>   s*   