我有一个数据值数组如下:
0.000000000000000000e+00
3.617000000000000171e+01
1.426779999999999973e+02
2.526699999999999946e+01
4.483190000000000168e+02
7.413999999999999702e+00
1.132390000000000043e+02
8.797000000000000597e+00
1.362599999999999945e+01
2.080880900000000111e+04
5.580000000000000071e+00
3.947999999999999954e+00
2.615000000000000213e+00
2.458000000000000185e+00
8.204600000000000648e+01
1.641999999999999904e+00
5.108999999999999986e+00
2.388999999999999790e+00
2.105999999999999872e+00
5.783000000000000362e+00
4.309999999999999609e+00
3.685999999999999943e+00
6.339999999999999858e+00
2.198999999999999844e+00
3.568999999999999950e+00
2.883999999999999897e+00
7.307999999999999829e+00
2.515000000000000124e+00
3.810000000000000053e+00
2.829000000000000181e+00
2.593999999999999861e+00
3.963999999999999968e+00
7.258000000000000007e+00
3.543000000000000149e+00
2.874000000000000110e+00
................... and so on.
我想绘制数据值的概率密度函数。我提到了(Wiki)和scipy.stats.gaussian_kde。但我没有得到这是正确与否。 我正在使用python。简单的数据绘图代码如下:
from matplotlib import pyplot as plt
plt.plot(Data)
但现在我想绘制PDF(概率密度函数)。但是我没有在python中获得任何库。
答案 0 :(得分:7)
您提供的数据集非常小,可以进行可靠的内核密度估算。因此,我将通过使用另一个数据集来说明程序(如果我理解你正在尝试做什么)
import numpy as np
import scipy.stats
# generate data samples
data = scipy.stats.expon.rvs(loc=0, scale=1, size=1000, random_state=123)
然后可以通过简单地调用
来获得核密度估计scipy.stats.gaussian_kde(data,bw_method=bw)
其中bw
是估算程序的(可选)参数。对于此数据集,并考虑bw
的三个值,拟合如下所示
# test values for the bw_method option ('None' is the default value)
bw_values = [None, 0.1, 0.01]
# generate a list of kde estimators for each bw
kde = [scipy.stats.gaussian_kde(data,bw_method=bw) for bw in bw_values]
# plot (normalized) histogram of the data
import matplotlib.pyplot as plt
plt.hist(data, 50, normed=1, facecolor='green', alpha=0.5);
# plot density estimates
t_range = np.linspace(-2,8,200)
for i, bw in enumerate(bw_values):
plt.plot(t_range,kde[i](t_range),lw=2, label='bw = '+str(bw))
plt.xlim(-1,6)
plt.legend(loc='best')
请注意,较大的bw
值会导致更平滑的pdf估计值,但是,建议使用负值的成本(在此示例中)是可能的,但这不是这种情况。
答案 1 :(得分:1)
示例:
# a is your data array
hist, bins = np.histogram(a, bins=100, normed=True)
bin_centers = (bins[1:]+bins[:-1])*0.5
plt.plot(bin_centers, hist)