问题描述
在R中,有一个函数叫做密度()
.该函数的语法是 -
In R, there is a function called density()
. The syntax for the function is -
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
(计算估计值的数值向量)&调整
.我将其他参数保留为其默认值(bw = "nrd0"
, n = 512
& kernel = "gaussian"
)
Of the different parameters, the ones I normally use are x
(a numeric vector from which the estimate is computed) & adjust
. I leave the other parameters to its default value (bw = "nrd0"
, n = 512
& kernel = "gaussian"
)
Python 中是否有一个函数可以接受相同(或等效)的输入并返回相同的输出.我正在寻找的主要输出是 512 (因为 n = 512) x &y 值.
Is there a function in Python which takes the same (or equivalent) input AND returns the same output. The main output I am looking for are the 512 (since n = 512) x & y values.
推荐答案
根据 Oliver 的建议,我使用 rpy2 从 Python 中调用 R 的密度函数.
Based on Oliver's suggestion, I used rpy2 to call R's density function from Python.
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])
这篇关于R 函数密度()的 Python 等价物(即相同的输出)是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!