我希望使用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代码。我还试过这个:
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)
以下是DEBUG日志:
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
我应该如何将R代码转换为Python的RPy2代码? 请帮忙!