Skip to content

Instantly share code, notes, and snippets.

@fclesio
Created July 3, 2019 10:56
Show Gist options
  • Select an option

  • Save fclesio/37e70148165bdac9c04dcd72c31946c0 to your computer and use it in GitHub Desktop.

Select an option

Save fclesio/37e70148165bdac9c04dcd72c31946c0 to your computer and use it in GitHub Desktop.
def get_word_topics(vectorizer, lda_model):
# Show top n keywords for each topic
def show_topics(vectorizer=vectorizer, lda_model=lda_model, n_words=20):
keywords = np.array(vectorizer.get_feature_names())
topic_keywords = []
for topic_weights in lda_model.components_:
top_keyword_locs = (-topic_weights).argsort()[:n_words]
topic_keywords.append(keywords.take(top_keyword_locs))
return topic_keywords
topic_keywords = show_topics(vectorizer=vectorizer, lda_model=lda_model, n_words=10)
# Topic - Keywords Dataframe
df_topic_keywords = pd.DataFrame(topic_keywords)
df_topic_keywords.columns = ['Word '+ str(i) for i in range(df_topic_keywords.shape[1])]
df_topic_keywords.index = ['Topic '+ str(i) for i in range(df_topic_keywords.shape[0])]
return df_topic_keywords
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment