[英]Fitting Gaussian curve to data in python
我正在嘗試擬合並繪制一些給定數據的高斯曲線。 這是我到目前為止:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Generate data
mu, sigma = 0, 0.1
y, xe = np.histogram(np.random.normal(mu, sigma, 1000))
x = .5 * (xe[:-1] + xe[1:])
def gauss (x, y):
p = [x0, y0, sigma]
return p[0] * np.exp(-(x-p[1])**2 / (2 * p[2]**2))
p0 = [1., 1., 1.]
fit = curve_fit(gauss, x, y, p0=p0)
plt.plot(gauss(x, y))
plt.show()
當我運行代碼時,我收到此錯誤:
TypeError: gauss() takes exactly 2 arguments (4 given)
我不明白我在哪里給了我的函數4個參數。 我也不相信我正在使用曲線功能,但我不確定我做錯了什么。 任何幫助,將不勝感激。
這是追溯:
Traceback (most recent call last):
File "F:\Numerical methods\rw893 final assignment.py", line 21, in <module>
fitE, fitI = curve_fit(gauss, x, y, p0=p0)
File "F:\Portable Python 2.7.5.1\App\lib\site-packages\scipy\optimize\minpack.py", line 515, in curve_fit
res = leastsq(func, p0, args=args, full_output=1, **kw)
File "F:\Portable Python 2.7.5.1\App\lib\site-packages\scipy\optimize\minpack.py", line 354, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
File "F:\Portable Python 2.7.5.1\App\lib\site-packages\scipy\optimize\minpack.py", line 17, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "F:\Portable Python 2.7.5.1\App\lib\site-packages\scipy\optimize\minpack.py", line 427, in _general_function
return function(xdata, *params) - ydata
TypeError: gauss() takes exactly 2 arguments (4 given)
檢查第一個scipy文檔docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.optimize.curve_fit.html :
scipy.optimize.curve_fit
scipy.optimize.curve_fit(f,xdata,ydata,p0 = None,sigma = None,** kw)
Use non-linear least squares to fit a function, f, to data. Assumes ydata = f(xdata, *params) + eps
要安裝的功能應該只采用標量 ( 不是 : *p0
)。 我想提醒您,在調用curve_fit
期間,您將初始化參數x0
, y0
, sigma
curve_fit
給函數gauss
。 你調用初始化p0 = [x0, y0, sigma]
。
函數gauss
返回值y = y0 * np.exp(-((x - x0) / sigma)**2)
。 因此,輸入值必須是x
, x0
, y0
, sigma
。 第一個參數x
是您知道的數據以及函數y
的結果。 后面的三個參數將被擬合 - 您將它們作為初始化參數交給它們。
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Create data:
x0, sigma = 0, 0.1
y, xe = np.histogram(np.random.normal(x0, sigma, 1000))
x = .5 * (xe[:-1] + xe[1:])
# Function to be fitted
def gauss(x, x0, y0, sigma):
p = [x0, y0, sigma]
return p[1]* np.exp(-((x-p[0])/p[2])**2)
# Initialization parameters
p0 = [1., 1., 1.]
# Fit the data with the function
fit, tmp = curve_fit(gauss, x, y, p0=p0)
# Plot the results
plt.title('Fit parameters:\n x0=%.2e y0=%.2e sigma=%.2e' % (fit[0], fit[1], fit[2]))
# Data
plt.plot(x, y, 'r--')
# Fitted function
x_fine = np.linspace(xe[0], xe[-1], 100)
plt.plot(x_fine, gauss(x_fine, fit[0], fit[1], fit[2]), 'b-')
plt.savefig('Gaussian_fit.png')
plt.show()
可能你的回調是在curve_fit
中用不同數量的參數調用的。 看一下它所說的文檔 :
模型函數f(x,...)。 它必須將自變量作為第一個參數,並將參數作為單獨的剩余參數。
為了確保這一點,你可能想在第一個參數之后取*args
並查看你得到的結果。
from numpy import loadtxt
import numpy as np
from scipy import *
from matplotlib import *
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(x, a, b, c, d, x0):
return a*np.exp(-(x-x0)**2/(2*d**2)) + c
x = np.linspace(0,4,50)
y = func(x, 2.5, 1.3, 0.5, 1.0, 2.0)
yn = y + 0.2*np.random.normal(size=len(x))
p = [1,1,1,1,1]
popt, pcov = curve_fit(func, x, yn, p0=p)
plt.plot(x,func(x,popt[0],popt[1],popt[2],popt[3],popt[4]))
plt.plot(x,yn,'r+')
plt.show()
這應該有所幫助。 這也可以擴展到3d高斯,然后輸入數組'x'應該是(x,y)值的k維數組,'yn'應該是z值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.