Skip to content

Instantly share code, notes, and snippets.

@smsubrahmannian
Last active April 7, 2018 05:53
Show Gist options
  • Save smsubrahmannian/a6ff56fcf5ff12657e96b64e40b8c994 to your computer and use it in GitHub Desktop.
Save smsubrahmannian/a6ff56fcf5ff12657e96b64e40b8c994 to your computer and use it in GitHub Desktop.
from gensim import models,corpora
import pyLDAvis.gensim
# lda_final is the lda model built with 12 topics
# vis is the pyLDAvis object
vis = pyLDAvis.gensim.prepare(lda_final, doc_term_matrix, dictionary,sort_topics=False)
def get_relevant_words(vis,lam=0.3,topn=10):
a = vis.topic_info
a['finalscore'] = a['logprob']*lam+(1-lam)*a['loglift']
a = a.loc[:,['Category','Term','finalscore']].groupby(['Category'])\
.apply(lambda x: x.sort_values(by='finalscore',ascending=False).head(topn))
a = a.loc[:,'Term'].reset_index().loc[:,['Category','Term']]
a = a[a['Category']!='Default']
return a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment