(Gensim)ValueError:无效的形状,带有alpha参数

时间:2013-05-12 17:00:00

标签: python lda gensim

首先,这是获取执行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

1 个答案:

答案 0 :(得分:1)

corpus2csc num_terms参数提供给num_terms=500。在您的情况下,lda[corpus]

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