将 R 代码转换为 Python 脚本

发布于 2024-09-13 12:25:29 字数 518 浏览 10 评论 0原文

我得到了以下 R 代码,我需要将其转换为 python 并在 python 环境中运行它,基本上我已经使用 rpy2 模块完成了此操作,但是用 python 做同样的事情看起来有点乏味,所以有人可以找到更好的方法吗使用 rpy2 模块将以下 R 代码重写为等效的 python 脚本?

mymad <- function (x) 
{
    center <- median(x)
    y <- abs(x - center)
    n <- length(y)
    if (n == 0) 
        return(NA)
    half <- (n + 1)/2
    1.4826 * if (n%%2 == 1) {
        sort(y, partial = half)[half]
    }
    else {
        sum(sort(y, partial = c(half, half + 1))[c(half, half + 
            1)])/2
    }
}

I got the following R code and I need to convert it to python and run it in python environment, basically I have done this with rpy2 module, but it looks kind of dull with python doing the same things, so could someone find a better way to rewrite the following R code to an equivalent python script with the rpy2 module?

mymad <- function (x) 
{
    center <- median(x)
    y <- abs(x - center)
    n <- length(y)
    if (n == 0) 
        return(NA)
    half <- (n + 1)/2
    1.4826 * if (n%%2 == 1) {
        sort(y, partial = half)[half]
    }
    else {
        sum(sort(y, partial = c(half, half + 1))[c(half, half + 
            1)])/2
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

私藏温柔 2024-09-20 12:25:29

您可以说明函数的用途,即中值绝对偏差。您所说的 mymad 是总体标准差的近似值,基于正态分布变量的大样本假设。

根据这个网站

def median(pool):
    copy = sorted(pool)
    size = len(copy)
    if size % 2 == 1:
        return copy[(size - 1) / 2]
    else:
        return (copy[size/2 - 1] + copy[size/2]) / 2

所以,你想要一个函数mad,它会验证:

mad(x) == median(abs(x-median(x)))

感谢Elenaher(给出他的评论积分),这里是代码:

def mad(x):
    return median([abs(val-median(x)) for val in x])

然后,我相信你正在计算:

def mymad(x):
    return 1.4826*mad(x)

You could have stated the purpose of your function, which is Median Absolute Deviation. What you call mymad is an approximation of the standard deviation of the population, based on the assumption of large samples of normally distributed variables.

According to this website:

def median(pool):
    copy = sorted(pool)
    size = len(copy)
    if size % 2 == 1:
        return copy[(size - 1) / 2]
    else:
        return (copy[size/2 - 1] + copy[size/2]) / 2

So, you want a function mad which would verify :

mad(x) == median(abs(x-median(x)))

Thanks to Elenaher (give his comment credits), here is the code:

def mad(x):
    return median([abs(val-median(x)) for val in x])

And then, I believe your are computing:

def mymad(x):
    return 1.4826*mad(x)
美羊羊 2024-09-20 12:25:29

可能比 numpy/Python 编写的慢一点,但实现起来肯定更快(因为没有重新发明轮子):

# requires rpy2 >= 2.1
from rpy2.robjects.packages import importr
stats = importr('stats')

stats.mad(x)

Probably a little slower than a numpy/Python written one, but certainly faster to implement (as no wheel gets reinvented):

# requires rpy2 >= 2.1
from rpy2.robjects.packages import importr
stats = importr('stats')

stats.mad(x)
哭了丶谁疼 2024-09-20 12:25:29
import numpy
# x is the input array
x = numpy.array( [1,2,4,3,1,6,7,5,4,6,7], float ) }
# mad = median( | x - median(x) | )
mad =  numpy.median( numpy.abs( ( x - numpy.median( x ) ) )
import numpy
# x is the input array
x = numpy.array( [1,2,4,3,1,6,7,5,4,6,7], float ) }
# mad = median( | x - median(x) | )
mad =  numpy.median( numpy.abs( ( x - numpy.median( x ) ) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文