numpy2ri 与 rpy2 2.2.2 的转换问题

发布于 2024-11-26 15:20:44 字数 1762 浏览 3 评论 0原文

我将 rpy2-2.2.2 与新的免费 Enthought python 发行版一起使用,其中包括 numpy 1.6.0 和 python 2.7.2。我 easy_installed rpy2 导致安装了 v.2.2.2 并且所有测试都成功。

我遇到的问题是我编写的代码在 rpy2 2.1.8 和 python 2.6 上运行良好。问题在于将数组从 numpy 转换为 R。

这是相关代码的片段:

import rpy2                    
import rpy2.rinterface as rinterface
import rpy2.robjects as rob    
import rpy2.rlike.container as rlc
import numpy as np            
import rpy2.robjects.numpy2ri  
r = rob.r
...
    HGr     = rob.conversion.py2ri(HG_reg)
    RHSr    = rob.conversion.py2ri(RHS)
    #
    CalData = rlc.TaggedList([HGr,RHSr],tags=('hg','rhs'))
    CalData = rob.DataFrame(CalData)

    r('''library(pls)''')
    #rob.globalEnv["HGr"] = HGr
    #rob.globalEnv["RHSr"] = RHSr
    rob.globalenv["CalData"] = CalData


    # perform the PLS regression
    if wetlflag:
        HGresults = r.plsr(r("hg ~ rhs.1 + rhs.2 + rhs.3 + rhs.4"),data=CalData,validation="LOO")

我很高兴地承认这不是最优雅的做事方式,但它以前和现在当我需要提供结果时都有效(!)。我得到的错误如下:

   Traceback (most recent call last):
  File "Mercury_PLS_WL_DF.py", line 224, in <module>
    HGr     = rob.conversion.py2ri(HG_reg)
  File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/rpy2-2.2.2dev_20110726-py2.7-macosx-10.5-i386.egg/rpy2/robjects/__init__.py", line 134, 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.

我在此处找到了讨论,并得到了这样的印象: numpy 数组现在自动转换为 R 数组,但注释掉 rob.conversion.py2ri(HG_reg) 语句并直接使用 numpy 数组似乎也会失败。我错过了一些明显的东西吗?为什么会在 2.1.8 和 2.2.2 之间中断?

I am using rpy2-2.2.2 with the new free Enthought python distribution that includes numpy 1.6.0 and python 2.7.2. I easy_installed rpy2 which resulted in v. 2.2.2 being installed and all tests were successful.

The problem I'm having is with code I wrote that worked fine with rpy2 2.1.8 and python 2.6. The issue is in converting from numpy to R for arrays.

Here is a snippet of the relevant code:

import rpy2                    
import rpy2.rinterface as rinterface
import rpy2.robjects as rob    
import rpy2.rlike.container as rlc
import numpy as np            
import rpy2.robjects.numpy2ri  
r = rob.r
...
    HGr     = rob.conversion.py2ri(HG_reg)
    RHSr    = rob.conversion.py2ri(RHS)
    #
    CalData = rlc.TaggedList([HGr,RHSr],tags=('hg','rhs'))
    CalData = rob.DataFrame(CalData)

    r('''library(pls)''')
    #rob.globalEnv["HGr"] = HGr
    #rob.globalEnv["RHSr"] = RHSr
    rob.globalenv["CalData"] = CalData


    # perform the PLS regression
    if wetlflag:
        HGresults = r.plsr(r("hg ~ rhs.1 + rhs.2 + rhs.3 + rhs.4"),data=CalData,validation="LOO")

I will gladly admit it's not the most elegant way to do things, but it worked before and now when I need to provide results all is broken (!). The error I get is the following:

   Traceback (most recent call last):
  File "Mercury_PLS_WL_DF.py", line 224, in <module>
    HGr     = rob.conversion.py2ri(HG_reg)
  File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/rpy2-2.2.2dev_20110726-py2.7-macosx-10.5-i386.egg/rpy2/robjects/__init__.py", line 134, 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.

I found the discussion here and got the impression that numpy arrays are now automatically converted to R arrays, but commenting out the rob.conversion.py2ri(HG_reg) statements and using the numpy arrays directly also seems to fail. Am I missing something obvious? Why would this break between 2.1.8 and 2.2.2?

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

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

发布评论

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

评论(1

风透绣罗衣 2024-12-03 15:20:44

来自http://rpy.sourceforge。 net/rpy2/doc-2.2/html/numpy.html#from-numpy-to-rpy2

警告

在 rpy2 的早期版本中,只需导入即可进行转换。导入模块时的副作用可能会导致问题,现在需要一个额外的步骤来激活转换:调用函数 rpy2.robjects.activate()。

因此,在导入之后放置 rpy2.robjects.activate() 就可以了。

From http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html#from-numpy-to-rpy2:

Warning

In earlier versions of rpy2, the import was all that was needed to have the conversion. A side-effect when importing a module can lead to problems, and there is now an extra step to make the conversion active: call the function rpy2.robjects.activate().

So put rpy2.robjects.activate() after the import and you should be fine.

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