有人可以向我解释如何在Scipy中将位置参数与gamma.fit函数一起使用吗?
在我看来,位置参数(μ)将分布的支持度从x≥0更改为y =(x-μ)≥0。如果μ为正,那么我们不会丢失所有不满足条件的数据x-μ≥0?
谢谢!
fit
查找适合度时,该函数将所有数据考虑在内。向数据中添加噪声会改变拟合参数,并且会导致分布不能很好地代表数据。因此,在使用时,我们必须变得聪明一点fit
。
以下是一些y1
使用loc=2
和scale=1
使用numpy生成数据的代码。它还会在0到10范围内的数据中添加噪声以创建y2
。拟合可y1
产生出色的结果,但是尝试拟合嘈杂y2
是有问题的。我们添加的噪声会抹去分布。但是,在拟合数据时,我们也可以保持1个或多个参数不变。在这种情况下,我们传递floc=2
到fit
,它会在2
执行拟合时强制将位置保留在该位置,从而返回更好的结果。
from scipy.stats import gamma
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,10,.1)
y1 = np.random.gamma(shape=1, scale=1, size=1000) + 2 # sets loc = 2
y2 = np.hstack((y1, 10*np.random.rand(100))) # add noise from 0 to 10
# fit the distributions, get the PDF distribution using the parameters
shape1, loc1, scale1 = gamma.fit(y1)
g1 = gamma.pdf(x=x, a=shape1, loc=loc1, scale=scale1)
shape2, loc2, scale2 = gamma.fit(y2)
g2 = gamma.pdf(x=x, a=shape2, loc=loc2, scale=scale2)
# again fit the distribution, but force loc=2
shape3, loc3, scale3 = gamma.fit(y2, floc=2)
g3 = gamma.pdf(x=x, a=shape3, loc=loc3, scale=scale3)
并做一些情节...
# plot the distributions and fits. to lazy to do iteration today
fig, axes = plt.subplots(1, 3, figsize=(13,4))
ax = axes[0]
ax.hist(y1, bins=40, normed=True);
ax.plot(x, g1, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape1, loc1, scale1), xy=(6,.2))
ax.set_title('gamma fit')
ax = axes[1]
ax.hist(y2, bins=40, normed=True);
ax.plot(x, g2, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape2, loc2, scale2), xy=(6,.2))
ax.set_title('gamma fit with noise')
ax = axes[2]
ax.hist(y2, bins=40, normed=True);
ax.plot(x, g3, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape3, loc3, scale3), xy=(6,.2))
ax.set_title('gamma fit w/ noise, location forced')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句