Skip to content

Commit

Permalink
Optimise show_topics to only call get_lambda once. Fix #1006 (#1028)
Browse files Browse the repository at this point in the history
* Optimised show_topics

* Fixed test

* Removed code duplication

* Fixed tests
  • Loading branch information
bhargavvader authored and tmylk committed Nov 27, 2016
1 parent c2d8187 commit 0b2f6b8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions gensim/models/ldamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,15 +800,19 @@ def show_topics(self, num_topics=10, num_words=10, log=False, formatted=True):
chosen_topics = sorted_topics[:num_topics // 2] + sorted_topics[-num_topics // 2:]

shown = []

topic = self.state.get_lambda()
for i in chosen_topics:
topic_ = topic[i]
topic_ = topic_ / topic_.sum() # normalize to probability distribution
bestn = matutils.argsort(topic_, num_words, reverse=True)
topic_ = [(self.id2word[id], topic_[id]) for id in bestn]
if formatted:
topic = self.print_topic(i, topn=num_words)
else:
topic = self.show_topic(i, topn=num_words)
topic_ = ' + '.join(['%.3f*"%s"' % (v, k) for k, v in topic_])

shown.append((i, topic))
shown.append((i, topic_))
if log:
logger.info("topic #%i (%.3f): %s", i, self.alpha[i], topic)
logger.info("topic #%i (%.3f): %s", i, self.alpha[i], topic_)

return shown

Expand Down

0 comments on commit 0b2f6b8

Please sign in to comment.