<s> import numpy as np <EOL> from scipy . linalg import block_diag <EOL> from scipy . sparse import csr_matrix <EOL> from scipy . special import psi <EOL> from sklearn . decomposition import LatentDirichletAllocation <EOL> from sklearn . decomposition . _online_lda import ( _dirichlet_expectation_1d , <EOL> _dirichlet_expectation_2d ) <EOL> from sklearn . utils . testing import assert_allclose <EOL> from sklearn . utils . testing import assert_true <EOL> from sklearn . utils . testing import assert_array_almost_equal <EOL> from sklearn . utils . testing import assert_almost_equal <EOL> from sklearn . utils . testing import assert_greater_equal <EOL> from sklearn . utils . testing import assert_raises_regexp <EOL> from sklearn . utils . testing import if_safe_multiprocessing_with_blas <EOL> from sklearn . exceptions import NotFittedError <EOL> from sklearn . externals . six . moves import xrange <EOL> def _build_sparse_mtx ( ) : <EOL> n_topics = <NUM_LIT:3> <EOL> block = n_topics * np . ones ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> blocks = [ block ] * n_topics <EOL> X = block_diag ( * blocks ) <EOL> X = csr_matrix ( X ) <EOL> return ( n_topics , X ) <EOL> def test_lda_default_prior_params ( ) : <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> prior = <NUM_LIT:1.> / n_topics <EOL> lda_1 = LatentDirichletAllocation ( n_topics = n_topics , doc_topic_prior = prior , <EOL> topic_word_prior = prior , random_state = <NUM_LIT:0> ) <EOL> lda_2 = LatentDirichletAllocation ( n_topics = n_topics , random_state = <NUM_LIT:0> ) <EOL> topic_distr_1 = lda_1 . fit_transform ( X ) <EOL> topic_distr_2 = lda_2 . fit_transform ( X ) <EOL> assert_almost_equal ( topic_distr_1 , topic_distr_2 ) <EOL> def test_lda_fit_batch ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , evaluate_every = <NUM_LIT:1> , <EOL> learning_method = '<STR_LIT>' , random_state = rng ) <EOL> lda . fit ( X ) <EOL> correct_idx_grps = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) ] <EOL> for component in lda . components_ : <EOL> top_idx = set ( component . argsort ( ) [ - <NUM_LIT:3> : ] [ : : - <NUM_LIT:1> ] ) <EOL> assert_true ( tuple ( sorted ( top_idx ) ) in correct_idx_grps ) <EOL> def test_lda_fit_online ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , learning_offset = <NUM_LIT> , <EOL> evaluate_every = <NUM_LIT:1> , learning_method = '<STR_LIT>' , <EOL> random_state = rng ) <EOL> lda . fit ( X ) <EOL> correct_idx_grps = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) ] <EOL> for component in lda . components_ : <EOL> top_idx = set ( component . argsort ( ) [ - <NUM_LIT:3> : ] [ : : - <NUM_LIT:1> ] ) <EOL> assert_true ( tuple ( sorted ( top_idx ) ) in correct_idx_grps ) <EOL> def test_lda_partial_fit ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , learning_offset = <NUM_LIT> , <EOL> total_samples = <NUM_LIT:100> , random_state = rng ) <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> lda . partial_fit ( X ) <EOL> correct_idx_grps = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) ] <EOL> for c in lda . components_ : <EOL> top_idx = set ( c . argsort ( ) [ - <NUM_LIT:3> : ] [ : : - <NUM_LIT:1> ] ) <EOL> assert_true ( tuple ( sorted ( top_idx ) ) in correct_idx_grps ) <EOL> def test_lda_dense_input ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , learning_method = '<STR_LIT>' , <EOL> random_state = rng ) <EOL> lda . fit ( X . toarray ( ) ) <EOL> correct_idx_grps = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) ] <EOL> for component in lda . components_ : <EOL> top_idx = set ( component . argsort ( ) [ - <NUM_LIT:3> : ] [ : : - <NUM_LIT:1> ] ) <EOL> assert_true ( tuple ( sorted ( top_idx ) ) in correct_idx_grps ) <EOL> def test_lda_transform ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . randint ( <NUM_LIT:5> , size = ( <NUM_LIT:20> , <NUM_LIT:10> ) ) <EOL> n_topics = <NUM_LIT:3> <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , random_state = rng ) <EOL> X_trans = lda . fit_transform ( X ) <EOL> assert_true ( ( X_trans > <NUM_LIT:0.0> ) . any ( ) ) <EOL> assert_array_almost_equal ( np . sum ( X_trans , axis = <NUM_LIT:1> ) , np . ones ( X_trans . shape [ <NUM_LIT:0> ] ) ) <EOL> def test_lda_fit_transform ( ) : <EOL> for method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . randint ( <NUM_LIT:10> , size = ( <NUM_LIT:50> , <NUM_LIT:20> ) ) <EOL> lda = LatentDirichletAllocation ( n_topics = <NUM_LIT:5> , learning_method = method , <EOL> random_state = rng ) <EOL> X_fit = lda . fit_transform ( X ) <EOL> X_trans = lda . transform ( X ) <EOL> assert_array_almost_equal ( X_fit , X_trans , <NUM_LIT:4> ) <EOL> def test_lda_partial_fit_dim_mismatch ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_topics = rng . randint ( <NUM_LIT:3> , <NUM_LIT:6> ) <EOL> n_col = rng . randint ( <NUM_LIT:6> , <NUM_LIT:10> ) <EOL> X_1 = np . random . randint ( <NUM_LIT:4> , size = ( <NUM_LIT:10> , n_col ) ) <EOL> X_2 = np . random . randint ( <NUM_LIT:4> , size = ( <NUM_LIT:10> , n_col + <NUM_LIT:1> ) ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , learning_offset = <NUM_LIT> , <EOL> total_samples = <NUM_LIT:20> , random_state = rng ) <EOL> lda . partial_fit ( X_1 ) <EOL> assert_raises_regexp ( ValueError , r"<STR_LIT>" , <EOL> lda . partial_fit , X_2 ) <EOL> def test_invalid_params ( ) : <EOL> X = np . ones ( ( <NUM_LIT:5> , <NUM_LIT:10> ) ) <EOL> invalid_models = ( <EOL> ( '<STR_LIT>' , LatentDirichletAllocation ( n_topics = <NUM_LIT:0> ) ) , <EOL> ( '<STR_LIT>' , <EOL> LatentDirichletAllocation ( learning_method = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , LatentDirichletAllocation ( total_samples = <NUM_LIT:0> ) ) , <EOL> ( '<STR_LIT>' , LatentDirichletAllocation ( learning_offset = - <NUM_LIT:1> ) ) , <EOL> ) <EOL> for param , model in invalid_models : <EOL> regex = r"<STR_LIT>" % param <EOL> assert_raises_regexp ( ValueError , regex , model . fit , X ) <EOL> def test_lda_negative_input ( ) : <EOL> X = - np . ones ( ( <NUM_LIT:5> , <NUM_LIT:10> ) ) <EOL> lda = LatentDirichletAllocation ( ) <EOL> regex = r"<STR_LIT>" <EOL> assert_raises_regexp ( ValueError , regex , lda . fit , X ) <EOL> def test_lda_no_component_error ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . randint ( <NUM_LIT:4> , size = ( <NUM_LIT:20> , <NUM_LIT:10> ) ) <EOL> lda = LatentDirichletAllocation ( ) <EOL> regex = r"<STR_LIT>" <EOL> assert_raises_regexp ( NotFittedError , regex , lda . transform , X ) <EOL> assert_raises_regexp ( NotFittedError , regex , lda . perplexity , X ) <EOL> def test_lda_transform_mismatch ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> X = rng . randint ( <NUM_LIT:4> , size = ( <NUM_LIT:20> , <NUM_LIT:10> ) ) <EOL> X_2 = rng . randint ( <NUM_LIT:4> , size = ( <NUM_LIT:10> , <NUM_LIT:8> ) ) <EOL> n_topics = rng . randint ( <NUM_LIT:3> , <NUM_LIT:6> ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , random_state = rng ) <EOL> lda . partial_fit ( X ) <EOL> assert_raises_regexp ( ValueError , r"<STR_LIT>" , <EOL> lda . partial_fit , X_2 ) <EOL> @ if_safe_multiprocessing_with_blas <EOL> def test_lda_multi_jobs ( ) : <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> for method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , n_jobs = <NUM_LIT:2> , <EOL> learning_method = method , <EOL> evaluate_every = <NUM_LIT:1> , <EOL> random_state = rng ) <EOL> lda . fit ( X ) <EOL> correct_idx_grps = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) ] <EOL> for c in lda . components_ : <EOL> top_idx = set ( c . argsort ( ) [ - <NUM_LIT:3> : ] [ : : - <NUM_LIT:1> ] ) <EOL> assert_true ( tuple ( sorted ( top_idx ) ) in correct_idx_grps ) <EOL> @ if_safe_multiprocessing_with_blas <EOL> def test_lda_partial_fit_multi_jobs ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , n_jobs = <NUM_LIT:2> , <EOL> learning_offset = <NUM_LIT> , total_samples = <NUM_LIT:30> , <EOL> random_state = rng ) <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> lda . partial_fit ( X ) <EOL> correct_idx_grps = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) ] <EOL> for c in lda . components_ : <EOL> top_idx = set ( c . argsort ( ) [ - <NUM_LIT:3> : ] [ : : - <NUM_LIT:1> ] ) <EOL> assert_true ( tuple ( sorted ( top_idx ) ) in correct_idx_grps ) <EOL> def test_lda_preplexity_mismatch ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT:0> ) <EOL> n_topics = rng . randint ( <NUM_LIT:3> , <NUM_LIT:6> ) <EOL> n_samples = rng . randint ( <NUM_LIT:6> , <NUM_LIT:10> ) <EOL> X = np . random . randint ( <NUM_LIT:4> , size = ( n_samples , <NUM_LIT:10> ) ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , learning_offset = <NUM_LIT> , <EOL> total_samples = <NUM_LIT:20> , random_state = rng ) <EOL> lda . fit ( X ) <EOL> invalid_n_samples = rng . randint ( <NUM_LIT:4> , size = ( n_samples + <NUM_LIT:1> , n_topics ) ) <EOL> assert_raises_regexp ( ValueError , r'<STR_LIT>' , lda . perplexity , X , <EOL> invalid_n_samples ) <EOL> invalid_n_topics = rng . randint ( <NUM_LIT:4> , size = ( n_samples , n_topics + <NUM_LIT:1> ) ) <EOL> assert_raises_regexp ( ValueError , r'<STR_LIT>' , lda . perplexity , X , <EOL> invalid_n_topics ) <EOL> def test_lda_perplexity ( ) : <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> for method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> lda_1 = LatentDirichletAllocation ( n_topics = n_topics , max_iter = <NUM_LIT:1> , <EOL> learning_method = method , <EOL> total_samples = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> lda_2 = LatentDirichletAllocation ( n_topics = n_topics , max_iter = <NUM_LIT:10> , <EOL> learning_method = method , <EOL> total_samples = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> distr_1 = lda_1 . fit_transform ( X ) <EOL> perp_1 = lda_1 . perplexity ( X , distr_1 , sub_sampling = False ) <EOL> distr_2 = lda_2 . fit_transform ( X ) <EOL> perp_2 = lda_2 . perplexity ( X , distr_2 , sub_sampling = False ) <EOL> assert_greater_equal ( perp_1 , perp_2 ) <EOL> perp_1_subsampling = lda_1 . perplexity ( X , distr_1 , sub_sampling = True ) <EOL> perp_2_subsampling = lda_2 . perplexity ( X , distr_2 , sub_sampling = True ) <EOL> assert_greater_equal ( perp_1_subsampling , perp_2_subsampling ) <EOL> def test_lda_score ( ) : <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> for method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> lda_1 = LatentDirichletAllocation ( n_topics = n_topics , max_iter = <NUM_LIT:1> , <EOL> learning_method = method , <EOL> total_samples = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> lda_2 = LatentDirichletAllocation ( n_topics = n_topics , max_iter = <NUM_LIT:10> , <EOL> learning_method = method , <EOL> total_samples = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> lda_1 . fit_transform ( X ) <EOL> score_1 = lda_1 . score ( X ) <EOL> lda_2 . fit_transform ( X ) <EOL> score_2 = lda_2 . score ( X ) <EOL> assert_greater_equal ( score_2 , score_1 ) <EOL> def test_perplexity_input_format ( ) : <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , max_iter = <NUM_LIT:1> , <EOL> learning_method = '<STR_LIT>' , <EOL> total_samples = <NUM_LIT:100> , random_state = <NUM_LIT:0> ) <EOL> distr = lda . fit_transform ( X ) <EOL> perp_1 = lda . perplexity ( X ) <EOL> perp_2 = lda . perplexity ( X , distr ) <EOL> perp_3 = lda . perplexity ( X . toarray ( ) , distr ) <EOL> assert_almost_equal ( perp_1 , perp_2 ) <EOL> assert_almost_equal ( perp_1 , perp_3 ) <EOL> def test_lda_score_perplexity ( ) : <EOL> n_topics , X = _build_sparse_mtx ( ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , max_iter = <NUM_LIT:10> , <EOL> random_state = <NUM_LIT:0> ) <EOL> distr = lda . fit_transform ( X ) <EOL> perplexity_1 = lda . perplexity ( X , distr , sub_sampling = False ) <EOL> score = lda . score ( X ) <EOL> perplexity_2 = np . exp ( - <NUM_LIT:1.> * ( score / np . sum ( X . data ) ) ) <EOL> assert_almost_equal ( perplexity_1 , perplexity_2 ) <EOL> def test_lda_empty_docs ( ) : <EOL> """<STR_LIT>""" <EOL> Z = np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:4> ) ) <EOL> for X in [ Z , csr_matrix ( Z ) ] : <EOL> lda = LatentDirichletAllocation ( max_iter = <NUM_LIT> ) . fit ( X ) <EOL> assert_almost_equal ( lda . components_ . sum ( axis = <NUM_LIT:0> ) , <EOL> np . ones ( lda . components_ . shape [ <NUM_LIT:1> ] ) ) <EOL> def test_dirichlet_expectation ( ) : <EOL> """<STR_LIT>""" <EOL> x = np . logspace ( - <NUM_LIT:100> , <NUM_LIT:10> , <NUM_LIT> ) <EOL> expectation = np . empty_like ( x ) <EOL> _dirichlet_expectation_1d ( x , <NUM_LIT:0> , expectation ) <EOL> assert_allclose ( expectation , np . exp ( psi ( x ) - psi ( np . sum ( x ) ) ) , <EOL> atol = <NUM_LIT> ) <EOL> x = x . reshape ( <NUM_LIT:100> , <NUM_LIT:100> ) <EOL> assert_allclose ( _dirichlet_expectation_2d ( x ) , <EOL> psi ( x ) - psi ( np . sum ( x , axis = <NUM_LIT:1> ) [ : , np . newaxis ] ) , <EOL> rtol = <NUM_LIT> , atol = <NUM_LIT> ) </s>
