2013-05-12 30 views
1

首先,這是獲得執行LDA的語料庫的主題分佈的正確方法嗎?(Gensim)ValueError:形狀無效,使用alpha參數

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語料稀疏矩陣轉換:

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

現在,當我的阿爾法參數添加到LDA並嘗試語料庫轉化爲稀疏矩陣如下出現問題如第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

corpus2cscnum_terms參數。在你的情況下,num_terms=500

lda[corpus]產生稀疏向量,但CSC格式需要一個明確的維度。如果您沒有明確提供num_termscorpus2csc會嘗試從您的數據中猜出它,可能會導致不匹配。

相關問題