将 python 对象转换为 rpy2

发布于 2024-08-25 08:21:14 字数 780 浏览 11 评论 0原文

以下代码应该在 rpy2 中创建热图,

import numpy as np
from rpy2.robjects import r
data = np.random.random((10,10))
r.heatmap(data)    

但是,它会导致以下错误

Traceback (most recent call last):
  File "z.py", line 8, in <module>
    labRow=rowNames, labCol=colNames)
  File "C:\Python25\lib\site-packages\rpy2\robjects\__init__.py", line 418, in __call__
    new_args = [conversion.py2ri(a) for a in args]
  File "C:\Python25\lib\site-packages\rpy2\robjects\__init__.py", line 93, in default_py2ri
    raise(ValueError("Nothing can be done for the type %s at the moment." %(type(o))))
ValueError: Nothing can be done for the type <type 'numpy.ndarray'> at the moment.

从文档中我了解到 r.heatmap 需要“数字矩阵”。如何将 np.array 转换为所需的数据类型?

The following code is supposed to create a heatmap in rpy2

import numpy as np
from rpy2.robjects import r
data = np.random.random((10,10))
r.heatmap(data)    

However, it results in the following error

Traceback (most recent call last):
  File "z.py", line 8, in <module>
    labRow=rowNames, labCol=colNames)
  File "C:\Python25\lib\site-packages\rpy2\robjects\__init__.py", line 418, in __call__
    new_args = [conversion.py2ri(a) for a in args]
  File "C:\Python25\lib\site-packages\rpy2\robjects\__init__.py", line 93, in default_py2ri
    raise(ValueError("Nothing can be done for the type %s at the moment." %(type(o))))
ValueError: Nothing can be done for the type <type 'numpy.ndarray'> at the moment.

From the documentation I learn that r.heatmap expects "a numeric matrix". How do I convert np.array to the required data type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜深人未静 2024-09-01 08:21:14

您需要添加

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

See more in rpy2 Documentation numpy 部分 (此处适用于较旧的 2.x 版本)

2.2.x 之前仅进口就足够了。

仅导入就足以
切换自动转换
numpy 对象转换为 rpy2 对象。

为什么将其设为可选导入,
虽然它本来可以包含在
函数 py2ri() (如
为此提交的原始补丁
函数)?

虽然两者都是有效且合理的
选项,做出设计决策
为了将 rpy2 与 numpy 解耦
最多,并且不要假设
自动安装 numpy
意味着程序员想要使用
它。

You need to add

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

See more in rpy2 documentation numpy section (here for the older 2.x version)

Prior to 2.2.x the import alone was sufficient.

That import alone is sufficient to
switch an automatic conversion of
numpy objects into rpy2 objects.

Why make this an optional import,
while it could have been included in
the function py2ri() (as done in the
original patch submitted for that
function) ?

Although both are valid and reasonable
options, the design decision was taken
in order to decouple rpy2 from numpy
the most, and do not assume that
having numpy installed automatically
meant that a programmer wanted to use
it.

痴情换悲伤 2024-09-01 08:21:14

对于 rpy2 2.2.4 我必须添加:

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

For rpy2 2.2.4 I had to add:

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
静待花开 2024-09-01 08:21:14

对于我(2.2.1),以下内容也有效(如 http 上所述: //rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html):

import rpy2.robjects as ro
from rpy2.robjects.numpy2ri import numpy2ri
ro.conversion.py2ri = numpy2ri

For me (2.2.1) the following also worked (as documented on http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html):

import rpy2.robjects as ro
from rpy2.robjects.numpy2ri import numpy2ri
ro.conversion.py2ri = numpy2ri
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文