在上一篇文章中,我们将使用Mallet版本的LDA算法对此模型进行改进,然后我们将重点介绍如何在给定任何大型文本语料库的情况下获得最佳主题数。

16.构建LDA Mallet模型

到目前为止,您已经看到了Gensim内置的LDA算法版本。然而,Mallet的版本通常会提供更高质量的主题。

Gensim提供了一个包装器,用于在Gensim内部实现Mallet的LDA。您只需要下载 zip 文件,解压缩它并在解压缩的目录中提供mallet的路径。看看我在下面如何做到这一点。gensim.models.wrappers.LdaMallet

# Download File: http://mallet.cs.umass.edu/dist/mallet-2.0.8.zip
mallet_path = 'path/to/mallet-2.0.8/bin/mallet' # update this path
ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, num_topics=20, id2word=id2word)
# Show Topics
pprint(ldamallet.show_topics(formatted=False))# Compute Coherence Score
coherence_model_ldamallet = CoherenceModel(model=ldamallet, texts=data_lemmatized, dictionary=id2word, coherence='c_v')
coherence_ldamallet = coherence_model_ldamallet.get_coherence()print('\nCoherence Score: ', coherence_ldamallet)
[(13,[('god', 0.022175351915726671),('christian', 0.017560827817656381),('people', 0.0088794630371958616),('bible', 0.008215251235200895),('word', 0.0077491376899412696),('church', 0.0074112053696280414),('religion', 0.0071198844038407759),('man', 0.0067936049221590383),('faith', 0.0067469935676330757),('love', 0.0064556726018458093)]),(1,[('organization', 0.10977647987951586),('line', 0.10182379194445974),('write', 0.097397469098389255),('article', 0.082483883409554246),('nntp_post', 0.079894209047330425),('host', 0.069737542931658306),('university', 0.066303010266865026),('reply', 0.02255404338163719),('distribution_world', 0.014362591143681011),('usa', 0.010928058478887726)]),(8,[('file', 0.02816690014008405),('line', 0.021396171035954908),('problem', 0.013508104862917751),('program', 0.013157894736842105),('read', 0.012607564538723234),('follow', 0.01110666399839904),('number', 0.011056633980388232),('set', 0.010522980454939631),('error', 0.010172770328863986),('write', 0.010039356947501835)]),(7,[('include', 0.0091670556506405262),('information', 0.0088169700741662776),('national', 0.0085576474249260924),('year', 0.0077667133447435295),('report', 0.0070406099268710129),('university', 0.0070406099268710129),('book', 0.0068979824697889113),('program', 0.0065219646283906432),('group', 0.0058866241377521916),('service', 0.0057180644157460714)]),(..truncated..)]Coherence Score:  0.632431683088复制代码

只需改变LDA算法,我们就可以将相干得分从.53增加到.63。不错!

17.如何找到LDA的最佳主题数量?

我找到最佳主题数的方法是构建具有不同主题数量(k)的许多LDA模型,并选择具有最高一致性值的LDA模型。

选择一个标志着主题连贯性快速增长的“k”通常会提供有意义和可解释的主题。选择更高的值有时可以提供更细粒度的子主题。

如果您在多个主题中看到相同的关键字重复,则可能表示'k'太大。

compute_coherence_values()(见下文)训练多个LDA模型,并提供模型及其对应的相关性分数。

def compute_coherence_values(dictionary, corpus, texts, limit, start=2, step=3):"""Compute c_v coherence for various number of topicsParameters:----------dictionary : Gensim dictionarycorpus : Gensim corpustexts : List of input textslimit : Max num of topicsReturns:-------model_list : List of LDA topic modelscoherence_values : Coherence values corresponding to the LDA model with respective number of topics"""coherence_values = []model_list = []for num_topics in range(start, limit, step):model = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, num_topics=num_topics, id2word=id2word)model_list.append(model)coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v')coherence_values.append(coherencemodel.get_coherence())return model_list, coherence_values
# Can take a long time to run.
model_list, coherence_values = compute_coherence_values(dictionary=id2word, corpus=corpus, texts=data_lemmatized, start=2, limit=40, step=6)
# Show graph
limit=40; start=2; step=6;
x = range(start, limit, step)
plt.plot(x, coherence_values)
plt.xlabel("Num Topics")
plt.ylabel("Coherence score")
plt.legend(("coherence_values"), loc='best')
plt.show()复制代码

选择最佳数量的LDA主题

# Print the coherence scoresfor m, cv in zip(x, coherence_values):print("Num Topics =", m, " has Coherence Value of", round(cv, 4))
Num Topics = 2  has Coherence Value of 0.4451Num Topics = 8  has Coherence Value of 0.5943Num Topics = 14  has Coherence Value of 0.6208Num Topics = 20  has Coherence Value of 0.6438Num Topics = 26  has Coherence Value of 0.643Num Topics = 32  has Coherence Value of 0.6478Num Topics = 38  has Coherence Value of 0.6525复制代码

如果相关性得分似乎在不断增加,那么选择在展平之前给出最高CV的模型可能更有意义。这就是这种情况。

因此,对于进一步的步骤,我将选择具有20个主题的模型。

# Select the model and print the topics
optimal_model = model_list[3]
model_topics = optimal_model.show_topics(formatted=False)
pprint(optimal_model.print_topics(num_words=10))
[(0,'0.025*"game" + 0.018*"team" + 0.016*"year" + 0.014*"play" + 0.013*"good" + ''0.012*"player" + 0.011*"win" + 0.007*"season" + 0.007*"hockey" + ''0.007*"fan"'),(1,'0.021*"window" + 0.015*"file" + 0.012*"image" + 0.010*"program" + ''0.010*"version" + 0.009*"display" + 0.009*"server" + 0.009*"software" + ''0.008*"graphic" + 0.008*"application"'),(2,'0.021*"gun" + 0.019*"state" + 0.016*"law" + 0.010*"people" + 0.008*"case" + ''0.008*"crime" + 0.007*"government" + 0.007*"weapon" + 0.007*"police" + ''0.006*"firearm"'),(3,'0.855*"ax" + 0.062*"max" + 0.002*"tm" + 0.002*"qax" + 0.001*"mf" + ''0.001*"giz" + 0.001*"_" + 0.001*"ml" + 0.001*"fp" + 0.001*"mr"'),(4,'0.020*"file" + 0.020*"line" + 0.013*"read" + 0.013*"set" + 0.012*"program" ''+ 0.012*"number" + 0.010*"follow" + 0.010*"error" + 0.010*"change" + ''0.009*"entry"'),(5,'0.021*"god" + 0.016*"christian" + 0.008*"religion" + 0.008*"bible" + ''0.007*"life" + 0.007*"people" + 0.007*"church" + 0.007*"word" + 0.007*"man" ''+ 0.006*"faith"'),(..truncated..)]复制代码

这些是所选LDA模型的主题。

18.在每个句子中找到主要话题

主题建模的一个实际应用是确定给定文档的主题。

为了找到这个,我们找到该文档中贡献百分比最高的主题编号。

下面的函数很好地将此信息聚合在一个可呈现的表中。format_topics_sentences()

def format_topics_sentences(ldamodel=lda_model, corpus=corpus, texts=data):# Init outputsent_topics_df = pd.DataFrame()# Get main topic in each documentfor i, row in enumerate(ldamodel[corpus]):row = sorted(row, key=lambda x: (x[1]), reverse=True)# Get the Dominant topic, Perc Contribution and Keywords for each documentfor j, (topic_num, prop_topic) in enumerate(row):if j == 0:  # => dominant topicwp = ldamodel.show_topic(topic_num)topic_keywords = ", ".join([word for word, prop in wp])sent_topics_df = sent_topics_df.append(pd.Series([int(topic_num), round(prop_topic,4), topic_keywords]), ignore_index=True)else:breaksent_topics_df.columns = ['Dominant_Topic', 'Perc_Contribution', 'Topic_Keywords']# Add original text to the end of the outputcontents = pd.Series(texts)sent_topics_df = pd.concat([sent_topics_df, contents], axis=1)return(sent_topics_df)df_topic_sents_keywords = format_topics_sentences(ldamodel=optimal_model, corpus=corpus, texts=data)# Format
df_dominant_topic = df_topic_sents_keywords.reset_index()
df_dominant_topic.columns = ['Document_No', 'Dominant_Topic', 'Topic_Perc_Contrib', 'Keywords', 'Text']# Show
df_dominant_topic.head(10)复制代码

每个文档的主导主题

19.找到每个主题最具代表性的文件

有时,主题关键字可能不足以理解主题的含义。因此,为了帮助理解该主题,您可以找到给定主题最有贡献的文档,并通过阅读该文档来推断该主题。呼!

# Group top 5 sentences under each topic
sent_topics_sorteddf_mallet = pd.DataFrame()sent_topics_outdf_grpd = df_topic_sents_keywords.groupby('Dominant_Topic')for i, grp in sent_topics_outdf_grpd:sent_topics_sorteddf_mallet = pd.concat([sent_topics_sorteddf_mallet, grp.sort_values(['Perc_Contribution'], ascending=[0]).head(1)], axis=0)# Reset Index
sent_topics_sorteddf_mallet.reset_index(drop=True, inplace=True)# Format
sent_topics_sorteddf_mallet.columns = ['Topic_Num', "Topic_Perc_Contrib", "Keywords", "Text"]# Show
sent_topics_sorteddf_mallet.head()复制代码

每个文档的最具代表性的主题

上面的表格输出实际上有20行,每个主题一个。它有主题编号,关键字和最具代表性的文档。该Perc_Contribution列只是给定文档中主题的百分比贡献。

20.主题文件分发

最后,我们希望了解主题的数量和分布,以判断讨论的范围。下表公开了该信息。

# Number of Documents for Each Topic
topic_counts = df_topic_sents_keywords['Dominant_Topic'].value_counts()# Percentage of Documents for Each Topic
topic_contribution = round(topic_counts/topic_counts.sum(), 4)# Topic Number and Keywords
topic_num_keywords = df_topic_sents_keywords[['Dominant_Topic', 'Topic_Keywords']]# Concatenate Column wise
df_dominant_topics = pd.concat([topic_num_keywords, topic_counts, topic_contribution], axis=1)# Change Column names
df_dominant_topics.columns = ['Dominant_Topic', 'Topic_Keywords', 'Num_Documents', 'Perc_Documents']# Show
df_dominant_topics复制代码

主题卷分布

21.结论

我们开始了解建模可以做什么主题。我们使用Gensim的LDA构建了一个基本主题模型,并使用pyLDAvis可视化主题。然后我们构建了mallet的LDA实现。您了解了如何使用一致性分数找到最佳主题数量,以及如何理解如何选择最佳模型。

最后,我们看到了如何聚合和呈现结果,以产生可能更具可操作性的见解。

希望你喜欢读这篇文章。如果您将您的想法留在下面的评论部分,我将不胜感激。

编辑:我看到你们中的一些人在使用LDA Mallet时遇到了错误,但我没有针对某些问题的解决方案。所以,我已经实现了一个变通方法和更有用的主题模型可视化。希望你会发现它很有帮助。变通模型的地址:www.machinelearningplus.com/nlp/topic-m…

上一篇文章链接地址:www.apexyun.com/shi-yong-ge…

查看英文原文

查看更多文章

公众号:银河系1号

联系邮箱:public@space-explore.com

(未经同意,请勿转载)

转载于:https://juejin.im/post/5cb34ba6f265da034e7e7f20

使用Gensim进行主题建模(二)相关推荐

  1. 主题建模-corpora语料库-PCA进行降维

    https://colab.research.google.com/drive/1F-1Ej7T2xnUKXSmDPjjOChNbBTvQlpnM?usp=sharing 考试 https://col ...

  2. 独家 | 使用Python的LDA主题建模(附链接)

    作者:Kamil Polak翻译:刘思婧 校对:孙韬淳本文约2700字,建议阅读5分钟本文为大家介绍了主题建模的概念.LDA算法的原理,示例了如何使用Python建立一个基础的LDA主题模型,并使用p ...

  3. 独家 | 利用Python实现主题建模和LDA 算法(附链接)

    作者:Susan Li翻译:陈之炎校对:陈汉青本文约1900字,建议阅读5分钟在这篇文章,我们将LDA应用于一组文档,并将文档按照主题分类. 标签:LDA 算法 主题建模是一种用于找出文档集合中抽象& ...

  4. 【火炉炼AI】机器学习042-NLP文本的主题建模

    (本文所使用的Python库和版本号: Python 3.6, Numpy 1.14, scikit-learn 0.19, matplotlib 2.2, NLTK 3.3) 文本的主题建模时用NL ...

  5. 决策树 建模_主题建模到类别树中

    决策树 建模 - This solution ranked 4th out of 10,000+ in All-India AI Hackathon, Automated Multi Label Cl ...

  6. 独家 | LDA主题建模和pyLDAvis可视化

    作者:Xuan Qi 翻译:方星轩 校对:欧阳锦 本文约1700字,建议阅读6分钟 本文的数据来自美国著名电视节目<老友记>.作者用python-Beautiful Soup抓取了224集 ...

  7. 把蛋糕拍扁:t-SNE降维和文本主题建模可视化实现

    前言 聚类算法的可视化,很美,t-SNE是其中一个很方便的工具,那么关于t-SNE的算法细节内容呢不是本文的重点,如果有需要可以去百度.本文没有选择mnist数据集进行实现,因为sklearn官网已经 ...

  8. 独家 | 图片主题建模?为什么不呢?!

    作者:Maarten Grootendorst 翻译:陈超 校对:赵茹萱本文约3200字,建议阅读5分钟 本文介绍了使用图片主题进行建模. 主题建模是一种允许用户在无监督情况下,在海量数据当中寻找主题 ...

  9. 需求分析与建模最佳实践_社交媒体和主题建模:如何在实践中分析帖子

    需求分析与建模最佳实践 主题建模的实际使用 (Practical use of topic modeling) There is a substantial amount of data genera ...

  10. python商品评论分析_NLP实战:用主题建模分析网购评论(附Python代码)

    现在电商行业势头正好,对在线零售商来说,他们不受库存或空间的限制,而实体店则必须在有限的空间中存储产品. 但是,在线购物也有它的局限之处,最大的难题之一就是检验产品的真伪.它的质量是否如宣传所说的那么 ...

最新文章

  1. 基于malloc与free函数的实现代码及分析
  2. 利用 %20 替换 空格
  3. MyBatis 解决模糊查询包含特殊字符
  4. 鸿蒙正式版什么时候上线,终于来了!华为鸿蒙正式版6月2日上线,EMUI时代落幕...
  5. docker volume源码分析
  6. 采用什么样的辅助工具能够有效地帮助孩子提高英语及数学的成绩?
  7. Linux--vmlinuz、vmlinux、initrd
  8. SpringBoot+Druid连接池
  9. Score UVA - 1585
  10. linux usb有线网卡驱动_Linux系统安装R8169网卡驱动的方法
  11. 信息系统规划方法-业务流程再造(BPR)
  12. Javascript解决音频audio在IOS系统下无法播放问题
  13. 【工具推荐】SwitchHosts管理本地 Hosts
  14. Python3 juypterlab pyecharts 饼图制作
  15. 【光模块单模和多模的区别及使用】
  16. POI导出Excel工具类(简单看完就会)
  17. 用python实现基于自媒体数据的人群聚类分析
  18. 如何安装Dev-C++(Dev-C++软件下载及安装教程)
  19. 正则环视 php,正则基础之 环视 Lookaround
  20. tushare 使用手册 股票信息获取

热门文章

  1. 注册表的基本操作(.Net)
  2. 网络工程师学习笔记(四)
  3. 看看你的密码有多安全?
  4. 报错’TypeError: only integer scalar arrays can be converted to a scalar index‘
  5. 文档级关系抽取方法总结
  6. 设计模式-01-设计模式简介及分类
  7. JavaEE学习14(应用)--用户自动登陆
  8. Flink Weekly | 每周社区动态更新
  9. java循环怎么分析_Java for循环的几种用法分析
  10. 靠java_人生靠反省,Java靠泛型