欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python连接高斯数据库_Python加载数据并执行多高斯fi

发布时间:2025/3/15 python 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python连接高斯数据库_Python加载数据并执行多高斯fi 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

简单制作单高斯和的参数化模型函数。为最初的猜测选择一个好的值(这是一个非常关键的步骤),然后让scipy.optimize稍微调整一下这些数字。

你可以这样做: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')

如您所见,这两种配合(视觉上)几乎没有区别。所以你不能确定源中是否有3个高斯子,或者只有2个高斯子。但是,如果您必须进行猜测,请检查最小的残差: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

在这种情况下,3高斯给出了一个更好的结果,但我也使我的初步猜测相当准确。

总结

以上是生活随笔为你收集整理的python连接高斯数据库_Python加载数据并执行多高斯fi的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。