SciPy interpolate.pchip_interpolate() function (3 examples)

Updated: March 7, 2024 By: Guest Contributor Post a comment

Introduction

The pchip_interpolate() function from SciPy’s interpolate module provides a powerful tool for piecewise cubic Hermite interpolating polynomial (PCHIP) interpolation. This method is particularly useful for retaining the shape and monotonicity of the interpolated data and is ideal for smoothly interpolating between data points without overfitting.

Understanding PCHIP Interpolation

PCHIP stands for Piecewise Cubic Hermite Interpolating Polynomial. Unlike spline interpolation, which may introduce oscillations or implausible values between points, PCHIP aims to produce a smooth curve that more naturally follows the input data’s trend. This is particularly important when dealing with monotonic data, where preserving the increase or decrease trend between data points is crucial.

Getting Started with pchip_interpolate()

To use pchip_interpolate(), you first need to import the function from SciPy:

from scipy.interpolate import pchip_interpolate

Next, you prepare your x and y data points. The x points represent the independent variable, while the y points are the dependent variable you wish to interpolate.

Basic Usage

For our first example, let’s interpolate a simple set of data points:

import numpy as np
from scipy.interpolate import pchip_interpolate

x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 4, 9, 16])  # y = x^2
x_new = np.linspace(0, 4, 10)
y_new = pchip_interpolate(x, y, x_new)

print("Interpolated values:", y_new)

Output:

Interpolated values: [ 0.          0.25240055  0.8340192   1.72222222  3.17489712  4.91106539
  7.11728395  9.66438043 12.62368541 16.        ]

This code block interpolates the given x and y data points over a new set of x values, generating smooth interpolations of y. As we can see, the interpolated values smoothly follow the original data’s quadratic trend.

Handling Non-uniform Spacing

PCHIP is particularly adept at handling data sets with non-uniform spacing between points. This capability is highlighted in the following example:

import numpy as np
from scipy.interpolate import pchip_interpolate

x = np.array([0, 1.5, 3, 4.5, 6])
y = np.log(x + 1)
x_new = np.linspace(0, 6, 30)
y_new = pchip_interpolate(x, y, x_new)

print("Resulting interpolated logarithmic curve:", y_new)

Output:

Resulting interpolated logarithmic curve: [0.         0.15409622 0.30130416 0.44049277 0.57053101 0.69028782
 0.79863217 0.89443299 0.97836011 1.0547518  1.1247463  1.18930355
 1.24938345 1.30594594 1.35995094 1.41211067 1.4616405  1.50859266
 1.55323838 1.59584889 1.63669542 1.6760492  1.71417911 1.75121173
 1.78707502 1.82168495 1.85495746 1.88680852 1.9171541  1.94591015]

Here, despite the non-uniform spacing of x values, pchip_interpolate() seamlessly provides a natural-looking interpolation, honoring the logarithmic growth pattern without introducing artifacts.

Advanced Usage: Incorporating Derivatives

One of the unique features of PCHIP interpolation is its ability to incorporate derivative information at the data points, offering more control over the shape of the interpolated curve. The following example demonstrates this capability:

import numpy as np
from scipy.interpolate import PchipInterpolator

# Using PchipInterpolator for more control
x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 2, 3, 5, 7])
derivatives = np.array([1, 0.5, 0, 0.5, 1])  # Derivative at each point

interpolator = PchipInterpolator(x, y, derivatives=derivatives)
x_new = np.linspace(0, 4, 10)
y_new = interpolator(x_new)

print("Interpolated values with derivative information:", y_new)

In this example, by specifying the derivative at each data point, we guide the interpolation process more finely, yielding a curve that closely aligns with our expectations and the nature of the data.

Conclusion

SciPy’s pchip_interpolate() function offers a flexible and powerful method for data interpolation, especially when working with datasets where maintaining the natural trend and monotonicity is paramount. Through its straightforward API and extended capabilities through the PchipInterpolator class, users can achieve high-quality interpolations suitable for a variety of scientific computing and data visualization tasks.