<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from time import time <EOL> from sklearn . feature_extraction . text import TfidfVectorizer , CountVectorizer <EOL> from sklearn . decomposition import NMF , LatentDirichletAllocation <EOL> from sklearn . datasets import fetch_20newsgroups <EOL> n_samples = <NUM_LIT> <EOL> n_features = <NUM_LIT:1000> <EOL> n_topics = <NUM_LIT:10> <EOL> n_top_words = <NUM_LIT:20> <EOL> def print_top_words ( model , feature_names , n_top_words ) : <EOL> for topic_idx , topic in enumerate ( model . components_ ) : <EOL> print ( "<STR_LIT>" % topic_idx ) <EOL> print ( "<STR_LIT:U+0020>" . join ( [ feature_names [ i ] <EOL> for i in topic . argsort ( ) [ : - n_top_words - <NUM_LIT:1> : - <NUM_LIT:1> ] ] ) ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> t0 = time ( ) <EOL> dataset = fetch_20newsgroups ( shuffle = True , random_state = <NUM_LIT:1> , <EOL> remove = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> data_samples = dataset . data [ : n_samples ] <EOL> print ( "<STR_LIT>" % ( time ( ) - t0 ) ) <EOL> print ( "<STR_LIT>" ) <EOL> tfidf_vectorizer = TfidfVectorizer ( max_df = <NUM_LIT> , min_df = <NUM_LIT:2> , <EOL> max_features = n_features , <EOL> stop_words = '<STR_LIT>' ) <EOL> t0 = time ( ) <EOL> tfidf = tfidf_vectorizer . fit_transform ( data_samples ) <EOL> print ( "<STR_LIT>" % ( time ( ) - t0 ) ) <EOL> print ( "<STR_LIT>" ) <EOL> tf_vectorizer = CountVectorizer ( max_df = <NUM_LIT> , min_df = <NUM_LIT:2> , <EOL> max_features = n_features , <EOL> stop_words = '<STR_LIT>' ) <EOL> t0 = time ( ) <EOL> tf = tf_vectorizer . fit_transform ( data_samples ) <EOL> print ( "<STR_LIT>" % ( time ( ) - t0 ) ) <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( n_samples , n_features ) ) <EOL> t0 = time ( ) <EOL> nmf = NMF ( n_components = n_topics , random_state = <NUM_LIT:1> , <EOL> alpha = <NUM_LIT> , l1_ratio = <NUM_LIT> ) . fit ( tfidf ) <EOL> print ( "<STR_LIT>" % ( time ( ) - t0 ) ) <EOL> print ( "<STR_LIT>" ) <EOL> tfidf_feature_names = tfidf_vectorizer . get_feature_names ( ) <EOL> print_top_words ( nmf , tfidf_feature_names , n_top_words ) <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( n_samples , n_features ) ) <EOL> lda = LatentDirichletAllocation ( n_topics = n_topics , max_iter = <NUM_LIT:5> , <EOL> learning_method = '<STR_LIT>' , <EOL> learning_offset = <NUM_LIT> , <EOL> random_state = <NUM_LIT:0> ) <EOL> t0 = time ( ) <EOL> lda . fit ( tf ) <EOL> print ( "<STR_LIT>" % ( time ( ) - t0 ) ) <EOL> print ( "<STR_LIT>" ) <EOL> tf_feature_names = tf_vectorizer . get_feature_names ( ) <EOL> print_top_words ( lda , tf_feature_names , n_top_words ) </s>
