Python:Interpolation

From PrattWiki
Revision as of 14:39, 12 November 2019 by DukeEgr93 (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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:

import numpy as np
from scipy.interpolate import interp1d

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')