外部函数调用中的 rpy2 自动 NumPy 转换 NA/NaN/Inf
rpy2 automatic NumPy conversion NA/NaN/Inf in foreign function call
当尝试使用简单数据从 python 调用 BayesTree R 包时,我收到错误 "NA/NaN/Inf in foreign function call",即使所有数据都是正实数。
源代码
import numpy as np
# R interface for python
import rpy2
# For importing R packages
from rpy2.robjects.packages import importr
# Activate conversion from numpy to R
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
train_x_py = np.array([[0.0, 0.0],
[0.0, 1.0],
[1.0, 1.0]])
# Any 3-length float vector fails for training y
train_y_py = np.array([1.0,2.0,3.0])
test_x_py = np.array([[0.2, 0.0],
[0.2, 0.2],
[1.0, 0.2]])
# Create R versions of the training and testing data
train_x = rpy2.robjects.r.matrix(train_x_py, nrow=3, ncol=2)
train_y = rpy2.robjects.vectors.FloatVector(train_y_py)
test_x = rpy2.robjects.r.matrix(test_x_py, nrow=3, ncol=2)
print(train_x)
print(train_y)
print(test_x)
BayesTree = importr('BayesTree')
response = BayesTree.bart(train_x, train_y, test_x,
verbose=False, ntree=100)
# The 7th return value is the estimated response
response = response[7]
print(response)
代码输出/错误
[,1] [,2]
[1,] 0 0
[2,] 0 1
[3,] 1 1
[1] 1 2 3
[,1] [,2]
[1,] 0.2 0.0
[2,] 0.2 0.2
[3,] 1.0 0.2
Traceback (most recent call last):
File "broken_rpy2.py", line 32, in <module>
verbose=False, ntree=100)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/robjects/functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/robjects/functions.py", line 106, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in (function (x.train, y.train, x.test = matrix(0, 0, 0), sigest = NA, :
NA/NaN/Inf in foreign function call (arg 7)
它所指的第 32 行的错误是:
response = BayesTree.bart(train_x, train_y, test_x,
verbose=False, ntree=100)
系统设置
操作系统:
Mac OS X 塞拉利昂 10.12.6
Python 版本:
Python3.6.1
R版本:
R 3.4.1
Python 包:
点子 9.0.1,
rpy2 2.8.6,
麻木 1.13.0
问题
这是我自己的用户错误,还是 rpy2 中的错误?
这是 R 包 "BayesTree" 中的问题。您可以使用以下代码直接在 R 中重现该问题(假设您已经安装了 BayesTree 包)。
train_x = matrix(c(0,0,1,0,1,1),nrow=3,ncol=2)
train_y = as.vector(c(1,2,3))
test_x = matrix(c(.2,.2,1.,.0,.2,.2),nrow=3,ncol=2)
result = bart(train_x,train_y,test_x,verbose=FALSE,ntree=100)
当尝试使用简单数据从 python 调用 BayesTree R 包时,我收到错误 "NA/NaN/Inf in foreign function call",即使所有数据都是正实数。
源代码
import numpy as np
# R interface for python
import rpy2
# For importing R packages
from rpy2.robjects.packages import importr
# Activate conversion from numpy to R
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
train_x_py = np.array([[0.0, 0.0],
[0.0, 1.0],
[1.0, 1.0]])
# Any 3-length float vector fails for training y
train_y_py = np.array([1.0,2.0,3.0])
test_x_py = np.array([[0.2, 0.0],
[0.2, 0.2],
[1.0, 0.2]])
# Create R versions of the training and testing data
train_x = rpy2.robjects.r.matrix(train_x_py, nrow=3, ncol=2)
train_y = rpy2.robjects.vectors.FloatVector(train_y_py)
test_x = rpy2.robjects.r.matrix(test_x_py, nrow=3, ncol=2)
print(train_x)
print(train_y)
print(test_x)
BayesTree = importr('BayesTree')
response = BayesTree.bart(train_x, train_y, test_x,
verbose=False, ntree=100)
# The 7th return value is the estimated response
response = response[7]
print(response)
代码输出/错误
[,1] [,2]
[1,] 0 0
[2,] 0 1
[3,] 1 1
[1] 1 2 3
[,1] [,2]
[1,] 0.2 0.0
[2,] 0.2 0.2
[3,] 1.0 0.2
Traceback (most recent call last):
File "broken_rpy2.py", line 32, in <module>
verbose=False, ntree=100)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/robjects/functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/robjects/functions.py", line 106, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in (function (x.train, y.train, x.test = matrix(0, 0, 0), sigest = NA, :
NA/NaN/Inf in foreign function call (arg 7)
它所指的第 32 行的错误是:
response = BayesTree.bart(train_x, train_y, test_x,
verbose=False, ntree=100)
系统设置
操作系统: Mac OS X 塞拉利昂 10.12.6
Python 版本: Python3.6.1
R版本: R 3.4.1
Python 包: 点子 9.0.1, rpy2 2.8.6, 麻木 1.13.0
问题
这是我自己的用户错误,还是 rpy2 中的错误?
这是 R 包 "BayesTree" 中的问题。您可以使用以下代码直接在 R 中重现该问题(假设您已经安装了 BayesTree 包)。
train_x = matrix(c(0,0,1,0,1,1),nrow=3,ncol=2)
train_y = as.vector(c(1,2,3))
test_x = matrix(c(.2,.2,1.,.0,.2,.2),nrow=3,ncol=2)
result = bart(train_x,train_y,test_x,verbose=FALSE,ntree=100)