Python處理PDF與CDF實(shí)例
在拿到數(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 stats import matplotlib.pyplot as plt import numpy as np import seaborn as sns arr = 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的方法:
使用numpy的數(shù)據(jù)處理函數(shù)histogram(),生成pdf分布數(shù)據(jù),進(jìn)一步生成cdf;
使用seaborn的cumfreq(),直接畫出cdf;

上圖所示為采用2種算法生成的cdf圖。下面是源代碼。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import seaborn as sns arr = 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ù)分布。這個實(shí)現(xiàn)需要把pdf和cdf分別進(jìn)行歸一化。

上圖所示為歸一化的pdf和cdf。下面是源代碼。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import seaborn as sns arr = 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()
以上這篇Python處理PDF與CDF實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python reversed反轉(zhuǎn)序列并生成可迭代對象
這篇文章主要介紹了Python reversed反轉(zhuǎn)序列并生成可迭代對象,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
Python實(shí)現(xiàn)批量提取Word文檔表格數(shù)據(jù)
在大數(shù)據(jù)處理與信息抽取領(lǐng)域中,Word文檔是各類機(jī)構(gòu)和個人普遍采用的一種信息存儲格式,本文將介紹如何使用Python實(shí)現(xiàn)對Word文檔中表格的提取,感興趣的可以了解下2024-03-03
python把數(shù)據(jù)框?qū)懭隡ySQL的方法
這篇文章主要介紹了如何讓python把數(shù)據(jù)框?qū)懭隡ySQL,下文利用上海市2016年9月1日公共交通卡刷卡數(shù)據(jù)的一份數(shù)據(jù)單展開其方法,需要的小伙伴可以參考一下2022-03-03
Matplotlib學(xué)習(xí)筆記之plt.xticks()用法
在matplotlib中ticks表示的是刻度,而刻度有兩層意思,一個是刻標(biāo)(locs),一個是刻度標(biāo)簽(tick?labels),下面這篇文章主要給大家介紹了關(guān)于Matplotlib學(xué)習(xí)筆記之plt.xticks()用法的相關(guān)資料,需要的朋友可以參考下2022-09-09
Linux添加Python?path方法及修改環(huán)境變量的三種方法
這篇文章主要介紹了Linux添加Python?path方法及修改環(huán)境變量的三種方法,Linux 下設(shè)置環(huán)境變量有三種方法,一種用于當(dāng)前終端,一種用于當(dāng)前用戶,一種用于所有用戶,本文對每種方法給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Python編程tkinter庫Canvas實(shí)現(xiàn)涂鴉顏色表及圍棋盤示例
這篇文章主要為大家介紹了Python編程中如何使用tkinter庫Canvas來實(shí)現(xiàn)涂鴉,顏色表及圍棋盤的示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

