简体   繁体   English

如何在Python代码中使用rpy2将R函数作为参数传递

[英]How to pass a R function as argument using rpy2 in a Python code

I am trying to make a Python interface for the brilliant NMF package - https://cran.r-project.org/web/packages/NMF/NMF.pdf (as it is much more flexible than the Python options). 我试图做的辉煌NMF包Python接口- https://cran.r-project.org/web/packages/NMF/NMF.pdf (因为它比Python的选项更灵活)。 So far so good. 到现在为止还挺好。

I come up with something like this: 我想出了这样的东西:

# Python rpy2
__NMF = importr("NMF")
n_comp_R = robjects.IntVector(n_components)
nmf_ro = self.__NMF.nmf(data, n_comp_R, methods, self.seed, nrun=10)

It works like a charm. 它像一种魅力。 Methods is a list of possible algorithms that I can use: 方法是我可以使用的可能算法的列表:

nmfAlgorithm() nmfAlgorithm()

[1] "brunet" "KL" "lee" "Frobenius" "offset" [1]“ brunet”“ KL”“ lee”“ Frobenius”“ offset”

[6] "nsNMF" "ls-nmf" "pe-nmf" "siNMF" "snmf/r" [6]“ nsNMF”“ ls-nmf”“ pe-nmf”“ siNMF”“ snmf / r”

[11] "snmf/l" [11]“ snmf / l”

Other possibility is to use a custom algorithm, as it is described in the NMF documentation 另一种可能是使用自定义算法,如NMF文档中所述

# R code
my.algorithm <- function(x, seed, param.1, param.2) {
    # do something with starting point ...
    # return updated starting point
    return(seed)
} 
res <- nmf(data, n_comp, my.algorithm)

How can I reproduce this using rpy2 ? 如何使用rpy2重现此rpy2

I've tried something like: 我已经尝试过类似的东西:

import rpy2.robjects as robjects

my_algorithm = robjects.r('''
function (x, seed, scale.factor = 1) 
{
    pca <- prcomp(t(x), retx = TRUE)
    factorization.rank <- nbasis(seed)
    cat(seed)
    basis(seed) <- abs(pca$rotation[, 1:factorization.rank])
    coef(seed) <- t(abs(pca$x[, 1:factorization.rank]))/scale.factor
    return(seed)
    }
''')
nmf_ro = __NMF.nmf(data, n_comp_R, my_algorithm.r_repr(), nrun=1)

But it didn't make the magic =( 但这并没有使魔术=(

NMF algorithm - No matching entry for key “key=function (x, seed, scale.factor >= 1) NMF算法-键“ key = function”(x,seed,scale.factor> = 1)没有匹配条目

{ {

pca <- prcomp(t(x), retx = TRUE) pca <-prcomp(t(x),retx = TRUE)

factorization.rank <- nbasis(seed) factorization.rank <-nbasis(种子)

cat(seed) 猫(种子)

basis(seed) <- abs(pca$rotation[, 1:factorization.rank]) 基础(种子)<-abs(pca $ rotation [,1:factorization.rank])

coef(seed) <- t(abs(pca$x[, 1:factorization.rank]))/scale.factor coef(种子)<-t(abs(pca $ x [,1:factorization.rank]))/ scale.factor

return(seed) 返回(种子)

}” in the registry. }”。

Use one of: 'brunet', 'Frobenius', 'KL', 'lee', 'ls-nmf', '.M#brunet', 'nsNMF', 'offset', 'pe-nmf', '.R#brunet', '.R#lee', '.R#nsNMF', '.R#offset', 'siNMF', '.siNMF', 'snmf/l', 'snmf/r'. 使用以下之一:'brunet','Frobenius','KL','lee','ls-nmf','。M#brunet','nsNMF','offset','pe-nmf','。R #brunet','。R#lee','。R#nsNMF','。R#offset','siNMF','。siNMF','snmf / l','snmf / r'。

warnings.warn(x, RRuntimeWarning) warnings.warn(x,RRuntimeWarning)

I wonder whether could someone help me here? 我想知道是否有人可以在这里帮助我?

The original questioner had his question answered on the NMF project on Github . 最初的提问者在Github上的NMF项目上回答了他的问题 As described there, you define your new algorithm as a function, then use setNMFMethod to add the function to the registry of algorithms that perform Nonnegative Matrix Factorization, and then you can call it by name. 如此处所述,您将新算法定义为一个函数,然后使用setNMFMethod将函数添加到执行非负矩阵分解的算法注册表中,然后可以按名称进行调用。

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

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