1919class TopicModel (object ):
2020 __metaclass__ = ABCMeta
2121
22- def __init__ (self , corpus ):
22+ def __init__ (self , corpus , max_iter = None ):
2323 self .corpus = corpus # a Corpus object
2424 self .document_topic_matrix = None # document x topic matrix
2525 self .topic_word_matrix = None # topic x word matrix
2626 self .nb_topics = None # a scalar value > 1
2727 self .model = None
28+ self .max_iter = max_iter
2829
2930 @abstractmethod
3031 def infer_topics (self , num_topics = 10 , ** kwargs ):
@@ -111,7 +112,7 @@ def most_likely_topics_for_document(self, doc_id):
111112
112113
113114class LatentDirichletAllocation (TopicModel ):
114- def infer_topics (self , num_topics = 10 , algorithm = "variational" , ** kwargs ):
115+ def infer_topics (self , num_topics = 10 , algorithm = "variational" , max_iter = None , ** kwargs ):
115116 self .nb_topics = num_topics
116117 lda_model = None
117118 topic_document = None
@@ -120,7 +121,7 @@ def infer_topics(self, num_topics=10, algorithm="variational", **kwargs):
120121 learning_method = "batch" ,
121122 n_jobs = - 1 ,
122123 random_state = 0 ,
123- max_iter = 20 ,
124+ max_iter = max_iter or 20 ,
124125 doc_topic_prior = 1.0 / num_topics ,
125126 topic_word_prior = 0.01 / num_topics ,
126127 )
@@ -155,9 +156,9 @@ def infer_topics(self, num_topics=10, algorithm="variational", **kwargs):
155156
156157
157158class NonNegativeMatrixFactorization (TopicModel ):
158- def infer_topics (self , num_topics = 10 , ** kwargs ):
159+ def infer_topics (self , num_topics = 10 , max_iter = None , ** kwargs ):
159160 self .nb_topics = num_topics
160- self .model = NMF (n_components = num_topics , init = "nndsvd" , solver = "cd" , random_state = 0 )
161+ self .model = NMF (n_components = num_topics , init = "nndsvd" , solver = "cd" , max_iter = max_iter or 200 , random_state = 0 )
161162 topic_document = self .model .fit_transform (self .corpus .sklearn_vector_space )
162163 self .topic_word_matrix = []
163164 self .document_topic_matrix = []
0 commit comments