How to implement linear interpolation?

Helpless picture Helpless · Sep 8, 2011 · Viewed 109.6k times · Source

Say I am given data as follows:

x = [1, 2.5, 3.4, 5.8, 6]
y = [2, 4, 5.8, 4.3, 4]

I want to design a function that will interpolate linearly between 1 and 2.5, 2.5 to 3.4, and so on using Python.

I have tried looking through this Python tutorial, but I am still unable to get my head around it.

Answer

Dave picture Dave · Dec 3, 2012
import scipy.interpolate
y_interp = scipy.interpolate.interp1d(x, y)
print y_interp(5.0)

scipy.interpolate.interp1d does linear interpolation by and can be customized to handle error conditions.