为什么RPY2不将Numpy阵列转换为R矩阵?

发布于 2025-01-30 12:01:12 字数 488 浏览 1 评论 0原文

我继承了一些使用RPY2的代码。但是,应该将numpy阵列转换为r矩阵的命令不起作用,我无法弄清原因或解决方法。

import numpy as np
import rpy2.robjects as ro

dist_square = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
assert dist_square.ndim == 2
assert dist_square.shape[0] == dist_square.shape[1]

# convert distance matrix to R
m = ro.vectors.Matrix(dist_square)

运行上面的代码会产生以下错误:

m = ro.vectors.Matrix(dist_square)
TypeError: Matrix() takes no arguments

为什么此错误并有办法纠正/规避它?

I inherited some code that uses rpy2. However, the command that is supposed to convert the numpy arrays into R matrices is't working and I can't figure out the reason why or a workaround.

import numpy as np
import rpy2.robjects as ro

dist_square = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
assert dist_square.ndim == 2
assert dist_square.shape[0] == dist_square.shape[1]

# convert distance matrix to R
m = ro.vectors.Matrix(dist_square)

Running the above piece of code produces the following error:

m = ro.vectors.Matrix(dist_square)
TypeError: Matrix() takes no arguments

Why is this error and is there a way to correct/circumvent it?

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

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

发布评论

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

评论(1

著墨染雨君画夕 2025-02-06 12:01:12

尝试在R基本包中使用构造函数。请参阅:

https://rpy2.github.io/ doc/v3.5.x/html/vector.html#矩阵

Try using the constructor in the R base package. See:

https://rpy2.github.io/doc/v3.5.x/html/vector.html#matrix

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