python中curvefit_scipy.optimize.curve_fit函数⽤法解析
在⽇常数据分析中,免不了要⽤到数据曲线拟合,⽽optimize.curve_fit()函数正好满⾜你的需求
scipy.optimize.curve_fit(f,xdata,ydata,p0=None,sigma=None,absolute_sigma=False,check_finite=True,bounds=(-
inf,inf),method=None,jac=None,**kwargs)
参数解析f 函数名
callable
The model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.linspace函数python
简单来说就是需要拟合的好函数y,包括⾃变量x,参数A,B;
⽽curve_fit的主要功能就是计算A,B
#要拟合的⼀次函数
def f_1(x, A, B):
return A * x + Bxdata
array_like or objectThe independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.
ydata
array_likeThe dependent data, a length M array - nominallyf(xdata,...).
p0
array_like , optionalInitial guess for the parameters (length N). If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).其它的参数均不再说明!
返回值解析poptarray
Optimal values for the parameters so that the sum of the squared residuals off(xdata,*popt)-ydatais minimized
即残差最⼩时参数的值
pcov2d array
The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov)).
How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.
If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.
简单例⼦例⼦1:拟合直线
# 引⽤库函数
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize as op
# 需要拟合的数据组
x_group = np.array([3, 6.1, 9.1, 11.9, 14.9])
y_group = np.array([0.0221, 0.0491, 0.0711, 0.0971, 0.1238])
# 需要拟合的函数
def f_1(x, A, B):
return A * x + B
# 得到返回的A,B值
A, B = op.curve_fit(f_1, x_group, y_group)[0]
# 数据点与原先的进⾏画图⽐较
plt.scatter(x_group, y_group, marker='o',label='real')
x = np.arange(0, 15, 0.01)
y = A * x + B
plt.plot(x, y,color='red',label='curve_fit')
plt.legend()
plt.title('%.5fx%.5f=y' % (A, B))
plt.show()例⼦2:官⽅例⼦import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
# 定义需要拟合的函数
def func(x, a, b, c):
return a * np.exp(-b * x) + c
# Define the data to be fit with some noise:
# ⽤numpy的random库⽣成⼲扰
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
np.random.seed(1729)
y_noise = 0.2 * al(size=xdata.size)
ydata = y + y_noise
plt.plot(xdata, ydata, 'b-', label='data')
# Fit for the parameters a, b, c of the function func:
popt, pcov = curve_fit(func, xdata, ydata)
print(popt)
plt.plot(xdata, func(xdata, *popt), 'r-',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
# Constrain the optimization to the region of 0 <= a <= 3, 0 <= b <= 1 and 0 <= c <= 0.5: # 限定范围进⾏拟合
popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5]))
print(popt)
plt.plot(xdata, func(xdata, *popt), 'g--',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
#结果
#[2.55423706 1.35190947 0.47450618]
#[2.43708905 1. 0.35015434]
参考
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论