Python 曲线拟合不准确

Python 曲线拟合不准确,python,scipy,curve-fitting,data-fitting,scipy-optimize,Python,Scipy,Curve Fitting,Data Fitting,Scipy Optimize,我试图尽可能好地拟合随时间波动的数据。首先,我平滑了数据,效果很好。我从中得到的平滑数据应该进一步通过拟合来表示,以获得更多的峰值。正如您在代码中看到的,我想使用logtanh函数来拟合数据。我很清楚,一些线程已经出现了这个问题,但我已经尝试过了,数据也不是很小或很大,我知道这也会导致问题 正如您所看到的,我尝试过的多项式拟合也很好,但它并没有消除所有的波浪值。它们会导致以下非常糟糕的衍生工具出现问题 import tkinter as tk from tkinter import filedi

我试图尽可能好地拟合随时间波动的数据。首先,我平滑了数据,效果很好。我从中得到的平滑数据应该进一步通过拟合来表示,以获得更多的峰值。正如您在代码中看到的,我想使用logtanh函数来拟合数据。我很清楚,一些线程已经出现了这个问题,但我已经尝试过了,数据也不是很小或很大,我知道这也会导致问题

正如您所看到的,我尝试过的多项式拟合也很好,但它并没有消除所有的波浪值。它们会导致以下非常糟糕的衍生工具出现问题

import tkinter as tk
from tkinter import filedialog
import numpy as np
import scipy.signal
from scipy.optimize import curve_fit
from numpy import diff
import matplotlib.pyplot as plt
from lmfit.models import StepModel, LinearModel



def loghypfunc(x, A, B, C, D, E):
    return A*np.log(1+x)+B*np.tanh(C*x)+D*x+E

def expfunc(t, c0, c1, c2, c3):
    return c0+c1*t-c2*np.exp(-c3*t)

def expdecay(x, a, b, c):
    return a * np.exp(-b * x) + c



path="C:/Users/Sammy/Documents/Masterarbeit WT/CSM und Kriechdaten/Kriechen/Creep_10mN_00008_LC_20210406_2121_DYN.txt"

dataFile = np.loadtxt(path, delimiter='\t', skiprows=2, usecols=(0, 1, 2, 3, 29, 30), dtype=float)
num_rows, num_cols = dataFile.shape

# time column
time = dataFile[:, [0]].transpose()
time = time.flatten()
refTime = time[0]  # get first time in column (reference)
# genullte Testzeit
timeNull = time - refTime
print("time", time)
flatTimeNull = timeNull.flatten()  # jetzt ein 1D array (one row)
##################################################################################
# indent displacement column
indentDis = dataFile[:, [4]].transpose()
indentDis = indentDis.flatten()
indentDis = indentDis - indentDis[0]
# the indendt data has to be smoothed so there is not such a big fluctuation
indentSmooth = scipy.signal.savgol_filter(indentDis, 2001, 3)
# null the indent Smooth data
indentSmooth_Null = indentSmooth - indentSmooth[0]
hind_Smooth_flat = indentSmooth_Null.flatten()  # jetzt ein 1D array
print('indent smooth', indentSmooth)
######################################################################

p0 = [100, 0.1, 100, 0.1]
c, cov = curve_fit(expfunc, time, indentSmooth, p0)
y_indent = expfunc(indentSmooth, *c)


p0 = [70, 0.5, 50, 0.1, 100]
popt, pcov = curve_fit(loghypfunc, time, indentSmooth, p0, maxfev = 10000)
y_indentTan = loghypfunc(indentSmooth, *popt)

modelh_t = np.poly1d(np.polyfit(time, indentSmooth, 8))

plt.plot(time, indentSmooth, 'r', label="Data smoothed")
plt.scatter(time, modelh_t(time), s=0.1, label="Polyfit")
plt.plot(time, y_indentTan, label="Curve fit Tangens function")
plt.plot(time, y_indent, label="Curve fit exp function")
plt.legend(loc="lower right")
plt.xlabel("time")
plt.ylabel("indent")

plt.show()
这是我从中获取数据的两个数组

time [  6.299596   6.349592   6.399589 ... 608.0109   608.060897 608.110894] 
indent smooth [120.81411822 121.07093706 121.32748184 ... 476.78825661 476.89357473 476.99915287]
这里是情节

我现在的问题是如何修复它。这是因为拟合的优化参数错误吗?但我想python应该做得足够好吧? 我的第二个猜测是,数据是沿着这个轴压缩的,因为数组大约有12000个值长。这可能是一个原因吗

如果您能给我一些关于fits的建议,我将不胜感激

问候
Hndrx

您是否尝试过更健壮的
lmfit
?谢谢你的回复。我听说了,但不确定它是否能帮助我,我会试一试你是对的。使用lmfit效果更好!你有什么解释为什么这更健壮吗?python中有很多fitting包,
lmfit
是基于
scipy的中级包。optimize
但使用可能性。你可以在这里找到一些