【问题标题】:Fitting binned lognormal data in Python在 Python 中拟合分级对数正态数据
【发布时间】:2017-02-10 15:50:45
【问题描述】:

我有一系列按体积分数百分比排列的粒度分布数据,如下所示:;

size   %
6.68   0.05
9.92   1.15
etc.

我需要将此数据拟合到对数正态分布,我计划使用 python 的 stats.lognorm.fit 函数来执行此操作,但这似乎期望输入作为变量数组而不是分箱数据,根据我的 @ 987654321@.

我计划使用 for 循环遍历数据并将每个大小条目 .extend 到占位符数组所需的次数,以创建一个包含与分箱数据相对应的变量列表的数组。

不过,这看起来真的很丑陋且效率低下,而且可能有一种简单的方法可以做到。有没有办法将分箱数据输入到stats.lognorm.fit 函数中?

【问题讨论】:

  • 我跳到了错误的结论,即您将累积百分比制成表格。

标签: python curve-fitting curve


【解决方案1】:

我想一种可能的解决方法是手动将 pdf 拟合到您的 bin 数据中,假设 x 值是每个间隔的中点,y 值是相应的 bin 频率。然后使用scipy.optimize.curve_fit 拟合基于 x 和 y 值的曲线。我认为结果的准确性将取决于您拥有的垃圾箱数量。示例如下:

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

def pdf(x, mu, sigma):
    """pdf of lognormal distribution"""

    return (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))

mu, sigma = 3., 1.                              # actual parameter value

data = np.random.lognormal(mu, sigma, size=1000)       # data generation
h = plt.hist(data, bins=30, normed = True)

y = h[0]                                        # frequencies for each bin, this is y value to fit 
xs = h[1]                                       # boundaries for each bin
delta = xs[1] - xs[0]                           # width of bins
x = xs[:-1] + delta /                           # midpoints of bins, this is x value to fit

popt, pcov = curve_fit(pdf, x, y, p0=[1, 1])    # data fitting, popt contains the fitted parameters
print(popt)
# [ 3.13048122  1.01360758]                       fitting results

fig, ax = plt.subplots()
ax.hist(data, bins=30, normed=True, align='mid', label='Histogram')
xr = np.linspace(min(xs), max(xs), 10000)
yr = pdf(xr, mu, sigma)
yf = pdf(xr, *popt)
ax.plot(xr, yr, label="Actual")
ax.plot(xr, yf, linestyle = 'dashed', label="Fitted")
ax.legend()

【讨论】:

  • 谢谢你们俩。
  • curve_fit 在 bin 大小不统一时不起作用。
猜你喜欢
  • 2016-04-17
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2014-07-01
  • 2019-04-10
  • 2020-03-09
相关资源
最近更新 更多