Python处理PDF与CDF
生活随笔
收集整理的这篇文章主要介绍了
Python处理PDF与CDF
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
在拿到数据后,最需要做的工作之一就是查看一下自己的数据分布情况。而针对数据的分布,又包括pdf和cdf两类。
下面介绍使用python生成pdf的方法:
上图所示为采用3种算法生成的pdf图。下面是源代码。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import seaborn as snsarr = np.random.normal(size=100)# plot histogram plt.subplot(221) plt.hist(arr)# obtain histogram data plt.subplot(222) hist, bin_edges = np.histogram(arr) plt.plot(hist)# fit histogram curve plt.subplot(223) sns.distplot(arr, kde=False, fit=stats.gamma, rug=True) plt.show()
下面介绍使用python生成cdf的方法:
上图所示为采用2种算法生成的cdf图。下面是源代码。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import seaborn as snsarr = np.random.normal(size=100)plt.subplot(121) hist, bin_edges = np.histogram(arr) cdf = np.cumsum(hist) plt.plot(cdf)plt.subplot(122) cdf = stats.cumfreq(arr) plt.plot(cdf[0])plt.show()
在更多时候,需要把pdf和cdf放在一起,可以更好的显示数据分布。这个实现需要把pdf和cdf分别进行归一化。
上图所示为归一化的pdf和cdf。下面是源代码。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import seaborn as snsarr = np.random.normal(size=100)hist, bin_edges = np.histogram(arr) width = (bin_edges[1] - bin_edges[0]) * 0.8 plt.bar(bin_edges[1:], hist/max(hist), width=width, color='#5B9BD5')cdf = np.cumsum(hist/sum(hist)) plt.plot(bin_edges[1:], cdf, '-*', color='#ED7D31')plt.xlim([-2, 2]) plt.ylim([0, 1]) plt.grid()plt.show()
转载于:https://www.cnblogs.com/wangjingchn/p/7376470.html
总结
以上是生活随笔为你收集整理的Python处理PDF与CDF的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 深入理解JavaScript定时函数se
- 下一篇: Python 元组遍历排序操作方法