ITPub博客

首页 > IT职业 > IT生活 > LDA主题模型简介及Python实现

LDA主题模型简介及Python实现

IT生活 作者:专注的阿熊 时间:2022-10-31 17:49:54 0 删除 编辑

import gensim

from gensim import corpora

import matplotlib.pyplot as plt

import matplotlib

import numpy as np

import warnings

warnings.filterwarnings('ignore')  # To ignore all warnings that arise here to enhance clarity

from gensim.models.coherencemodel import CoherenceModel

from gensim.models.ldamodel import LdaModel

  # 准备数据

PATH = "E:/data/output.csv"

file_object2=open(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n')  # 一行行的读取内容

data_set=[] # 建立存储分词的列表

for i in range(len(file_object2)):

     result=[]

     seg_list = file_object2[i].split()

     for w in seg_list :# 读取每一行分词

         result.append(w)

     data_set.append(result)

print(data_set)

dictionary = corpora.Dictionary(data_set)  # 构建 document-term matrix

corpus = [dictionary.doc2bow(text) for text in data_set]

#Lda = gensim.models.ldamodel.LdaModel  # 创建 LDA 对象

# 计算困惑度

def perplexity(num_topics):

     ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30)

     print(ldamodel.print_topics(num_topics=num_topics, num_words=15))

     print(ldamodel.log_perplexity(corpus))

     return ldamodel.log_perplexity(corpus)

# 计算 coherence

def coherence(num_topics):

     ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30,random_state = 1)

     print(ldamodel.print_topics(num_topics=num_topics, num_words=10))

     ldacm = CoherenceModel(model=ldamodel, texts=data_set, dictionary=dictionary, coherence='c_v')

     print(ldacm.get_coherence())

     return ldacm.get_coherence()

# 绘制困惑度折线图

x = range(1,15)

# z = [perplexity(i) for i in x]

y = [coherence(i) for i in x]

plt.plot(x, y)

plt.xlabel(' 主题数目 ')

plt.ylabel('coherence 大小 ')

plt.rcParams['font.sans-serif']=['SimHei']

matplotlib.rcParams['axes.unicode_minus']=False

plt.title(' 主题 -coherence 变化情况 ')

plt.show()

from gensim.models import LdaModel

import pandas as pd

from gensim.corpora import Dictionary

from gensim import corpora, models

import csv

# 准备数据

PATH = "E:/data/output1.csv"

file_object2=外汇跟单gendan5.comopen(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n')  # 一行行的读取内容

data_set=[] # 建立存储分词的列表

for i in range(len(file_object2)):

     result=[]

     seg_list = file_object2[i].split()

     for w in seg_list :# 读取每一行分词

         result.append(w)

     data_set.append(result)

dictionary = corpora.Dictionary(data_set)  # 构建 document-term matrix

corpus = [dictionary.doc2bow(text) for text in data_set]

lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes = 30,random_state=1)

topic_list=lda.print_topics()

print(topic_list)

result_list =[]

for i in lda.get_document_topics(corpus)[:]:

     listj=[]

     for j in i:

         listj.append(j[1])

     bz=listj.index(max(listj))

     result_list.append(i[bz][0])

print(result_list)

import pyLDAvis.gensim

pyLDAvis.enable_notebook()

data = pyLDAvis.gensim.prepare(lda, corpus, dictionary)

pyLDAvis.save_html(data, 'E:/data/topic.html')


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2921183/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    410
  • 访问量
    276749