Skip to main content
 首页 » 编程设计

python之概率密度函数 numpy 直方图/scipy stats

2024年10月25日23shanyou

我们有数组a=range(10)。使用 numpy.histogram:

hist,bins=numpy.histogram(a,bins=(np.max(a)-np.min(a))/1, range=np.min(a),np.max(a)),density=True) 

根据 numpy 教程:

If density=True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1.

结果是:

array([ 0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.2]) 

我尝试使用 scipy.stats 做同样的事情:

mean = np.mean(a) 
sigma = np.std(a) 
norm.pdf(a, mean, sigma) 

但是结果不同:

array([ 0.04070852,  0.06610774,  0.09509936,  0.12118842,  0.13680528,0.13680528,  0.12118842,  0.09509936,  0.06610774,  0.04070852]) 

我想知道为什么。

更新:我想设置一个更笼统的问题。我们如何在不使用 density=True 的 numpy.histogram 的情况下获得数组的概率密度函数?

请您参考如下方法:

If density=True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1.

那里的“规范化”并不意味着它将使用正态分布进行转换。它只是说 bin 中的每个值将除以条目总数,以便总密度等于 1。