我试图在我的主题建模项目中可视化主题,但 pyLDAvis 似乎不起作用。我的代码如下:
!pip install pyLDAvis==2.1.2
import pyLDAvis.gensim_models as gensimvis
pyLDAvis.enable_notebook()
lda_viz = gensimvis.prepare(ldamodel, corpus, dictionary)
但它总是给我同样的错误:TypeError: object of type 'LdaModel' has no len()
我正在使用 colab,我最初尝试简单地使用!pip install pyLDAvis
,但我在 Stackoverflow 中阅读了另一个问题,即新版本的 pyLDAvis 与 colab 存在一些问题。我不知道问题可能是什么。Ldamodel 之所以有效,是因为它使用以下代码打印主题:
ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics = 1, id2word=dictionary,
passes=10,iterations=20, chunksize = 100,
eval_every = None, random_state=0)
ldamodel.save('/content/drive/MyDrive/CompLingProject/dictionary.gensim')
topics = ldamodel.print_topics(num_words=4)
for topic in topics:
print(topic)
我查找了几个主题建模教程,它们都使用相同的代码。