Python/gensim主题模型库

本文介绍了如何使用Python的gensim库进行主题建模,包括LSI和LDA模型的实现。LSI模型能将文档转换到低维度空间,而LDA模型则是基于贝叶斯的非监督学习算法,用于发现文档中的潜在主题。文章还讨论了模型训练、转换以及相似度计算的过程。
摘要由CSDN通过智能技术生成

每个py文件称之为模块,每个具有init.py文件的目录被称为包。只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用。

  1. 如果要使用的模块和当前文件在同一目录,只要import相应的文件名就可以。
  2. 如果使用的模块不在同一目录下,使用sys.path.append方法将模块所在目录加入到搜素目录中。然后进行import即可。这种方法是暂时的。
  3. 使用PYTHONPATH变量,不同的路径之间用分号隔开。设置的路径会自动加入到sys.path中,加入模块时使用import即可。

查看python已经安装的模块
help(‘modules’)

安装模块
1.直接添加到路径下。

import sys
sys.path.append(“path”)

2.单文件模块
直接把文件拷贝到$python_dir/lib。

gensim库

主题模型是对文字隐含主题进行建模的方法。主题表现为词语的条件概率分布,与主题关系越密切的词语,条件概率就越大。
主题模型
对于每一篇文档,左边的矩阵C是已知的,右边两个矩阵未知,主题模型通过大量已知的”词语-文档”矩阵C,通过训练,推导出右边的两个矩阵。主题模型训练的方法有两种,pLSA和LDA。
使用模型将文档vector转换成另一个vector,这样可以修正vector的含义,减少噪音,期望达到更好的效果。
from gensim import corpora, models, similarities
//从texts中得到distinct
dictionary=corpora.Dictionary(texts)
dictionary.save(‘data.dict’)
//将得到的主题word赋予相应的数字id
dictionary.token2id
//加载dict数据文件
dictionary=corpora.Dictionary.load(‘data.dict’)

//将一个文档转换为vector
new_doc=”Human computer interaction”
//计算文档中主题词的个数,(distinctid,appears)
new_vec=dictionary.doc2bow(new_doc.lower().split())
corpus=[dictionary.doc2bow(text) for text in texts]
corpora.MmC

首先,需要安装gensim,可以使用以下命令进行安装: ``` pip install gensim ``` 接下来,我们使用gensim实现LDA主题模型文本分析及可视化的步骤如下: 1. 导入所需的和数据集 ``` import logging import gensim from gensim import corpora from gensim.models.ldamodel import LdaModel from gensim.models import CoherenceModel import pyLDAvis.gensim import pandas as pd logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) # 导入数据集 df = pd.read_csv('data.csv') texts = df['text'].tolist() ``` 2. 对文本进行预处理 ``` from nltk.corpus import stopwords from nltk.stem.wordnet import WordNetLemmatizer import string stop = set(stopwords.words('english')) exclude = set(string.punctuation) lemma = WordNetLemmatizer() def clean(doc): stop_free = " ".join([i for i in doc.lower().split() if i not in stop]) punc_free = ''.join(ch for ch in stop_free if ch not in exclude) normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split()) return normalized doc_clean = [clean(doc).split() for doc in texts] ``` 3. 创建词袋模型,并生成LDA模型 ``` # 创建词袋模型 dictionary = corpora.Dictionary(doc_clean) doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean] # 生成LDA模型 lda_model = LdaModel(doc_term_matrix, num_topics=10, id2word=dictionary, passes=50) ``` 4. 计算主题模型的一致性得分 ``` coherence_model_lda = CoherenceModel(model=lda_model, texts=doc_clean, dictionary=dictionary, coherence='c_v') coherence_lda = coherence_model_lda.get_coherence() print('Coherence Score:', coherence_lda) ``` 5. 可视化主题模型 ``` vis = pyLDAvis.gensim.prepare(lda_model, doc_term_matrix, dictionary) pyLDAvis.display(vis) ``` 以上就是使用gensim实现LDA主题模型文本分析及可视化的步骤。需要注意的是,这里仅提供了一个简单的示例,实际应用中还需要根据具体情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值