R 函数密度() 的 Python 等效项(即相同的输出)是什么?

2024-02-16

在R中,有一个函数叫做density() https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/density。该函数的语法是 -

density(x, bw = "nrd0", adjust = 1, kernel = c("gaussian", "epanechnikov", 
        "rectangular", "triangular", "biweight","cosine", "optcosine"),
        weights = NULL, window = kernel, width, give.Rkern = FALSE, n = 512, 
        from, to, cut = 3, na.rm = FALSE, …)

在不同的参数中,我通常使用的是x(计算估计值的数值向量)&adjust。我将其他参数保留为其默认值(bw = "nrd0", n = 512 & kernel = "gaussian")

Python 中是否有一个函数接受相同(或等效)的输入 AND返回相同的输出。我正在寻找的主要输出是 512(因为 n = 512)x 和 y 值。


根据 Oliver 的建议,我使用 rpy2 从 Python 调用 R 的密度函数。

R 中的代码

column <- c(63, 45, 47, 28, 59, 28, 59)

output <- density(column, adjust=1) #all other parameters set to default

x <- output$x
y <- output$y

Python 代码

from rpy2 import robjects
from rpy2.robjects.packages import importr
from rpy2.robjects import vectors
import numpy as np

stats = importr("stats")

column = vectors.IntVector([63, 45, 47, 28, 59, 28, 59])

output = stats.density(column, adjust=1)

x = np.array(output[0])
y = np.array(output[1])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R 函数密度() 的 Python 等效项(即相同的输出)是什么? 的相关文章

随机推荐