用python拟合数据的多峰对数正态分布

2024-04-24 11:08:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我在实验室用仪器测量了以下数据。由于仪器根据颗粒的直径将不同大小的颗粒收集在箱子中,因此测量基本上是“装箱”的:

import numpy as np
import matplotlib.pylab as plt
from lmfit import models

y = np.array([196, 486, 968, 2262, 3321, 4203, 15072, 46789, 95201, 303494, 421484, 327507, 138931, 27973])
bins = np.array([0.0150, 0.0306, 0.0548, 0.0944, 0.1540, 0.2560, 0.3830, 0.6050, 0.9510, 1.6400, 2.4800, 3.6700, 5.3800, 9.9100, 15])

bin_width=np.diff(bins)
x_plot = np.add(bins[:-1],np.divide(bin_width,2))
x=x_plot
y=y

enter image description here

这里绘制的是数据的外观。有一个模式在0.1左右,另一个模式在2左右,以x刻度为单位。你知道吗

在这一研究领域内,将“多峰”对数正态分布拟合到此类数据是很常见的:鉴于此,我使用LMFIT拟合了大约2的模式:

model = models.LognormalModel()
params = model.make_params(center=1.5, sigma=0.6, amplitude=2214337)

result = model.fit(y, params, x=x)
print(result.fit_report())

plt.plot(x, y, label='data')
plt.plot(x, result.best_fit, label='fit')
plt.xscale("log")
plt.yscale("log")
plt.legend()
plt.show()

enter image description here

正如预期的那样,这将很好地适合2左右的第二个模式。我的问题是,我该如何在0.1左右拟合第二个模式(实际上,两个模式的总和应该拟合数据)?我意识到也有人认为三种模式更好,但我假设一旦我了解如何使用两种模式,添加第三种模式应该是微不足道的。你知道吗


Tags: 数据importmodelplotmodelsasnp模式
2条回答

这是一个对数正态的混合分布。您可以简单地获取数据日志并拟合高斯混合:

import numpy as np
from sklearn.mixture import GaussianMixture

# Make data from two log-normal distributions
# NOTE: 2d array of shape (n_samples, n_features)
n = 10000
x = np.zeros((n,1))
x[:n//2] = np.random.lognormal(0,1, size=(n//2,1))
x[n//2:] = np.random.lognormal(2,0.5, size=(n//2,1))

# Log transform the data
x_transformed = np.log(x)

# Make gaussian mixture model.
# n_init makes multiple initial guesses and
# depending on data, 1 might be good enough
# Decrease tolerance for speedup or increase for better precision
m = GaussianMixture(n_components=2, n_init=10, tol=1e-6)

# Fit the model
m.fit(x_transformed)

# Get the fitted parameters
# NOTE: covariances are stdev**2
print(m.weights_) # [0.50183897 0.49816103]
print(m.means_) # [1.99866785, -0.00528186]
print(m.covariances_) # [0.25227372,0.99692494]

lmfit.Models可以加在一起,如下所示:

model = (models.LognormalModel(prefix='p1_') +
         models.LognormalModel(prefix='p2_') +
         models.LognormalModel(prefix='p3_') )

params = model.make_params(p1_center=0.3, p1_sigma=0.2, p1_amplitude=1e4,
                           p2_center=1.0, p2_sigma=0.4, p2_amplitude=1e6,
                           p3_center=1.5, p3_sigma=0.6, p3_amplitude=2e7)

在复合模型中,模型的每个组件都有自己的“前缀”(任何字符串),该前缀在参数名之前。使用以下工具进行拟合后,可以获得模型组件的字典:

components = result.eval_components()
# {'p1_': Array, 'p2_': Array, 'p3_': Array}
for compname, comp in components.keys():
    plt.plot(x, comp, label=compname)

为了拟合在半对数或对数图上表示的数据,可以考虑将模型拟合到log(y)。否则,当y的值非常低时,拟合将不会对不匹配非常敏感。你知道吗

注意,lmfit模型和参数支持边界。您可能希望或发现需要放置边界,例如

params['p1_amplitude'].min = 0
params['p1_sigma'].min = 0
params['p1_center'].max = 0.5
params['p3_center'].min = 1.0

相关问题 更多 >