NumPy interp()
function in Python also known as interpolation returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp). It is a one-dimensional linear interpolation for monotonically increasing sample points.
In this article, I will explain how to use numpy.interp()
function syntax, parameters, and usage of how to get the interpolated values of the NumPy array with examples.
1. Quick Examples of Interpolate Function
If you are in a hurry, below are some quick examples of the NumPy interpolate function.
# Quick examples of interpolate function
# Example 1: Use numpy.interp() function
# Perform linear interpolation
x = [2, 5, 6]
xp = [1, 3, 4, 7]
fp = [2, 0, 4, 1]
interpolated_values = np.interp(x, xp, fp)
# Example 2: Get the interpolate values
# On graphical representation
arr = np.linspace(0, 2*np.pi, 12)
arr1 = np.sin(arr)
xvals = np.linspace(0, 2*np.pi, 60)
yinterp = np.interp(xvals, arr, arr1)
plt.plot(arr, arr1, 'o')
plt.plot(xvals, yinterp, '-x')
plt.show()
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp(x, xp, fp, period=360)
# Example 4: Use interp() function
# To numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp(x, xp, fp)
2. Syntax of NumPy interp()
Following is the syntax of numpy.interp()
function.
# Syntax of numpy.interp()
numpy.interp(x, xp, fp, left=None, right=None, period=None)
2.1 Parameters of interp()
The numpy.interp()
function allows the following parameters
x
– array_like : The x-coordinates at which to evaluate the interpolated values.xp
– The x-coordinates of the data points, must be increasing if the argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.fp
– The y-coordinates of the data points, the same length as xp.left
– Optional float or complex corresponding to fp. left is the value to return for x < xp[0], by default optional values and fp[0].right
– Optional float or complex corresponding to fp. right is the value to return for x > xp[-1], by default optional values and fp[-1]period
– None or float, optional: period is the period for the x-coordinates. If it is given, left and right are ignored if period is specified. This is also optional.
2.2 Return Value
It returns the interpolated values of type float, the same shape as x. Raises ValueError if xp and fp have different lengths. If xp or fp are not 1-D sequences If period == 0.
3. Use numpy.interp() Function
To use the numpy.interp()
function for linear interpolation. In this example, you will use arrays for both x
(multiple x-coordinates) and xp
(known x-coordinates) to perform the interpolation.
In the below example, interpolated_values
will contain the interpolated y-values corresponding to x = [2, 5, 6]
using the given data points. The numpy.interp()
function will calculate the interpolated values based on the linear interpolation formula.
# Import numpy
import numpy as np
x = [2, 5, 6]
xp = [1, 3, 4, 7]
fp = [2, 0, 4, 1]
# Use numpy.interp() function
# Perform linear interpolation
interpolated_values = np.interp(x, xp, fp)
print("After getting the interpolated values:\n",interpolated_values)
Yields below output.
4. Get the Interpolate Values on Graphical Representation
You can also use the NumPy interp()
function for graphical representation using the MatLab library. Plot an interpolant to the sine function.
import numpy as np
import matplotlib.pyplot as plt
# Get the interpolate values on graphical representation
arr = np.linspace(0, 2*np.pi, 12)
arr1 = np.sin(arr)
xvals = np.linspace(0, 2*np.pi, 60)
yinterp = np.interp(xvals, arr, arr1)
plt.plot(arr, arr1, 'o')
plt.plot(xvals, yinterp, '-x')
plt.show()
Yields below output.
You can see the Parabolic graph of the interp()
method in Numpy.
5. Get Interpolation with Periodic x-coordinates
To interpolate with periodic x-coordinates using the numpy.interp()
function. The period=360
parameter indicates that the x-coordinates are periodic with a period of 360 degrees. The function will calculate the interpolated values for the given x
array using the periodic data points provided in xp
and fp
. When you run this code, it will calculate and print the interpolated values based on the periodic data points provided.
import numpy as np
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
# Get interpolation with periodic x-coordinates
arr2 = np.interp(x, xp, fp, period=360)
print("After getting the interpolated values:\n",arr2)
# Output:
# After getting the interpolated values:
# [ 6.5 11. 8.75 13.25 8.75 8.675
# 8.625 8.69642857]
6. Use interp() Function to NumPy Complex Interpolation
You can use the numpy.interp()
function to perform complex interpolation by working with complex numbers in Python. The numpy.interp()
function handles complex numbers just like real numbers.
In the below example, np.argsort()
is used to obtain the sorted indices of the xp
array, which are then used to sort both xp
and fp
arrays. This ensures that xp
is in ascending order, allowing the np.interp()
function to perform complex interpolation correctly.
import numpy as np
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3 + 6j]
# Sort xp and fp arrays to ensure xp is in ascending order
sorted_indices = np.argsort(xp)
xp_sorted = np.array(xp)[sorted_indices]
fp_sorted = np.array(fp)[sorted_indices]
# Use interp() function for numpy complex interpolation
arr2 = np.interp(x, xp_sorted, fp_sorted)
print("After getting the complex interpolation values:\n", arr2)
# Output:
# After getting the complex interpolation values:
3 [0. +2.j 1.5+3.j]
Frequently Asked Questions
What is NumPy interpolate function used for?
NumPy interpolation functions are used to perform various types of interpolation on data points. Interpolation is the process of estimating unknown values that fall between known values in a sequence of numbers or data points.
What are the common interpolation methods provided by NumPy?
NumPy provides several interpolation methods, including linear interpolation, polynomial interpolation, spline interpolation, and more. The numpy.interp()
function performs one-dimensional linear interpolation.
What is the role of the period parameter in numpy.interp()?
The period
parameter in the numpy.interp()
function allows you to specify a period for the x-coordinates. It is useful when dealing with periodic functions. If specified, data points are considered to be periodic with the specified period.
How does the numpy.interp() function work?
The numpy.interp()
function performs one-dimensional linear interpolation. Given known data points (xp
for x-coordinates and fp
for y-coordinates), and a set of target x-coordinates (x
), the function estimates corresponding y-values using linear interpolation.
How can I perform complex interpolation using NumPy?
You can perform complex interpolation in NumPy by representing both x-coordinates and y-coordinates as complex numbers. The numpy.interp()
function handles complex numbers just like real numbers, allowing you to perform complex interpolation easily.
Conclusion
In this article, I have explained how to use numpy.interp()
function to get the interpolated values, the same shape as x with examples.
Happy Learning!!
Related Articles
- NumPy Array Copy
- Python NumPy Square Root
- NumPy tile() Function in Python
- NumPy where() Multiple Conditions
- How to Transpose Matrix in NumPy
- How To Use NumPy dot() Function in Python
- Numpy loadtxt() Explained with Examples
- How to get square root values of an array?
- How to Calculate Maximum of Array in NumPy