Python curve_fit具有多个自变量(以获取某些不知道参数的值)

发布于 2025-02-09 22:44:50 字数 644 浏览 2 评论 0原文

有没有一种方法可以使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2025-02-16 22:44:50

您需要通过x1x2一个对象中,请参阅xdata in

测量数据的自变量。通常应该是
M长度序列或(K,M)形阵列用于K的功能
预测变量,但实际上可以是任何对象。


示例:

import numpy as np
from scipy.optimize import curve_fit

# generate sample data for a1, b1, c1, a2, b2, c2, d = 1, 2, 3, 4, 5, 6, 7
np.random.seed(0)
x1 = np.random.randint(0, 100, 100)
x2 = np.random.randint(0, 100, 100)
y1 = (1 * x1**3 + 2 * x1**2 + 3 * x1) + (4 * x2**3 + 5 * x2**2 + 6 * (x2+np.random.randint(-1, 1, 100))) + 7

def func(x, a1, b1, c1, a2, b2, c2, d):
    return (a1 * x[0]**3 + b1 * x[0]**2 + c1 * x[0]) + (a2 * x[1]**3 + b2 * x[1]**2 + c2 * x[1]) + d

popt, _ = curve_fit(func, np.stack([x1, x2]), y1)

结果:

array([1.00000978, 1.99945039, 2.97065876, 4.00001038, 4.99920966,
       5.97424668, 6.71464229])

You need to pass x1 and x2 in one object, see description of xdata in docs for curve_fit:

The independent variable where the data is measured. Should usually be
an M-length sequence or an (k,M)-shaped array for functions with k
predictors, but can actually be any object.

Example:

import numpy as np
from scipy.optimize import curve_fit

# generate sample data for a1, b1, c1, a2, b2, c2, d = 1, 2, 3, 4, 5, 6, 7
np.random.seed(0)
x1 = np.random.randint(0, 100, 100)
x2 = np.random.randint(0, 100, 100)
y1 = (1 * x1**3 + 2 * x1**2 + 3 * x1) + (4 * x2**3 + 5 * x2**2 + 6 * (x2+np.random.randint(-1, 1, 100))) + 7

def func(x, a1, b1, c1, a2, b2, c2, d):
    return (a1 * x[0]**3 + b1 * x[0]**2 + c1 * x[0]) + (a2 * x[1]**3 + b2 * x[1]**2 + c2 * x[1]) + d

popt, _ = curve_fit(func, np.stack([x1, x2]), y1)

Result:

array([1.00000978, 1.99945039, 2.97065876, 4.00001038, 4.99920966,
       5.97424668, 6.71464229])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文