Open In App

Interpolation in Machine Learning

In machine learning, interpolation refers to the process of estimating unknown values that fall between known data points. This can be useful in various scenarios, such as filling in missing values in a dataset or generating new data points to smooth out a curve. In this article, we are going to explore fundamentals and implementation of different types of interpolation along with it's application in machine learning.

In machine learning, interpolation is an essential method for estimating values within a range of known data points. Forecasting values at intermediate points entails building a function that roughly mimics the behavior of the underlying data.

Interpolation in Machine Learning

The practice of guessing unknown values based on available data points is known as interpolation in the context of machine learning. In tasks like regression and classification, where the objective is to predict outcomes based on input features, it is important. Machine learning algorithms are capable of producing well-informed predictions for unknown or intermediate values by interpolating between known data points.

Interpolation Types

The intricacy and applicability of interpolation techniques varied for various kinds of data. Typical forms of interpolation include the following:

Interpolation in Linear Form

A straightforward but efficient technique for guessing values between two known data points is linear interpolation.

The value of y at any intermediate point x can be approximated using the following formula, given two data points:[Tex] (⁽ 1, 1 ) (x 1​ ,y 1​  ) and ( 2 , 2 ) (x 2​ ,y 2​  ).[/Tex] i.e [Tex]y=y_1+(x−x_1)⋅(y_2−y_1)/x_2−x_1 [/Tex]

Implementation

Output:

Interpolated value at [0.5 0.5]: [0.76124023]

Linear Interpolation

Polynomial Interpolation

Implementation

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

# Generate some sample data
x = np.linspace(0, 10, 10)
y = np.sin(x)

# Perform polynomial interpolation
poly_interp = interp1d(x, y, kind='cubic')

# Generate points for plotting the interpolated curve
x_interp = np.linspace(0, 10, 100)
y_interp = poly_interp(x_interp)

# Plot the original data and the interpolated curve
plt.scatter(x, y, label='Original Data')
plt.plot(x_interp, y_interp, color='red', label='Polynomial Interpolation')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Polynomial Interpolation with interp1d')
plt.legend()
plt.grid(True)
plt.show()

Output:

Polynomial Interpolation

Spline Interpolation

Spline interpolation is a method of interpolation where the interpolating function is a piecewise-defined polynomial called a spline. Unlike polynomial interpolation, which uses a single polynomial to fit all the data points, spline interpolation divides the data into smaller segments and fits a separate polynomial to each segment. This approach results in a smoother interpolating function that can better capture the local behavior of the data. The most common type of spline interpolation is cubic spline interpolation, which uses cubic polynomials for each segment and ensures continuity of the first and second derivatives at the endpoints of each segment. Spline interpolation is particularly useful for smoothing noisy data or interpolating functions with complex shapes.

Implementation

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import CubicSpline

# Generate some sample data points
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([5, 6, 9, 8, 7, 4, 6, 7, 8, 5])

# Create a CubicSpline interpolation
cs = CubicSpline(x, y)

# Generate points for plotting the interpolated curve
x_interp = np.linspace(1, 10, 100)
y_interp = cs(x_interp)

# Plot original data points and interpolated curve
plt.figure(figsize=(8, 6))
plt.plot(x, y, 'o', label='Data Points')
plt.plot(x_interp, y_interp, label='Cubic Spline Interpolation')
plt.title('Cubic Spline Interpolation')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.grid(True)
plt.show()

Output:

download-(3)

Radial Basis Function Interpolation

Radial Basis Function (RBF) interpolation is a method of interpolation that uses radial basis functions to approximate the underlying data. Unlike polynomial interpolation, which fits a single polynomial to the entire dataset, RBF interpolation uses a combination of radial basis functions centered at each data point to construct the interpolating function.

Implementation

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import RBFInterpolator

# Generate random data points
rng = np.random.default_rng()
x_data = rng.uniform(-1, 1, size=(100, 2))
y_data = np.sum(x_data, axis=1) * np.exp(-6 * np.sum(x_data**2, axis=1))

# Generate a grid for visualization
x_grid = np.mgrid[-1:1:50j, -1:1:50j]
x_flat = np.column_stack((x_grid[0].flatten(), x_grid[1].flatten()))

# Perform RBF interpolation
rbf_interpolator = RBFInterpolator(x_data, y_data)
y_flat = rbf_interpolator(x_flat)
y_grid = y_flat.reshape(50, 50)

# Plot the interpolated surface and scatter plot of original points
fig, ax = plt.subplots()
ax.pcolormesh(x_grid[0], x_grid[1], y_grid)
p = ax.scatter(x_data[:,0], x_data[:,1], c=y_data, s=50, ec='k')
fig.colorbar(p)
plt.title('RBF Interpolation with Random Data')
plt.xlabel('X1')
plt.ylabel('X2')
plt.show()

Output:

download-(4)

Applications Of Interpolation in Machine Learning

Interpolation is a method used in various fields for estimating values between known data points. Some common applications of interpolation include:


Article Tags :