将Python列表转换为R数字向量

时间:2012-07-18 12:51:16

标签: python r vector rpy2

我需要将我的数据放入Beta发行版并检索 alpha 参数。

我一直用Python编写代码,但在SciPy中似乎没有任何beta拟合函数。要么我在Matlab中做了一些我不太熟悉的事情,要么在带有R及其fitdistr函数的Python中做了所有事情。所以我去了后者。

from rpy2.robjects.packages import importr

MASS = importr('MASS')

然后我在范围[0,1]中取出我的numpy浮点数向量,然后将其传递给fitdistr

myVector = myVector.tolist()
MASS.fitdistr(myVector,"beta")

太糟糕了,它想要某种其他的矢量。是不是rpy和rpy2应该为我做所有的转换?

Error in function (x, densfun, start, ...)  : 
  'x' must be a non-empty numeric vector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 82, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 34, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in function (x, densfun, start, ...)  : 
  'x' must be a non-empty numeric vector

我需要在这做什么?

1 个答案:

答案 0 :(得分:1)

答案在这里: Converting python objects for rpy2

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

请参阅http://rpy.sourceforge.net/rpy2/doc/html/numpy.html