NameError:未定义名称'gensim'

问题描述 投票:0回答:2

我已经导入了我需要的所有包裹

from gensim import corpora
from gensim import models
from gensim.models import LdaModel
from gensim.models import TfidfModel
from gensim.models import CoherenceModel

然后我需要运行LdaMallet模型,所以我像这样导入它们

from gensim.models.wrappers import LdaMallet

当运行下面的代码时,我有一些Namerror

mallet_path = 'mallet-2.0.8/bin/mallet' # update this path

ldamallet = gensim.models.wrappers.LdaMallet(mallet_path,corpus=corpus, num_topics=20, id2word=dictionary)

发生了错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-1c656d4f8c21> in <module>()
      1 mallet_path = 'mallet-2.0.8/bin/mallet' # update this path
      2 
----> 3 ldamallet = gensim.models.wrappers.LdaMallet(mallet_path,corpus=corpus, num_topics=20, id2word=dictionary)

NameError: name 'gensim' is not defined

我以为我已经导入了我需要的所有东西,并且在我尝试使用槌之前,lda模型运行良好。所以有什么问题?

python gensim lda mallet
2个回答
1
投票

只需直接使用LdaMallet(mallet_path,corpus=corpus, num_topics=20, id2word=dictionary),因为您已经从gensim.models.wrappers导入了所需的方法


1
投票

因为你有这个import

from gensim import models

你需要在你的代码中引用wrappers作为models.wrappers等,而不是gensim.models.wrappers

但你也是这样做的:

from gensim.models.wrappers import LdaMallet

所以你可以直接引用LdaMallet,如:

ldamallet = LdaMallet(mallet_path,corpus=corpus, num_topics=20, id2word=dictionary)

请注意,我在这里遗漏了gensim.models.wrappers.;你不需要它。

© www.soinside.com 2019 - 2024. All rights reserved.