Question

I wish to use LDA in Python using RPy. I have already tried this using gensim package but I still wish to try RPy2 out.

While using R I use this code:

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)

I want to convert the code above, to python code for RPy2. I have tried this yet:

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
RTT = importr('RTextTools')
tmod = importr('topicmodels')
tm = importr('tm')

#CommentBunch is a list of strings.

matrix = RTT.create_matrix(CommentBunch, 
                           language = "english", 
                           removeNumber = True, 
                           removePunctuation = True, 
                           stemWords = True, 
                           weighting = tm.weightTf)

lda = tmod.LDA(matrix, 250)

Following is the DEBUG log:

 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

How am I supposed to convert the R code to RPy2 code for Python? Please help!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top