计算一致性得分时出错– AttributeError:“ dict”对象没有属性“ id2token”

我是NLP的初学者,这是我第一次进行主题建模。我能够生成模型,但是无法生成一致性度量。

将术语文档矩阵从df->稀疏矩阵-> gensim语料库转换为新的gensim格式

sparse_counts = scipy.sparse.csr_matrix(data_dtm)
corpus = matutils.Sparse2Corpus(sparse_counts)
corpus

计算一致性得分时出错– AttributeError:“ dict”对象没有属性“ id2token”

df_lemmatized.head()

计算一致性得分时出错– AttributeError:“ dict”对象没有属性“ id2token”

# Gensim also requires dictionary of the all terms and their respective location in the term-document matrix
tfidfv = pickle.load(open("tfidf.pkl","rb"))
id2word = dict((v,k) for k,v in tfidfv.vocabulary_.items())
id2word

计算一致性得分时出错– AttributeError:“ dict”对象没有属性“ id2token”

这是我的模特

lda = models.LdaModel(corpus=corpus,id2word=id2word,num_topics=15,passes=10,random_state=43)
lda.print_topics()

计算一致性得分时出错– AttributeError:“ dict”对象没有属性“ id2token”

最后,这是我尝试使用相干模型获得相干得分的地方:

# Compute Perplexity
print('\nPerplexity: ',lda.log_perplexity(corpus))  

# Compute Coherence Score
coherence_model_lda = CoherenceModel(model=lda,texts=df_lemmatized.long_title,dictionary=id2word,coherence='c_v')
coherence_lda = coherence_model_lda.get_coherence()
print('\nCoherence Score: ',coherence_lda)

这是错误:

---> 57如果不是dictionary.id2token:#可能未在标准gensim.corpora.Dictionary中初始化      58 setattr(dictionary,'id2token',{v:k对于k,v在dictionary.token2id.items()}中)      59 AttributeError:“ dict”对象没有属性“ id2token”

zxj3125 回答:计算一致性得分时出错– AttributeError:“ dict”对象没有属性“ id2token”

我没有您的数据,所以我无法重现该错误。所以,我会猜一个!问题出在您的WKWebView内部,应该是id2word而不只是corpora.dictionary.Dictionary。因此,您需要执行以下操作:

dict

我认为它现在应该可以正常工作!

本文链接:https://www.f2er.com/3039418.html

大家都在问