【问题标题】:Python, how to fit dataset to 1 or 2 equationsPython,如何将数据集拟合到 1 或 2 个方程
【发布时间】:2021-03-26 00:32:10
【问题描述】:

我有以下来自机械压痕测试的数据集:

https://www.dropbox.com/s/jovjl55sjjyph3r/Test%20dataset.csv?dl=0

该图显示了球形探头的位移与记录的力的关系。我需要用特定的方程拟合这些数据(如果你熟悉 DMT 模型)。 我已经生成了下面的代码,但我无法得到一个好的拟合结果。代码没有给出错误或警告,所以我不知道问题出在绘图上还是实际拟合上。

我是否正确编写了拟合代码?我是否将变量 Fad 和 R 正确传递到函数中?绘制拟合曲线的代码是否正确?

此外,在代码中,您可以注意到 2 个不同的拟合函数。 Function1 基于 2 个方程:

a = ((R/K)*(x+Fad))^(1/3)

y = ((a^2)/R)

Function2 与 function1 相同,但两个方程组合在一个方程中。有趣的是,他们给出了 2 个不同的情节!

重要的是,我想使用 2 方程方法,因为我应该使用其他更复杂的模型来拟合相同的数据集。在这些模型中,方程不能像这种情况那样容易地组合起来。

非常感谢社区为解决此问题提供的任何帮助。

import pandas
from matplotlib import pyplot as plt
from scipy import optimize
import numpy as np


df = pandas.read_table("Test dataset.csv", sep = ',', header=0)
df = df.astype(float) #Change data from object to float
print(df.shape)
print(df)

df_Fad = -(df.iloc[0, 0])
print("Adhesion force = {} N".format(df_Fad))
R = 280*1e-6
print("Probe radius = {} m".format(df_Fad))


df_x = df["Corr Force A [N]"].to_list()
df_y = df["Corr Displacement [m]"].to_list()


#Define fitting function1
def DMT(x, R, K, Fad):
    a = ((R/K)*(x+Fad))**(1/3)
    return ((a**2)/R)

custom_DMT = lambda x, K: DMT(x, R, K, df_Fad) #Fix Fad value
pars_DMT, cov_DMT = optimize.curve_fit(f=custom_DMT, xdata=df_x, ydata=df_y)
print("K = ", round(pars_DMT[0],2))
print ("E = ", round(pars_DMT[0]*(4/3),2))

ax0 = df.plot(kind='scatter', x="Corr Force A [N]", y="Corr Displacement [m]", color='lightblue')
plt.plot(df_x, DMT(np.array(df_y), pars_DMT[0], R, df_Fad), "--", color='black')
ax0.set_title("DMT fitting")
ax0.set_xlabel("Force / N")
ax0.set_ylabel("Displacement / m")
ax0.legend(['Dataset'])
plt.tight_layout()


#Define fitting function2 => function2 = funtion1 in one line
def DMT2(x, Fad, R, K):
    return ((x+Fad)**(2/3))/((R**(1/3))*(K**(2/3)))

custom_DMT2 = lambda x, K: DMT2(x, df_Fad, R, K) #Fix Fad value
pars_DMT2, cov_DMT2 = optimize.curve_fit(f=custom_DMT2, xdata=df_x, ydata=df_y)
print("K = ", round(pars_DMT2[0],2))
print ("E = ", round(pars_DMT2[0]*(4/3),2))

ax1 = df.plot(kind='scatter', x="Corr Force A [N]", y="Corr Displacement [m]", color='lightblue')
plt.plot( df_x, DMT2(np.array(df_y), pars_DMT2[0], df_Fad, R), "--", color='black')
ax1.set_title("DMT fitting")
ax1.set_xlabel("Force / N")
ax1.set_ylabel("Displacement / m")
ax1.legend(['Dataset'])
plt.tight_layout()

plt.show()

【问题讨论】:

    标签: python pandas matplotlib curve-fitting model-fitting


    【解决方案1】:

    经过进一步的尝试和研究,我已经解决了这个问题,尽管对上面一行代码的疑问仍然存在。我决定在这里发布我的解决方案,希望这对其他人有所帮助。

    我可以看到代码:

    def DMT(x, R, K, Fad):
        a = ((R/K)*(x+Fad))**(1/3)
        return ((a**2)/R)
    

    在拟合实验数据方面效果很好,这意味着更多的方程可以在 Python 中轻松使用和组合,这很棒。重要的是,变量(x、R、K、Fad)的引入顺序与它们在方程中出现的顺序相同。不这样做会产生随机结果。

    问题出在代码行:

    plt.plot(df_x, DMT(np.array(df_y), pars_DMT[0], R, df_Fad), "--", color='black')
    

    一开始我以为参数(x, R, K, Fad)的顺序不对。我试过了:

    plt.plot(df_x, DMT(np.array(df_y), R, pars_DMT[0], df_Fad), "--", color='black')
    

    但这并没有解决问题。谁能告诉我这条线有什么问题?

    无论如何,我解决这个问题的方法是使用以下代码从计算的参数K(R和df_Fad是固定的)直接计算拟合线的y数据:

    df["Fitting Displacement [m]"] = ((df["Corr Force A [N]"]+df_Fad)**(2/3))/((R**(1/3))*(pars_DMT[0]**(2/3)))
    

    这无论如何都是保存拟合结果的必要步骤。

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2022-06-10
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 2016-06-17
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多