Scipy Curve_fit:为什么我的拟合如此差,如何改善?

时间:2019-06-05 16:30:53

标签: python scipy curve-fitting

我正在尝试使用python的curve_fit库拟合我的数据。尽管我可以捕获数据模式,但实际拟合度很差。有什么方法可以改善合身质量吗?

enter image description here

这是我的代码:

import numpy as np
from scipy.optimize import curve_fit
from matplotlib import pyplot as plt

x=np.array([ 4.29977288,  4.18759576,  3.937875  ,  3.68784896,  3.43711213,
    3.19099287,  2.94166468,  2.68543877,  2.4289324 ,  2.19035861,
    1.93962193,  1.69285434,  1.44271633,  1.18869615,  0.94761142,
    0.69828307,  0.44606364,  0.19355101, -0.05367106, -0.30303661,
   -0.55272018, -0.79877747, -1.04806864, -1.29706657, -1.54567223,
   -1.79685098, -2.05011095, -2.29874144, -2.54813208, -2.80178461,
   -3.04828379, -3.29893363, -3.54727073, -3.79908534, -4.04661293]);

y=np.array([ 20.8744534 ,  20.89824536,  20.3763843 ,  19.79924837,
    19.19485964,  18.57716717,  17.93772371,  17.28834168,
    16.62367817,  15.94336213,  15.24389099,  14.52471466,
    13.7787734 ,  13.00299723,  12.18721413,  11.31510566,
    10.36672642,   9.32224105,   8.14237084,   6.78034367,
     5.19700447,   3.32945537,   1.10437136,  -1.48805508,
    -4.25695201,  -6.94906329,  -9.41648974, -11.54747381,
   -13.33444597, -14.90663076, -16.36783375, -17.72241553,
   -18.9592222 , -20.06703821, -21.07669491])

def func(x,A,B,C):
    a=1+B/A
    b=1-B/A
    k=C/np.log(a/b)
    y=A*np.tanh((x-C)/(2*k))
    return y

A_0=25
B_0=10
C_0=1.2


popt,pcov = curve_fit(func,x,y,p0=[A_0,B_0,C_0])
print(pcov)

plt.plot(x,y,label='Data')
plt.plot(x,func(x, *popt),'.',label='Fit')
plt.legend()
plt.show()

3 个答案:

答案 0 :(得分:2)

这不是curve_fit的问题,而是您正在使用的功能的问题。找到一个能胜任工作的功能并不总是那么容易,但是例如,一个错误功能会做得更好:

import numpy as np
from scipy import special
from scipy.optimize import curve_fit
from matplotlib import pyplot as plt

x=np.array([ 4.29977288,  4.18759576,  3.937875  ,  3.68784896,  3.43711213,
    3.19099287,  2.94166468,  2.68543877,  2.4289324 ,  2.19035861,
    1.93962193,  1.69285434,  1.44271633,  1.18869615,  0.94761142,
    0.69828307,  0.44606364,  0.19355101, -0.05367106, -0.30303661,
   -0.55272018, -0.79877747, -1.04806864, -1.29706657, -1.54567223,
   -1.79685098, -2.05011095, -2.29874144, -2.54813208, -2.80178461,
   -3.04828379, -3.29893363, -3.54727073, -3.79908534, -4.04661293]);

y=np.array([ 20.8744534 ,  20.89824536,  20.3763843 ,  19.79924837,
    19.19485964,  18.57716717,  17.93772371,  17.28834168,
    16.62367817,  15.94336213,  15.24389099,  14.52471466,
    13.7787734 ,  13.00299723,  12.18721413,  11.31510566,
    10.36672642,   9.32224105,   8.14237084,   6.78034367,
     5.19700447,   3.32945537,   1.10437136,  -1.48805508,
    -4.25695201,  -6.94906329,  -9.41648974, -11.54747381,
   -13.33444597, -14.90663076, -16.36783375, -17.72241553,
   -18.9592222 , -20.06703821, -21.07669491])

def func(x,A,B,C):
    a=1+B/A
    b=1-B/A
    k=C/np.log(a/b)
    y=A*np.tanh((x-C)/(2*k))
    return y

def erf(x, a, b, c, d):
    return d + 0.5*c*(1 + special.erf(a*(x-b)))

A_0=25
B_0=10
C_0=1.2


popt,pcov = curve_fit(func,x,y,p0=[A_0,B_0,C_0])
perf, pecov = curve_fit(erf, x, y, p0=(0.5,0,40,-20))

plt.plot(x,y, 'o', label='Data')
plt.plot(x,func(x, *popt),'-',label='Fit')
plt.plot(x, erf(x, *perf), '--', label='erf fit')
plt.legend()
plt.show()

enter image description here

答案 1 :(得分:2)

我不确定您是否一定要使用所使用的函数,还是可以使用多项式。在后一种情况下,您可以使用polyfit

请记住,您不能只使用任何高阶多项式,否则最终会过度拟合数据。您可以查看拟合的均方根误差,以评估其准确性

fit = np.poly1d(np.polyfit(x, y, 5))

plt.plot(x, y, '.', label='Data')
plt.plot(x, fit(x), label='Fit')
plt.legend()

enter image description here

答案 2 :(得分:0)

我认为可能是S型或峰方程对数据进行建模,这是峰方程和建模误差的示例图:

models errors

好像有两个组合信号,其中一个是低振幅循环信号。第二个信号不是简单的正弦波。我的想法是,您可能需要一个复杂的模型,该模型是两个不同方程的总和,以在单个模型中捕获这两个组件。