rpy2 出现问题,rpart 将数据从 python 正确传递到 r
我正在尝试使用 Python 2.6.5 和 R 10.0 通过 RPY2 运行 rpart。
我在 python 中创建一个数据框并将其传递,但收到一条错误消息:
Error in function (x) : binary operation on non-conformable arrays
Traceback (most recent call last):
File "partitioningSANDBOX.py", line 86, in <module>
model=r.rpart(**rpart_params)
File "build/bdist.macosx-10.3-fat/egg/rpy2/robjects/functions.py", line 83, in __call__
File "build/bdist.macosx-10.3-fat/egg/rpy2/robjects/functions.py", line 35, in __call__
rpy2.rinterface.RRuntimeError: Error in function (x) : binary operation on non-conformable arrays
任何人都可以帮助我确定我做错了什么而抛出此错误吗?
我的代码的相关部分是这样的:
import numpy as np
import rpy2
import rpy2.robjects as rob
import rpy2.robjects.numpy2ri
#Fire up the interface to R
r = rob.r
r.library("rpart")
datadict = dict(zip(['responsev','predictorv'],[cLogEC,csplitData]))
Rdata = r['data.frame'](**datadict)
Rformula = r['as.formula']('responsev ~.')
#Generate an RPART model in R.
Rpcontrol = r['rpart.control'](minsplit=10, xval=10)
rpart_params = {'formula' : Rformula, \
'data' : Rdata,
'control' : Rpcontrol}
model=r.rpart(**rpart_params)
两个变量 cLogEC 和 csplitData 是 float 类型的 numpy 数组。
另外,我的数据框如下所示:
In [2]: print Rdata
------> print(Rdata)
responsev predictorv
1 0.6020600 312
2 0.3010300 300
3 0.4771213 303
4 0.4771213 249
5 0.9242793 239
6 1.1986571 297
7 0.7075702 287
8 1.8115750 270
9 0.6020600 296
10 1.3856063 248
11 0.6127839 295
12 0.3010300 283
13 1.1931246 345
14 0.3010300 270
15 0.3010300 251
16 0.3010300 246
17 0.3010300 273
18 0.7075702 252
19 0.4771213 252
20 0.9294189 223
21 0.6127839 252
22 0.7075702 267
23 0.9294189 252
24 0.3010300 378
25 0.3010300 282
公式如下所示:
In [3]: print Rformula
------> print(Rformula)
responsev ~ .
I am trying to run rpart through RPY2 using Python 2.6.5 and R 10.0.
I create a data frame in python and pass it along but I get an error stating:
Error in function (x) : binary operation on non-conformable arrays
Traceback (most recent call last):
File "partitioningSANDBOX.py", line 86, in <module>
model=r.rpart(**rpart_params)
File "build/bdist.macosx-10.3-fat/egg/rpy2/robjects/functions.py", line 83, in __call__
File "build/bdist.macosx-10.3-fat/egg/rpy2/robjects/functions.py", line 35, in __call__
rpy2.rinterface.RRuntimeError: Error in function (x) : binary operation on non-conformable arrays
Can anyone help me determine what I am doing wrong to throw this error?
the relevant part of my code is this:
import numpy as np
import rpy2
import rpy2.robjects as rob
import rpy2.robjects.numpy2ri
#Fire up the interface to R
r = rob.r
r.library("rpart")
datadict = dict(zip(['responsev','predictorv'],[cLogEC,csplitData]))
Rdata = r['data.frame'](**datadict)
Rformula = r['as.formula']('responsev ~.')
#Generate an RPART model in R.
Rpcontrol = r['rpart.control'](minsplit=10, xval=10)
rpart_params = {'formula' : Rformula, \
'data' : Rdata,
'control' : Rpcontrol}
model=r.rpart(**rpart_params)
The two variables cLogEC and csplitData are numpy arrays of float type.
Also, my data frame looks like this:
In [2]: print Rdata
------> print(Rdata)
responsev predictorv
1 0.6020600 312
2 0.3010300 300
3 0.4771213 303
4 0.4771213 249
5 0.9242793 239
6 1.1986571 297
7 0.7075702 287
8 1.8115750 270
9 0.6020600 296
10 1.3856063 248
11 0.6127839 295
12 0.3010300 283
13 1.1931246 345
14 0.3010300 270
15 0.3010300 251
16 0.3010300 246
17 0.3010300 273
18 0.7075702 252
19 0.4771213 252
20 0.9294189 223
21 0.6127839 252
22 0.7075702 267
23 0.9294189 252
24 0.3010300 378
25 0.3010300 282
and the formula looks like this:
In [3]: print Rformula
------> print(Rformula)
responsev ~ .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题与 rpart 中的 R 特殊代码有关(准确地说,是以下块,特别是最后一行
:)。
解决这个问题的一种方法是避免输入该代码块(见下文),或者可以从 Python 构造一个嵌套计算(以便 Parent.frame() 运行)。这并不像人们希望的那么简单,但也许我将来会找到时间让它变得更容易。
The problem is related to R idiosyncratic code in rpart (to be precise, the following block, in particular the last line:
).
One way to work around that is to avoid entering that block of code (see below) or may be to construct a nested evaluation from Python (so that parent.frame() behaves). This is not as simple as one would hope, but may be I'll find time to make it easier in the future.