欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python處理PDF與CDF實(shí)例

 更新時(shí)間:2020年02月26日 08:42:07   作者:Apple@Jing  
今天小編就為大家分享一篇Python處理PDF與CDF實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在拿到數(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 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()

在更多時(shí)候,需要把pdf和cdf放在一起,可以更好的顯示數(shù)據(jù)分布。這個(gè)實(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í)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論