在Python中使用gensim在Tweets上运行LDA时出错

2024-10-02 10:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码,可以对Tweets运行LDA分析:

import logging, gensim, bz2
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

# load id->word mapping (the dictionary), one of the results of step 2 above
id2word = 'enams4nieuw.dict'
# load corpus iterator
mm = gensim.corpora.MmCorpus('enams4nieuw.mm')

print(mm)

# extract 100 LDA topics, using 1 pass and updating once every 1 chunk (10,000 documents)
lda = gensim.models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=100, update_every=1, chunksize=10000, passes=1)

当我尝试运行此脚本时,我收到以下带有错误消息的日志:

^{pr2}$

有人能解决这个问题吗?在


Tags: ofthe代码importloggingloadcorpustweets
2条回答

将变量id2word设置为字符串。在

看来你有一个文件名我猜你把字典弄脏了?在

id2word需要是字典。在

我也犯了同样的错误,看起来ldamodel.py尝试获取关键字的最大值而不是索引/ID的最大值,所以我的解决方案是简单地交换dict中的列

my_dict2 = {y:x for x,y in my_dict.items()}

相关问题 更多 >

    热门问题