Scipy lognorm 拟合直方图 [英] Scipy lognorm fitting to histogram

查看:58
本文介绍了Scipy lognorm 拟合直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将对数正态 pdf 拟合到一些分箱数据,但我的曲线与数据不太匹配,请参见下图.我的代码是:

I'm fitting a lognormal pdf to some binned data, but my curve doesn't quite match the data, see image below. My code is:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import lognorm

data = genfromtxt('data.txt')
data = np.sort(data)

# plot histogram in log space

ax.hist(data, bins=np.logspace(0,5,200),normed=1)
ax.set_xscale("log")

shape,loc,scale = lognorm.fit(data)

print shape, loc, scale

pdf = sp.stats.lognorm.pdf(data, shape, loc, scale)

ax.plot(data,pdf)

plt.show()

这是它的样子:

我是否需要以某种方式为拟合提供对形状、位置和比例的合理猜测?

Do I need to somehow provide the fit with sensible guesses for shape, loc and scale?

谢谢!

推荐答案

您尝试拟合的数据看起来不像对数正态分布.在对数 x 尺度上绘制时,对数正态分布应该看起来像正态分布.在您展示的图中,情况并非如此.当分布不能很好地拟合数据时,您会得到奇怪的参数.

The data you are trying to fit does not look like a lognormal distribution. The lognormal distribution, when plotted on a logarithmic x scale should look like a normal distribution. This is not the case in the plot you show. When the distribution does not fit the data well you get weird parameters.

在尝试拟合某些内容之前,您需要了解数据的真正分布方式(严格来说,这与 SO 无关).

You will need to find out how your data is really distributed (which, strictly speaking, is off-topic at SO) before attempting to fit something.

这是我们使用从对数正态分布随机抽取的数据时得到的结果:

This is what we get when using data randomly drawn from a lognormal distribution:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import lognorm

np.random.seed(42)

data = lognorm.rvs(s=0.5, loc=1, scale=1000, size=1000)

# plot histogram in log space
ax = plt.subplot(111)
ax.hist(data, bins=np.logspace(0,5,200), density=True)
ax.set_xscale("log")

shape,loc,scale = lognorm.fit(data)

x = np.logspace(0, 5, 200)
pdf = lognorm.pdf(x, shape, loc, scale)

ax.plot(x, pdf, 'r')

plt.show()

这篇关于Scipy lognorm 拟合直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆