Get scipy interpolation function formula

I have done some work in Python, but I'm new to scipy . I am trying to use methods from the interpolate library to come up with a function that will approximate a data set.

I found some examples to get started and could get the sample code below in Python (x, y) :

 import numpy as np from scipy.interpolate import interp1d, Rbf import pylab as P # show the plot (empty for now) P.clf() P.show() # generate random input data original_data = np.linspace(0, 1, 10) # random noise to be added to the data noise = (np.random.random(10)*2 - 1) * 1e-1 # calculate f(x)=sin(2*PI*x)+noise f_original_data = np.sin(2 * np.pi * original_data) + noise # create interpolator rbf_interp = Rbf(original_data, f_original_data, function='gaussian') # Create new sample data (for input), calculate f(x) #using different interpolation methods new_sample_data = np.linspace(0, 1, 50) rbf_new_sample_data = rbf_interp(new_sample_data) # draw all results to compare P.plot(original_data, f_original_data, 'o', ms=6, label='f_original_data') P.plot(new_sample_data, rbf_new_sample_data, label='Rbf interp') P.legend() 

The plot is displayed as follows:

interpolation-plot

Now, is there a way to get a polynomial expression representing an interpolated function created by Rbf (i.e. a method created as rbf_interp )?

Or, if this is not possible with Rbf , any suggestions using a different interpolation method, a different library, or even another tool are also welcome.

+6
source share
4 answers

RBF uses any functions that you specify, it is, of course, a global model, so yes there is the result of the function, but, of course, its true that you probably will not like it, since it is the sum for many Gaussians. You received:

  rbf.nodes # the factors for each of the RBF (probably gaussians) rbf.xi # the centers. rbf.epsilon # the width of the gaussian, but remember that the Norm plays a role too 

This way you can calculate the distances (using rbf.xi then paste the distances with coefficients in rbf.nodes and rbf.epsilon into the Gaussian (or any other function you asked to use). (You can check the python code __call__ and _call_norm )

So you get something like sum(rbf.nodes[i] * gaussian(rbf.epsilon, sqrt((rbf.xi - center)**2)) for i, center in enumerate(rbf.nodes)) , to give some kind of funny code / formula, the RBF function is written in the documentation, but you can also check the python code.

+4
source

Answer: no, there is no “good” way to write down the formula, or at least not in short form. Some types of interpolations, such as RBF and Loess, do not directly look for a parametric mathematical function that fits the data, and instead they calculate the value of each new data point separately as a function of other points.

These interpolations are guaranteed to always work well for your data (for example, in your case), and the reason for this is that you need a very large number of parameters to describe them (basically all of your data points). Think of it this way: you can interpolate linearly by connecting consecutive data points with straight lines. You can put any data in this way and then describe the function in mathematical form, but this will require a large number of parameters (at least as many as the number of points). In fact, what you are doing right now is pretty much a smoothed version of this.

If you want the formula to be short, this means that you want to describe the data using a mathematical function that does not have a large number of parameters (in particular, the number of parameters should be much less than the number of data points). Examples include logistic functions, polynomial functions, and even a sinusoidal function (which you used to generate data). Obviously, if you know which function generated the data, which will be the function you want to put.

+2
source

RBF probably means Radial Basis Function . I would not be surprised if scipy.interpolate.Rbf is the function you are looking for.

However, I doubt that you can find a polynomial expression to represent your result.

If you want to try different interpolation methods, check out the relevant Scipy documentation , which contains a link to RBF, splines ...

+1
source

I don't think SciPys RBF will provide you with the actual function. But one thing you could do is try out the function that SciPys RBF provided (i.e. 100 points). Then use the Lagrangian interpretation with these points. This will create a polynomial function for you. Here is an example of how it would look. If you do not want to use Lagrange interpolation, you can also use the "Newton dividend difference method" to generate a polynomial function. enter image description here

0
source

Source: https://habr.com/ru/post/926305/


All Articles