我应该使用哪个库/函数将多元元音拟合到我的数据中?
我的数据取决于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 scipy.optimize.curve_fit() 函数进行多元曲线拟合。它有很好的文档记录,StackOverflow 上有多个关于使用它进行多变量拟合的问题和答案。
对于您的情况,这样的事情可以帮助您开始
需要注意的几件事,您需要确保传递给 curve_fit 的
X
和y
() 具有正确的尺寸。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
A couple of things to note, you need to make sure that the
X
andy
you pass tocurve_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 helpcurve_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.