LDA主題模型簡介及Python實現
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部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2921183/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- LDA主題模型LDA模型
- 主題模型值LDA模型LDA
- 主題模型-LDA淺析模型LDA
- 通俗理解LDA主題模型LDA模型
- 文字主題模型之LDA(一) LDA基礎模型LDA
- Gensim做中文主題模型(LDA)模型LDA
- 用WordCloud詞雲+LDA主題模型,帶你讀一讀《芳華》(python實現)CloudLDA模型Python
- 文字主題抽取:用gensim訓練LDA模型LDA模型
- 用scikit-learn學習LDA主題模型LDA模型
- 文字主題模型之LDA(二) LDA求解之Gibbs取樣演算法模型LDA演算法
- 文字主題模型之LDA(三) LDA求解之變分推斷EM演算法模型LDA演算法
- [python] LDA處理文件主題分佈及分詞、詞頻、tfidf計算PythonLDA分詞
- 淺談話題模型:LSA、PLSA、LDA模型LDA
- 【轉】概念主題模型簡記模型
- 用 LDA 做主題模型:當 MLlib 邂逅 GraphXLDA模型
- [python] LDA處理文件主題分佈程式碼入門筆記PythonLDA筆記
- Python greenlet使用介紹及實現原理Python
- Python線性分類模型簡介Python模型
- 執行緒池的介紹及簡單實現執行緒
- 實現Python壓力測試工具|Python 主題月Python
- Epoll 模型簡介模型
- socket 模型簡介模型
- 博主簡介
- pLSA主題模型模型
- 主題:Linux檔案系統簡介Linux
- 《Python機器學習實踐》簡介Python機器學習
- Python-split()函式用法及簡單實現Python函式
- 主題模型TopicModel:Unigram、LSA、PLSA主題模型詳解模型
- 預處理(3):python實現用scikit-learn實現的線性判別分析(LDA)PythonLDA
- Select 模型簡介模型
- OpenGL簡單介紹及實踐
- Flume NG 簡介及配置實戰
- Android Lint 實踐 —— 簡介及常見問題分析Android
- 《機器學習Python實現_10_10_整合學習_xgboost_原理介紹及迴歸樹的簡單實現》機器學習Python
- CRC校驗原理簡介及C程式碼實現說明C程式
- DotNet Dictionary 實現簡介
- RSA加密演算法簡單介紹以及python實現加密演算法Python
- vue 實現原理及簡單示例實現Vue