R 函数密度()的 Python 等价物(即相同的输出)是什么? [英] What is the Python equivalent (i.e. same output) for the R function density()?

查看:65
本文介绍了R 函数密度()的 Python 等价物(即相同的输出)是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 等价物(即相同的输出)是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆