scikit learn: polynomial interpolation of higher dimensions

1.7k views Asked by At

In this link of the scikit-learn project: http://scikit-learn.org/stable/auto_examples/linear_model/plot_polynomial_interpolation.html, it is shown how to apply polynomial interpolation to approximate a certain function for example.

This example is set for 2D-points. However:

  • How can it be extended to fit for 3D-points?

  • Or is this even possible with scikit-learn? In the documentation I couldn't find any hints yet.

Thank you in advance for any information and with best regards.

Dan

Edit 1:

Thank you Robin for your answers! Also pointing out at the rapid growth of complexity was a valuable hint!

I've stumbled on one problem so far, which is related to the 2D-array X in model.fit(X,z)

The 2D-array looks like that:

[[ 0.1010101   0.35353535]
 [ 0.4040404   0.65656566]
 [ 0.80808081  1.11111111]
 [ 1.21212121  1.31313131]]

while the function z is those of an paraboloid:

(((x**2)/(4**2) + ((y**2)/(8**2))) * 2)

Running model.fit(X,z) returns the following error message:

ValueError: Found arrays with inconsistent numbers of samples: [10 20]

From where does the inconsistency results?

1

There are 1 answers

0
Robin Spiess On BEST ANSWER

Yes, the same approach can be used for higher dimensional data. Just use the same code with an X containing more columns.

# For some degree, X and y
model = make_pipeline(PolynomialFeatures(degree), Ridge())
model.fit(X, y)

To add some background info: The Polynomial Features preprocessing step just creates all possible combinations of your features. This means even for a 2d input and degree 2 the feature space is already 6 dimensional (1, a, b, a*b, a*a, b*b). With more features this number grows even more rapidly.

For your second question, the fit function only accepts vectors and not functions. Therefore create a vector y = z(X[:,0],X[:,1]) and use this in the fit function instead model.fit(X,y).