使用rpy2在Python中使用R包

2024-05-12 23:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

R中有一个包我需要在我的数据上使用。我所有的数据预处理都已经在python中完成了,所有的建模也都已经完成了。R中的包是“PMA”。在使用Rs-PLS包之前,我已经使用了r2py,如下所示

import numpy as np
from rpy2.robjects.numpy2ri import numpy2ri
import rpy2.robjects as ro

def Rpcr(X_train,Y_train,X_test):
    ro.r('''source('R_pls.R')''')
    r_pls=ro.globalenv['R_pls']
    r_x_train=numpy2ri(X_train)
    r_y_train=numpy2ri(Y_train)
    r_x_test=numpy2ri(X_test)

    p_res=r_pls(r_x_train,r_y_train,r_x_test)
    yp_test=np.array(p_res[0])
    yp_test=yp_test.reshape((yp_test.size,))
    yp_train=np.array(p_res[1])
    yp_train=yp_train.reshape((yp_train.size,))
    ncomps=np.array(p_res[2])
    ncomps=ncomps.reshape((ncomps.size,))

return yp_test,yp_train,ncomps

当我遵循此格式时,给出了一个错误,即函数numpy2ri不存在。你知道吗

因此,我一直在工作的rpy2手册,并尝试了一些事情没有成功。我在R中使用的包的实现方式如下:

library('PMA')
cspa=CCA(X,Z,typex="standard", typez="standard", K=1, penaltyx=0.25, penaltyz=0.25)
# X and Z are dataframes with dimension ppm and pXq
# cspa returns an R object which I need two attributes u and v
U<-cspa$u
V<-cspa$v

因此,尝试实现我在rpy2上看到的一些东西时,尝试用python加载模块,并像这样在python中使用它

import rpy2.robjects as ro
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage as STAP
from rpy2.robjects import numpy2ri
from rpy2.robjects.packages import importr

base=importr('base'
scca=importr('PMA')
numpy2ri.activate() # To turn NumPy arrays X1 and X2 to r objects
out=scca.CCA(X1,X2,typex="standard",typez="standard", K=1, penaltyz=0.25,penaltyz=0.25)

出现以下错误

OMP: Error #15: Initializing libomp.dylib, but found libiomp5.dylib already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the  program. That is dangerous, since it can degrade performance or cause incorrect results. The best   thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by   avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://openmp.llvm.org/

中止陷阱:6

我还尝试直接使用R代码,使用他们的一个例子

  string<-'''SCCA<-function(X,Z,K,alpha){
  library("PMA")
  scca<-CCA(X,Z,typex="standard",typez="standard",K=K penaltyx=alpha,penaltyz=alpha)
  u<-scca$u
  v<-scca$v
  out<-list(U=u,V=v)
  return(out)}'''

  scca=STAP(string,"scca")

据我所知,它可以像r函数一样直接使用

 numpy2ri.activate()
 scca(X,Z,1,0.25)

这将导致与上述相同的错误。你知道吗

因此,我不知道如何修复它,一直无法找到任何类似的。你知道吗


Tags: thetotestimportasnptrainstandard
1条回答
网友
1楼 · 发布于 2024-05-12 23:34:38

由于某种原因,这个错误是mac操作系统的问题。https://stackoverflow.com/a/53014308/1628393

所以你要做的就是 是用这个命令修改它,它工作得很好

os.environ['KMP_DUPLICATE_LIB_OK']='True'
string<-'''SCCA<-function(X,Z,K,alpha){
library("PMA")
scca<-CCA(X,Z,typex="standard",typez="standard",K=Kpenaltyx=alpha,penaltyz=alpha)
u<-scca$u
v<-scca$v
out<-list(U=u,V=v)
return(out)}'''

scca=STAP(string,"scca")

然后函数被

scca.SCCA(X,Z,1,0.25)

相关问题 更多 >