使用curve_fit拟合列表中2个变量的数据 [英] Use of curve_fit to fit data of 2 variables in a list

查看:51
本文介绍了使用curve_fit拟合列表中2个变量的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是scipy和 curve_fit 的新手.

我有2个列表:

x个值:

  [0.723938224,0.965250965,1.206563707,1.447876448,1.689189189,1.930501931、2.171814672] 

y个值:

  [2.758,2.443,2.142333333,1.911,1.817666667,1.688333333,1.616] 

我想对这2个数据集执行 curve_fit ,但是我似乎无法弄清楚它们之间的关系.我大致知道了将两者融合在一起的方程式:

我知道有一个方程可以将它们拟合在一起:

  0.74/(((9.81 *(x/100))^(1/2)) 

但是我如何仅使用python曲线拟合来证明该方程式为上述方程式.如果我在excel中做类似的事情,它将自动给我方程式.在python中如何工作?

我不确定如何执行 curve_fit 并绘制趋势线.有人可以帮忙吗?谢谢.

解决方案

首先,让我们定义曲线拟合函数.您说Excel告诉您该函数的格式为 a/(b *(x/c)** d).我告诉你,Excel除了自动填充之外什么都不知道.该方程式可以很容易地转换为((a * c ** d)/b)/x ** d ,因此我们实际上要考虑的函数的形式为 a/x** b .现在到实际的

对我很好.如果您问我,则不应让Excel靠近任何统计数据.

I am kind of new to scipy and curve_fit.

I have 2 lists:

x values:

[0.723938224, 0.965250965, 1.206563707, 1.447876448, 1.689189189, 
1.930501931, 2.171814672]

y values:

[2.758, 2.443, 2.142333333, 1.911, 1.817666667, 1.688333333, 1.616]

I would like to perform a curve_fit on these 2 datasets, but I cannot seem to figure out the relationship. I known roughly the equation that fits them both together:

I know that there' an equation that fits them together:

0.74/((9.81*(x/100))^(1/2))

But how would I prove that the equation is the equation above just using python curve fits. If I do a similar thing in excel, it would automatically give me the equation. How would it work in python?

I am not sure how to perform the curve_fit and draw the trendline. Could someone help? Thanks.

解决方案

For a start, let's define the curve fit function. You say that Excel tells you the function is of the form a/(b*(x/c)**d). I tell you, Excel knows nothing about anything apart from autofill; this equation can easily be transformed into ((a*c**d)/b)/x**d, so the function we actually have to consider is of the form a/x**b. Now to the actual curve fitting with scipy:

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

x = [0.723938224, 0.965250965, 1.206563707, 1.447876448, 1.689189189, 1.930501931, 2.171814672]
y = [2.758, 2.443, 2.142333333, 1.911, 1.817666667, 1.688333333, 1.616]

def func(x, a, b):
    return a/(x**b)

#start values, not really necessary here but good to know the concept
p0 = [2, 0.5]
#the actual curve fitting, returns the parameters in popt and the covariance matrix in pcov
popt, pcov = curve_fit(func, np.asarray(x), np.asarray(y), p0)
#print out the parameters a, b
print(*popt)
#a=2.357411406488454, b=0.5027391574181408

#plot the function to see, if the fit is any good
#first the raw data
plt.scatter(x, y, marker="x", color="red", label="raw data")
#then the fitted curve
x_fit = np.linspace(0.9*min(x), 1.1*max(x), 1000)
y_fit = func(x_fit, *popt)
plt.plot(x_fit, y_fit, color="blue", label="fitted data")

plt.legend()
plt.show()

Output:

Looks good to me. And one shouldn't let Excel near any statistical data, if you asked me.

这篇关于使用curve_fit拟合列表中2个变量的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆