Created
July 3, 2019 10:56
-
-
Save fclesio/37e70148165bdac9c04dcd72c31946c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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