简体   繁体   English

如何在rpy2中创建一个两列矩阵

[英]How to create a two-column matrix in rpy2

I'm using rpy2 to run a method from an R library.我正在使用 rpy2 运行 R 库中的方法。 According to the documentation:根据文档:

method_name(x, range.x)

x: a two-column numeric matrix.
range.x: a list containing two vectors.

And it includes an example:它包括一个例子:

data(geyser, package="MASS")
x <- cbind(geyser$duration, geyser$waiting)
est <- method_name(x, range.x)

I checked the type of geyser$duration and geyser$waiting and both are double .我检查了geyser$durationgeyser$waiting的类型,两者都是double I also tried replacing geyser$duration and geyser$waiting by g = c(.016, 2.15, 4.00) and h = c(.012, 2.11, 2.50) in R, and the code still works.我还尝试在 R 中将geyser$durationgeyser$waiting替换为g = c(.016, 2.15, 4.00)h = c(.012, 2.11, 2.50) ,并且代码仍然有效。

In my current Python code, I have:在我当前的 Python 代码中,我有:

import numpy as np
import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector, FloatVector, ListVector # I tried these before too
from rpy2.robjects import numpy2ri, pandas2ri

numpy2ri.activate()
pandas2ri.activate()

base = rpackages.importr(('base'))

a = np.array([1.2, 2.1, 2.5]); b = np.array([5.2, 1.3, 2.15])
x = base.cbind(base.c(a), base.c(b))

ranges = base.range(x)
result = method_name(x, ranges)

As you can see, I'm trying to make my code as similar to the example as possible.如您所见,我正在尝试使我的代码与示例尽可能相似。 However, I can't make the method work.但是,我无法使该方法起作用。 I get the error Error in seq.default(a[2L], b[2L], length = M[2L]) which probably has to do with a problem in the arguments.我收到错误Error in seq.default(a[2L], b[2L], length = M[2L])这可能与 arguments 中的问题有关。

There's and obvious problem with ranges because it contains just two values, the minimum and maximum of x , however, it should contain two minimum values and to maximum values (one pair for each column of the matrix). ranges存在明显的问题,因为它只包含两个值,即x的最小值和最大值,但是,它应该包含两个最小值和最大值(矩阵的每一列对应一对)。 I can achieve that by doing this:我可以通过这样做来实现:

ranges = base.cbind(base.range(a), base.range(b))

But this implies that there's a problem with the way I'm creating the matrix.但这意味着我创建矩阵的方式存在问题。 Otherwise, I would get two pairs of values just by using base.range(x) .否则,我将通过使用base.range(x)获得两对值。

I also tried x = robjects.r.matrix(x, ncol = 2) but didn't work.我也试过x = robjects.r.matrix(x, ncol = 2)但没有用。 I still get just a global minimum and maximum value for the whole matrix when calling range .调用range时,我仍然只得到整个矩阵的全局最小值和最大值。

What is the correct way of creating this matrix so that the method can run?创建此矩阵以便该方法可以运行的正确方法是什么?

According to the documentation of the range function, it accepts as input vectors (one dimensional arrays).根据 function range的文档,它接受作为输入向量(一维数组)。 Thus, it would work by applying it to each column of your matrix or by applying it first directly to the a and b elements as you have mentioned.因此,它可以通过将其应用于矩阵的每一列或首先将其直接应用于您提到的ab元素来工作。 Thus, you second approach should work因此,您的第二种方法应该有效

# Define a,b vectors
a = np.array([1.2, 2.1, 2.5]); b = np.array([5.2, 1.3, 2.15])

# Calculate vector ranges
range_a = base.range(base.c(a))
range_b = base.range(base.c(b))

# Define the matrix 
x = base.cbind(base.c(a), base.c(b))
print(x)
>>> [[1.2  5.2 ]
    [2.1  1.3 ]
    [2.5  2.15]]

# Define the ranges
ranges = base.cbind(range_a, range_b)
print(ranges)
>>> [[1.2 1.3]
    [2.5 5.2]]

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

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