简体   繁体   English

使用python 3.x从scipy使用** curve_fit **函数拟合多个高斯

[英]Fitting multiple gaussian using **curve_fit** function from scipy using python 3.x

I am trying to trimodal gaussian functions using scipy and python 3.x. 我正在尝试使用scipy和python 3.x进行三峰高斯函数。 I think I'm really almost there but I'm scratching my head here because I can't quite figure out what is going wrong with it. 我想我真的快要到那里了,但是我在这里挠头,因为我不太想知道问题出在哪里。

        data =np.loadtxt('mock.txt')
        my_x=data[:,0]
        my_y=data[:,1]

        def gauss(x,mu,sigma,A):
            return A*np.exp(-(x-mu)**2/2/sigma**2)
        def trimodal_gauss(x,mu1,sigma1,A1,mu2,sigma2,A2,mu3,sigma3,A3):
            return gauss(x,mu1,sigma1,A1)+gauss(x,mu2,sigma2,A2)+gauss(x,mu3,sigma3,A3)



        """""
        Gaussian fitting parameters recognized in each file
        """""
        first_centroid=(10180.4*2+9)/9
        second_centroid=(10180.4*2+(58.6934*1)+7)/9
        third_centroid=(10180.4*2+(58.6934*2)+5)/9
        centroid=[]
        centroid+=(first_centroid,second_centroid,third_centroid)

        apparent_resolving_power=1200
        sigma=[]
        for i in range(len(centroid)):
            sigma.append(centroid[i]/((apparent_resolving_power)*2.355))

        height=[1,1,1]

        p=[]    

        p = [list(t) for t in zip(centroid, sigma, height)] 


        for i in range(9):
            popt, pcov = curve_fit(trimodal_gauss,my_x,my_y,p0=p[i]) 

Using this code, I get the following error. 使用此代码,我得到以下错误。

TypeError: trimodal_gauss() missing 6 required positional arguments: 'mu2', 'sigma2', 'A2', 'mu3', 'sigma3', and 'A3'

I understand what the error message is saying but I don't think I understand how I'm not providing the 6 initial guesses. 我理解错误消息在说什么,但我不认为我不提供6个初始猜测。

I appreciate your input! 感谢您的投入!

It looks like you are trying to call curve_fit nine separate times, and give it a different initial parameter guess by specifying p0=p[i] (which is probably not what your code does, because p is a nested list). 看来您正在尝试分别调用9次curve_fit ,并通过指定p0=p[i]来给它一个不同的初始参数猜测值(这可能不是您的代码所做的,因为p是一个嵌套列表)。

You should make sure that p is a one-dimensional array with 9 elements, and call curve_fit only once. 您应该确保p是具有9个元素的一维数组,并且只调用一次curve_fit Something like 就像是

p = np.array([list(t) for t in zip(centroid, sigma, height)]).flatten()
popt, pcov = curve_fit(trimodal_gauss,my_x,my_y,p0=p]) 

might work. 可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM