In this tutorial, you'll briefly learn how to use SciPy API's BarycentricInterpolator and KroghInterpolator classes in Python.
Interpolation is a method of estimating unknown data points in a known range to make the curve smoother. Univariate interpolation is a type of curve fitting that seeks the curve that best fits a set of two-dimensional data points. Since the data points are sampled from a single variable function, it is called univariate interpolation.
The tutorial covers;
- Preparing test data
- BarycentricInterpolator method
- KroghInterpolator method
- Source code listing
We'll start by loading the required libraries.
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
y = [-1, -2, 1, 3, 2, -1, -3, -1, 1, 2]
x = np.linspace(1, len(y), len(y))
plt.figure(figsize=(8, 6))
plt.plot(x, y, '.r', markersize=10)
plt.plot(x, y)
plt.grid()
plt.show()
BarycentricInterpolator builds a polynomial with provided set of points. It enables the evaluation of the polynomial, effective interpolation of the y values, and updates y through the addition of new x values. The below listing
shows the implementation of BarycentricInterpolator method. First, we'll fit model on x and
y data, then provide new x data to find out new estimated y values.
xnew = np.linspace(min(x), max(x), num=100) bary = interpolate.BarycentricInterpolator(x, y) yfit = bary(xnew) plt.plot(x, y, '.') plt.plot(xnew, yfit) plt.grid() plt.show()
KroghInterpolator evaluates the polynomial and all of its derivatives. The implementation of KroghInterpolator method is similar to above listing. We'll fit model on x and
y data, then provide new x data to find out new estimated y values.
krogh = interpolate.KroghInterpolator(x, y) yfit = krogh(xnew) plt.plot(x, y, '.') plt.plot(xnew, yfit) plt.grid() plt.show()
Now we can visualize both methods on a graph and compare each method performance
with a given data. The following listing shows how to plot all in one
graph.
xnew = np.linspace(min(x), max(x), num=100)
# fit each method on x, y data
bary = interpolate.BarycentricInterpolator(x,y)
krogh = interpolate.KroghInterpolator(x,y)
y_bary = bary(xnew)
y_krogh = krogh(xnew)
# visualize on a graph
ig, ax = plt.subplots(2,1, figsize=(12, 8))
ax[0].plot(x, y, '.r', markersize=10)
ax[0].plot(xnew, y_bary, 'g')
ax[0].set_title("BarycentricInterpolator")
ax[0].grid()
ax[1].plot(x, y, '.r', markersize=10)
ax[1].plot(xnew, y_krogh, 'b')
ax[1].set_title("KroghInterpolator")
ax[1].grid()
plt.tight_layout()
plt.show()
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
# Preparing data
y = [-1, -2, 1, 3, 2, -1, -3, -1, 1, 2] x = np.linspace(1, len(y), len(y))
plt.figure(figsize=(8, 6))
plt.plot(x, y, '.r', markersize=10)
plt.plot(x, y)
plt.grid() plt.show()
xnew = np.linspace(min(x), max(x), num=100)
# fit each method on x, y data bary = interpolate.BarycentricInterpolator(x,y) krogh = interpolate.KroghInterpolator(x,y) y_bary = bary(xnew) y_krogh = krogh(xnew)
# visualize on a graph
ig, ax = plt.subplots(2,1, figsize=(12, 8)) ax[0].plot(x, y, '.r', markersize=10) ax[0].plot(xnew, y_bary, 'g') ax[0].set_title("BarycentricInterpolator") ax[0].grid() ax[1].plot(x, y, '.r', markersize=10) ax[1].plot(xnew, y_krogh, 'b') ax[1].set_title("KroghInterpolator") ax[1].grid() plt.tight_layout() plt.show()
No comments:
Post a Comment