Python處理PDF與CDF實例
在拿到數(shù)據(jù)后,最需要做的工作之一就是查看一下自己的數(shù)據(jù)分布情況。而針對數(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()
在更多時候,需要把pdf和cdf放在一起,可以更好的顯示數(shù)據(jù)分布。這個實現(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實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA設(shè)置背景圖片的方法步驟2. Java類加載機制實現(xiàn)步驟解析3. 增大python字體的方法步驟4. Spring security 自定義過濾器實現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實例代碼)5. docker /var/lib/docker/aufs/mnt 目錄清理方法6. Python os庫常用操作代碼匯總7. Python TestSuite生成測試報告過程解析8. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法9. JAMon(Java Application Monitor)備忘記10. Python OpenCV去除字母后面的雜線操作
