用 python 拟合直方图
我有一个直方图,
H=hist(my_data,bins=my_bin,histtype='step',color='r')
我可以看到形状几乎是高斯的,但我想用高斯函数拟合这个直方图并打印我得到的平均值和西格玛的值。你能帮助我吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有一个在 py2.6 和 py3.2 上运行的示例:
Here you have an example working on py2.6 and py3.2:
这是一个使用 scipy.optimize 拟合非线性函数(如高斯函数)的示例,即使数据位于范围不明确的直方图中,因此简单的均值估计也会失败。偏移常量还会导致简单的正态统计失败(只需删除普通高斯数据的 p[3] 和 c[3] )。
输出:
Here is an example that uses scipy.optimize to fit a non-linear functions like a Gaussian, even when the data is in a histogram that isn't well ranged, so that a simple mean estimate would fail. An offset constant also would cause simple normal statistics to fail ( just remove p[3] and c[3] for plain gaussian data).
Output:
从
Python 3.8
开始,标准库提供NormalDist
对象作为统计
模块。NormalDist
对象可以使用NormalDist.from_samples
方法并提供对其平均值的访问(NormalDist.mean
) 和标准差 (NormalDist.stdev
):Starting
Python 3.8
, the standard library provides theNormalDist
object as part of thestatistics
module.The
NormalDist
object can be built from a set of data with theNormalDist.from_samples
method and provides access to its mean (NormalDist.mean
) and standard deviation (NormalDist.stdev
):这是仅使用
matplotlib.pyplot
和numpy
包的另一个解决方案。它仅适用于高斯拟合。它基于最大似然估计,并且已在此主题。
这是相应的代码:
这里是输出。
Here is another solution using only
matplotlib.pyplot
andnumpy
packages.It works only for Gaussian fitting. It is based on maximum likelihood estimation and have already been mentioned in this topic.
Here is the corresponding code :
and here is the output.
我有点困惑,norm.fit 显然只适用于扩展的采样值列表。我尝试给它两个数字列表或元组列表,但它似乎只会展平所有内容并威胁作为单个样本的输入。由于我已经有了基于数百万个样本的直方图,如果没有必要,我不想扩展它。值得庆幸的是,正态分布的计算很简单,所以......
我确信这必须由图书馆提供,但由于我在任何地方都找不到它,所以我将其发布在这里。请随意指出正确的方法并否决我:-)
I was a bit puzzled that
norm.fit
apparently only worked with the expanded list of sampled values. I tried giving it two lists of numbers, or lists of tuples, but it only appeared to flatten everything and threat the input as individual samples. Since I already have a histogram based on millions of samples, I didn't want to expand this if I didn't have to. Thankfully, the normal distribution is trivial to calculate, so...I'm sure this must be provided by the libraries, but as I couldn't find it anywhere, I'm posting this here instead. Feel free to point to the correct way to do it and downvote me :-)