简体   繁体   English

使用curve_fit在python中拟合数据

[英]Fitting data in python using curve_fit

I'm trying to fit data in python to obtain the coefficients for the best fit.我试图在 python 中拟合数据以获得最佳拟合的系数。

The equation I need to fit is:我需要拟合的方程是:

Vs = a*(qt^b) (fs^c) (ov^d) Vs = a*(qt^b) (fs^c) (ov^d)

Whereby I have the data for qt, fs and ov and need to obtain the values for a,b,c,d.因此,我有 qt、fs 和 ov 的数据,需要获取 a、b、c、d 的值。

The code I'm using is:我正在使用的代码是:

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

qt = [10867.073,    8074.986,   2208.366,   3066.566,   2945.326,   4795.766,   2249.813,   2018.3]
fs = [229.6,    17.4,   5.3,    0.1,    0.1,    0.1,    0.1,    0.1]     
ov = [19.159,   29.054, 37.620, 44.854, 51.721, 58.755, 65.622, 72.492]  
Vs = [149.787,  125.3962,   133.927,    110.047,    149.787,    137.809,    201.506,    154.925] 
d = [1.018, 1.518,  2.0179, 2.517,  3.017,  3.517,  4.018,  4.52]
 
def func(a, b, c, d):   
return a*qt**b*fs**c*ov**d
 
popt, pcov = curve_fit(func, Vs, d) 
print(popt[0], popt[1], popt[2])
plt.plot(Vs, d, 'ro',label="Original Data") 
plt.plot(Vs, func(Vs,*popt), label="Fitted Curve") 
plt.gca().invert_yaxis() 
plt.show()

Which produces the following output (Significant figures cut by me):产生以下 output (由我切割的重要数字):

-0.333528 -0.1413381 -0.3553966 -0.333528 -0.1413381 -0.3553966

在此处输入图像描述

I was hoping to get something more like below where the data it fitted but it hasn't been done perfectly (note the one below is just an example and is not correct).我希望在它适合的数据下面得到更多的东西,但它还没有完美地完成(注意下面的只是一个例子,并不正确)。

在此处输入图像描述

The main hitch is the graphical representation of the result.主要障碍是结果的图形表示。 What is drawn has no signifiance: Even with perfect data and with perfect fitting the points will appear verry scattered.画出来的东西没有意义:即使有完美的数据和完美的拟合,点也会显得非常分散。 This is misleading.这是误导。

Better draw (Vs from computation) divided by (Vs from data) and compare to 1 which should be the exact value if the fitting was perfect.更好地绘制(来自计算的 Vs)除以(来自数据的 Vs)并与 1 进行比较,如果拟合完美,这应该是确切的值。

Note that your problem is a simple linear regression in logarithm scale.请注意,您的问题是对数尺度的简单线性回归。

ln(Vs) = A + b * ln(qt) + c * ln(fs) + d * ln(ov) ln(Vs) = A + b * ln(qt) + c * ln(fs) + d * ln(ov)

Linear regression straight away gives A, b, c, d and a = exp(A).线性回归直接给出 A、b、c、d 和 a = exp(A)。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM