使用numpy的一组值的概率密度函数

时间:2016-06-22 12:29:22

标签: python numpy matplotlib statistics probability-density

以下是我想要绘制PDF的数据。 https://gist.github.com/ecenm/cbbdcea724e199dc60fe4a38b7791eb8#file-64_general-out

以下是脚本

import numpy as np
import matplotlib.pyplot as plt
import pylab

data = np.loadtxt('64_general.out')
H,X1 = np.histogram( data, bins = 10, normed = True, density = True) # Is this the right way to get the PDF ?
plt.xlabel('Latency')
plt.ylabel('PDF')
plt.title('PDF of latency values')

plt.plot(X1[1:], H)
plt.show()

当我绘制上述内容时,我得到以下内容。

  1. 以上是计算一系列值的PDF的正确方法
  2. 还有其他方法可以确认我得到的结果是实际的PDF。例如,如何在我的情况下显示pdf = 1下的区域。
  3. enter image description here

1 个答案:

答案 0 :(得分:1)

  1. 这是近似 PDF的合法方式。由于np.histogram使用各种技术对值进行分级,因此您无法获得输入中每个数字的确切频率。要获得更精确的近似值,您应计算每个数字的出现次数并将其除以总计数。此外,由于这些是离散值,因此绘图可以绘制为点或条,以给出更正确的印象。

  2. 在离散情况下,频率之和应等于1.在连续情况下,您可以使用np.trapz()来近似积分。