Difference between revisions of "Python:Interpolation"

From PrattWiki
Jump to navigation Jump to search
(Created page with "== Introduction == Interpolation is a process by which "gaps" in a data set may be filled using relatively simple equations. Interpolation differs from fitting in that: * Int...")
 
(Basic Types of Interpolation)
Line 6: Line 6:
 
== Basic Types of Interpolation ==
 
== Basic Types of Interpolation ==
 
There are several basic types of interpolation; the examples below are based on the following data set:
 
There are several basic types of interpolation; the examples below are based on the following data set:
 +
<center>
 
$$\begin{array}{c|c}
 
$$\begin{array}{c|c}
 
\mbox{Time}~t, \mbox{s} & \mbox{Temperature}~T, ^o\mbox{C}\\ \hline
 
\mbox{Time}~t, \mbox{s} & \mbox{Temperature}~T, ^o\mbox{C}\\ \hline
Line 14: Line 15:
 
20 & 14
 
20 & 14
 
\end{array}$$
 
\end{array}$$
 +
</center>
 
=== Nearest Neighbor ===
 
=== Nearest Neighbor ===
 
Nearest neighbor interpolation means that for any given input, the output will be based on the dependent value in the data set obtained at the independent value of the data set closest to the input.  For example, in the data set above, $$f(4)$$ would give a temperature of 3 since time 4 is closest to time 2 in the data set.  Similarly, $$f(11)$$ would return a temperature if 15 since time 11 is closest to time 12.
 
Nearest neighbor interpolation means that for any given input, the output will be based on the dependent value in the data set obtained at the independent value of the data set closest to the input.  For example, in the data set above, $$f(4)$$ would give a temperature of 3 since time 4 is closest to time 2 in the data set.  Similarly, $$f(11)$$ would return a temperature if 15 since time 11 is closest to time 12.
Line 26: Line 28:
  
 
=== Example Code ===
 
=== Example Code ===
In Python, interpolation can be performed using the <code>interp1d</code> method of the <code>scipy.interpolate</code> package.  This method will create an interpolation function based on the independent data, the dependent data, and the kind of interpolation you want:
+
In Python, interpolation can be performed using the <code>interp1d</code> method of the <code>scipy.interpolate</code> package.  This method will create an interpolation function based on the independent data, the dependent data, and the kind of interpolation you want with options inluding <code>nearest</code>, <code>linear</code>, and <code>cubic</code>. 
  
<syntaxhighlight lang=python>
+
<html>
import numpy as np
+
<iframe src="https://trinket.io/embed/python3/5db88258fd?start=result" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
from scipy.interpolate import interp1d
+
</html>
 
 
t = np.array([0, 2, 8, 12, 20])
 
T = np.array([5, 3, 10, 15, 14])
 
 
 
nn_fun = interp1d(t, T, kind='nearest')
 
lin_fun = interp1d(t, T, kind='nearest')
 
 
 
</syntaxhighlight>
 

Revision as of 20:07, 12 November 2019

Introduction

Interpolation is a process by which "gaps" in a data set may be filled using relatively simple equations. Interpolation differs from fitting in that:

  • Interpolations are required to exactly hit all the data points, whereas fits may not hit any of the data points, and
  • Interpolations are based on, often simple, mathematical formulas without regard to the underlying system which produced the data.

Basic Types of Interpolation

There are several basic types of interpolation; the examples below are based on the following data set:

$$\begin{array}{c|c} \mbox{Time}~t, \mbox{s} & \mbox{Temperature}~T, ^o\mbox{C}\\ \hline 0 & 5 \\ 2 & 3 \\ 8 & 10 \\ 12 & 15 \\ 20 & 14 \end{array}$$

Nearest Neighbor

Nearest neighbor interpolation means that for any given input, the output will be based on the dependent value in the data set obtained at the independent value of the data set closest to the input. For example, in the data set above, $$f(4)$$ would give a temperature of 3 since time 4 is closest to time 2 in the data set. Similarly, $$f(11)$$ would return a temperature if 15 since time 11 is closest to time 12.

There are several advantages of nearest neighbor:

  • Very simple "calculation" - really, there is no calculation other than finding out which independent value is closest
  • The interpolated values are always values in the data set - if you have some system that is only capable of producing particular values, nearest neighbor interpolation will never return an impossible value.

There are also disadvantages:

  • Technically undetermined half-way between measured data points,
  • Potentially large discontinuities between data points, and
  • No potential to estimate any kind of rate information between points.

Example Code

In Python, interpolation can be performed using the interp1d method of the scipy.interpolate package. This method will create an interpolation function based on the independent data, the dependent data, and the kind of interpolation you want with options inluding nearest, linear, and cubic.