Python处理PDF与CDF实例
在拿到数据后,最需要做的工作之一就是查看一下自己的数据分布情况。而针对数据的分布,又包括pdf和cdf两类。
下面介绍使用python生成pdf的方法:
使用matplotlib的画图接口hist(),直接画出pdf分布;
使用numpy的数据处理函数histogram(),可以生成pdf分布数据,方便进行后续的数据处理,比如进一步生成cdf;
使用seaborn的distplot(),好处是可以进行pdf分布的拟合,查看自己数据的分布类型;

上图所示为采用3种算法生成的pdf图。下面是源代码。
from scipy import statsimport matplotlib.pyplot as pltimport numpy as npimport seaborn as snsarr = np.random.normal(size=100)# plot histogramplt.subplot(221)plt.hist(arr)# obtain histogram dataplt.subplot(222)hist, bin_edges = np.histogram(arr)plt.plot(hist)# fit histogram curveplt.subplot(223)sns.distplot(arr, kde=False, fit=stats.gamma, rug=True)plt.show()
下面介绍使用python生成cdf的方法:
使用numpy的数据处理函数histogram(),生成pdf分布数据,进一步生成cdf;
使用seaborn的cumfreq(),直接画出cdf;

上图所示为采用2种算法生成的cdf图。下面是源代码。
from scipy import statsimport matplotlib.pyplot as pltimport numpy as npimport 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 statsimport matplotlib.pyplot as pltimport numpy as npimport seaborn as snsarr = np.random.normal(size=100)hist, bin_edges = np.histogram(arr)width = (bin_edges[1] - bin_edges[0]) * 0.8plt.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()
以上这篇Python处理PDF与CDF实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持好吧啦网。
相关文章:
1. Intellij IDEA如何去掉@Autowired 注入警告的方法2. .NET的基元类型包括什么及Unmanaged和Blittable类型详解3. IntelliJ IDEA 下载安装超详细教程(推荐)4. java编写一个花名随机抽取器的实现示例5. IntelliJ IDEA恢复删除文件的方法6. idea设置代码格式化的方法步骤7. SpringBoot + Vue 项目部署上线到Linux 服务器的教程详解8. python GUI库图形界面开发之PyQt5中QWebEngineView内嵌网页与Python的数据交互传参详细方法实例9. 初学者如何快速搭建Express开发系统步骤详解10. 在VS Code 中调试远程服务器的PHP代码详解

网公网安备