I'm new to the world of machine learning and data fitting and I'm currently trying to transfer some Matlab code to Python. In this code my goal is to fit a surface to a given set of datapoints which have (x,y,z) coordinates. The Matlab code looks like the following:
[xData, yData, zData] = prepareSurfaceData( X0, Y0, ZPhi0 );
% Type of polynom
ft = fittype( 'poly44' );
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft );
[x0,y0]=meshgrid(x,y);
And I obtain the values that solve the following expression:
Linear model Poly44:
val(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 +
p21*x^2*y + p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y
+ p22*x^2*y^2 + p13*x*y^3 + p04*y^4
However, I've been looking for a way to replicate this in Python but it seems as if all the functions available are only meant for 2D. My initial approach has been to use numpy.polynomial.polifit
, but I'm stuck as I would like something along the lines:
xData = X0.T
yData = Y0.T
zData = Zphi0.T
pol_degree = 44
coefs, stats = poly.polynomial.polyfit((xData, yData), zData, pol_degree, full=True)
This of course does not work and I've tried other packages but haven't got any results. I've seen a post suggesting that a good way to do this would be to separate the dimensions and fit xy an xz for example, like this:
coefsxy = poly.polynomial.polyfit(xData, yData, pol_degree)
coefsxz = poly.polynomial.polyfit(xData, zData, pol_degree)
But I don't see how can I join these two expressions and obtain the surface that I need.
Can somebody explain me what would be the best way to transfer this Matlab code to Python?