【问题标题】:Fitting gaussian and lorentz to data in python将高斯和洛伦兹拟合到python中的数据
【发布时间】:2017-09-15 20:47:16
【问题描述】:

我已经将一组数据加载到 python 并认为我已经适合高斯和洛伦兹形状,但是我需要它打印到每个参数的值和相关的错误,我不知道该怎么做.

我是编程新手,因此我们将不胜感激!

这是我的代码:

import numpy as np                                                
import matplotlib.pyplot as plt                                    
import pylab as p                                                  
from scipy.optimize import curve_fit, leastsq                      
import math
from scipy.stats import norm

E,I = np.loadtxt('resonance_data.txt', unpack = True)

# Function to be fitted
def gauss(E, I0, E0, sigma):
    n = len(E)
    mean = sum(E*I)/sum(I)
    sigma = (np.sqrt(sum((E - mean)**2)/sum(I)))
    I0 = max(I)
    E0 = 31
    return I0* np.exp(-((E-E0)/sigma)**2)

sigmaerror = sigma - 28.01177

print sigmaerror

def lorentz(E,I0,E0,gamma):
    I0 = max(I)
    E0 = 31
    return I0*((gamma**2)/(((E-E0)**2)+gamma**2))

# Initialization parameters
init_vals = [45., 31., 33.]

best_vals, covar = curve_fit(gauss,E,I,p0=init_vals)
print best_vals

print curve_fit(gauss, E, I, p0=[max(I), mean, sigma])
print curve_fit(lorentz, E, I, p0=[max(I), mean, sigma])

plt.plot(E,I,'b+:',label='data')

popt,pcov = curve_fit(gauss,E,I,p0=[max(I), mean, sigma])
plt.plot(E,gauss(E,*popt),'r-',label='Gaussian')

popt,pcov = curve_fit(lorentz,E,I,p0=[max(I),mean,sigma])
plt.plot(E,lorentz(E,*popt), 'g', label='Lorentz')

plt.legend()
plt.title('Energy vs Intensity')
plt.xlabel('Energy')
plt.ylabel('Intensity')
plt.show()
print 'sigma =',sigma, 'error =', sigmaerror
print 'E0 = 34.22349966'
print 'I0 = 44.84743332'

【问题讨论】:

  • 最优参数存储在best_valsdocumentation 中解释得很好。

标签: python curve-fitting gaussian


【解决方案1】:

只需打印poptpcov

print(popt)
print(pcov)

它们按照您提供给模型的顺序包含参数和协方差。

【讨论】:

    猜你喜欢
    • 2017-09-09
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多