- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据集,其中包含从 0 到 1e-5 的值。我想数据可以用对数正态分布来描述。因此,我使用 scipy.stats.lognorm 来拟合我的数据,并希望使用 matplotlib 在同一图形上绘制原始数据和拟合分布。
然后,我通过线图添加拟合分布。但是,这会将 Y 轴更改为非常大的数字:
所以图中看不到原始数据(sample)!
我检查了所有变量,发现变量 pdf_fitted
太大 (>1e7)。我真的不明白为什么将 scistats.lognorm.fit
简单拟合到由同一分布 scistats.lognorm.pdf
生成的样本不起作用。以下是演示我的问题的代码:
from matplotlib import pyplot as plt
from scipy import stats as scistats
import numpy as np
# generate a sample for x between 0 and 1e-5
x = np.linspace(0, 1e-5, num=1000)
y = scistats.lognorm.pdf(x, 3, loc=0, scale=np.exp(10))
h = plt.hist(y, bins=40) # plot the sample by histogram
# plt.show()
# fit the sample by using Log Normal distribution
param = scistats.lognorm.fit(y)
print("Log-normal distribution parameters : ", param)
pdf_fitted = scistats.lognorm.pdf(
x, *param[:-2], loc=param[-2], scale=param[-1])
plt.plot(x, pdf_fitted, label="Fitted Lognormal distribution")
plt.ticklabel_format(style='sci', scilimits=(-3, 4), axis='x')
plt.legend()
plt.show()
最佳答案
您面临的直接问题是您的健康状况真的非常糟糕。如果您将图上的 x 和 y 比例设置为日志,您可以看到这一点,例如 plt.xscale('log')
和 plt.yscale('log')
。这使您可以在单个图上同时查看直方图和拟合数据:
所以它在两个方向上都偏离了许多数量级。
您从 stats.lognorm
表示的概率分布生成样本并拟合它的整个方法是错误的。这是一种正确的方法,使用与您在问题中提供的对数范数分布相同的参数:
from matplotlib import pyplot as plt
from scipy import stats as scistats
import numpy as np
plt.figure(figsize=(12,7))
realparam = [.1, 0, np.exp(10)]
# generate pdf data around the mean value
m = realparam[2]
x = np.linspace(m*.6, m*1.4, num=10000)
y = scistats.lognorm.pdf(x, *realparam)
# generate a matching random sample
sample = scistats.lognorm.rvs(*realparam, size=100000)
# plot the sample by histogram
h = plt.hist(sample, bins=100, density=True)
# fit the sample by using Log Normal distribution
param = scistats.lognorm.fit(sample)
print("Log-normal distribution parameters : ", param)
pdf_fitted = scistats.lognorm.pdf(x, *param)
plt.plot(x, pdf_fitted, lw=5, label="Fitted Lognormal distribution")
plt.legend()
plt.show()
输出:
Log-normal distribution parameters : (0.09916091013245995, -215.9562383088556, 22245.970148671593)
关于python - 使用scipy对数正态分布拟合小值数据,然后在matplotlib中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53838781/
我想在 python 中找出一个整数的 log10,但我得到了一个错误,比如数学域错误 我的代码是这样的w=math.log10(q*q1)/math.log10(2) 其中 q1,q2 是整数 是的
舍入小数 在 NumPy 中,主要有五种方法来舍入小数: 截断 去除小数部分,并返回最接近零的浮点数。使用 trunc() 和 fix() 函数。 示例: import numpy as n
我有一个数值范围为 0 到 100 的 slider 。 我想将它们映射到 100 到 10,000,000 的范围内。 我在网上看到过一些函数,但它们都是用 C++ 编写的。我需要它在 Javasc
我想请用户输入一个整数(N),然后显示他/她输入的整数的 10 对数。我已经成功计算了 10 对数,但不知道如何像下面这样显示它: Write in an Integer: 455666 455666
我将 x 轴设置为对数刻度。最大值为10000,最小值为1。 GraphPane mypane = zedgraphcontrol.GraphPane; mypane.XAxis.Type = Axi
我正在尝试编写一个快速算法来计算 log gamma function 。目前我的实现看起来很幼稚,只是迭代了 1000 万次来计算 gamma 函数的对数(我还使用 numba 来优化代码)。 im
这个问题在这里已经有了答案: How to show minor tick labels on log-scale with Matplotlib (2 个答案) 关闭 7 年前。 将行 plt.y
抱歉标题不好 ;) 我正在尝试重新创建我在其他一些工作中遇到的 matlab 图,但我不太了解他们使用的比例。 y轴增量如下(从上往下[+ve y]): 0.9999,0.999,0.99,0.9,0
由于 1000 的以 10 为底的对数是 3,您可能期望 Math::log(1000, 10) 返回 3。相反,它返回 2.9999999999999996。 这是因为 Ruby 中的 float
我对对数 X 轴有疑问。阈值大于 0,x 的最小值为 1,并且所有 X 值都大于 0。并且仍然给我相同的错误 Can't plot zero or subzero values on a logari
我需要在我的应用程序中实现折线图,我想使用 MPAndroidChart。问题是 y 轴上的值将介于 1 和 1x10^-12 之间。这就是为什么我需要在该轴上的对数 View 。 有没有办法用那个库
我正在尝试按照 Logarithmic slider 中的示例进行操作. 这是我使用的代码: Timeline._MIN_PER_MINUTE = 1; Timeline._MIN_PER_HOUR
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我尝试为对数 y 轴绘制条形图。不幸的是,如果我将 y 轴设置为对数,则不再有条形图。我该怎么做才能实现这一目标?是否可以在 bar-function 中设置引用点(默认似乎为零)? 我的代码是: i
所以我一直在努力掌握 Big Oh 的计算方法。我觉得我已经掌握了基础知识,但对看似非常简单的计算感到困惑。所以如果下面的计算有很大的 O(n log n)(我真的希望我至少做对了)改变循环的顺序对复
我知道二维绘图的 semilogx 和 semilogy。 SURF 和 MESH 有什么等价物吗? 最佳答案 如上述链接所述,要将所有三个轴设置为对数刻度,请使用 set(gca, 'XScale'
这看起来很简单,但我在用 Ruby 计算 log (Base 5) 时遇到了问题。 显然标准的 base-10 日志工作正常: >> value = Math::log(234504) => 12.3
这段代码是用 C 语言根据 pollard 的对数 rho 算法(来自 wiki)编写的。在此代码中,如果我输入 alpha=2、beta=5、N=1019,则必须返回 a=681、b=378、A=3
有了this question之后通过指向 an external site 的链接回答,我意识到我解决了一个问题,只是为了得到另一个问题:在对数刻度上,MESH 和 SURF 函数的 C=Z 参数不
我正在尝试解决 the SPOJ problem PGCD , 它询问最大公约数表中出现了多少个素数。 我想到的第一个想法是先通过筛分生成素数。 然后,对于每个素数 p,查看有多少对(a,b),其中
我是一名优秀的程序员,十分优秀!