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

发布于 2025-01-18 01:26:46 字数 1002 浏览 1 评论 0原文

我的数据取决于 4 个自变量 (x1,x2,x3,x4),我需要一个模型(Python 中提供)来评估数据点之外的 f(x1,x2,x3,x4)。原则上,如果我将 3 个变量设置为常量值,我始终可以使用合理次数 (<5) 的多项式拟合来对剩余维度中的数据进行插值,因此我想生成一个能够进行插值的函数使用多元多项式拟合同时在所有维度上进行计算。必须注意的是,底层函数是非线性的(这意味着我应该期望 x1^n*x2^m 形式的项,其中 n,m 不为 0)。你有什么建议吗?

为了说明这一点,我包含了一小部分数据样本:(

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

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

I have data that depends on 4 independent variables (x1,x2,x3,x4) and I need a model (available in Python) to evaluate f(x1,x2,x3,x4) outside the data points. In principle, if I set 3 of my variables as constant values I can always use a polynomial fit of a reasonable degree (<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?

To illustrate I am including a small sample of data:

(Note that the fact that some variables appear to be constant is due to the fact that this is just a small sample)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

番薯 2025-01-25 01:26:47

您可以使用 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)

需要注意的几件事,您需要确保传递给 curve_fit 的 Xy () 具有正确的尺寸。 X 的维度必须为 N x M,其中 N 是您拥有的数据点数量,M 是您拥有的自变量数量。 y 的长度应为 N。

您还必须根据您想要的形式定义拟合函数,并尝试为参数给出初始猜测 p0在函数中帮助 curve_fit 找到最佳值。

希望有帮助,StackOverflow 上有很多关于使用 curve_fit() 进行多变量拟合的好答案(请参阅 此处此处)和 curve_fit 文档 也应该有帮助。

You can do multivariate curve fitting use the scipy.optimize.curve_fit() function. It is well documented and there are multiple questions and answers on StackOverflow on using it for multivariate fitting.

For your case, something like this can help you start off

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)

A couple of things to note, you need to make sure that the X and y you pass to curve_fit() have the correct dimensions. X must have dimensions of N x M, where N is the number of data points you have, and M is the number of independent variables you have. y should be of length N.

You must also define your fitting function based on the form that you would like, and try and give an initial guess, p0, for the parameters in the function to help curve_fit find the optimal values.

Hope that helps, there are lots of good answers on multivariate fitting with curve_fit() on StackOverflow (see here and here) and the curve_fit documentation should be of help as well.

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