溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python如何處理PDF與CDF

發(fā)布時(shí)間:2021-08-03 10:41:19 來源:億速云 閱讀:551 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Python如何處理PDF與CDF的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

在拿到數(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ù)的分布類型;

Python如何處理PDF與CDF

上圖所示為采用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;

Python如何處理PDF與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()

在更多時(shí)候,需要把pdf和cdf放在一起,可以更好的顯示數(shù)據(jù)分布。這個(gè)實(shí)現(xiàn)需要把pdf和cdf分別進(jìn)行歸一化。

Python如何處理PDF與CDF

上圖所示為歸一化的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()

感謝各位的閱讀!關(guān)于“Python如何處理PDF與CDF”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI