【发布时间】:2019-06-17 07:27:25
【问题描述】:
--- 示例 ---
我有一个数据集(样本),它在一维数组 (see the attached .json file) 中包含 1000 个损坏值(这些值非常小)。样本似乎遵循对数正态分布:
---问题和我已经尝试过的---
我尝试了这篇文章Fitting empirical distribution to theoretical ones with Scipy (Python)? 和这篇文章Scipy: lognormal fitting 中的建议,以通过对数正态分布拟合我的数据。这些都不起作用。 :(
我总是在 Y 轴上得到非常大的东西,如下所示:
这是我在 Python 中使用的代码(data.json 文件可以从here 下载):
from matplotlib import pyplot as plt
from scipy import stats as scistats
import json
with open("data.json", "r") as f:
sample = json.load(f) # load data: a 1000 * 1 array with many small values( < 1e-6)
fig, axis = plt.subplots() # initiate a figure
N, nbins, patches = axis.hist(sample, bins = 40) # plot sample by histogram
axis.ticklabel_format(style = 'sci', scilimits = (-3, 4), axis = 'x') # make X-axis to use scitific numbers
axis.set_xlabel("Value")
axis.set_ylabel("Count")
plt.show()
fig, axis = plt.subplots()
param = scistats.lognorm.fit(sample) # fit data by Lognormal distribution
pdf_fitted = scistats.lognorm.pdf(nbins, * param[: -2], loc = param[-2], scale = param[-1]) # prepare data for ploting fitted distribution
axis.plot(nbins, pdf_fitted) # draw fitted distribution on the same figure
plt.show()
我尝试了另一种分布,但是当我尝试绘制结果时,Y 轴总是太大,我无法用我的直方图绘制。我哪里失败了???
我还尝试了另一个问题中的建议:Use scipy lognormal distribution to fit data with small values, then show in matplotlib。但是变量pdf_fitted的值总是太大。
--- 期待结果---
基本上我想要的是这样的:
这是我在上面截图中使用的 Matlab 代码:
fname = 'data.json';
sample = jsondecode(fileread(fname));
% fitting distribution
pd = fitdist(sample, 'lognormal')
% A combined command for plotting histogram and distribution
figure();
histfit(sample,40,"lognormal")
所以如果你对 Python/Scipy/Numpy/Matplotlib 中 fitdist 和 histfit 的等效命令有任何想法,请发布!
非常感谢!
【问题讨论】:
标签: python matlab matplotlib scipy