用python计算lda语言模型的困惑度并作图_第1页
用python计算lda语言模型的困惑度并作图_第2页
用python计算lda语言模型的困惑度并作图_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

⽤python计算lda语⾔模型的困惑度并作图困惑度⼀般在⾃然语⾔处理中⽤来衡量训练出的语⾔模型的好坏。在⽤LDA做主题和词聚类时,⽂章中的公式为:perplexity=exp^{-(∑log(p(w)))/(N)}P(w)=∑zp(z|d)*p(w|z)【z,d分别指训练过的主题和测试集的各篇⽂档】。分母的N是测试集中出现的所有词,或者说是测试集的总长度,不排重。因⽽python程序代码块需要包括⼏个⽅⾯:2.统计测试集长度,即计算perplexity的分母计算困惑度对于不同的Topic数量的模型,计算的困惑度,画折线图。python代码如下:1#-*-coding:UTF-8-*-2importnumpyimportmathimportstringimportmatplotlib.pyplotasplt6importre7defdictionary_found(wordlist): #对模型训练出来的词转换成⼀个词为KEY,概率为值的字典。word_dictionary1={}foriinxrange(len(wordlist)):11 ifi%2==0:ifword_dictionary1.has_key(wordlist[i])==True:word_probability=word_dictionary1.get(wordlist[i])word_probability=float(word_probability)+float(wordlist[i+1])word_dictionary1.update({wordlist[i]:word_probability})else:word_dictionary1.update({wordlist[i]:wordlist[i+1]})else:passreturnword_dictionary121deflook_into_dic(dictionary,testset): #对于测试集的每⼀个词,在字典中查找其概率。'''CalculatestheTF-listforperplexity'''frequency=[]letter_list=[]26 a=0.0forletterintestset.split():ifletternotinletter_list:letter_list.append(letter)letter_frequency=(dictionary.get(letter))frequency.append(letter_frequency)else:passforeachinfrequency:ifeach!=None:a+=float(each)else:passreturna4041deff_testset_word_count(testset): #测试集的词数统计'''returethesumofwordsintestsetwhichisthedenominatoroftheformulaofPerplexity'''testset_clean=testset.split()return(len(testset_clean)-testset.count("\n"))46deff_perplexity(word_frequency,word_count): #计算困惑度'''SearchtheprobabilityofeachwordindictionaryCalculatestheperplexityoftheLDAmodelforeveryparameterT'''duishu=-math.log(word_frequency)kuohaoli=duishu/word_countperplexity=math.exp(kuohaoli)returnperplexity54defgraph_draw(topic,perplexity): #做主题数与困惑度的折线图x=topicy=perplexityplt.plot(x,y,color="red",linewidth=2)plt.xlabel("NumberofTopic")plt.ylabel("Perplexity")plt.show()6263topic=[]perplexity_list=[]f1=open('/home/alber/lda/GibbsLDA/jd/test.txt','r') #测试集⽬录testset=f1.read()testset_word_count=f_testset_word_count(testset) #callthefunctiontocountthesum-wordsintestset69foriinxrange(14):dictionary={}topic.append(5*(3i+1)) #模型⽂件名的迭代公式trace="/home/alber/lda/GibbsLDA/jd/stats/model-final-"+str(5*(i+1))+".txt"#模型⽬录f=open(trace,'r')text=f.readlines()word_list=[]forlineintext:if"Topic"notinline:line_clean=line.split()word_list.extend(line_clean)else:password_dictionary=dictionary_found(word_list)frequency=look_into_dic(word_dictionary,testset)perplexity=f_perplexity(frequency,testset_word_count)perplexity_list.append(perplexity)86graph_draw(top

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论