python连接高斯数据库,Python加载数据并进行多高斯拟合

I've been looking for a way to do multiple Gaussian fitting to my data. Most of the examples I've found so far use a normal distribution to make random numbers. But I am interested in looking at the plot of my data and checking if there are 1-3 peaks.

I can do this for one peak, but I don't know how to do it for more.

I have tried using lmfit, and of course scipy, but with no nice results.

Thanks for any help!

解决方案

Simply make parameterized model functions of the sum of single Gaussians. Choose a good value for your initial guess (this is a really critical step) and then have scipy.optimize tweak those numbers a bit.

Here's how you might do it:

import numpy as np

import matplotlib.pyplot as plt

from scipy import optimize

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

def gaussian(x, height, center, width, offset):

return height*np.exp(-(x - center)**2/(2*width**2)) + offset

def three_gaussians(x, h1, c1, w1, h2, c2, w2, h3, c3, w3, offset):

return (gaussian(x, h1, c1, w1, offset=0) +

gaussian(x, h2, c2, w2, offset=0) +

gaussian(x, h3, c3, w3, offset=0) + offset)

def two_gaussians(x, h1, c1, w1, h2, c2, w2, offset):

return three_gaussians(x, h1, c1, w1, h2, c2, w2, 0,0,1, offset)

errfunc3 = lambda p, x, y: (three_gaussians(x, *p) - y)**2

errfunc2 = lambda p, x, y: (two_gaussians(x, *p) - y)**2

guess3 = [0.49, 0.55, 0.01, 0.6, 0.61, 0.01, 1, 0.64, 0.01, 0] # I guess there are 3 peaks, 2 are clear, but between them there seems to be another one, based on the change in slope smoothness there

guess2 = [0.49, 0.55, 0.01, 1, 0.64, 0.01, 0] # I removed the peak I'm not too sure about

optim3, success = optimize.leastsq(errfunc3, guess3[:], args=(data[:,0], data[:,1]))

optim2, success = optimize.leastsq(errfunc2, guess2[:], args=(data[:,0], data[:,1]))

optim3

plt.plot(data[:,0], data[:,1], lw=5, c='g', label='measurement')

plt.plot(data[:,0], three_gaussians(data[:,0], *optim3),

lw=3, c='b', label='fit of 3 Gaussians')

plt.plot(data[:,0], two_gaussians(data[:,0], *optim2),

lw=1, c='r', ls='--', label='fit of 2 Gaussians')

plt.legend(loc='best')

plt.savefig('result.png')

a4bfc2546b7a88276a3a30099dd49401.png

As you can see, there is almost no difference between these two fits (visually). So you can't know for sure if there were 3 Gaussians present in the source or only 2. However, if you had to make a guess, then check for the smallest residual:

err3 = np.sqrt(errfunc3(optim3, data[:,0], data[:,1])).sum()

err2 = np.sqrt(errfunc2(optim2, data[:,0], data[:,1])).sum()

print('Residual error when fitting 3 Gaussians: {}\n'

'Residual error when fitting 2 Gaussians: {}'.format(err3, err2))

# Residual error when fitting 3 Gaussians: 3.52000910965

# Residual error when fitting 2 Gaussians: 3.82054499044

In this case, 3 Gaussians gives a better result, but I also made my initial guess fairly accurate.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 双高斯可以使用 Python 中的 Scipy 库中的 curve_fit 函数来实现。以下是一个简单的例子: ```python import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt # 定义双高斯函数 def double_gaussian(x, a1, b1, c1, a2, b2, c2): return a1*np.exp(-(x-b1)**2/(2*c1**2)) + a2*np.exp(-(x-b2)**2/(2*c2**2)) # 生成一些随机数据 x = np.linspace(-10, 10, 100) y = double_gaussian(x, 1, -5, 1, 0.5, 5, 2) + np.random.normal(0, 0.1, 100) # 使用 curve_fit 进行 popt, pcov = curve_fit(double_gaussian, x, y) # 绘制结果 plt.plot(x, y, 'b-', label='data') plt.plot(x, double_gaussian(x, *popt), 'r-', label='fit') plt.legend() plt.show() ``` 在上面的代码中,我们首先定义了一个双高斯函数 double_gaussian,然后使用该函数生成了一些随机数据。接着,我们使用 curve_fit 函数进行,并将结果绘制出来。结果包含了六个参数,分别是两个高斯函数的振幅、中心和标准差。 ### 回答2: 双高斯是指使用两个高斯函数来数据曲线。Python中可以使用scipy库中的curve_fit函数进行高斯。 首先,导入需要的库: ```python import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt ``` 然后,定义一个双高斯函数,该函数包括两个高斯分布的参数以及自变量x: ```python def double_gaussian(x, a1, b1, c1, a2, b2, c2): return a1 * np.exp(-(x - b1) ** 2 / (2 * c1 ** 2)) + a2 * np.exp(-(x - b2) ** 2 / (2 * c2 ** 2)) ``` 接下来,准备好待数据集: ```python x = np.linspace(0, 10, 100) y = double_gaussian(x, 1, 5, 1, 2, 7, 0.5) + np.random.normal(0, 0.1, len(x)) ``` 然后,使用curve_fit函数进行高斯: ```python initial_guess = [1, 4, 1, 1, 5, 0.5] params, _ = curve_fit(double_gaussian, x, y, p0=initial_guess) ``` 最后,绘制原始数据曲线: ```python plt.scatter(x, y, label='Original Data') plt.plot(x, double_gaussian(x, *params), color='red', label='Fitted Curve') plt.legend() plt.show() ``` 这样,就可以得到一个双高斯的曲线,其中params包含了得到的参数值。根据自己的数据集,可以调整初值和其他参数来获得更好的效果。 ### 回答3: 双高斯是一种常用的数据方法,在python中可以通过一些库来实现。主要的步骤包括数据准备、模型构建和过程。 首先,需要准备待数据。假设我们有一组数据,包含自变量x和因变量y。可以将这组数据存储在两个分开的数组中,如x和y。 接下来,构建双高斯模型。双高斯模型由两个高斯函数叠而成,每个高斯函数由两个参数决定:峰值位置(平均值)和峰值的宽度(标准差)。可以使用高斯函数的数学表达式来构建模型,并定义一个函数来表示双高斯模型。 模型构建完成后,可以使用数据方法进行。在python中,可以使用scipy库中的curve_fit函数进行。此函数传入两个参数:函数和待数据。在过程中,使用最小二乘法来找到最佳的参数值,使模型与数据之间的误差最小。 完成后,可以得到最佳的参数值,分别对应于两个高斯函数的峰值位置和峰值宽度。可以使用这些参数值来绘制曲线,以及进行其他分析和应用。 总结来说,python可以通过准备数据、构建双高斯模型和使用数据方法实现双高斯。这种方法广泛应用于数据分析、信号处理和其他科学研究领域。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值