简体   繁体   English

How to import a function from an R package as if it was native Python function and use all its outputs?

[英]How to import a function from an R package as if it was native Python function and use all its outputs?

There is a function called dea(x, y, *args) in library(Benchmarking) which returns useful objects. library(Benchmarking) 中有一个名为 dea(x, y, *args) 的 function,它返回有用的对象。 I've described 3 key ones below:我在下面描述了 3 个关键:

crs = dea(mydata_matrix_x, my_data_matrix_y, RTS="IN", ORIENTATION= "in") # both matrixes have N rows

efficiency(crs) # a 'numeric' type object which looks like a 1xN vector
peers(crs) # A matrix: Nx2 (looks to me like a pandas dataframe when run in .ipynb file with R kernel)
lambda(crs) # A matrix: Nx2 of type dbl (also looks like a dataframe)

Now I would like to programatically vary my_data_matrix_x .现在我想以编程方式改变my_data_matrix_x This matrix represents my inputs.这个矩阵代表我的输入。 At first it will be a Nx10 matrix.起初它将是一个 Nx10 矩阵。 However I intend to drop each column sequentially and run dea() on the Nx9 matrix, then graph the efficiency(crs) scores that come out.但是,我打算按顺序删除每一列并在 Nx9 矩阵上运行 dea(),然后绘制出的效率(crs)分数。 The issue is I have no idea how to achieve this in R (amongst other things) and would rather circumvent the issue by writing all my code in Python and importing this dea() function somehow from an R script The issue is I have no idea how to achieve this in R (amongst other things) and would rather circumvent the issue by writing all my code in Python and importing this dea() function somehow from an R script

I believe the best solution available to me will be to read and write from files:我相信对我来说最好的解决方案是从文件中读取和写入:

from Benchmarking_script.r import dea

def test_inputs(data, input):
    INPUTS = ['input 1', 'input2', 'input3', 'input4,' 'input5']
    OUTPUTS = ['output1', 'output2']
    data_inputs = data.drop(f"{input}", axis=1)
    data_outputs = data[OUTPUTS]

    data_inputs.to_csv("my_inputs.csv")
    data_outputs.to_csv("my_outputs.csv")

    run Benchmarking.dea(data_inputs, data_outputs, RTS="crs", ORIENTATION="in")

clearly this last line won't work: I am interested to hear flexible (and simple!) ways to run this dea() function idiomatically as if it was a native Python function CF 显然最后一行行不通:我很想听听灵活(简单!)的方式来运行这个 dea() function,就像它是一个原生 Python ZC1C425268E68385D1AB5074

Related SO questions相关的 SO 问题

The closest answer on SO I've found has been Importing any function from an R package into python我发现的最接近 SO 的答案是Importing any function from an R package into Z6BFCZEEEB439A7BDD25

When adapting the code I've written在调整我编写的代码时

import pandas as pd
data = pd.read_csv("path/to_data.csv")


import rpy2
import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)

packnames = ('Benchmarking')
utils.install_packages(StrVector(packnames))

Benchmarking = importr('Benchmarking')

crs = Benchmarking.dea(data['Age'], data['CO2'], RTS='crs', ORIENTATION='in')

--------------------------------------------------------------
NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'pandas.core.series.Series'>'

So importing the function natively as a Python file hasn't worked因此,将 function 本地导入为 Python 文件并没有奏效

The second approach is the way to go.第二种方法是go的方式。 You need to use a converter context so python and r variables would be converted automatically.您需要使用转换器上下文,以便自动转换 python 和 r 变量。 Specifically, try pandas2ri submodule shipped with rpy2 .具体来说,尝试使用pandas2ri附带的rpy2子模块。 Something like this:像这样的东西:

from rpy2.robjects import pandas2ri

with pandas2ri:
    crs = Benchmarking.dea(data['Age'], data['CO2'], RTS='crs', ORIENTATION='in')

If this doesn't work, update your post with the error.如果这不起作用,请使用错误更新您的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM