簡體   English   中英

使用 rpy2 在 Python 中使用 R 包

[英]Using R packages in Python using 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_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)

這會導致與上述相同的錯誤。

所以我不知道如何修復它,也找不到類似的東西。

出於某種原因的錯誤是 mac-os 問題。 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM