文章插图
主题建模Genism包专门用于主题建模 。主题模型是一种统计模型,用于发现出现在文档集合中的抽象“主题” 。
我将展示如何使用LDA(Latent Dirichlet Allocation)提取主题:生成统计模型,允许使用未观察到的组来解释观察集,这些组可以解释为什么数据的某些部分是相似的 。基本上,文档被表示为潜在主题的随机混合,其中每个主题的特征是分布在单词上 。
让我们看看我们可以从科技新闻中提取哪些主题 。我需要指定模型必须聚类的主题数量,我将尝试使用3个:
y = "TECH"corpus = dtf[dtf["y"]==y]["text_clean"] ## pre-process corpus lst_corpus = [] for string in corpus: lst_words = string.split() lst_grams = [" ".join(lst_words[i:i + 2]) for i in range(0, len(lst_words), 2)] lst_corpus.append(lst_grams)## map words to an id id2word = gensim.corpora.Dictionary(lst_corpus)## create dictionary word:freq dic_corpus = [id2word.doc2bow(word) for word in lst_corpus] ## train LDA lda_model = gensim.models.ldamodel.LdaModel(corpus=dic_corpus, id2word=id2word, num_topics=3, random_state=123, update_every=1, chunksize=100, passes=10, alpha='auto', per_word_topics=True) ## output lst_dics = [] for i in range(0,3): lst_tuples = lda_model.get_topic_terms(i) for tupla in lst_tuples: lst_dics.append({"topic":i, "id":tupla[0], "word":id2word[tupla[0]], "weight":tupla[1]}) dtf_topics = pd.DataFrame(lst_dics, columns=['topic','id','word','weight']) ## plot fig, ax = plt.subplots() sns.barplot(y="word", x="weight", hue="topic", data=https://www.isolves.com/it/ai/2020-06-19/dtf_topics, dodge=False, ax=ax).set_title('Main Topics') ax.set(ylabel="", xlabel="Word Importance") plt.show()

文章插图
仅仅用3个主题来概括这6年的内容可能有点难,但正如我们所看到的,所有关于苹果公司的内容都以同样的主题结束 。
结论本文演示了如何使用NLP分析文本数据并为机器学习模型提取特征 。
我展示了如何检测数据使用的语言,以及如何预处理和清除文本 。然后我解释了长度的不同度量,用Textblob进行了情绪分析,并使用SpaCy进行命名实体识别 。最后,我解释了使用scikiti - learning的传统词频方法与使用Gensim的现代语言模型的区别 。
作者:Mauro Di Pietro
deephub翻译组
推荐阅读
- 茶在生活中的其他用处,玫瑰花茶泡法技巧
- 把MySQL中的各种锁及其原理都画出来
- 互动直播中的前端技术——即时通讯
- 茶叶在旅行中的作用,白茶的保健功效介绍
- 敦煌月牙泉其中的水,辨证茶疗与疾病的关系
- 人的身材在一天中的什么时候最高?
- 一款强大的本地文件内容搜索软件,可搜索文件中的文字
- 图解 Go 微服务中的熔断器和重试
- 血缘关系在中国文化中的作用
- Netty 中的内存分配浅析
