2017-08-09 9 views
-1

単純なデータでPythonからBayesTree Rパッケージを呼び出そうとすると、すべてのデータムが "NA/NaN/Inf in foreign function call"正の実数です。rpy2自動NumPy変換NA/NaN/Infの外部関数呼び出し

ソースコード

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のバージョン: のPython 3.6.1

Rバージョン: R 3.4.1

Pythonパッケージ: pip 9.0.1、 rpy2 2.8.6、 numpy 1.13.0

質問

これは自分のユーザーエラーですか、これはrpy2のバグですか?

+0

修正し、numpyを使用せずにこの問題をテストしました。 R行列が同じコード出力を生成する限り、エラーが発生します。 –

+0

1分でタイトルを編集すると、Rで全く同じ問題が再現されました。これは、BayesTreeパッケージ内で発生しています。 Python/Numpy/rpy2サイドの修正はありません。 –

+1

タイトルを変更するのではなく、答えを投稿するだけで(または解決策と思われるもの)あなた自身がそれを受け入れます。 :) – MSeifert

答えて

0

これは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) 
関連する問題