使用RPy2进行主题建模

2024-06-15 08:20:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望使用RPy在Python中使用LDA。我已经用gensim包试过了,但我还是想试试RPy2。在

使用R时,我使用以下代码:

library(RTextTools)
library(topicmodels)
library(tm)

...Get Data Here and Store to `data`...

matrix <- create_matrix(as.vector(data$body), 
                    language = "english", 
                    removeNumbers = TRUE,
                    removePunctuation = TRUE,
                    stemWords = FALSE, 
                    weighting = weightTf)

mat <- as.matrix(matrix)
list <- rowSums(matrix)
rowTotals <- apply(matrix , 1, sum)
matrix.new   <- matrix[rowTotals > 0]

lda <- LDA(matrix, 250)

我想把上面的代码转换成RPy2的python代码。我已经试过了:

^{pr2}$

以下是调试日志:

 Connection to database established!
 Error in (function (x, k, method = "VEM", control = NULL, model = NULL,  :
 Each row of the input matrix needs to contain at least one non-zero entry

 Traceback (most recent call last):
 File "C:\Requirements\Python27\lib\site-packages\rpdb2.py", line 14499, in <module>
 ret = rpdb2.main()
 File "C:\Requirements\Python27\lib\site-packages\rpdb2.py", line 14470, in main
 StartServer(_rpdb2_args, fchdir, _rpdb2_pwd, fAllowUnencrypted, fAllowRemote, secret)
 File "C:\Requirements\Python27\lib\site-packages\rpdb2.py", line 14220, in StartServer
 imp.load_source('__main__', _path)
 File "c:\requirements\msr\msr14-mysql\rpylda.py", line 69, in <module>
 lda = tmod.LDA(matrix, 20)
 File "C:\Requirements\Python27\lib\site-packages\rpy2\robjects\functions.py", line 86, in __call__
 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
 File "C:\Requirements\Python27\lib\site-packages\rpy2\robjects\functions.py", line 35, in __call__
 res = super(Function, self).__call__(*new_args, **new_kwargs)
 rpy2.rinterface.RRuntimeError: Error in (function (x, k, method = "VEM", control
 = NULL, model = NULL,  :
 Each row of the input matrix needs to contain at least one non-zero entry

我应该如何将Python的R代码转换成RPy2代码? 请帮忙!在


Tags: to代码inpylibpackageslinesite