gpt4 book ai didi

python - (Gensim) 带有 alpha 参数的 ValueError : invalid shape,

转载 作者:太空宇宙 更新时间:2023-11-03 18:59:01 26 4
gpt4 key购买 nike

首先,这是获取执行LDA的语料库主题分布的正确方法吗?

lda = LdaModel(corpus,  num_topics=500, update_every=0, passes=2)
#get the topics distribution of the corpus
result=lda[corpus]

现在,当我将 alpha 参数添加到 LDA 并尝试将语料库转换为稀疏矩阵时,就会出现问题,如下所示:

  1- lda = LdaModel(corpus,  num_topics=500, update_every=0, passes=2,alpha=0.5)
2- result=lda[corpus]
3- gensim.matutils.corpus2csc(result).T

在从 gensim 语料库到稀疏矩阵的转换过程中,如第 3 行所示,我收到错误 ValueError: invalid shape

我只有在添加 ALPHA 参数时才会遇到这个问题!

完整的回溯:

    ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-788-7fb54d5da9fb> in <module>()
----> 1 xp,xc=issam.lda(c)

C:\Anaconda\lib\issamKit.py in lda(X)
1745 corpus=gensim.matutils.Sparse2Corpus(X.T)
1746 lda = LdaModel(corpus, num_topics=500, update_every=0, passes=2,alpha=1)
-> 1747 return lda,gensim.matutils.corpus2csc(lda[corpus]).T
1748 def lsi(X):
1749 import gensim

C:\Anaconda\lib\site-packages\gensim-0.8.6-py2.7.egg\gensim\matutils.pyc in corpus2csc(corpus, num_terms, dtype, num_docs, num_nnz, printprogress)
97 data = numpy.asarray(data, dtype=dtype)
98 indices = numpy.asarray(indices)
---> 99 result = scipy.sparse.csc_matrix((data, indices, indptr), shape=(num_terms, num_docs), dtype=dtype)
100 return result
101

C:\Anaconda\lib\site-packages\scipy\sparse\compressed.pyc in __init__(self, arg1, shape, dtype, copy)
66 # Read matrix dimensions given, if any
67 if shape is not None:
---> 68 self.shape = shape # spmatrix will check for errors
69 else:
70 if self.shape is None:

C:\Anaconda\lib\site-packages\scipy\sparse\base.pyc in set_shape(self, shape)
69
70 if not (shape[0] >= 1 and shape[1] >= 1):
---> 71 raise ValueError('invalid shape')
72
73 if (self._shape != shape) and (self._shape is not None):

ValueError: invalid shape

最佳答案

corpus2csc提供num_terms参数。在您的情况下,num_terms=500

lda[corpus] 生成稀疏向量,但 CSC 格式需要确定的维度。当您未明确提供 num_terms 时,corpus2csc 会尝试从您的数据中猜测它,可能会导致不匹配。

关于python - (Gensim) 带有 alpha 参数的 ValueError : invalid shape,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16509883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com