Python:我如何在 3D 中应用 polyfit 特征与 3 个数组 x[]、y[]、z[]

Python: How can I apply the polyfit feature in 3D with 3 arrays x[], y[], z[]

我有这 3 个数组和我的数据:

X=np.array(x)
Y=np.array(y)
Z=np.array(z)

我知道如何绘制我的点,以及如何在 2D 中应用 polyfit。如何从我的 3D 数据中获取 polyfit 系数?我可以绘制我的 3D 拟合曲线吗?

我不确定是否可以使用 np.polyfit(),但我找到了一个可以帮助 here 的参考。它实现查找系数如下:

import numpy as np

# note I have changed the capital to lowercase since the rest of the code is that way
x=np.array(x)
y=np.array(y)
z=np.array(z)

degree = 3

# Set up the canonical least squares form
Ax = np.vander(x, degree)
Ay = np.vander(y, degree)
A = np.hstack((Ax, Ay))

# Solve for a least squares estimate
(coeffs, residuals, rank, sing_vals) = np.linalg.lstsq(A, z)

# Extract coefficients and create polynomials in x and y
xcoeffs = coeffs[0:degree]
ycoeffs = coeffs[degree:2 * degree]

fx = np.poly1d(xcoeffs)
fy = np.poly1d(ycoeffs)

我希望你在寻找什么,关于计算的进一步解释在上面的 link 中。