Introduction to Python SciPy Interpolation

In This Article, You Will Learn About Python SciPy Interpolation.

Python SciPy Interpolation – Before moving ahead, let’s know a bit about Python SciPy Matlab Arrays

Table of Contents

 

SciPy Interpolation

Interpolation is the process of finding or generating numbers between a set of points or numbers.

e.g., 4 and 5, we might get generated numbers such as 4.6 and 4.9.

Interpolation has often been used to give an alternate value in Machine Learning while finding missing data in the dataset.

The method of substituting the values is known as imputation.

In addition to imputation, interpolation is commonly used when we want to smooth discrete data points.

Implementation of Interpolation

To get started with Interpolation, SciPy provides a method called scipy.interpolation, which comes with many functions to deal with interpolation.

Interp1d() is a function that is used to interpolate distributions using one variable.

It is a function that takes the x along with one or more y elements and then returns a function called by using new numbers and returns the corresponding to y.

Example –  For given x and y interpolate values from 5.1, 5.2… to 6.1.

				
					from scipy.interpolate import interp1d
import numpy as np

x = np.arange(8)
y = 1*x + 2

interpolation_function = interp1d(x, y)

new_array = interpolation_function(np.arange(5.1, 6.1, 0.2))

print(new_array)

				
			
				
					Output - 

[7.1 7.3 7.5 7.7 7.9]
				
			

As show above, it returned an array in a range of given number.

Spline interpolation

Spline interpolation is when the points are fitted to a one-piece function defined by polynomials, also known as Splines.

For the Spline interpolation, SciPy has provided UnivariateSpline() function that takes two arguments, x, and y and produces a callable function called new x. 

One-piece Function: A function that has a different definition for various sizes.

Example –  Finding univariate spline interpolation for 5.1, 5.2… to 6.1 for the following non- linear points.

				
					from scipy.interpolate import UnivariateSpline
import numpy as np

x = np.arange(8)
y = x**3 + np.cos(x) + 1 

interpolation_function = UnivariateSpline(x, y)

new_array = interpolation_function(np.arange(5.1, 6.1, 0.2))

print(new_array)
				
			
				
					Output - 

[133.79378819 150.11992548 167.71811206 186.63516855 206.9179156]
				
			

As a result, it returned an array in a range of given number.

Interpolation with Radial Basis Function

The Radial Basis Function is one term that is defined to correspond to a reference fixed point.

SciPy has provided Rbf() function for the Radial Basis Function that takes two arguments x and y and produces a callable function called with new x.

Example – Finding values for 5.1, 5.2 … 6.1 by using RBF.

				
					from scipy.interpolate import Rbf
import numpy as np

x = np.arange(8)
y = x**3 + np.sin(x) + 1

interpolation_function = Rbf(x, y)

new_array = interpolation_function(np.arange(5.1, 6.1, 0.2))

print(new_array)

				
			
				
					Output - 

[132.41533831 148.03101638 165.08064737 183.99457339 205.19538658]
				
			

As a result, it returned an array in a range of given number by using Rbf function.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Connect on:

1 thought on “Introduction to Python SciPy Interpolation”

Leave a Comment

Stay in the loop

codingstreets