Python處理PDF與CDF實(shí)例
在拿到數(shù)據(jù)后,最需要做的工作之一就是查看一下自己的數(shù)據(jù)分布情況。而針對(duì)數(shù)據(jù)的分布,又包括pdf和cdf兩類。
下面介紹使用python生成pdf的方法:
使用matplotlib的畫圖接口hist(),直接畫出pdf分布;
使用numpy的數(shù)據(jù)處理函數(shù)histogram(),可以生成pdf分布數(shù)據(jù),方便進(jìn)行后續(xù)的數(shù)據(jù)處理,比如進(jìn)一步生成cdf;
使用seaborn的distplot(),好處是可以進(jìn)行pdf分布的擬合,查看自己數(shù)據(jù)的分布類型;

上圖所示為采用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的數(shù)據(jù)處理函數(shù)histogram(),生成pdf分布數(shù)據(jù),進(jìn)一步生成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()
在更多時(shí)候,需要把pdf和cdf放在一起,可以更好的顯示數(shù)據(jù)分布。這個(gè)實(shí)現(xiàn)需要把pdf和cdf分別進(jìn)行歸一化。

上圖所示為歸一化的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實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Android如何加載Base64編碼格式圖片2. 詳解Android studio 動(dòng)態(tài)fragment的用法3. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題4. 基于android studio的layout的xml文件的創(chuàng)建方式5. 圖文詳解vue中proto文件的函數(shù)調(diào)用6. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)7. 什么是python的自省8. 使用Android studio查看Kotlin的字節(jié)碼教程9. Vuex localStorage的具體使用10. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備