学软件开发好还是网站开发好,成都设计公司哪家好,万维网官方网站,phpstudy做网站困惑度perplexity#xff1a;句子的概率的倒数。如果句子的概率越大#xff0c;说明这句话越符合人话的规律#xff0c;即p#xff08;句子#xff09;#xff0c;pp困惑度越小。模型对该句子就越不困惑。 通俗一点解释下就是#xff0c;困惑度表示的对于一篇文章来说…困惑度perplexity句子的概率的倒数。如果句子的概率越大说明这句话越符合人话的规律即p句子pp困惑度越小。模型对该句子就越不困惑。 通俗一点解释下就是困惑度表示的对于一篇文章来说我们有多不确定它是属于某个主题的。即主题的个数越多模型的困惑度就越低但是注意一点当主题数很多的时候生成的模型往往会过拟合所以不能单纯依靠困惑度来判断一个模型的好坏。这时候我们的另一个判断标准就有作用了。biubiu~一致性 困惑度可视化
def perplexity_visible_model(self, topic_num, data_num):description: 绘制困惑度-主题数目曲线param {type} return: # texts self.fenci_data()_, corpus self.weibo_lda()x_list []y_list []for i in range(1,topic_num):model_name ./lda_{}_{}.model.format(i, data_num)try:lda models.ldamodel.LdaModel.load(model_name)-perplexity lda.log_perplexity(corpus)print(perplexity)x_list.append(i)y_list.append(perplexity)except Exception as e:print(e)plt.xlabel(num topics)plt.ylabel(perplexity score)plt.legend((perplexity_values), locbest)plt.show()主题一致性coherence。更高的一致性分数表示更好的aspect可解释性意味着更有意义还有语义上更连贯。
def visible_model(self, topic_num, data_num):description: 可视化模型param :topic_num:主题的数量param :data_num:数据的量return: 可视化lda模型dictionary, _ self.weibo_lda()texts self.fenci_data()x_list []y_list []for i in range(1,topic_num):model_name ./lda_{}_{}.model.format(i, data_num)try:lda models.ldamodel.LdaModel.load(model_name)cv_tmp CoherenceModel(modellda, textstexts, dictionarydictionary, coherencec_v)x_list.append(i)y_list.append(cv_tmp.get_coherence())except:print(没有这个模型:{}.format(model_name))plt.plot(x_list, y_list)plt.xlabel(num topics)plt.ylabel(coherence score)plt.legend((coherence_values), locbest)plt.show()可借鉴网址 https://zhuanlan.zhihu.com/p/106982034 实战
#找到最佳k通过主题一致性得分去找
import tomotopy as tp
tp.isa
def find_k(docs,min_k1,max_k20,min_df2):#min_df 词语最少出现在两个文档中import matplotlib.pyplot as pltscores []for k in range(min_k,max_k):mdl tp.LDAModel(min_df min_df,k k,seed 555)#print(mdl,mdl)for words in docs:if words:mdl.add_doc(words)mdl.train(20)coh tp.coherence.Coherence(mdl)scores.append(coh.get_score())plt.plot(range(min_k,max_k),scores)plt.xlabel(number of topics)plt.ylabel(coherence)plt.show()
find_k(docs df[words],min_k1,max_k40,min_df2) 通过图形我暂时将主题定为10个。其中的tomotopy可见网址tomotopy | 速度最快的LDA主题模型