本文整理汇总了Python中gensim.models.ldamodel.LdaModel.__getitem__方法的典型用法代码示例。如果您正苦于以下问题:Python LdaModel.__getitem__方法的具体用法?Python LdaModel.__getitem__怎么用?Python LdaModel.__getitem__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gensim.models.ldamodel.LdaModel
的用法示例。
在下文中一共展示了LdaModel.__getitem__方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LDA
# 需要导入模块: from gensim.models.ldamodel import LdaModel [as 别名]
# 或者: from gensim.models.ldamodel.LdaModel import __getitem__ [as 别名]
class LDA(object):
def __init__(self, model, vocab, corpus=None, topics=200, passes=1):
self._model_file = model
self._dict_file = vocab
self._corpus_file = corpus
self._topics = topics
self._passes = passes
def train(self):
self._corpus = SentenceDocCorpus(self._corpus_file)
self._lda = LdaModel(self._corpus, num_topics = self._topics, id2word = self._corpus.dictionary, passes = self._passes)
self._dictionary = self._corpus.dictionary
self._lda.save(self._model_file)
self._dictionary.save(self._dict_file)
def load(self):
self._lda = LdaModel.load(self._model_file)
self._dictionary = Dictionary.load(self._dict_file)
def topics(self, words):
return self._lda[self._dictionary.doc2bow(common.filter(words))]
def topic_vector(self, words):
return np.array([v for k, v in self._lda.__getitem__(self._dictionary.doc2bow(common.filter(words)), eps=0)])
示例2: LDA
# 需要导入模块: from gensim.models.ldamodel import LdaModel [as 别名]
# 或者: from gensim.models.ldamodel.LdaModel import __getitem__ [as 别名]
class LDA(BaseEstimator, TransformerMixin):
def __init__(self, **params):
self.params = params
def fit(self, X, y=None):
corpus = Sparse2Corpus(X, documents_columns=False)
self.lda = LdaModel(corpus, **self.params)
return self
def transform(self, X, y=None):
corpus = Sparse2Corpus(X, documents_columns=False)
topics = np.array([map(lambda x: x[1], self.lda.__getitem__(c, eps=0)) for c in corpus])
print topics.shape
return topics