Python TypeError:获取了参数的多个值';字典';
我阅读了之前被问及此错误的其他问题。但我仍然不知道我犯了什么错误。当我调用函数时,我得到了此错误。我是这个论坛的新手,任何帮助我解决问题的人都将不胜感激。这是我的代码Python TypeError:获取了参数的多个值';字典';,python,python-3.x,typeerror,lda,Python,Python 3.x,Typeerror,Lda,我阅读了之前被问及此错误的其他问题。但我仍然不知道我犯了什么错误。当我调用函数时,我得到了此错误。我是这个论坛的新手,任何帮助我解决问题的人都将不胜感激。这是我的代码 def lda_train(self, documents): # create dictionary dictionary= corpora.Dictionary(documents) dictionary.compactify() dictionary.save(
def lda_train(self, documents):
# create dictionary
dictionary= corpora.Dictionary(documents)
dictionary.compactify()
dictionary.save(self.DICTIONARY_FILE) # store the dictionary, for future reference
print ("============ Dictionary Generated and Saved ============")
############# Create Corpus##########################
corpus = [dictionary.doc2bow(text) for text in documents]
print('Number of unique tokens: %d' % len(dictionary))
print('Number of documents: %d' % len(corpus))
return dictionary,corpus
def compute_coherence_values(dictionary,corpus,documents, limit, start=2, step=3):
num_topics = 10
coherence_values = []
model_list = []
for num_topics in range(start, limit, step):
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,id2word=dictionary, num_topics=num_topics, random_state=100, alpha='auto')
model_list.append(model)
coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v')
coherence_values.append(coherencemodel.get_coherence())
return model_list, coherence_values
当我使用以下代码在main中调用此函数时:
if __name__ == '__main__':
limit=40
start=2
step=6
obj = LDAModel()
lda_input = get_lda_input_from_corpus_folder('./dataset/TRAIN')
dictionary,corpus =obj.lda_train(lda_input)
model_list, coherence_values = obj.compute_coherence_values(dictionary=dictionary,corpus=corpus, texts=lda_input, start=2, limit=40, step=6)
我收到一条错误消息:
model_list, coherence_values=obj.compute_coherence_values(dictionary=dictionary,corpus=corpus, texts=lda_input, start=2, limit=40, step=6)
TypeError: compute_coherence_values() got multiple values for argument 'dictionary'
TL;DR
改变
def compute\u coherence\u值(字典、语料库、文档、限制、开始=2、步骤=3)
到
def compute\u coherence\u值(self、dictionary、corpus、documents、limit、start=2、step=3)
您忘记将
self
作为第一个参数传递,因此实例将作为dictionary
参数传递,但您也将dictionary
作为显式关键字参数传递
这种行为很容易复制:
class Foo:
def bar(a):
pass
Foo().bar(a='a')
TypeError: bar() got multiple values for argument 'a'
TL;DR
改变
def compute\u coherence\u值(字典、语料库、文档、限制、开始=2、步骤=3)
到
def compute\u coherence\u值(self、dictionary、corpus、documents、limit、start=2、step=3)
您忘记将
self
作为第一个参数传递,因此实例将作为dictionary
参数传递,但您也将dictionary
作为显式关键字参数传递
这种行为很容易复制:
class Foo:
def bar(a):
pass
Foo().bar(a='a')
TypeError: bar() got multiple values for argument 'a'
谢谢你的回复,我还没有收到。我应该通过自我论证或其他任何修改吗?模型列表,一致性值=obj。计算一致性值(dictionary=dic,corpus=corpus,text=lda\u input,start=2,limit=40,step=6)类型错误:计算一致性值()得到一个意外的关键字参数“text”@ameera,在
compute\u coherence\u values
definition中没有text
参数……感谢您的回复,我仍然没有得到。我应该通过自参数或任何其他更改吗?model\u list,coherence\u values=obj.compute\u coherence\u values(dictionary=dic,corpus=corpus,text=lda\u input,start=2,limit=40,step=6)TypeError:compute_coherence_values()得到一个意外的关键字参数“text”@ameera好吧,在compute_coherence_values
定义中没有text
参数。。。。