【问题标题】:Subtracting FloatVector from FloatVector using rpy2使用 rpy2 从 FloatVector 中减去 FloatVector
【发布时间】:2018-11-22 22:38:40
【问题描述】:

我在 rpy2 的帮助下在我的 Python 代码中使用 R 中的 grf 包。但是,我对 rpy2 返回的结构有点困惑。

在这里,我试图找到两个向量的差值,只需相减即可。如果它只是 R 中的一个向量,我会简单地使用 - 减去。

但这是我的问题:

    pred_w1.rx2("predictions")
    Out[92]: 
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x1c17447e08 / R:0x7fe214bf7c00>
   [-0.548409, -0.224735, 0.948285, 0.269825, ..., 0.259327, -0.267804, 
    0.255273, 0.287592]


    pred_w1.rx2("predictions")
    Out[93]: 
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x1c18f02808 / R:0x7fe214bf7c00>
    [-0.548409, -0.224735, 0.948285, 0.269825, ..., 0.259327, 
    -0.267804, 0.255273, 0.287592]

现在当我尝试这样减去时:

    pred_w1.rx2("predictions") - pred_w0.rx2("predictions")`

我得到这个错误:

    File "<ipython-input-95-161c70b4f42f>", line 1, in <module>
pred_w1.rx2("predictions") - pred_w0.rx2("predictions")
    TypeError: unsupported operand type(s) for -: 'FloatVector' and 'FloatVector'

我认为这应该相当容易。我只是对 rpy2 不太熟悉。

【问题讨论】:

    标签: rpy2


    【解决方案1】:

    使用委托人ro(如r o操作员):

    pred_w1.rx2("predictions").ro - pred_w0.rx2("predictions")

    (更多文档:https://rpy2.github.io/doc/v2.9.x/html/vector.html#operators

    【讨论】:

      猜你喜欢
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 2011-11-12
      • 2020-09-24
      相关资源
      最近更新 更多