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

标签 python r scikit-learn kernel-density

在 R 中,有一个函数叫做 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 中是否有一个函数接受相同(或等效)的输入并且返回相同的输出。我正在寻找的主要输出是 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])

关于python - R 函数 density() 的 Python 等价物(即相同的输出)是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65759744/

相关文章:

python - 如何使正则表达式匹配整个字符串?

python - 在 Pyramid 中混合身份验证策略

oracle - 如何在不公开密码的情况下从R通过ODBC访问Oracle数据库?

python-3.x - 我在理解 sklearn 的 TfidfVectorizer 结果时遇到问题

python - Scikit-learn out-of-core 文本分类内存消耗

python - 如何强制以特定的明显模式对数据进行聚类?

python - 如何在两个数据类之间绘制分隔线?

python - 在多行之间过滤和应用条件

r - 在R data.table中创建虚拟变量

r - 根据上次发生的条件进行更新