Fitting 3D data points to polynomial surface and getting the surface equation back
NickName:Bart M Ask DateTime:2020-08-05T18:09:59

Fitting 3D data points to polynomial surface and getting the surface equation back

I am new to Python 3D fitting, and the related optimisation techniques. I tried to understand similar topics and to find an answer based on least squares methods, but my success has been rather limited.

Problem: I have a number (ca. 400) of 3D points stored in a np array.

data = np.array([[x1, y1, z1], [x2, y2, z2], [x3, y3, z3], [x4, y4, z4], ... ])

I would like to fit a polynomial surface (order 2 or 3) to these points, and then get the parameters of the surface equation, so I could calculate z for any given pair of (x, y) values. For example:

z = (A * x ** 2) + (B * y ** 2) + (C * x * y) + (D * x) + (E * y) + F

I need to get an output that would contain the A, B, C, D, E, and F parameters. Is there a nice 'Pythonic' way of doing this?

Copyright Notice:Content Author:「Bart M」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63262910/fitting-3d-data-points-to-polynomial-surface-and-getting-the-surface-equation-ba

Answers
Péter Leéh 2020-08-05T11:09:02

curve_fit accepts multi-dimensional array for independent variable, but your function must accept the same:\nimport numpy as np\nfrom scipy.optimize import curve_fit\n\ndata = np.array(\n [[0, 0, 1],\n [1, 1, 2],\n [2, 1, 3],\n [3, 0, 5],\n [4, 0, 2],\n [5, 1, 3],\n [6, 0, 7]]\n)\n\ndef func(X, A, B, C, D, E, F):\n # unpacking the multi-dim. array column-wise, that's why the transpose\n x, y, z = X.T\n\n return (A * x ** 2) + (B * y ** 2) + (C * x * y) + (D * x) + (E * y) + F\n\npopt, _ = curve_fit(func, data, data[:,2])\n\nfrom string import ascii_uppercase\nfor i, j in zip(popt, ascii_uppercase):\n print(f"{j} = {i:.3f}")\n\n# A = 0.060\n# B = 2004.446\n# C = -0.700\n# D = 0.521\n# E = -2003.046\n# F = 1.148\n\nNote that in general you should provide initial guess for parameters to achieve good fitting results.",


More about “Fitting 3D data points to polynomial surface and getting the surface equation back” related questions

Fitting 3D data points to polynomial surface and getting the surface equation back

I am new to Python 3D fitting, and the related optimisation techniques. I tried to understand similar topics and to find an answer based on least squares methods, but my success has been rather lim...

Show Detail

Local surface fitting to 3d points

I have set of 3d points, but it contains some outliers. My goal is to map original set of 3d points to "smoothed" set of 3d points and get rid of outliers. The approach I choose is - local surface

Show Detail

Surface Estimation

TL;DR - How do I only analyse the surface data points for surface estimation? I have a 3D object, and I would like to estimate the surface shape. The problem is: MATLAB curve fitting toolbox takes ...

Show Detail

How to improve surface fit to 3D data?

I have been using scipy to fit 3d data to a surface, which is defined as a polynomial function. But the result looks not so close to the data. How can I improve the fitting? import numpy as np from...

Show Detail

Fitting polynomial surface to a 3D dataframe representing family of curves

I have a 3-D dataframe representing family of curves: yi=f(xi) which are also depending on a third variable lets say zi (yi=g(xi,zi)) as shown in the graphic below: Family Of Curves The yi data are

Show Detail

Surface Equation

I want a set of 3D points and I want to find the equation that they describe. Something like z = f(x, y). The surface fitting method I am using is this one: X,Y = np.meshgrid(np.arange(0,2,0.5), np.

Show Detail

Python Polynomial Regression on 3D Data points

My problem is, that I have about 50.000 non-linear data points (x,y,z) with z depending on the independent variables x and y. From one side, so from a two-dimensional perspective, the data points l...

Show Detail

How to fit a set of 3D data points using a third or higher degree of polynomial surface regression?

I have input data points (x,y,z), all positive, and need to fit them to a surface. More specifically, I have to create a grid from the x and y data points and evaluate the data points on this grid to

Show Detail

Fitting a surface to collection of 3D data points in java

Hi I have a cloud of XYZ data points. I want to estimate a surface which best fits these points, so that later on I can input an XY pair and get back the Z value where this XY pair lies on the surf...

Show Detail

print surface fit equation in python

I'm trying to fit a surface model to a 3D data-set (x,y,z) using matplotlib. Where z = f(x,y). So, I'm going for the quadratic fitting with equation: f(x,y) = ax^2+by^2+cxy+dx+ey+f So far, I

Show Detail