Data Science and Computing with Python for Pilots and Flight Test Engineers
Linear Interpolation
Interpolation lets us estimate the value that the data might have in places between data points, where we do not actually have a data point to know for sure. (It is not to be confused with extrapolation, which tries to estimate values of data outside of the range, where data points are available, which can be very prone to error and uncertainty.)
Linear interpolation in one dimension is one of the simplest forms of interpolation. It draws straight lines between adjacent exisiting data points. In doing so, it creates a continuous curve that goes through all data points, and has kinks at each data point with straight lines in between. The value of the curve is the value we estimate the data might have for values were we do not have any data.
While for more advanced interpolation techniques we will use other libraries in the future (such as SciPy and scikit-learn), for the purpose of linear interpolation in 1D discussed in this lesson, we can use the function interp from the NumPy library. The only thing to pay attention to is that the data in the data array must be ordered on the horizontal axis in monotonous (ascending or descending) order. The vertical axis values do not need to be monotonous. The code below shows an example.
import numpy as np
import matplotlib.pyplot as plt
# Create a 2-dimensional array containing the input data we want to plot:
data = np.array([[0, 0.1],
[1, 1.1],
[2, 3.9],
[3, 8.5],
[4, 18],
[5, 10]])
xdata = np.array(data[:,0]) # horizontal data values (values of x)
ydata = np.array(data[:,1]) # vertical data values (values of y)
The first line of code below determines at which values we want to evaluate the interpolated function. The second line of code computes the interpolated values at these points. The interp function from the NumPy library takes the horizontal axis values of interest as a variable, as well as the horizontal and vertical data values.
x = np.linspace(0, 5, num=101)
y = np.interp(x, xdata, ydata)
\(y\) now contains the linearly interpolated values at the points saved in \(x\). The cell below merely just plots the result.
plt.plot(x, y, 'r-', label='Linear Interpolation')
plt.plot(xdata, ydata, 'bo', label='Data')
plt.legend()
plt.show()
Copy and paste the code cells above into your Jupyter notebook, evaluate the cells, and see the plotted result.