我希望使用 RPy 在 Python 中使用 LDA。我已经使用gensim
包尝试过这个,但我仍然想尝试RPy2
一下。
使用 RI 时使用以下代码:
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)
以下是调试日志:
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 代码?请帮忙!