【问题标题】:converting a Python list to an R numeric vector将 Python 列表转换为 R 数值向量
【发布时间】:2012-07-17 12:21:50
【问题描述】:

我需要将我的数据放入 Beta 分布并检索 alpha 参数。

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

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

我需要在这里做什么?

【问题讨论】:

    标签: python r vector rpy2


    【解决方案1】:

    答案在这里: Converting python objects for rpy2

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

    http://rpy.sourceforge.net/rpy2/doc/html/numpy.html:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-22
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 2021-12-25
      • 2021-10-03
      相关资源
      最近更新 更多