<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import itertools <EOL> import warnings <EOL> import numpy as np <EOL> from scipy import stats , sparse <EOL> from numpy . testing import run_module_suite <EOL> from sklearn . utils . testing import assert_equal <EOL> from sklearn . utils . testing import assert_almost_equal <EOL> from sklearn . utils . testing import assert_raises <EOL> from sklearn . utils . testing import assert_true <EOL> from sklearn . utils . testing import assert_array_equal <EOL> from sklearn . utils . testing import assert_array_almost_equal <EOL> from sklearn . utils . testing import assert_not_in <EOL> from sklearn . utils . testing import assert_less <EOL> from sklearn . utils . testing import assert_warns <EOL> from sklearn . utils . testing import ignore_warnings <EOL> from sklearn . utils . testing import assert_warns_message <EOL> from sklearn . utils . testing import assert_greater <EOL> from sklearn . utils . testing import assert_greater_equal <EOL> from sklearn . utils import safe_mask <EOL> from sklearn . datasets . samples_generator import ( make_classification , <EOL> make_regression ) <EOL> from sklearn . feature_selection import ( <EOL> chi2 , f_classif , f_oneway , f_regression , mutual_info_classif , <EOL> mutual_info_regression , SelectPercentile , SelectKBest , SelectFpr , <EOL> SelectFdr , SelectFwe , GenericUnivariateSelect ) <EOL> def test_f_oneway_vs_scipy_stats ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X1 = rng . randn ( <NUM_LIT:10> , <NUM_LIT:3> ) <EOL> X2 = <NUM_LIT:1> + rng . randn ( <NUM_LIT:10> , <NUM_LIT:3> ) <EOL> f , pv = stats . f_oneway ( X1 , X2 ) <EOL> f2 , pv2 = f_oneway ( X1 , X2 ) <EOL> assert_true ( np . allclose ( f , f2 ) ) <EOL> assert_true ( np . allclose ( pv , pv2 ) ) <EOL> def test_f_oneway_ints ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . randint ( <NUM_LIT:10> , size = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> y = np . arange ( <NUM_LIT:10> ) <EOL> fint , pint = f_oneway ( X , y ) <EOL> f , p = f_oneway ( X . astype ( np . float ) , y ) <EOL> assert_array_almost_equal ( f , fint , decimal = <NUM_LIT:4> ) <EOL> assert_array_almost_equal ( p , pint , decimal = <NUM_LIT:4> ) <EOL> def test_f_classif ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:3> , n_redundant = <NUM_LIT:2> , <EOL> n_repeated = <NUM_LIT:0> , n_classes = <NUM_LIT:8> , <EOL> n_clusters_per_class = <NUM_LIT:1> , flip_y = <NUM_LIT:0.0> , <EOL> class_sep = <NUM_LIT:10> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> F , pv = f_classif ( X , y ) <EOL> F_sparse , pv_sparse = f_classif ( sparse . csr_matrix ( X ) , y ) <EOL> assert_true ( ( F > <NUM_LIT:0> ) . all ( ) ) <EOL> assert_true ( ( pv > <NUM_LIT:0> ) . all ( ) ) <EOL> assert_true ( ( pv < <NUM_LIT:1> ) . all ( ) ) <EOL> assert_true ( ( pv [ : <NUM_LIT:5> ] < <NUM_LIT> ) . all ( ) ) <EOL> assert_true ( ( pv [ <NUM_LIT:5> : ] > <NUM_LIT> ) . all ( ) ) <EOL> assert_array_almost_equal ( F_sparse , F ) <EOL> assert_array_almost_equal ( pv_sparse , pv ) <EOL> def test_f_regression ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , n_informative = <NUM_LIT:5> , <EOL> shuffle = False , random_state = <NUM_LIT:0> ) <EOL> F , pv = f_regression ( X , y ) <EOL> assert_true ( ( F > <NUM_LIT:0> ) . all ( ) ) <EOL> assert_true ( ( pv > <NUM_LIT:0> ) . all ( ) ) <EOL> assert_true ( ( pv < <NUM_LIT:1> ) . all ( ) ) <EOL> assert_true ( ( pv [ : <NUM_LIT:5> ] < <NUM_LIT> ) . all ( ) ) <EOL> assert_true ( ( pv [ <NUM_LIT:5> : ] > <NUM_LIT> ) . all ( ) ) <EOL> F , pv = f_regression ( X , y , center = False ) <EOL> F_sparse , pv_sparse = f_regression ( sparse . csr_matrix ( X ) , y , center = False ) <EOL> assert_array_almost_equal ( F_sparse , F ) <EOL> assert_array_almost_equal ( pv_sparse , pv ) <EOL> def test_f_regression_input_dtype ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . rand ( <NUM_LIT:10> , <NUM_LIT:20> ) <EOL> y = np . arange ( <NUM_LIT:10> ) . astype ( np . int ) <EOL> F1 , pv1 = f_regression ( X , y ) <EOL> F2 , pv2 = f_regression ( X , y . astype ( np . float ) ) <EOL> assert_array_almost_equal ( F1 , F2 , <NUM_LIT:5> ) <EOL> assert_array_almost_equal ( pv1 , pv2 , <NUM_LIT:5> ) <EOL> def test_f_regression_center ( ) : <EOL> X = np . arange ( - <NUM_LIT:5> , <NUM_LIT:6> ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> n_samples = X . size <EOL> Y = np . ones ( n_samples ) <EOL> Y [ : : <NUM_LIT:2> ] *= - <NUM_LIT:1.> <EOL> Y [ <NUM_LIT:0> ] = <NUM_LIT:0.> <EOL> F1 , _ = f_regression ( X , Y , center = True ) <EOL> F2 , _ = f_regression ( X , Y , center = False ) <EOL> assert_array_almost_equal ( F1 * ( n_samples - <NUM_LIT:1.> ) / ( n_samples - <NUM_LIT> ) , F2 ) <EOL> assert_almost_equal ( F2 [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> def test_f_classif_multi_class ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:3> , n_redundant = <NUM_LIT:2> , <EOL> n_repeated = <NUM_LIT:0> , n_classes = <NUM_LIT:8> , <EOL> n_clusters_per_class = <NUM_LIT:1> , flip_y = <NUM_LIT:0.0> , <EOL> class_sep = <NUM_LIT:10> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> F , pv = f_classif ( X , y ) <EOL> assert_true ( ( F > <NUM_LIT:0> ) . all ( ) ) <EOL> assert_true ( ( pv > <NUM_LIT:0> ) . all ( ) ) <EOL> assert_true ( ( pv < <NUM_LIT:1> ) . all ( ) ) <EOL> assert_true ( ( pv [ : <NUM_LIT:5> ] < <NUM_LIT> ) . all ( ) ) <EOL> assert_true ( ( pv [ <NUM_LIT:5> : ] > <NUM_LIT> ) . all ( ) ) <EOL> def test_select_percentile_classif ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:3> , n_redundant = <NUM_LIT:2> , <EOL> n_repeated = <NUM_LIT:0> , n_classes = <NUM_LIT:8> , <EOL> n_clusters_per_class = <NUM_LIT:1> , flip_y = <NUM_LIT:0.0> , <EOL> class_sep = <NUM_LIT:10> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectPercentile ( f_classif , percentile = <NUM_LIT> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( f_classif , mode = '<STR_LIT>' , <EOL> param = <NUM_LIT> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> def test_select_percentile_classif_sparse ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:3> , n_redundant = <NUM_LIT:2> , <EOL> n_repeated = <NUM_LIT:0> , n_classes = <NUM_LIT:8> , <EOL> n_clusters_per_class = <NUM_LIT:1> , flip_y = <NUM_LIT:0.0> , <EOL> class_sep = <NUM_LIT:10> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> X = sparse . csr_matrix ( X ) <EOL> univariate_filter = SelectPercentile ( f_classif , percentile = <NUM_LIT> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( f_classif , mode = '<STR_LIT>' , <EOL> param = <NUM_LIT> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r . toarray ( ) , X_r2 . toarray ( ) ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> X_r2inv = univariate_filter . inverse_transform ( X_r2 ) <EOL> assert_true ( sparse . issparse ( X_r2inv ) ) <EOL> support_mask = safe_mask ( X_r2inv , support ) <EOL> assert_equal ( X_r2inv . shape , X . shape ) <EOL> assert_array_equal ( X_r2inv [ : , support_mask ] . toarray ( ) , X_r . toarray ( ) ) <EOL> assert_equal ( X_r2inv . getnnz ( ) , X_r . getnnz ( ) ) <EOL> def test_select_kbest_classif ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:3> , n_redundant = <NUM_LIT:2> , <EOL> n_repeated = <NUM_LIT:0> , n_classes = <NUM_LIT:8> , <EOL> n_clusters_per_class = <NUM_LIT:1> , flip_y = <NUM_LIT:0.0> , <EOL> class_sep = <NUM_LIT:10> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectKBest ( f_classif , k = <NUM_LIT:5> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_classif , mode = '<STR_LIT>' , param = <NUM_LIT:5> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> def test_select_kbest_all ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:10> , <EOL> shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectKBest ( f_classif , k = '<STR_LIT:all>' ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X , X_r ) <EOL> def test_select_kbest_zero ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:20> , n_features = <NUM_LIT:10> , <EOL> shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectKBest ( f_classif , k = <NUM_LIT:0> ) <EOL> univariate_filter . fit ( X , y ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:10> , dtype = bool ) <EOL> assert_array_equal ( support , gtruth ) <EOL> X_selected = assert_warns_message ( UserWarning , '<STR_LIT>' , <EOL> univariate_filter . transform , X ) <EOL> assert_equal ( X_selected . shape , ( <NUM_LIT:20> , <NUM_LIT:0> ) ) <EOL> def test_select_heuristics_classif ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:3> , n_redundant = <NUM_LIT:2> , <EOL> n_repeated = <NUM_LIT:0> , n_classes = <NUM_LIT:8> , <EOL> n_clusters_per_class = <NUM_LIT:1> , flip_y = <NUM_LIT:0.0> , <EOL> class_sep = <NUM_LIT:10> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectFwe ( f_classif , alpha = <NUM_LIT> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> for mode in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_classif , mode = mode , param = <NUM_LIT> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> assert_array_almost_equal ( support , gtruth ) <EOL> def assert_best_scores_kept ( score_filter ) : <EOL> scores = score_filter . scores_ <EOL> support = score_filter . get_support ( ) <EOL> assert_array_equal ( np . sort ( scores [ support ] ) , <EOL> np . sort ( scores ) [ - support . sum ( ) : ] ) <EOL> def test_select_percentile_regression ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:5> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectPercentile ( f_regression , percentile = <NUM_LIT> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> assert_best_scores_kept ( univariate_filter ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_regression , mode = '<STR_LIT>' , param = <NUM_LIT> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> X_2 = X . copy ( ) <EOL> X_2 [ : , np . logical_not ( support ) ] = <NUM_LIT:0> <EOL> assert_array_equal ( X_2 , univariate_filter . inverse_transform ( X_r ) ) <EOL> assert_array_equal ( X_2 . astype ( bool ) , <EOL> univariate_filter . inverse_transform ( X_r . astype ( bool ) ) ) <EOL> def test_select_percentile_regression_full ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:5> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectPercentile ( f_regression , percentile = <NUM_LIT:100> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> assert_best_scores_kept ( univariate_filter ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_regression , mode = '<STR_LIT>' , param = <NUM_LIT:100> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . ones ( <NUM_LIT:20> ) <EOL> assert_array_equal ( support , gtruth ) <EOL> def test_invalid_percentile ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:10> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:2> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> assert_raises ( ValueError , SelectPercentile ( percentile = - <NUM_LIT:1> ) . fit , X , y ) <EOL> assert_raises ( ValueError , SelectPercentile ( percentile = <NUM_LIT> ) . fit , X , y ) <EOL> assert_raises ( ValueError , GenericUnivariateSelect ( mode = '<STR_LIT>' , <EOL> param = - <NUM_LIT:1> ) . fit , X , y ) <EOL> assert_raises ( ValueError , GenericUnivariateSelect ( mode = '<STR_LIT>' , <EOL> param = <NUM_LIT> ) . fit , X , y ) <EOL> def test_select_kbest_regression ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , n_informative = <NUM_LIT:5> , <EOL> shuffle = False , random_state = <NUM_LIT:0> , noise = <NUM_LIT:10> ) <EOL> univariate_filter = SelectKBest ( f_regression , k = <NUM_LIT:5> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> assert_best_scores_kept ( univariate_filter ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_regression , mode = '<STR_LIT>' , param = <NUM_LIT:5> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> def test_select_heuristics_regression ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , n_informative = <NUM_LIT:5> , <EOL> shuffle = False , random_state = <NUM_LIT:0> , noise = <NUM_LIT:10> ) <EOL> univariate_filter = SelectFpr ( f_regression , alpha = <NUM_LIT> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> for mode in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_regression , mode = mode , param = <NUM_LIT> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> assert_array_equal ( support [ : <NUM_LIT:5> ] , np . ones ( ( <NUM_LIT:5> , ) , dtype = np . bool ) ) <EOL> assert_less ( np . sum ( support [ <NUM_LIT:5> : ] == <NUM_LIT:1> ) , <NUM_LIT:3> ) <EOL> def test_select_fdr_regression ( ) : <EOL> def single_fdr ( alpha , n_informative , random_state ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT> , n_features = <NUM_LIT:20> , <EOL> n_informative = n_informative , shuffle = False , <EOL> random_state = random_state , noise = <NUM_LIT:10> ) <EOL> with warnings . catch_warnings ( record = True ) : <EOL> univariate_filter = SelectFdr ( f_regression , alpha = alpha ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_regression , mode = '<STR_LIT>' , param = alpha ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> num_false_positives = np . sum ( support [ n_informative : ] == <NUM_LIT:1> ) <EOL> num_true_positives = np . sum ( support [ : n_informative ] == <NUM_LIT:1> ) <EOL> if num_false_positives == <NUM_LIT:0> : <EOL> return <NUM_LIT:0.> <EOL> false_discovery_rate = ( num_false_positives / <EOL> ( num_true_positives + num_false_positives ) ) <EOL> return false_discovery_rate <EOL> for alpha in [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] : <EOL> for n_informative in [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:10> ] : <EOL> false_discovery_rate = np . mean ( [ single_fdr ( alpha , n_informative , <EOL> random_state ) for <EOL> random_state in range ( <NUM_LIT:30> ) ] ) <EOL> assert_greater_equal ( alpha , false_discovery_rate ) <EOL> if false_discovery_rate != <NUM_LIT:0> : <EOL> assert_greater ( false_discovery_rate , alpha / <NUM_LIT:10> ) <EOL> def test_select_fwe_regression ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:200> , n_features = <NUM_LIT:20> , <EOL> n_informative = <NUM_LIT:5> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectFwe ( f_regression , alpha = <NUM_LIT> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> f_regression , mode = '<STR_LIT>' , param = <NUM_LIT> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:20> ) <EOL> gtruth [ : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support [ : <NUM_LIT:5> ] , np . ones ( ( <NUM_LIT:5> , ) , dtype = np . bool ) ) <EOL> assert_less ( np . sum ( support [ <NUM_LIT:5> : ] == <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> def test_selectkbest_tiebreaking ( ) : <EOL> Xs = [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ] <EOL> y = [ <NUM_LIT:1> ] <EOL> dummy_score = lambda X , y : ( X [ <NUM_LIT:0> ] , X [ <NUM_LIT:0> ] ) <EOL> for X in Xs : <EOL> sel = SelectKBest ( dummy_score , k = <NUM_LIT:1> ) <EOL> X1 = ignore_warnings ( sel . fit_transform ) ( [ X ] , y ) <EOL> assert_equal ( X1 . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> assert_best_scores_kept ( sel ) <EOL> sel = SelectKBest ( dummy_score , k = <NUM_LIT:2> ) <EOL> X2 = ignore_warnings ( sel . fit_transform ) ( [ X ] , y ) <EOL> assert_equal ( X2 . shape [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> assert_best_scores_kept ( sel ) <EOL> def test_selectpercentile_tiebreaking ( ) : <EOL> Xs = [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ] <EOL> y = [ <NUM_LIT:1> ] <EOL> dummy_score = lambda X , y : ( X [ <NUM_LIT:0> ] , X [ <NUM_LIT:0> ] ) <EOL> for X in Xs : <EOL> sel = SelectPercentile ( dummy_score , percentile = <NUM_LIT> ) <EOL> X1 = ignore_warnings ( sel . fit_transform ) ( [ X ] , y ) <EOL> assert_equal ( X1 . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> assert_best_scores_kept ( sel ) <EOL> sel = SelectPercentile ( dummy_score , percentile = <NUM_LIT> ) <EOL> X2 = ignore_warnings ( sel . fit_transform ) ( [ X ] , y ) <EOL> assert_equal ( X2 . shape [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> assert_best_scores_kept ( sel ) <EOL> def test_tied_pvalues ( ) : <EOL> X0 = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> y = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> for perm in itertools . permutations ( ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) : <EOL> X = X0 [ : , perm ] <EOL> Xt = SelectKBest ( chi2 , k = <NUM_LIT:2> ) . fit_transform ( X , y ) <EOL> assert_equal ( Xt . shape , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> assert_not_in ( <NUM_LIT> , Xt ) <EOL> Xt = SelectPercentile ( chi2 , percentile = <NUM_LIT> ) . fit_transform ( X , y ) <EOL> assert_equal ( Xt . shape , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> assert_not_in ( <NUM_LIT> , Xt ) <EOL> def test_tied_scores ( ) : <EOL> X_train = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> y_train = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> for n_features in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> sel = SelectKBest ( chi2 , k = n_features ) . fit ( X_train , y_train ) <EOL> X_test = sel . transform ( [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ] ) <EOL> assert_array_equal ( X_test [ <NUM_LIT:0> ] , np . arange ( <NUM_LIT:3> ) [ - n_features : ] ) <EOL> def test_nans ( ) : <EOL> X = [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ] ] <EOL> y = [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> for select in ( SelectKBest ( f_classif , <NUM_LIT:2> ) , <EOL> SelectPercentile ( f_classif , percentile = <NUM_LIT> ) ) : <EOL> ignore_warnings ( select . fit ) ( X , y ) <EOL> assert_array_equal ( select . get_support ( indices = True ) , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) <EOL> def test_score_func_error ( ) : <EOL> X = [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ] ] <EOL> y = [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> for SelectFeatures in [ SelectKBest , SelectPercentile , SelectFwe , <EOL> SelectFdr , SelectFpr , GenericUnivariateSelect ] : <EOL> assert_raises ( TypeError , SelectFeatures ( score_func = <NUM_LIT:10> ) . fit , X , y ) <EOL> def test_invalid_k ( ) : <EOL> X = [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ] ] <EOL> y = [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> assert_raises ( ValueError , SelectKBest ( k = - <NUM_LIT:1> ) . fit , X , y ) <EOL> assert_raises ( ValueError , SelectKBest ( k = <NUM_LIT:4> ) . fit , X , y ) <EOL> assert_raises ( ValueError , <EOL> GenericUnivariateSelect ( mode = '<STR_LIT>' , param = - <NUM_LIT:1> ) . fit , X , y ) <EOL> assert_raises ( ValueError , <EOL> GenericUnivariateSelect ( mode = '<STR_LIT>' , param = <NUM_LIT:4> ) . fit , X , y ) <EOL> def test_f_classif_constant_feature ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:10> , n_features = <NUM_LIT:5> ) <EOL> X [ : , <NUM_LIT:0> ] = <NUM_LIT> <EOL> assert_warns ( UserWarning , f_classif , X , y ) <EOL> def test_no_feature_selected ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . rand ( <NUM_LIT> , <NUM_LIT:10> ) <EOL> y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:4> , size = <NUM_LIT> ) <EOL> strict_selectors = [ <EOL> SelectFwe ( alpha = <NUM_LIT> ) . fit ( X , y ) , <EOL> SelectFdr ( alpha = <NUM_LIT> ) . fit ( X , y ) , <EOL> SelectFpr ( alpha = <NUM_LIT> ) . fit ( X , y ) , <EOL> SelectPercentile ( percentile = <NUM_LIT:0> ) . fit ( X , y ) , <EOL> SelectKBest ( k = <NUM_LIT:0> ) . fit ( X , y ) , <EOL> ] <EOL> for selector in strict_selectors : <EOL> assert_array_equal ( selector . get_support ( ) , np . zeros ( <NUM_LIT:10> ) ) <EOL> X_selected = assert_warns_message ( <EOL> UserWarning , '<STR_LIT>' , selector . transform , X ) <EOL> assert_equal ( X_selected . shape , ( <NUM_LIT> , <NUM_LIT:0> ) ) <EOL> def test_mutual_info_classif ( ) : <EOL> X , y = make_classification ( n_samples = <NUM_LIT:100> , n_features = <NUM_LIT:5> , <EOL> n_informative = <NUM_LIT:1> , n_redundant = <NUM_LIT:1> , <EOL> n_repeated = <NUM_LIT:0> , n_classes = <NUM_LIT:2> , <EOL> n_clusters_per_class = <NUM_LIT:1> , flip_y = <NUM_LIT:0.0> , <EOL> class_sep = <NUM_LIT:10> , shuffle = False , random_state = <NUM_LIT:0> ) <EOL> univariate_filter = SelectKBest ( mutual_info_classif , k = <NUM_LIT:2> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> mutual_info_classif , mode = '<STR_LIT>' , param = <NUM_LIT:2> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:5> ) <EOL> gtruth [ : <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> univariate_filter = SelectPercentile ( mutual_info_classif , percentile = <NUM_LIT> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> mutual_info_classif , mode = '<STR_LIT>' , param = <NUM_LIT> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:5> ) <EOL> gtruth [ : <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> def test_mutual_info_regression ( ) : <EOL> X , y = make_regression ( n_samples = <NUM_LIT:100> , n_features = <NUM_LIT:10> , n_informative = <NUM_LIT:2> , <EOL> shuffle = False , random_state = <NUM_LIT:0> , noise = <NUM_LIT:10> ) <EOL> univariate_filter = SelectKBest ( mutual_info_regression , k = <NUM_LIT:2> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> assert_best_scores_kept ( univariate_filter ) <EOL> X_r2 = GenericUnivariateSelect ( <EOL> mutual_info_regression , mode = '<STR_LIT>' , param = <NUM_LIT:2> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:10> ) <EOL> gtruth [ : <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> univariate_filter = SelectPercentile ( mutual_info_regression , percentile = <NUM_LIT:20> ) <EOL> X_r = univariate_filter . fit ( X , y ) . transform ( X ) <EOL> X_r2 = GenericUnivariateSelect ( mutual_info_regression , mode = '<STR_LIT>' , <EOL> param = <NUM_LIT:20> ) . fit ( X , y ) . transform ( X ) <EOL> assert_array_equal ( X_r , X_r2 ) <EOL> support = univariate_filter . get_support ( ) <EOL> gtruth = np . zeros ( <NUM_LIT:10> ) <EOL> gtruth [ : <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> assert_array_equal ( support , gtruth ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_module_suite ( ) </s>
