首先,确认你已经安装好pyldavis和gensim库。
pip install gensim
pip install pyldavis
其次,如果报错not attribute 是因为pyldavis的版本不同,新版本需要做如下更改:把所有的pyLDAvis.gensim改成gensimvis即可。
import pyLDAvis.gensim as gensimvis
pyLDAvis.enable_notebook()
vis_sub_10 = gensimvis.prepare(lda_fst, corpus, dic, sort_topics = False)
vis_sub_20 = gensimvis.prepare(lda_snd, corpus, dic, sort_topics = False)
pyLDAvis.display(vis_sub_10)
或者你也可以安装低版本的pyldavis
pip install pyLDAvis==2.1.2
import pyLDAvis.gensim
pyLDAvis.enable_notebook()
vis_sub_10 = pyLDAvis.gensimda_fst, corpus, dic, sort_topics = False)
vis_sub_20 = pyLDAvis.gensim.prepare(lda_snd, corpus, dic, sort_topics = False)
pyLDAvis.display(vis_sub_10)
参考链接: http://cache.baiducontent.com/c?m=zAKWy52YTTPOQZNZxRyGmDA1gug9JxKD8901fdKZgXz_1BxCM2IhZznbOGDQG-xauyI_EuQ7wa3NvUNYhH4sHVShWY9yzjJtAwAvN4cWmdYa8v4YuC1fbZfD4siHDc89u-O75PF8mZjdZq3mUeNuJZ8gqrsvJRVXve1NhCrirOOJlGWPneoSTJCTNy3FKguCcUp4BA7Lg1fo1LDyRAtHe3seM-noT0B0-3pwP82Ypey&p=9b759a4ed39c00e6039fc7710f40&newp=8b2a975f848017ef12bd9b75085692695d0fc20e3cd7d601298ffe0cc4241a1a1a3aecbe25261004d7c67f6403a94c5ce9f53d763d0034f1f689df08d2ecce7e35da64623056&s=cfcd208495d565ef&user=baidu&fm=sc&query=module+%27pyLDAvis%27+has+no+attribute+%27gensim%27&qid=e49f8733000254a0&p1=1.
最后,本篇原代码来自王老师的b站课程金融科技工具箱,感谢老师的无偿分享。