Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用rpy2在Python中使用R包_Python_R_Numpy_Rpy2 - Fatal编程技术网

使用rpy2在Python中使用R包

使用rpy2在Python中使用R包,python,r,numpy,rpy2,Python,R,Numpy,Rpy2,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中有一个包我需要在我的数据上使用。我所有的数据预处理都已经用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
并得到以下错误

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")
这将导致与上述相同的错误


因此,我不知道如何修复它,也找不到类似的东西

由于某种原因,该错误是mac os的问题

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

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")
 numpy2ri.activate()
 scca(X,Z,1,0.25)
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)