简体   繁体   English

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

[英]converting a Python list to an R numeric vector

I need to fit my data into a Beta distribution and retrieve the alpha parameter. 我需要将数据放入Beta分布中并检索alpha参数。

I've been coding in Python, but there doesn't seem to be any beta fitting function in SciPy. 我一直在用Python进行编码,但是SciPy中似乎没有任何beta拟合功能。 Either I do everything in Matlab, which I'm not too familiar with, or in Python with R and its fitdistr function. 要么在我不太熟悉的Matlab中做任何事情,要么在Python中使用R及其fitdistr函数。 So I went for the latter. 所以我去了后者。

from rpy2.robjects.packages import importr

MASS = importr('MASS')

Then I take my numpy vector of floats in the range [0,1) and I pass it to fitdistr : 然后,我将浮点数的numpy向量放在[0,1)范围内,并将其传递给fitdistr

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

Too bad that it wants some kind of other vector. 太糟糕了,它想要某种其他向量。 Weren't rpy and rpy2 supposed to do all the conversions for me? 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

What do I need to do here? 我在这里需要做什么?

The answer was here: Converting python objects for rpy2 答案在这里: 为rpy2转换python对象

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

See http://rpy.sourceforge.net/rpy2/doc/html/numpy.html : 参见http://rpy.sourceforge.net/rpy2/doc/html/numpy.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM