为什么RPY2不将Numpy阵列转换为R矩阵?
我继承了一些使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在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