“'x'必须是非空的数字向量”rpy2错误:为什么它不起作用?

问题描述 投票:0回答:1

我正在使用Colab和Python为我的数据找到最合适的分布。我是新手,因此遇到了很多问题。到目前为止,这是我的代码:

from rpy2.robjects import pandas2ri
from rpy2.robjects.packages import importr

MASS = importr('MASS')
pandas2ri.activate()

df_temp = pd.DataFrame()
df_temp["Values"] = [37.50,46.79,48.30,46.04,43.40,39.25]
ri_temp = pandas2ri.py2ri(df_temp)

params_temp = MASS.fitdistr(ri_temp, 'normal')
print(params_temp)

现在,有很多事情我还不明白。请尽量描述!:)例如,我不知道为什么我必须使用pandas2ri.activate()。我的代码产生的错误是这样的:

/usr/local/lib/python3.6/dist-packages/rpy2/rinterface/__init__.py:146:
  RRuntimeWarning: Error in (function (x, densfun, start, ...)  : 

  'x' must be a non-empty numeric vector

......之间的追溯......

warnings.warn(x, RRuntimeWarning)
  RRuntimeError: Error in (function (x, densfun, start, ...)  : 

  'x' must be a non-empty numeric vector

那么,问题是什么?

我首先使用pandas的原因是我将数据存储在列表中。如果我可以避免使用熊猫,那会有什么选择呢?当我尝试简单地解析MASS.fitdistr(list, 'normal')时,它也给了我错误。另外,使用r找到给定列表数据的最佳拟合分布可能有更好的选择吗?有什么建议?

python r python-3.x pandas
1个回答
0
投票

这有助于:my_array = np.asarray(my_list)。然后使用数组作为我的输入:params_temp = MASS.fitdistr(my_array, 'normal')

感谢akrun。

© www.soinside.com 2019 - 2024. All rights reserved.