Python curve_fit具有多个自变量(以获取某些不知道参数的值)
有没有一种方法可以使用curve_fit来适合具有以下多个自变量的函数?
我尝试获取A1,B1,C1,A2,B2,C2,A3,B3,C3和D时X1,X2,X3和Y1(因变量)的值。我想通过使用scipy.ptimize优化这些值,以最大程度地减少错误。在真实情况下,对于X1,X2,X3和Y1,我有数百多个数据点。
或者,如果有一种更好的方法或更合适的方法来获取A1,B1,C1,A2,B2,C2,A3,B3,B3,C3和D的值?
import numpy as np
from scipy.optimize import curve_fit
x1 = [3,2,1]
x2 = [3,4,2]
x3 = [1,2,4]
y1 = [5,7,9]
def func(x1, x2, a1, b1, c1, a2, b2, c2, d):
return (a1*x1**3+b1*x1**2+c1*x1) +(a2*x2**3+b2*x2**2+c2*x2) + d
def func2(x1, x2, x3, a1, b1, c1, a2, b2, c2, a3, b3, c3, d):
return (a1*x1**3+b1*x1**2+c1*x1) +(a2*x2**3+b2*x2**2+c2*x2) + (a3*x3**3+b3*x3**2+c3*x3) + d
Is there a way to use curve_fit to fit for a function with multiple independent variables like below?
I try to get the value for a1, b1, c1, a2, b2, c2, a3, b3, c3 and d while x1, x2, x3 and y1 (dependent variable) are all known. I want to optimize these values to minimize my error by using scipy.optimize. Be noted in real situation, for x1, x2, x3 and y1, I have more than hundred data points.
Or if there is a better way or more appropriate way to get the value for a1, b1, c1, a2, b2, c2, a3, b3, c3 and d?
import numpy as np
from scipy.optimize import curve_fit
x1 = [3,2,1]
x2 = [3,4,2]
x3 = [1,2,4]
y1 = [5,7,9]
def func(x1, x2, a1, b1, c1, a2, b2, c2, d):
return (a1*x1**3+b1*x1**2+c1*x1) +(a2*x2**3+b2*x2**2+c2*x2) + d
def func2(x1, x2, x3, a1, b1, c1, a2, b2, c2, a3, b3, c3, d):
return (a1*x1**3+b1*x1**2+c1*x1) +(a2*x2**3+b2*x2**2+c2*x2) + (a3*x3**3+b3*x3**2+c3*x3) + d
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要通过
x1
和x2
在一个对象中,请参阅xdata
in示例:
结果:
You need to pass
x1
andx2
in one object, see description ofxdata
in docs forcurve_fit
:Example:
Result: