Gensim LdaModel 报错: ValueError: not enough values to unpack (expected 2, got 1) 解决方法
在使用 Gensim 的 LdaModel 时,可能会遇到如下错误:
da_model = gensim.models.ldamodel.LdaModel(tfidf_matrix.todense(), num_topics=8, id2word=dict(enumerate(feature_names)),
passes=10)
报错:ValueError: not enough values to unpack (expected 2, got 1)
该错误的原因是,LdaModel 需要一个稀疏矩阵作为输入,而 tfidf_matrix.todense() 方法返回的是一个密集矩阵,导致出现了数值不匹配的错误。
解决方法:
将 tfidf_matrix.todense() 改为 tfidf_matrix,因为 tfidf_matrix 本身就是一个稀疏矩阵。
修改后的代码如下:
da_model = gensim.models.ldamodel.LdaModel(tfidf_matrix, num_topics=8, id2word=dict(enumerate(feature_names)), passes=10)
经过修改后,代码应该能够正常运行。
原文地址: https://www.cveoy.top/t/topic/nJbM 著作权归作者所有。请勿转载和采集!