感谢您的光临。我试图从这张空白的图表中获得帮助。我正在按照#17 https://www.machinelearningplus.com/nlp/topic-modeling-gensim-python/教程来使用LDAMallet构建不同主题数的连贯性得分图。这是我的代码:
os.environ['MALLET_HOME'] = 'C:\\mallet\\mallet-2.0.8'
mallet_path = 'C:\\mallet\\mallet-2.0.8\\bin\\mallet'
dictionary = gensim.corpora.Dictionary(processed_docs[:])
bow_corpus = [dictionary.doc2bow(doc) for doc in processed_docs]
def compute_coherence_values(dictionary, bow_corpus, documents, limit, start=2, step=3):
"""
Compute c_v coherence for various number of topics
Parameters:
----------
dictionary : Premium Billing data
corpus : Gensim bow_corpus
texts : document
limit : Max num of topics
Returns:
-------
model_list : List of LDA topic models
coherence_values : Coherence values corresponding to the LDA model with respective number of topics
"""
coherence_values = []
model_list = []
for num_topics in range(start, limit, step):
model = gensim.models.wrappers.LdaMallet(mallet_path, corpus=bow_corpus, num_topics=num_topics, id2word=dictionary)
model_list.append(model)
coherencemodel = CoherenceModel(model=model, texts=documents, dictionary=dictionary, coherence='c_v')
coherence_values.append(coherencemodel.get_coherence())
return model_list, coherence_values
# Can take a long time to run.
model_list, coherence_values = compute_coherence_values(dictionary=dictionary, bow_corpus=bow_corpus,
documents=documents, start=2, limit=40, step=6)
# Show graph
limit=40; start=2; step=6;
x = range(start, limit, step)
plt.plot(x, coherence_values)
plt.xlabel("Num Topics")
plt.ylabel("Coherence score")
plt.legend(("coherence_values"), loc='best')
plt.show()
# Print the coherence scores
for m, cv in zip(x, coherence_values):
print("Num Topics =", m, " has Coherence Value of", round(cv, 4))
数据:
我希望它看起来像:
请帮助
答案 0 :(得分:1)
我有同样的问题。有人说这是gensim最新版本的错误。我接受了某人的建议并降级了,但仍然有同样的问题。 我猜您想找到最佳数量的群集。我的建议是看pyLDAvis。
答案 1 :(得分:1)
我认为函数CoherenceModel中为参数“ texts”分配的值中存在问题。我不确定如何定义传递的值“ documents”,但是我使用了以下内容:
coherence_model_lda = CoherenceModel(model=lda_model, texts=[tokens], dictionary=dict, coherence='c_v')
我将“令牌”定义为单词列表。如果我传递texts =令牌,它给了我nan,那么当我在上面的列表中传递它时,它就可以正常工作!