Gensim LDA模型训练报错:TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]
Gensim LDA模型训练报错:TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]
在使用Gensim训练LDA模型时,可能会遇到以下错误:
da_model = gensim.models.ldamodel.LdaModel(tfidf_matrix, num_topics=8, id2word=dict((i, s)
for i, s in enumerate(feature_names)),
passes=10)
报错信息:TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]
该错误通常是因为将稀疏矩阵传递给LdaModel模型导致的。Gensim的LdaModel模型需要稠密矩阵作为输入。
解决方法
将tfidf_matrix转换为稠密矩阵即可解决该错误:
da_model = gensim.models.ldamodel.LdaModel(tfidf_matrix.todense(), num_topics=8, id2word=dict((i, s)
for i, s in enumerate(feature_names)),
passes=10)
解释:
tfidf_matrix.todense()将稀疏矩阵转换为稠密矩阵。LdaModel能够正常处理稠密矩阵,并完成LDA模型训练。
希望以上内容能够帮助您解决该错误。
原文地址: http://www.cveoy.top/t/topic/nI9C 著作权归作者所有。请勿转载和采集!