承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601
当前位置:首页  >  软件开发  >  python的lda模型

python的lda模型

管理员 2023-08-07 07:54:42 软件开发 0 ℃ 0 评论 2052字 收藏

python的lda模型

Python的LDA模型(Latent Dirichlet Allocation)是一种无监督学习的主题建模算法,可用于从文本数据中辨认隐藏的话题。该算法在利用程序中的多个领域中得到了广泛的利用,例如文本发掘、情感分析、内容推荐和广告定位等。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
import gensim
from gensim import models
from gensim.utils import simple_preprocess
from gensim.parsing.preprocessing import STOPWORDS
from nltk.stem import WordNetLemmatizer, SnowballStemmer
from nltk.stem.porter import *
np.random.seed(2021)
df = pd.read_csv('news.csv')
stemmer = SnowballStemmer('english')
def lemmatize_stemming(text):
return stemmer.stem(WordNetLemmatizer().lemmatize(text, pos='v'))
def preprocess(text):
result = []
for token in simple_preprocess(text):
if token not in STOPWORDS and len(token) >3:
result.append(lemmatize_stemming(token))
return result
processed_docs = df['text'].map(preprocess)
dictionary = gensim.corpora.Dictionary(processed_docs)
dictionary.filter_extremes(no_below=5, no_above=0.5, keep_n=100000)
bow_corpus = [dictionary.doc2bow(doc) for doc in processed_docs]
lda_model = models.ldamodel.LdaModel(bow_corpus, num_topics=10, id2word=dictionary, passes=10, alpha='auto', per_word_topics=True)
for idx, topic in lda_model.print_topics(⑴):
print('Topic: {} \nWords: {}'.format(idx, topic))

以上代码展现了怎样使用Python中的Gensim库实现LDA模型。

在本示例中,我们从输入的新闻文本数据中去除停用词、进行了分词、词干化和词形还原等文本预处理步骤。然后我们创建了词典,过滤了低频词和高频词。使用LDA模型进行训练,并得到了输出的主题辞。

该LDA模型的输出结果可帮助我们更好地理解文本数据中的主题。通过处理新闻数据,我们可以发现这些新闻触及到的主题包括政治、经济、金融、科技等领域。

文章来源:丸子建站

文章标题:python的lda模型

https://www.wanzijz.com/view/69423.html

上一篇:python的p值表

下一篇:python的ps安装

TAG: php教程 centos

相关文章

Related articles

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信