R函数density()的Python等价物(即相同的输出)是什么?

2024-09-28 21:43:24 发布

您现在位置:Python中文网/ 问答频道 /正文

在R中,有一个名为^{}的函数。函数的语法为-

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中是否有函数接受相同(或等效)的输入并返回相同的输出。我要寻找的主要输出是512(因为n=512)x&;y值


Tags: 函数false参数语法triangulargaussiandensitykernel
1条回答
网友
1楼 · 发布于 2024-09-28 21:43:24

根据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])

相关问题 更多 >