<s> import numpy as np <EOL> from sklearn . utils import check_array <EOL> from sklearn . utils . testing import assert_array_almost_equal <EOL> from sklearn . utils . testing import assert_array_equal <EOL> from sklearn . utils . testing import assert_equal <EOL> from sklearn . utils . testing import assert_true <EOL> from sklearn . utils . testing import assert_less <EOL> from sklearn . utils . testing import assert_raises <EOL> from sklearn . utils . testing import ignore_warnings <EOL> from sklearn . utils . testing import TempMemmap <EOL> from sklearn . decomposition import DictionaryLearning <EOL> from sklearn . decomposition import MiniBatchDictionaryLearning <EOL> from sklearn . decomposition import SparseCoder <EOL> from sklearn . decomposition import dict_learning_online <EOL> from sklearn . decomposition import sparse_encode <EOL> rng_global = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_samples , n_features = <NUM_LIT:10> , <NUM_LIT:8> <EOL> X = rng_global . randn ( n_samples , n_features ) <EOL> def test_dict_learning_shapes ( ) : <EOL> n_components = <NUM_LIT:5> <EOL> dico = DictionaryLearning ( n_components , random_state = <NUM_LIT:0> ) . fit ( X ) <EOL> assert_true ( dico . components_ . shape == ( n_components , n_features ) ) <EOL> def test_dict_learning_overcomplete ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> dico = DictionaryLearning ( n_components , random_state = <NUM_LIT:0> ) . fit ( X ) <EOL> assert_true ( dico . components_ . shape == ( n_components , n_features ) ) <EOL> def test_dict_learning_reconstruction ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> dico = DictionaryLearning ( n_components , transform_algorithm = '<STR_LIT>' , <EOL> transform_alpha = <NUM_LIT> , random_state = <NUM_LIT:0> ) <EOL> code = dico . fit ( X ) . transform ( X ) <EOL> assert_array_almost_equal ( np . dot ( code , dico . components_ ) , X ) <EOL> dico . set_params ( transform_algorithm = '<STR_LIT>' ) <EOL> code = dico . transform ( X ) <EOL> assert_array_almost_equal ( np . dot ( code , dico . components_ ) , X , decimal = <NUM_LIT:2> ) <EOL> def test_dict_learning_reconstruction_parallel ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> dico = DictionaryLearning ( n_components , transform_algorithm = '<STR_LIT>' , <EOL> transform_alpha = <NUM_LIT> , random_state = <NUM_LIT:0> , n_jobs = - <NUM_LIT:1> ) <EOL> code = dico . fit ( X ) . transform ( X ) <EOL> assert_array_almost_equal ( np . dot ( code , dico . components_ ) , X ) <EOL> dico . set_params ( transform_algorithm = '<STR_LIT>' ) <EOL> code = dico . transform ( X ) <EOL> assert_array_almost_equal ( np . dot ( code , dico . components_ ) , X , decimal = <NUM_LIT:2> ) <EOL> def test_dict_learning_lassocd_readonly_data ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> with TempMemmap ( X ) as X_read_only : <EOL> dico = DictionaryLearning ( n_components , transform_algorithm = '<STR_LIT>' , <EOL> transform_alpha = <NUM_LIT> , random_state = <NUM_LIT:0> , n_jobs = - <NUM_LIT:1> ) <EOL> code = dico . fit ( X_read_only ) . transform ( X_read_only ) <EOL> assert_array_almost_equal ( np . dot ( code , dico . components_ ) , X_read_only , decimal = <NUM_LIT:2> ) <EOL> def test_dict_learning_nonzero_coefs ( ) : <EOL> n_components = <NUM_LIT:4> <EOL> dico = DictionaryLearning ( n_components , transform_algorithm = '<STR_LIT>' , <EOL> transform_n_nonzero_coefs = <NUM_LIT:3> , random_state = <NUM_LIT:0> ) <EOL> code = dico . fit ( X ) . transform ( X [ np . newaxis , <NUM_LIT:1> ] ) <EOL> assert_true ( len ( np . flatnonzero ( code ) ) == <NUM_LIT:3> ) <EOL> dico . set_params ( transform_algorithm = '<STR_LIT>' ) <EOL> code = dico . transform ( X [ np . newaxis , <NUM_LIT:1> ] ) <EOL> assert_equal ( len ( np . flatnonzero ( code ) ) , <NUM_LIT:3> ) <EOL> def test_dict_learning_unknown_fit_algorithm ( ) : <EOL> n_components = <NUM_LIT:5> <EOL> dico = DictionaryLearning ( n_components , fit_algorithm = '<STR_LIT>' ) <EOL> assert_raises ( ValueError , dico . fit , X ) <EOL> def test_dict_learning_split ( ) : <EOL> n_components = <NUM_LIT:5> <EOL> dico = DictionaryLearning ( n_components , transform_algorithm = '<STR_LIT>' , <EOL> random_state = <NUM_LIT:0> ) <EOL> code = dico . fit ( X ) . transform ( X ) <EOL> dico . split_sign = True <EOL> split_code = dico . transform ( X ) <EOL> assert_array_equal ( split_code [ : , : n_components ] - <EOL> split_code [ : , n_components : ] , code ) <EOL> def test_dict_learning_online_shapes ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_components = <NUM_LIT:8> <EOL> code , dictionary = dict_learning_online ( X , n_components = n_components , <EOL> alpha = <NUM_LIT:1> , random_state = rng ) <EOL> assert_equal ( code . shape , ( n_samples , n_components ) ) <EOL> assert_equal ( dictionary . shape , ( n_components , n_features ) ) <EOL> assert_equal ( np . dot ( code , dictionary ) . shape , X . shape ) <EOL> def test_dict_learning_online_verbosity ( ) : <EOL> n_components = <NUM_LIT:5> <EOL> from sklearn . externals . six . moves import cStringIO as StringIO <EOL> import sys <EOL> old_stdout = sys . stdout <EOL> try : <EOL> sys . stdout = StringIO ( ) <EOL> dico = MiniBatchDictionaryLearning ( n_components , n_iter = <NUM_LIT:20> , verbose = <NUM_LIT:1> , <EOL> random_state = <NUM_LIT:0> ) <EOL> dico . fit ( X ) <EOL> dico = MiniBatchDictionaryLearning ( n_components , n_iter = <NUM_LIT:20> , verbose = <NUM_LIT:2> , <EOL> random_state = <NUM_LIT:0> ) <EOL> dico . fit ( X ) <EOL> dict_learning_online ( X , n_components = n_components , alpha = <NUM_LIT:1> , verbose = <NUM_LIT:1> , <EOL> random_state = <NUM_LIT:0> ) <EOL> dict_learning_online ( X , n_components = n_components , alpha = <NUM_LIT:1> , verbose = <NUM_LIT:2> , <EOL> random_state = <NUM_LIT:0> ) <EOL> finally : <EOL> sys . stdout = old_stdout <EOL> assert_true ( dico . components_ . shape == ( n_components , n_features ) ) <EOL> def test_dict_learning_online_estimator_shapes ( ) : <EOL> n_components = <NUM_LIT:5> <EOL> dico = MiniBatchDictionaryLearning ( n_components , n_iter = <NUM_LIT:20> , random_state = <NUM_LIT:0> ) <EOL> dico . fit ( X ) <EOL> assert_true ( dico . components_ . shape == ( n_components , n_features ) ) <EOL> def test_dict_learning_online_overcomplete ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> dico = MiniBatchDictionaryLearning ( n_components , n_iter = <NUM_LIT:20> , <EOL> random_state = <NUM_LIT:0> ) . fit ( X ) <EOL> assert_true ( dico . components_ . shape == ( n_components , n_features ) ) <EOL> def test_dict_learning_online_initialization ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> V = rng . randn ( n_components , n_features ) <EOL> dico = MiniBatchDictionaryLearning ( n_components , n_iter = <NUM_LIT:0> , <EOL> dict_init = V , random_state = <NUM_LIT:0> ) . fit ( X ) <EOL> assert_array_equal ( dico . components_ , V ) <EOL> def test_dict_learning_online_partial_fit ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> V = rng . randn ( n_components , n_features ) <EOL> V /= np . sum ( V ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) [ : , np . newaxis ] <EOL> dict1 = MiniBatchDictionaryLearning ( n_components , n_iter = <NUM_LIT:10> * len ( X ) , <EOL> batch_size = <NUM_LIT:1> , <EOL> alpha = <NUM_LIT:1> , shuffle = False , dict_init = V , <EOL> random_state = <NUM_LIT:0> ) . fit ( X ) <EOL> dict2 = MiniBatchDictionaryLearning ( n_components , alpha = <NUM_LIT:1> , <EOL> n_iter = <NUM_LIT:1> , dict_init = V , <EOL> random_state = <NUM_LIT:0> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> for sample in X : <EOL> dict2 . partial_fit ( sample [ np . newaxis , : ] ) <EOL> assert_true ( not np . all ( sparse_encode ( X , dict1 . components_ , alpha = <NUM_LIT:1> ) == <EOL> <NUM_LIT:0> ) ) <EOL> assert_array_almost_equal ( dict1 . components_ , dict2 . components_ , <EOL> decimal = <NUM_LIT:2> ) <EOL> def test_sparse_encode_shapes ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> V = rng . randn ( n_components , n_features ) <EOL> V /= np . sum ( V ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) [ : , np . newaxis ] <EOL> for algo in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> code = sparse_encode ( X , V , algorithm = algo ) <EOL> assert_equal ( code . shape , ( n_samples , n_components ) ) <EOL> def test_sparse_encode_input ( ) : <EOL> n_components = <NUM_LIT:100> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> V = rng . randn ( n_components , n_features ) <EOL> V /= np . sum ( V ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) [ : , np . newaxis ] <EOL> Xf = check_array ( X , order = '<STR_LIT:F>' ) <EOL> for algo in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> a = sparse_encode ( X , V , algorithm = algo ) <EOL> b = sparse_encode ( Xf , V , algorithm = algo ) <EOL> assert_array_almost_equal ( a , b ) <EOL> def test_sparse_encode_error ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> V = rng . randn ( n_components , n_features ) <EOL> V /= np . sum ( V ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) [ : , np . newaxis ] <EOL> code = sparse_encode ( X , V , alpha = <NUM_LIT> ) <EOL> assert_true ( not np . all ( code == <NUM_LIT:0> ) ) <EOL> assert_less ( np . sqrt ( np . sum ( ( np . dot ( code , V ) - X ) ** <NUM_LIT:2> ) ) , <NUM_LIT:0.1> ) <EOL> def test_sparse_encode_error_default_sparsity ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . randn ( <NUM_LIT:100> , <NUM_LIT:64> ) <EOL> D = rng . randn ( <NUM_LIT:2> , <NUM_LIT:64> ) <EOL> code = ignore_warnings ( sparse_encode ) ( X , D , algorithm = '<STR_LIT>' , <EOL> n_nonzero_coefs = None ) <EOL> assert_equal ( code . shape , ( <NUM_LIT:100> , <NUM_LIT:2> ) ) <EOL> def test_unknown_method ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> V = rng . randn ( n_components , n_features ) <EOL> assert_raises ( ValueError , sparse_encode , X , V , algorithm = "<STR_LIT>" ) <EOL> def test_sparse_coder_estimator ( ) : <EOL> n_components = <NUM_LIT:12> <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> V = rng . randn ( n_components , n_features ) <EOL> V /= np . sum ( V ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) [ : , np . newaxis ] <EOL> code = SparseCoder ( dictionary = V , transform_algorithm = '<STR_LIT>' , <EOL> transform_alpha = <NUM_LIT> ) . transform ( X ) <EOL> assert_true ( not np . all ( code == <NUM_LIT:0> ) ) <EOL> assert_less ( np . sqrt ( np . sum ( ( np . dot ( code , V ) - X ) ** <NUM_LIT:2> ) ) , <NUM_LIT:0.1> ) </s>
