Python处理PDF与CDF
生活随笔
收集整理的這篇文章主要介紹了
Python处理PDF与CDF
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在拿到數據后,最需要做的工作之一就是查看一下自己的數據分布情況。而針對數據的分布,又包括pdf和cdf兩類。
下面介紹使用python生成pdf的方法:
上圖所示為采用3種算法生成的pdf圖。下面是源代碼。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import seaborn as snsarr = 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的方法:
上圖所示為采用2種算法生成的cdf圖。下面是源代碼。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import 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放在一起,可以更好的顯示數據分布。這個實現需要把pdf和cdf分別進行歸一化。
上圖所示為歸一化的pdf和cdf。下面是源代碼。
from scipy import stats import matplotlib.pyplot as plt import numpy as np import seaborn as snsarr = 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()?
轉載于:https://www.cnblogs.com/wangjingchn/p/7376470.html
總結
以上是生活随笔為你收集整理的Python处理PDF与CDF的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入理解JavaScript定时函数se
- 下一篇: TensorFlow-Bitcoin-R