2015-03-31 77 views
1

使用gensim鸣叫运行LDA时,我有下面的代码,运行在鸣叫的LDA分析:错误关于Python

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) 

当我尝试运行此脚本时,我收到下面的日志,错误信息:

MmCorpus(40152 documents, 13061 features, 384671 non-zero entries) 
2015-03-31 16:52:50,246 : INFO : loaded corpus index from enams4nieuw.mm.index 
2015-03-31 16:52:50,246 : INFO : initializing corpus reader from enams4nieuw.mm 
2015-03-31 16:52:50,246 : INFO : accepted corpus with 40152 documents, 13061 features, 384671 non-zero entries 
Traceback (most recent call last): 
    File "C:/Users/gerbuiker/PycharmProjects/twitter-streaming.py/lda.py", line 15, in <module> 
    lda = gensim.models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=100, update_every=1, chunksize=10000, passes=1) 
    File "C:\Users\gerbuiker\AppData\Roaming\Python\Python27\site-packages\gensim\models\ldamodel.py", line 244, in __init__ 
self.num_terms = 1 + max(self.id2word.keys()) 
AttributeError: 'str' object has no attribute 'keys' 

Process finished with exit code 1 

任何人都有这个解决方案吗?

回答

1

将变量id2word设置为字符串。

看来你有一个文件名 - 我假设你腌你的字典?

id2word需要是字典。

0

我有同样的错误,它似乎像ldamodel.py试图采取关键字的最大值而不是索引/ ID的,所以我的解决方案只是交换字典中的列。

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