I have this 3 arrays with my data:
X=np.array(x)
Y=np.array(y)
Z=np.array(z)
I know how to plot my points, and how to aply the polyfit in 2D. How can I get the polyfit coefficients from my data in 3D? And can I plot my 3D fitting curve?
I am not sure if it's possible using np.polyfit()
, but I found a reference that could help here . It implements finding the coefficients as follows:
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)
I hope that what you were looking for, further explention about the calculations are in the link above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.