使用 Python 拟合 3D 多项式曲面 [英] Fit 3D Polynomial Surface with Python

查看:40
本文介绍了使用 Python 拟合 3D 多项式曲面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 代码,它根据 x 和 y 值计算 z 值.总的来说,我有 7 个 x 值和 7 个 y 值以及 49 个 z 值,它们排列在一个网格中(x 和 y 分别对应一个轴,z 是高度).

I have a python code that calculates z values dependent on x and y values. Overall, I have 7 x-values and 7 y-values as well as 49 z-values that are arranged in a grid (x and y correspond each to one axis, z is the height).

现在,我想以 z = f(x,y) 的形式拟合一个 2 次多项式曲面.

Now, I would like to fit a polynomial surface of degree 2 in the form of z = f(x,y).

我找到了一个执行此计算的 Matlab 命令.(https://www.mathworks.com/help/curvefit/fit.html)

I found a Matlab command that does this calculation. (https://www.mathworks.com/help/curvefit/fit.html)

load franke
sf = fit([x, y],z,'poly23')
plot(sf,[x,y],z)

我想用 Python 计算我的 2 度函数的参数.我尝试使用带有以下拟合函数的 scipy curve_fit 函数:

I want to calculate the parameters of my 2 degree function in Python. I tried to use the scipy curve_fit function with the following fit function:

def func(a, b, c, d ,e ,f ,g ,h ,i ,j, x, y):
    return a + b * x**0 * y**0 + c * x**0 * y**1 + d * x**0 * y**2 
             + e * x**1 * y**0 + f * x**1 * y**1 + g * x**1 * y**2
             + h * x**2 * y**0 + i * x**2 * y**1 + j * x**2 * y**2
    
guess = (1,1,1,1,1,1,1,1,1,1)
params, pcov = optimize.curve_fit(func, x, y, guess)

但此时我感到困惑,我不确定这是否是获取拟合函数参数的正确方法.这个问题可能有另一种解决方案吗?非常感谢!

But at this point I am getting confused and I am not sure, if this is the right approach to get the parameters for my fit function. Is there possibly another solution for this problem? Thank's a lot!

推荐答案

现在,两年后,我能够解决这个问题.这是一个具有多项式特征的经典线性回归问题,其中输入变量排列在网格中.在下面的代码中,我计算了我需要的多项式特征,分别是那些将解释我的目标变量的特征.

Now, two years later, I am able to solve the problem. It is a classical linear regression problem with polynomial features, where the input variables are arranged in a mesh. In the code below, I calculated the polynomial features I need, respectively that ones, that will explain my target variable.

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(0)
# set dimension of the data
dim = 12
# create random data, which will be the target values
Z = (np.ones((dim,dim)) * np.arange(1,dim+1,1))**3 + np.random.rand(dim,dim) * 200 

# create a 2D-mesh
x = np.arange(1,dim+1).reshape(dim,1)
y = np.arange(1,dim+1).reshape(1,dim)
X,Y = np.meshgrid(x,y)

# calculate polynomial features based on the input mesh
features = {}
features['x^0*y^0'] = np.matmul(x**0,y**0).flatten()
features['x*y'] = np.matmul(x,y).flatten()
features['x*y^2'] = np.matmul(x,y**2).flatten()
features['x^2*y^0'] = np.matmul(x**2, y**0).flatten()
features['x^2*y'] = np.matmul(x**2, y).flatten()
features['x^3*y^2'] = np.matmul(x**3, y**2).flatten()
features['x^3*y'] = np.matmul(x**3, y).flatten()
features['x^0*y^3'] = np.matmul(x**0, y**3).flatten()
dataset = pd.DataFrame(features)

# fit a linear regression model
reg = LinearRegression().fit(dataset.values, Z.flatten())
# get coefficients and calculate the predictions 
z_pred = reg.intercept_ + np.matmul(dataset.values, reg.coef_.reshape(-1,1)).reshape(dim,dim)

# visualize the results
fig = plt.figure(figsize = (5,5))
ax = Axes3D(fig)
# plot the fitted curve
ax.plot_wireframe(X, Y, z_pred, label = 'prediction')
# plot the target values
ax.scatter(X, Y, Z, c = 'r', label = 'datapoints')
ax.view_init(25, 80)
plt.legend()

这篇关于使用 Python 拟合 3D 多项式曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆