# Fitting a polynomial to a set of points or to a skeleton

## Available data

Available to me is a set of points which can be represented as shown in image 1:

Also available to me is a non-continuous path derived from this data. It is not important how this non-continuous path is obtained. It is however important, that it roughly represents a curve I intent to approximate. This non-continuous path is shown in image 2:

## Goal

I want to approximate this data using a polynomial of either second or third degree. Examples of these approximations are shown in images 3, 4:

## Problem / question

Now I am looking for a way to obtain the red curve. Some details confuse me, where my knowledge is likely to be simply lacking. For example, how to fit a polynomial, when technically what I require is not a function, at least not in this coordinate system, because there will be situations where an x value is being assigned two y values.

I thought of possibly using the ends of my path to define a new x-axis, but I consider this approach faulty. Another consideration are Splines.

How should I go about obtaining this red curve from the non-continuous path (preferred) or from original data? What sources should I look into?

Apologies if this is an already answered question which I suspect it might be. However I have been issuing search queries for this without success, hence my question.

You can try fitting a multi-dimensional polynomial regression. It seems that for your data, a two dimensional regression model should be fine: $$(a)x^2 + (b)x + (c)y^2 + (d)y + (e)xy + (f)$$

In python for example, you can fit a proper model using:

import numpy as np
from sklearn.preprocessing import PolynomialFeatures

X = np.array([ [x0,y0], [x1,y1], ...  ])
poly = PolynomialFeatures(2)
poly.fit_transform(X)
poly = PolynomialFeatures(interaction_only=True)
poly.fit_transform(X)


• Thank you, this offers me a nice starting point. – Koenigsberg Sep 19 '19 at 7:46
• I ended up implementing Curve Global Approximation to fit a spline to these points. – Koenigsberg Dec 1 '19 at 16:53