--------------------------------------------------------------------------- ValueError Traceback most recent call last ipython-input-30-011387cb6968 in module 34 fo
在CoherenceModel的初始化中,如果将coherence参数设为'c_v',则需要提供texts参数,因此可以在循环中增加判断,如果coherence为'c_v',则需要检查texts是否存在,如果不存在,则抛出异常;如果存在,则继续计算一致性指标。修改后的代码如下:
coherence = []
max_topics = 10
for num_topics in range(1, max_topics+1):
lda_model = LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics)
if coherence == 'c_v' and texts is None:
raise ValueError("'texts' should be provided for c_v coherence.")
else:
coherence_model_lda = CoherenceModel(model=lda_model, corpus=corpus, dictionary=dictionary, coherence='c_v', texts=texts)
coherence_lda = coherence_model_lda.get_coherence()
coherence.append(coherence_lda)
其中,texts是用于计算一致性指标的文本数据。如果在初始化时未提供该参数,则会抛出异常。
原文地址: http://www.cveoy.top/t/topic/bxxS 著作权归作者所有。请勿转载和采集!