我应该使用哪个库/函数来将多元多项式拟合到我的数据?

问题描述 投票:0回答:1

我的数据取决于 4 个自变量 (x1,x2,x3,x4),我需要一个模型(Python 中提供)来评估数据点之外的 f(x1,x2,x3,x4)。原则上,如果我将 3 个变量设置为常量值,我始终可以使用合理次数的多项式拟合 (<5) to interpolate the data in the remaining dimension so I would like to generate a function that is capable to interpolate in all dimensions at once using a multivariate polynomial fit. It must be noted that the underlying function is non-linear (meaning that I should expect terms of the form x1^n*x2^m where n,m are not 0). What do you recommend?

为了说明这一点,我提供了一小部分数据样本:

(请注意,某些变量看似恒定是因为这只是一个小样本)

x1  x2  x3  x4  f
15  10  5   3   0.621646
15  10  5   5   0.488879
15  10  5   10  0.490204
15  10  7   0   0.616027
15  10  7   0.5 0.615497
15  10  7   1   0.619804
15  10  7   3   0.614494
15  10  7   5   0.556772
15  10  7   10  0.555393
15  20  0.5 0   0.764692
15  20  0.5 0.5 0.78774
15  20  0.5 1   0.799749
15  20  0.5 3   0.567796
15  20  0.5 5   0.328497
15  20  0.5 10  0.0923708
15  20  1   0   0.802219
15  20  1   0.5 0.811475
15  20  1   1   0.822908
15  20  1   3   0.721053
15  20  1   5   0.573549
15  20  1   10  0.206259
15  20  2   0   0.829069
15  20  2   0.5 0.831135
15  0   7   1   0.240144
15  0   7   3   0.258186
15  0   7   5   0.260836
python numpy scipy data-fitting
1个回答
0
投票

您可以使用

scipy.optimize.curve_fit()
函数进行多元曲线拟合。它有很好的文档记录,并且 StackOverflow 上有多个关于使用它进行多变量拟合的问题和答案。

对于您的情况,类似的事情可以帮助您开始

import numpy
from scipy.optimize import curve_fit

# Example function to fit to your data
def non_linear_func(x, a, b, c, d):
    return x[0] ** a * x[1] ** b + x[2] ** c + x[3] ** d 

# X is your multivariate x data
# f is your y data

# p0 is an initial guess for your a,b,c,d... in your fitting function
p0 = [1,2,3,4]

fitParams, fitCov = curve_fit(non_linear_func, X, y, p0=p0)

有几件事需要注意,您需要确保传递给

X
y
curve_fit()
具有正确的尺寸。
X
的维度必须为 N x M,其中 N 是您拥有的数据点的数量,M 是您拥有的自变量的数量。
y
的长度应为 N。

您还必须根据您想要的形式定义拟合函数,并尝试对函数中的参数进行初步猜测,

p0
,以帮助
curve_fit
找到最佳值。

希望有所帮助,StackOverflow 上有很多关于使用

curve_fit()
进行多元拟合的好答案(请参阅 herehere),并且 curve_fit 文档 也应该有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.