1

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

1 回答 1

1

给出参数corpus2cscnum_terms在你的情况下,num_terms=500.

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

于 2013-12-04T22:25:18.033 回答