stats.gaussian_kde方法如何计算pdf? [英] How does the stats.gaussian_kde method calcute the pdf?

查看:514
本文介绍了stats.gaussian_kde方法如何计算pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scipy中的scipy.stats.gaussian_kde方法从数据中生成随机样本.

I am using the scipy.stats.gaussian_kde method from scipy to generate random samples from the data.

工作正常!我现在发现,该方法还具有内置函数,可以计算给定点集(我的数据)的概率密度函数.

It works fine! What I have now found out is that the method also has inbuilt functions to calculate the probability density function of the given set of points (my data).

I would like to know how it calculates the pdf provided a set of points.

I would like to know how it calculates the pdf provided a set of points.

这是一个小例子:

import numpy as np
import scipy.stats
from scipy import stats

def getDistribution1(data):
    kernel = stats.gaussian_kde(data,bw_method=0.06)
    class rv(stats.rv_continuous):
        def _rvs(self, *x, **y):
            return kernel.resample(int(self._size)) #random variates
        def _cdf(self, x):
            return kernel.integrate_box_1d(0,max(x)) #Integrate pdf between two bounds (-inf to x here!)
        def _pdf(self, x):
            return kernel.evaluate(x)  #Evaluate the estimated pdf on a provided set of points
    return rv(name='kdedist')

test_data = np.random.random(100) # random test data 
distribution_data = getDistribution1(test_data)
pdf_data = distribution_data.pdf(test_data) # the pdf of the data

在上面的代码中,存在三种方法,

In the above piece of code, there exists three methods,

  1. rvs根据数据生成随机样本
  2. cdf是pdf从0到max(data)的整数
  3. pdf是数据的pdf
  1. rvs to generate random samples based on data
  2. cdf which is the integral of the pdf from 0 to max(data)
  3. pdf which is the pdf of the data

我需要这份pdf文件的原因是因为现在我正在尝试根据概率为我的数据计算权重. 以便我可以给每个数据点一个概率,然后将其用作权重.

The reason I need this pdf is because now I am trying to calculate weights for my data based on probability. So that I can give each of my data point a probability which I can then use as my weights.

我还想从这里知道如何计算体重?

I would also like to know from here how I should proceed to calculate my weights?

P.S.请原谅我在交叉验证中问了同样的问题,似乎没有任何回应!

P.S. Forgive me for asking the same question in cross validated, there seems to be no response!

推荐答案

在线文档具有指向源代码的链接,对于gaussian_kde,其位于此处:

The online docs have a link to the source code, which for gaussian_kde is here: https://github.com/scipy/scipy/blob/v0.15.1/scipy/stats/kde.py#L193

这篇关于stats.gaussian_kde方法如何计算pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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